For the next eight questions, use the following class: public class Student { pr
ID: 3668402 • Letter: F
Question
For the next eight questions, use the following class:
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() {
String s = name + " " + major + " " + gpa + " " + hours;
return s;
}}
1.
Which of the following could be used to instantiate a new Student s1? Select all that apply.
Select one or more:
Student s1 = new Student("Jane Doe", "Computer Science", 3.333, 33);
a. s1 = new Student();
b. new Student s1 = ("Jane Doe", "Computer Science", 3.333, 33);
c. Student s1 = new Student();
d. new Student(s1);
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? Select all that apply.
Select one or more:
a. s1.toString();
b. s1.getHours();
c. s1.getClassRank();
d. s1 = getClassRank();
e. getClassRank(s1);
f. String s = s1.getClassRank();
What type of variable is name?
Select one:
a. instance data
b. local variable
c. formal parameter
d. actual parameter
e. none of the above
3.
What type of variable is newGPA?
Select one:
a. instance data
b. local variable
c. formal parameter
d. actual parameter
e. none of the above
What type of variable is s?
Select one:
a. instance data
b. local variable
c. formal parameter
d. actual parameter
e. none of the above
5. Write a statement to print a text representation of s1 to the console.
Answer:
6. Another method that might be desired is one to access a student's major. It is defined as:
public String getMajor()
What statement should be placed inside this method?
Answer:
7. Another method that might be desired is one to modify a student's name. What would be the method header for such a setter method?
answer:
Explanation / Answer
answers:
for question1:
Syntax for creation of object is;Classname objectname=new classname(pass arguments if any);
already Student s1 = new Student("Jane Doe", "Computer Science", 3.333, 33); is correct
for question2:
option 4 is correct
because that method returns String
Option b is correct
all class variables become local variables
for question3:
option c is correct
because function call arguments are called actual parameters
ex;fun1(x,b,c) x,b,c are called actual parameters
function declaration arguments are called formal parameters
Ex:fun1(int m,int j) here m,j are formal parameters
variable s is local variable so option b is correct
for question5:
Jane Doe Computer Science 3.333 33
for question6:
return major;
for question7:
public void setstudentname(String s)
{
}
EX;public void setstudentname(String s)
{
name=s;
}
setstudentname("smith");