In java, write a Student class that minimally stores the following data fields f
ID: 3825416 • Letter: I
Question
In java, write a Student class that minimally stores the following data fields for a student:
-Name String
-Student id number String
-Number of credits Int
-Total grade points earned Int
The following methods should also be provided:
-A constructor that initializes the name and id fields
-A method that returns the student name field
-A method that returns the student ID field
-A method that determines if two student objects are equal if their student id are the same (override equals from the class Object)
-Methods to set and retrieve the total number of credits
-Method to set and retrieve the total number of grade points earned
-A method that returns the GPA (grade points divided by credits)
Explanation / Answer
Here goes the required program with the required getters,setters and overiding the equals function.
Code:
public class Student{
String studentName;
String idNumber;
int noOfCredits;
int totalGradePoints;
Student(String name,String idNum){
studentName=name;
idNumber=idNum;
}
public String getStudentName(){
return studentName;
}
public String getIdNumber(){
return idNumber;
}
public void setTotalGradePoints(int tcp){
totalGradePoints=tcp;
}
public int getTotalGradePoints(){
return totalGradePoints;
}
public void setNoOfCredit(int noc){
noOfCredits=noc;
}
public int getNoOfCredit(){
return noOfCredits;
}
public int getGpa(){
return totalGradePoints/noOfCredits;
}
@Override
public boolean equals(Object other) {
if (!(other instanceof Student)) {
return false;
}
Student that = (Student) other;
// Custom equality check here.
return this.studentName.equals(that.studentName)
&& this.idNumber.equals(that.idNumber)&& (this.noOfCredits==that.noOfCredits)&& (this.totalGradePoints==that.totalGradePoints);
}
public static void main(String []args){
}
}
Hope this helps.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.