How to pass an object as a parameter in java and compare values of some it to another object










-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.











share|improve this question






















  • 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















-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.











share|improve this question






















  • 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













-1












-1








-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.











share|improve this question














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.








java drjava






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 18:13









Aditya SoodAditya Sood

11




11












  • 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

















  • 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
















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





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





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






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





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





public void compareToStudent(Student s2) and then evaluate the objects properties

– the_dani
Nov 14 '18 at 18:18












3 Answers
3






active

oldest

votes


















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.






share|improve this answer






























    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;






    share|improve this answer






























      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.






      share|improve this answer























      • Thanks for the help!

        – Aditya Sood
        Nov 15 '18 at 19:11










      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53306445%2fhow-to-pass-an-object-as-a-parameter-in-java-and-compare-values-of-some-it-to-an%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      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.






      share|improve this answer



























        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.






        share|improve this answer

























          0












          0








          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.






          share|improve this answer













          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.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 14 '18 at 18:22









          John CamerinJohn Camerin

          436211




          436211























              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;






              share|improve this answer



























                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;






                share|improve this answer

























                  0












                  0








                  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;






                  share|improve this answer













                  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;







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 14 '18 at 18:24









                  MarkMark

                  3,69921126




                  3,69921126





















                      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.






                      share|improve this answer























                      • Thanks for the help!

                        – Aditya Sood
                        Nov 15 '18 at 19:11















                      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.






                      share|improve this answer























                      • Thanks for the help!

                        – Aditya Sood
                        Nov 15 '18 at 19:11













                      0












                      0








                      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.






                      share|improve this answer













                      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.







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Nov 14 '18 at 18:38









                      Pushpesh Kumar RajwanshiPushpesh Kumar Rajwanshi

                      8,30521027




                      8,30521027












                      • Thanks for the help!

                        – Aditya Sood
                        Nov 15 '18 at 19:11

















                      • Thanks for the help!

                        – Aditya Sood
                        Nov 15 '18 at 19:11
















                      Thanks for the help!

                      – Aditya Sood
                      Nov 15 '18 at 19:11





                      Thanks for the help!

                      – Aditya Sood
                      Nov 15 '18 at 19:11

















                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53306445%2fhow-to-pass-an-object-as-a-parameter-in-java-and-compare-values-of-some-it-to-an%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      這個網誌中的熱門文章

                      What does pagestruct do in Eviews?

                      Dutch intervention in Lombok and Karangasem

                      Channel Islands