-1

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.
  } 
  }
Aditya Sood
  • 1
  • 1
  • 2
  • Please mention what you have tried. As a heads up, you will be comparing an instance of `Student` against the value you shall be passing. – CS_noob Nov 14 '18 at 18:16
  • What specifically about passing it as a parameter and using it do you need help with? You seem to understand how to give a function a parameter, since you did so with `setGpa`. – Carcigenicate Nov 14 '18 at 18:16
  • Note that your code isn't valid Java code, and that it's very hard to read, because it's not indented properly. So you're shooting yourself in the foot by writing code that you have a hard time to read and understand. – JB Nizet Nov 14 '18 at 18:17
  • When you do `setName` you are passing in a pair of Strings for the name. The same concept applies to passing any other Object. – Compass Nov 14 '18 at 18:17
  • `public void compareToStudent(Student s2)` and then evaluate the objects properties – the_dani Nov 14 '18 at 18:18
  • First read about implementing an interface, then read about Comparable interface. https://docs.oracle.com/javase/7/docs/api/java/lang/Comparable.html – Liad Saubron Nov 14 '18 at 18:18

3 Answers3

0

Welcome and good luck with your studies. The compareToStudent method needs to take a Student parameter. An example of a parameter is newGrade in the setGrade method. However, for compareToStudent the parameter will be of type Student. Once you have that, the compareToStudent method can compare field values from the Student instance it is within and the passed in Student, such as String.compare(this.name, student.name)

When you've tried that, if you need more help, update the question with the updated code and more specific problems.

John Camerin
  • 677
  • 5
  • 15
0

You can accomplish what you want like this:

public void compareToStudent(Student other) {
    System.out.println("This student: " + getGPA());
    System.out.println("Parameter student: " + other.getGPA());

    if (other.getGPA() > getGPA()) {
        // Do stuff
    }
}

We pass another Student object into this method and can access methods through other.whatevermethod and do stuff with it.

I also added a function called getGPA() which is just this:

public int getGPA() {
    return grade;
}
Mark
  • 5,089
  • 2
  • 20
  • 31
0

You can implement your method like this,

public void compareToStudent(Student otherStudent) {
    if (this.getGpa() < otherStudent.getGpa()) {
        System.out.println("Other student has a higher gpa");
    } else if (this.getGpa() > otherStudent.getGpa()) {
        System.out.println("Other student has a lower gpa");
    } else {
        System.out.println("Both students have same gpa");
    }
}

public double getGpa() {
    return gpa;
}

public static void main(String args[]) {
    Student student1 = new Student("Tyson", "Mike", 8);
    Student student2 = new Student("Reid", "Bruce", 7);

    student1.setGpa(4.0); // Set the gpas for both students or you could create one more parameter in your constructor class for taking gpa
    student2.setGpa(4.5);

    student1.compareToStudent(student2);
}

When you implement an instance method in a class, this refers to the current object on which the method gets invoked, using which you can access the values stored in it.

And other student object you can take as parameter, using which you can compare the values of two objects and implement stuff the way you want.

Also, you should always generate getters/setters for variables that you declare private, so they can be accessed using them. I've implemented getGPpa() and you can see its usage in the method compareToStudent

Finally main(String args[]) method shows how you can call the method and print the results.

Pushpesh Kumar Rajwanshi
  • 18,127
  • 2
  • 19
  • 36