The following classes are defined, this is for me to study for my next test. Aft
ID: 3714337 • Letter: T
Question
The following classes are defined, this is for me to study for my next test. After all the classes are listed, there are some questions. The language is java too.
---------------------------------------
public class Course
{
private String title;
private int courseNumber;
private int grades[];
//initial values constructor
public Course(String t, int cn, int g[]){
title=t;
courseNumber=cn;
grades=g;
}
//get methods
public String getTitle(){return title;}
public int getCourseNum(){return courseNumber;}
public int[] getGrades(){return grades;}
//method to find average grade
public double Average(){
double sum=0;
for(int i=0; i sum=sum+grades[i];
return sum/grades.length; // return average
}
//method that return maxim grade for a class
public int maxGrade(){
int max=grades[0];
for(int i=1;i if(grades[i]>max)
max=grades[i];
return max;
}
public String toString(){
return String.format("%3d %s",courseNumber,title);
}
}
---------------------------------------
public class Student
{
private String name;
private Course listOfCourse[];
//initial valued constructor
public Student(String n, Course list[]){
name=n;
listOfCourse=list;
}
//get methods
public Course[] getList(){return listOfCourse;}
public String getName(){return name;}
//method to determine best courses based on Average grade
//method returns the course with best average grade
public Course Best(){
Course c;
c=listOfCourse[0];
for(int i=1;i if(c.Average() c=listOfCourse[i];
return c;
}
//toString method
public String toString(){
return name;
}
}
---------------------------------------
public class Instructor
{
private String name;
private Student list[]; //list of students
//inial valued constructor
public Instructor(String n, Student s[])
{
name=n;
list=s;
}
//get methods
public Student[] getStudent()
{
return list;
} // returns all the students of an instructor
}
---------------------------------------
public class Test {
public static void main(String[] args)
{
int g1[]={90,89,40,70};
int g2[]={100,90,80,70,100};
int g3[]={89,95,80,70};
int g4[]={99,79,80,60};
int g5[]={100,100,80,90,100};
int g6[]={100,50,80,95,100};
Course list1[]=new Course[4];
list1[0]=new Course("Programming 1", 180,g1);
list1[1]=new Course("History", 120, g2);
list1[2]=new Course("Mathematics", 136,g3);
list1[3]=new Course("Data Mining", 251,g4);
Course list2[]=new Course[2];
list2[0]=new Course("Art", 104,g5);
list2[1]=new Course("Music", 100,g6);
Student L[]=new Student[2];
L[0]=new Student("Sara", list1);
L[1]=new Student("Mike",list2);
Instructor A=new Instructor("James Gary",L);
}
}
---------------------------------------
Determine the type of the following variable/objects such as String, int,double, Course, Student, Instructor etc. based on the classes defined above
1) list2[1].getGrades() :
2) list2[1].Average() :
3) list2[0].getGrades()[3] :
4) L[0].getList()[0] :
5) L[1].getList()[1].Average() :
6) L[1].Best().getTitle() :
7) A :
8) A.getStudent() :
9) A.getStudent()[0].getName() :
10) L[0] :
Explanation / Answer
int [ ] , because list2[1] is an object of Course and getGrades() is method of Course and will return integer array. double , because list2[1] is an object of Course and Average() is a method of Course and will return double. int , because list2[0] is an object of Course and getGrades() is a method of Course which will return integer array but here it is the 3rd element of the array returned by getGrades() , therefore datatype will be int. Course , because L[0] is an object of Student and its method getList() will return Course [ ] (i.e Course array) but here only retrieving the 0th element of the array , therefore type will be Course. double , because L[1].getList()[1] will return type Course and then Average() (method of Course) will return double. String , here L[1].Best() will return Course and getTitle() method of Course will return string. Instructor , A is the object of Instructor therefore its type will be Instructor. Student[ ] , method getStudent() of Instructor will return Instructor array. String , as in 8th que A.getStudent() will return Instructor array but here A.getStudent()[0] represents 0th element of Instructor array and getName() method of Instructor will return String. Student , L[0] is object of Student therefore its type will be Student.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.