Circle[] circleArray = new Circle[10]; The above statement declares the array ci
ID: 3582059 • Letter: C
Question
Circle[] circleArray = new Circle[10];
The above statement declares the array cicleArray to hold 10 elements of type Circle. But the storage for each element object of type Circle is not defined until each element is created with an "= new Circle()" statement. The author does that in a loop by invoking the no-argument Constructor:
for (int i = 0; i < circleArray.length; i++) {
circleArray[i] = new Circle() <--- Declares a single element to be of type Circle.
}
We know now not to declare a variable in the for statement as well as not to use terse variables such as i.
The above loop is fine when each element of the Array is to have default values. But what happens if we want to give each element object specific data hard-coded into the program? We could not use a loop in that case. For example:
circleArray[0] = new Circle(2.5);
circleArray[1] = new Circle(5.6);
etc.
1. Using the Final Project UML Class Diagram and the data in the UML Notation for Objects, show us how to declare an array of type Student and define the first 4 elements invoking the proper Constructor from the Student Class.
2. Show us how to define the the 5th element by first invoking the no-argument Constructor and then invoking the individual Mutator (set) methods to assign data to that object.
UML Notations for Objects (data): astudent[0]: Student student Name John Smith quiz 9.0 quiz 8.5 quiz3 9.7 midterm Exam 91.0 finalExam 94.0 Student[2]: Student studentName Sally Wilcox quiz 8.0 quiz2 8.2 quiz3 7.8 midterm Exam 84.0 final Exam 86.0 Student[4]: Student student Name Wendy Abrams quiz1 9.5 quiz 9.7 quiz3 9.8 midterm Exam 92.0 finalExam 95.0 a Student[1]: Student student Name Doris McGrath quiz1 6.2 quiz 7.9 quiz 8.2 midterm Exam 80.0 finalExam 83.0 a Student[3]: Student student Name Bill Johnson quiz1 7.0 quiz 7.5 quiz 8.1 midterm Exam 85.0 final Exam 87.0Explanation / Answer
import java.util.*;
//student class
class Student
{
//instance variables of class
String studentName;
double quiz1;
double quiz2;
double quiz3;
double midtermExam;
double finalExam;
//default constructor
public Student()
{
studentName="";
quiz1=0;
quiz2=0;
quiz3=0;
midtermExam=0;
finalExam=0;
}
//parametrizewd constructor
public Student(String studentName,double theQuiz1,double theQuiz2,double theQuiz3,double theMidterm,double theFinal)
{
this.studentName=studentName;
quiz1=theQuiz1;
quiz2=theQuiz2;
quiz3=theQuiz3;
midtermExam=theMidterm;
finalExam=theFinal;
}
//setter for class variables
public void setName(String theName)
{
studentName=theName;
}
public void setQuiz1(double theGrade)
{
quiz1=theGrade;
}
public void setQuiz2(double theGrade)
{
quiz2=theGrade;
}
public void setQuiz3(double theGrade)
{
quiz3=theGrade;
}
public void setMidterm(double theGrade)
{
midtermExam=theGrade;
}
public void setFinalExam(double theGrade)
{
finalExam=theGrade;
}
//getter for class variables
public String getName()
{
return studentName;
}
public double getQuiz1()
{
return quiz1;
}
public double getQuiz2()
{
return quiz2;
}
public double getQuiz3()
{
return quiz3;
}
public double getMidterm()
{
return midtermExam;
}
public double getFinalExam()
{
return finalExam;
}
//string rep of object
public String toString()
{
return " Name: "+getName()+" Quiz1: "+getQuiz1()+" Quiz2: "+getQuiz2()+" Quiz3: "+getQuiz3()+" Mid Term Exam: "+getMidterm()+" Final Exam: "+getFinalExam();
}
}
//driver class
public class StudentDriver{
public static void main(String []args){
//declare array of students
Student students[]=new Student[5];
//initialize 4 student objects using parametrized constructor
students[0]=new Student("Jack",9,9,9,9,9);
students[1]=new Student("Jill",6,8,7,9,8);
students[2]=new Student("Peter",6,7,4,4,4);
students[3]=new Student("Laura",8,9,6,7,9);
//initialize the last object using default constructor
students[4]=new Student();
//set the values of object
students[4].setName("Ali");
students[4].setQuiz1(8);
students[4].setQuiz2(10);
students[4].setQuiz3(10);
students[4].setMidterm(10);
students[4].setFinalExam(10);
//display the student data
for(int i=0;i<students.length;i++)
System.out.println(students[i]);
}
}
Sample Output:
Name: Jack
Quiz1: 9.0
Quiz2: 9.0
Quiz3: 9.0
Mid Term Exam: 9.0
Final Exam: 9.0
Name: Jill
Quiz1: 6.0
Quiz2: 8.0
Quiz3: 7.0
Mid Term Exam: 9.0
Final Exam: 8.0
Name: Peter
Quiz1: 6.0
Quiz2: 7.0
Quiz3: 4.0
Mid Term Exam: 4.0
Final Exam: 4.0
Name: Laura
Quiz1: 8.0
Quiz2: 9.0
Quiz3: 6.0
Mid Term Exam: 7.0
Final Exam: 9.0
Name: Ali
Quiz1: 8.0
Quiz2: 10.0
Quiz3: 10.0
Mid Term Exam: 10.0
Final Exam: 10.0