Q1:Write a public Java class called Student that represents one student. The cla
ID: 638870 • Letter: Q
Question
Q1:Write a public Java class called Student that represents one student. The class should include:
- Instance variables ID (int), firstName (String), and GPA (double).
- A constructor with no parameters that sets the instance variables, respectively, to these values: -111 (not set) -2.22
- A constructor with three parameters that sets the instance variables to the parameter values.
- Getter methods for each instance variable.
- Setter methods for each instance variable.
- equals and toString methods.
Q2: Write a Java console application that uses the Student class created in Question 1 to create three student objects:
- s1 is created with the no-parameter constructor.
- s2 is created with the no-parameter constructor but the user is prompted for the student data.
- s3 is created with the three-parameter constructor. Use these values:
78
Mary
3.25
Use the Java project created in Question 1 and simply add a class to it called StudentDemo. Print the information stored in each student object. Compare and print whether student objects s2 and s3 are equal.
Explanation / Answer
import java.util.Scanner;
class Student {
private int ID;
private String firstName;
private double GPA;
//default constructor
public Student() {
this.id=-111;
this.gpa=-2.22;
this.firstName="Not set";
}
//paramaterized constructor
public Student(int ID,String firstName,double GPA) {
this.GPA=GPA;
this.ID=ID;
this.firstName=firstName;
}
public int getID() {
return ID;
}
public void setID(int iD) {
ID = iD;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public double getGPA() {
return GPA;
}
public void setGPA(double gPA) {
GPA = gPA;
}
//overrides the equals method and returns the student info
@Override
public String toString() {
return this.firstName+" having ID "+this.ID+" has scored "+this.GPA+" GPA";
}
//overrides the equals method to check the two students are equal if they have same name,id and gpa
@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (!(obj instanceof Student)) {
return false;
}
Student student=(Student)obj;
return this.ID==student.getID()&&this.firstName.equals(student.getFirstName())&&Double.compare(this.GPA, student.getGPA())==0;
}
}
public class StudentDemo {
public static void main(String[] args) {
Student s1=new Student();
Student s2=new Student();
System.out.println("Please enter first name : ");
Scanner sc=new Scanner(System.in);
String enteredfirstName=sc.next();
System.out.println("Please enter Id : ");
int enteredId=sc.nextInt();
System.out.println("Please enter GPA : ");
double enteredGPA=sc.nextDouble();
s2.setFirstName(enteredfirstName);
s2.setID(enteredId);
s2.setGPA(enteredGPA);
sc.close();
Student s3=new Student(78, "MARY", 3.25);
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
if(s2.equals(s3)){
System.out.println("s2 and s3 are equal");
}else{
System.out.println("s2 and s3 are not equal");
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.