For the question below, use the following class definition. import java.text.Dec
ID: 3779083 • Letter: F
Question
For the question below, 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 + " " + major + " " + df.format(gpa) + " " + hours } } Which of the following could be used to instantiate a new Student s1? 1. Student s1 = new Student( ); 2. new Student s1 = ("Jane Doe", "Computer Science", 3.333, 33); 3. Student s1 = new Student("Jane Doe", "Computer Science", 3.333, 33); 4. new Student(s1); 5. s1 = new Student( );
Explanation / Answer
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");
return name + " " + major + " " + df.format(gpa) + " " + hours;
// xxxx needs to be replaced return name + " " + major + " " +
// df.format(gpa) + " " + hours
}
public static void main(String args[]) {
Student s1 = new Student("Jane Doe", "Computer Science", 3.333, 33); // valid
System.out.println(s1.toString());
}
}
//Note: please find below explanation on the object creation
1. Student s1 = new Student( ); // not valid since we don't have a default constructor
2. new Student s1 = ("Jane Doe", "Computer Science", 3.333, 33); //not valid
3. Student s1 = new Student("Jane Doe", "Computer Science", 3.333, 33); // valid since we have the constructor
4. new Student(s1); //not valid since we dont have the constructor with Student para
5. s1 = new Student( ); //not valid since we dont have the default constructor
----------------output---------
Jane Doe
Computer Science
xxxx3
33
------------output-----------
//Note: feel free to as question/doubts. God bless you!!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.