I think my parameter is wrong?
INSTRUCTIONS:
Quote:
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:
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):
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: Quote:
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?
Re: I think my parameter is wrong?
The code BBCode isn't working, and the error is in the Runner now.
Re: I think my parameter is wrong?
Quote:
Originally Posted by
Calaminh
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:
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):
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.
Re: I think my parameter is wrong?
Quote:
Originally Posted by
Fubarable
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:
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.
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.