Goal: In this lab, we will practice how to write a more advanced class in Java,
ID: 3705860 • Letter: G
Question
Goal: In this lab, we will practice how to write a more advanced class in Java, which includes accessor/mutator methods, toString method, and this keyword. 1. Implement the Student Class. Implement the following class: public class Student * The basic feature of a student * public String name; private String student id; private double GPA; *Construct a student object (TWO constructors)* Accessors and mutators (one pair per each feature* * toString method */ Fill in the blanks above according to the guidelines in the comments. Also write a main method that will use the Student constructors to create two student objects. Your main method should print out the details of these two students.Explanation / Answer
class Student
{
public String name;
private String student_id;
private double GPA;
//constructors
public Student(String name,String student_id,double GPA)
{
this.name = name;
this.student_id = student_id;
this.GPA = GPA;
}
public Student(String student_id,double GPA)
{
this.student_id = student_id;
this.GPA = GPA;
}
//mutators and accessors
public void setName(String name)
{
this.name = name;
}
public String getName()
{
return name;
}
public void setStudent_ID(String student_id)
{
this.student_id = student_id;
}
public String getStudent_ID()
{
return student_id;
}
public void setGPA(double GPA)
{
this.GPA = GPA;
}
public double getGPA()
{
return GPA;
}
public String toString()
{
return " Student Name : "+name +" Student ID : "+student_id +" GPA : "+GPA;
}
}
class TestStudent
{
public static void main (String[] args)
{
//creating objects
Student s1 = new Student("Tim Lee","S3455",3.8);
Student s2 = new Student("Sandy Johnson","S3468",4.5);
//calling toString method
System.out.println(s1);
System.out.println(s2);
}
}
Output:
Student Name : Tim Lee Student ID : S3455 GPA : 3.8
Student Name : Sandy Johnson Student ID : S3468 GPA : 4.5
?Do ask if any doubt. Please upvote.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.