I'm an APCS student and currently we are learning to build classes. In the code below, I've written most of the methods we need like setGpa,setName etc. I need help with the last methods which is compareToStudent. This method is intend to be used for like s2.compareToStudent(s1). So it should take student s1 and s2's gpa and compare them and print which is higher. I know how to compare using >,< == and print lines.
I need help on how to pass s2 as parameter and write method that obtain's s1 object's gpa and s2 object's gpa. How do you pass an object as parameter into that method and then use that for obtaining values.
Please Help. Thanks!
import java.util.*;
public class Student
{
//Private Variables (Available throughout Class)
private String firstName;
private String lastName;
private int grade;
private double gpa;
private int studentID;
Random rn = new Random();
public Student(String lName, String fName, int classGrade)
//Parameter
Constructor
{
lastName= lName;
firstName=fName;
grade=classGrade;
gpa=0.0;
studentID=rn.nextInt(201850)+201806;
}
public Student() //Default Constructor
{
firstName=null;
lastName=null;
grade = 9;
gpa = 0.00;
studentID=rn.nextInt(201850)+201806;
}
public void printStudentInfo()
{
System.out.println("Student Information");
System.out.println("Name : " +firstName+" "+lastName);
System.out.println("Grade : " +grade);
System.out.println("Gpa : " +gpa);
System.out.println("Student ID : " +studentID);
System.out.println();
}
public void setGpa(double newGpa)
{
gpa=newGpa;
}
public void setName(String newlastName, String newfirstName)
{
firstName=newfirstName;
lastName=newlastName;
}
public void setGrade(int newGrade)
{
grade=newGrade;
}
// Challenge Methods ///////////////////////
public void printGrad()
{
System.out.println("Here's the graduation status :");
if(grade==9)
System.out.println(firstName+ " " +lastName+ " is expected to graduate in
4 years.)");
if(grade==10)
System.out.println(firstName+ " " +lastName+ " is expected to graduate in
3 years.");
if(grade==11)
System.out.println(firstName+ " " +lastName+ " is expected to graduate in
2 years.");
if(grade==12)
System.out.println(firstName+ " " +lastName+ " is expected to graduate
this year.");
}
public void compareToStudent()
{
//NEED HELP HERE.
}
}