Results 1 to 5 of 5
  1. #1
    Calaminh is offline Member
    Join Date
    Nov 2011
    Posts
    15
    Rep Power
    0

    Default I think my parameter is wrong?

    INSTRUCTIONS:
    1) Modify your Grade class to have a method named compareTo. This method must take in another Grade as a parameter and return an int.



    The int returned must follow a couple rules:



    it must return a negative int if ‘this’ Grade is lower than the incoming grade. It does not matter the value of the int.



    it must return a 0 if ‘this’ Grade is equal to the incoming grade.



    it must return a positive int if ‘this’ Grade is higher than the incoming grade. It does not matter the value of the int.
    RUNNER:

    Java Code:
    import java.util.Scanner;
    public class GradeRunner2
    {
    public static void main(String[] args)
    {
    Scanner scan = new Scanner(System.in);
    
    System.out.println("Enter the first grade: ");
    Grade grade1 = new Grade(scan.nextLine());
    
    System.out.println("Enter the second grade: ");
    Grade grade2 = new Grade(scan.nextLine());
    
    if( grade1.compareTo(grade2) < 0)
    {
    System.out.println(grade1.getLetterGrade() +" is lower than "+ grade2.getLetterGrade());
    }
    else if( grade1.compareTo(grade2) > 0)
    {
    System.out.println(grade1.getLetterGrade() +" is higher than "+ grade2.getLetterGrade());
    }
    else
    {
    System.out.println(grade1.getLetterGrade() +" is equal to "+ grade2.getLetterGrade());
    }
    
    }
    }
    MY PROGRAM (VERY LAST METHOD):

    Java Code:
    public class Grade
    {
        private String myLetterGrade;
        private double myNumericGrade;
        
        public Grade( double GPA )
        
        {
            if(GPA == 0.0)
            {
                myLetterGrade = "F";
            }
            else if( GPA>=0.1 && GPA<=0.7 )
            {
                myLetterGrade = "D-";
            }
            else if( GPA>=0.8 && GPA<=1.0 )
            {
                myLetterGrade = "D";
            }
            else if( GPA>=1.1 && GPA<=1.3 )
            {
                myLetterGrade = "D+";
            }
            else if( GPA>=1.4 && GPA<=1.7 )
            {
                myLetterGrade = "C-";
            }
            else if( GPA>=1.8 && GPA<=2.0 )
            {
                myLetterGrade = "C";
            }
            else if( GPA>=2.1 && GPA<=2.3 )
            {
                myLetterGrade = "C+";
            }
            else if( GPA>=2.4 && GPA<=2.7 )
            {
                myLetterGrade = "B-";
            }
            else if( GPA>=2.8 && GPA<=3.0 )
            {
                myLetterGrade = "B";
            }
            else if( GPA>=3.1 && GPA<=3.3 )
            {
                myLetterGrade = "B+";
            }
            else if( GPA>=3.4 && GPA<=3.7 )
            {
                myLetterGrade = "A-";
            }
            else if( GPA>=3.8 && GPA<=4.0 )
            {
                myLetterGrade = "A";
            }
            else
            {
                myLetterGrade = "A+";
            }
        }
    
        public Grade( String letter )
        
        {
            if( letter.equals("F") )
            {
                myNumericGrade = 0.0;
            }
            else if( letter.equals("D-") )
            {
                myNumericGrade = 0.7;
            }
            else if( letter.equals("D") )
            {
                myNumericGrade = 1.0;
            }
            else if( letter.equals("D+") )
            {
                myNumericGrade = 1.3;
            }
            else if( letter.equals("C-") )
            {
                myNumericGrade = 1.7;
            }
            else if( letter.equals("C") )
            {
                myNumericGrade = 2.0;
            }
            else if( letter.equals("C+") )
            {
                myNumericGrade = 2.3;
            }
            else if( letter.equals("B-") )
            {
                myNumericGrade = 2.7;
            }
            else if( letter.equals("B") )
            {
                myNumericGrade = 3.0;
            }
            else if( letter.equals("B+") )
            {
                myNumericGrade = 3.3;
            }
            else if( letter.equals("A-") )
            {
                myNumericGrade = 3.7;
            }
            else if( letter.equals("A") )
            {
                myNumericGrade = 4.0;
            }   
            else
            {
                myNumericGrade = 4.0;       
            }
        } 
        
        public String getLetterGrade()
        {
            return myLetterGrade;
        }
    
        public double getNumericGrade()
        {
            return myNumericGrade;
        }
        
        public int compareTo(Grade A, Grade B)
        {
            String C = A.getLetterGrade();
            String D = B.getLetterGrade();
            
            if(C.compareTo(B) > 0)
            {
                return 1;
            }
            else if(B.compareTo(B) < 0)
            {
                return -1;    
            }
            else
            {
                return 0;    
            }
        }
    }
    ERROR IN THE RUNNER:
    compareTo(Grade,Grade) in Grade cannot be applied to (Grade)
    What my problem is:

    I made two grades to compare to one another, but I think that's wrong - is that not what the instructions told me to do? What should I do?
    Last edited by Calaminh; 11-14-2011 at 04:15 AM.

  2. #2
    Calaminh is offline Member
    Join Date
    Nov 2011
    Posts
    15
    Rep Power
    0

    Default Re: I think my parameter is wrong?

    The code BBCode isn't working, and the error is in the Runner now.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: I think my parameter is wrong?

    Quote Originally Posted by Calaminh View Post
    INSTRUCTIONS:

    1) Modify your Grade class to have a method named compareTo. This method must take in another Grade as a parameter and return an int.
    The int returned must follow a couple rules:
    it must return a negative int if ‘this’ Grade is lower than the incoming grade. It does not matter the value of the int.
    it must return a 0 if ‘this’ Grade is equal to the incoming grade.
    it must return a positive int if ‘this’ Grade is higher than the incoming grade. It does not matter the value of the int.

    RUNNER:

    Java Code:
    import java.util.Scanner;
    public class GradeRunner2
    {
    public static void main(String[] args)
    {
    Scanner scan = new Scanner(System.in);
    
    System.out.println("Enter the first grade: ");
    Grade grade1 = new Grade(scan.nextLine());
    
    System.out.println("Enter the second grade: ");
    Grade grade2 = new Grade(scan.nextLine());
    
    if( grade1.compareTo(grade2) < 0)
    {
    System.out.println(grade1.getLetterGrade() +" is lower than "+ grade2.getLetterGrade());
    }
    else if( grade1.compareTo(grade2) > 0)
    {
    System.out.println(grade1.getLetterGrade() +" is higher than "+ grade2.getLetterGrade());
    }
    else
    {
    System.out.println(grade1.getLetterGrade() +" is equal to "+ grade2.getLetterGrade());
    }
    
    }
    }
    MY PROGRAM (VERY LAST METHOD):

    Java Code:
    public class Grade
    {
        private String myLetterGrade;
        private double myNumericGrade;
        
        public Grade( double GPA )
        
        {
            if(GPA == 0.0)
            {
                myLetterGrade = "F";
            }
            else if( GPA>=0.1 && GPA<=0.7 )
            {
                myLetterGrade = "D-";
            }
            else if( GPA>=0.8 && GPA<=1.0 )
            {
                myLetterGrade = "D";
            }
            else if( GPA>=1.1 && GPA<=1.3 )
            {
                myLetterGrade = "D+";
            }
            else if( GPA>=1.4 && GPA<=1.7 )
            {
                myLetterGrade = "C-";
            }
            else if( GPA>=1.8 && GPA<=2.0 )
            {
                myLetterGrade = "C";
            }
            else if( GPA>=2.1 && GPA<=2.3 )
            {
                myLetterGrade = "C+";
            }
            else if( GPA>=2.4 && GPA<=2.7 )
            {
                myLetterGrade = "B-";
            }
            else if( GPA>=2.8 && GPA<=3.0 )
            {
                myLetterGrade = "B";
            }
            else if( GPA>=3.1 && GPA<=3.3 )
            {
                myLetterGrade = "B+";
            }
            else if( GPA>=3.4 && GPA<=3.7 )
            {
                myLetterGrade = "A-";
            }
            else if( GPA>=3.8 && GPA<=4.0 )
            {
                myLetterGrade = "A";
            }
            else
            {
                myLetterGrade = "A+";
            }
        }
    
        public Grade( String letter )
        
        {
            if( letter.equals("F") )
            {
                myNumericGrade = 0.0;
            }
            else if( letter.equals("D-") )
            {
                myNumericGrade = 0.7;
            }
            else if( letter.equals("D") )
            {
                myNumericGrade = 1.0;
            }
            else if( letter.equals("D+") )
            {
                myNumericGrade = 1.3;
            }
            else if( letter.equals("C-") )
            {
                myNumericGrade = 1.7;
            }
            else if( letter.equals("C") )
            {
                myNumericGrade = 2.0;
            }
            else if( letter.equals("C+") )
            {
                myNumericGrade = 2.3;
            }
            else if( letter.equals("B-") )
            {
                myNumericGrade = 2.7;
            }
            else if( letter.equals("B") )
            {
                myNumericGrade = 3.0;
            }
            else if( letter.equals("B+") )
            {
                myNumericGrade = 3.3;
            }
            else if( letter.equals("A-") )
            {
                myNumericGrade = 3.7;
            }
            else if( letter.equals("A") )
            {
                myNumericGrade = 4.0;
            }   
            else
            {
                myNumericGrade = 4.0;       
            }
        } 
        
        public String getLetterGrade()
        {
            return myLetterGrade;
        }
    
        public double getNumericGrade()
        {
            return myNumericGrade;
        }
        
        public int compareTo(Grade A, Grade B)
        {
            String C = A.getLetterGrade();
            String D = B.getLetterGrade();
            
            if(C.compareTo(B) > 0)
            {
                return 1;
            }
            else if(B.compareTo(B) < 0)
            {
                return -1;    
            }
            else
            {
                return 0;    
            }
        }
    }
    ERROR IN THE RUNNER:

    What my problem is:

    I made two grades to compare to one another, but I think that's wrong - is that not what the instructions told me to do? What should I do?

    The instructions state: "This method must take in another Grade as a parameter and return an int. "

    To my eye, that tells me that it takes only one parameter, another Grade object, not two. You will compare the current instance's letter grade (the letter grade of "this") with the parameter's letter grade value and return your int based on that.

  4. #4
    Calaminh is offline Member
    Join Date
    Nov 2011
    Posts
    15
    Rep Power
    0

    Default Re: I think my parameter is wrong?

    Quote Originally Posted by Fubarable View Post
    The instructions state: "This method must take in another Grade as a parameter and return an int. "

    To my eye, that tells me that it takes only one parameter, another Grade object, not two. You will compare the current instance's letter grade (the letter grade of "this") with the parameter's letter grade value and return your int based on that.
    I just don't understand this.

    I know I'm supposed to compare A to B (as indicated in the runner), but how am I suppose to do that with only one object in the parameter?

    Is it:

    Java Code:
        public int compareTo(Grade A)
        {
            String C = A.getLetterGrade();
            
            if(this.getLetterGrade().compareTo(C) > 0)
            {
                return 1;
            }
            else if(this.getLetterGrade().compareTo(C) < 0)
            {
                return -1;    
            }
            else
            {
                return 0;    
            }
        }
    When I run it, I get a null pointer exception.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: I think my parameter is wrong?

    You're supposed to compare to Grade objects, one is the one calling the method, and the other is passed into the parameter. So inside of the method, the letterGrade variable refers to the object that is calling the method, and the parameter's letterGrade variable is the one you're comparing it to.

Similar Threads

  1. Replies: 4
    Last Post: 06-11-2013, 01:37 AM
  2. Type parameter
    By Pojahn_M in forum New To Java
    Replies: 10
    Last Post: 09-20-2011, 12:20 AM
  3. Class as a Parameter.
    By Soxman04 in forum New To Java
    Replies: 2
    Last Post: 05-12-2011, 06:36 PM
  4. how to pass parameter from one jsp to another jsp
    By kader_sit in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 06-10-2010, 02:04 PM
  5. Can a method take itself as parameter?
    By bukake in forum New To Java
    Replies: 10
    Last Post: 09-06-2008, 09:26 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •