For questions 13-15, use the following class definition


For questions 13-15, use the following class definition

import java.text.DecimalFormat;

public class Student

{

private String name;

private String major;

private double gpa;

private int hours;

public Student(String newName, String newMajor, double newGPA, int newHours)

{

name = newName;

major = newMajor;

gpa = newGPA;

hours = newHours;

}

public String toString( )

{

DecimalFormat df = new DecimalFormat("xxxx"); // xxxx needs to be replaced

return name + "\n" + major + "\n" + df.format(gpa) + "\n" + hours

}

}

1) Which of the following could be used to instantiate a new Student s1?


a) Student s1 = new Student( );

b) s1 = new Student( );

c) Student s1 = new Student("Jane Doe", "Computer Science", 3.333, 33);

d) new Student s1 = ("Jane Doe", "Computer Science", 3.333, 33);

e) new Student(s1);

Answer: c. Explanation: To instantiate a class, the object is assigned the value returned by calling the constructor preceded by the reserved word new, as in new Student( ). The constructor might require parameters, and for Student, the parameters must be are two String values, a double, followed by an int.

2) Assume that another method has been defined that will compute and return the student’s class rank (Freshman, Sophomore, etc). It is defined as:public String getClassRank( )

 Given that s1 is a student, which of the following would properly be used to get s1’s class rank?


a) s1 = getClassRank( );

b) s1.toString( );

c) s1.getHours( );

d) s1.getClassRank( );

e) getClassRank(s1);

Answer: d. Explanation: To call a method of an object requires passing that object a message which is the same as the method name, as in object.methodname(parameters). In this situation, the object is s1, the method is getClassRank, and this method expects no parameters. Answers a and e are syntactically illegal while answer b returns information about the Student but not his/her class rank, and there is no “getHours” method so c is also syntactically illegal.