I've got an assignment where I need to write a class that holds 3 test scores. The class constructor is suppose to accept 3 test scores and assign them to test score fields.
I'm starting off slow, and began with only writing 1 int for the class constructor. I've written the driver class where it'd only accept 1 also and it works fine.
The issue I'm running into is adding in more to the constructor.
Here's my code that works with one score.
//CLASS public class TestGrade2 { private int score; public TestGrade2(int s) { score = s; } public int getScore() { return score; } public char getLetterGrade() { char grade; if (score < 60) grade = 'F'; else if (score < 70) grade = 'D'; else if (score < 80) grade = 'C'; else if (score < 90) grade = 'B'; else grade = 'A'; return grade; } }
//DRIVER import java.util.Scanner; public class TestGrade2Demo { public static void main(String[] args) { int testScore; char letterGrade; Scanner keyboard = new Scanner(System.in); //first test System.out.print("Enter your first numeric grade"); testScore = keyboard.nextInt(); TestGrade2 test = new TestGrade2(testScore); letterGrade = test.getLetterGrade(); System.out.print("Your first grade is a " + test.getLetterGrade()); } }
Works fine.
Now I need to add another test score into here.
//Class public class TestGrade2 { private int score; private int score2; public TestGrade2(int s, int s2) { score = s; score2 = s2; } public int getScore() { return score; } public int getScore2() { return score2; } public char getLetterGrade() { char grade; if (score < 60) grade = 'F'; else if (score < 70) grade = 'D'; else if (score < 80) grade = 'C'; else if (score < 90) grade = 'B'; else grade = 'A'; return grade; } public char getLetterGrade2() { char grade2; if (score2 < 60) grade2 = 'F'; else if (score2 < 70) grade2 = 'D'; else if (score2 < 80) grade2 = 'C'; else if (score2 < 90) grade2 = 'B'; else grade2 = 'A'; return grade2; } }
//DRIVER import java.util.Scanner; public class TestGrade2Demo { public static void main(String[] args) { int testScore; int testScore2; char letterGrade; char letterGrade2; Scanner keyboard = new Scanner(System.in); //first test System.out.print("Enter your first numeric grade"); testScore = keyboard.nextInt(); //second test System.out.println("Enter your second numeric grade"); testScore2 = keyboard.nextInt(); TestGrade2 test = new TestGrade2(testScore, testScore2); letterGrade = test.getLetterGrade(); letterGrade2 = test.getLetterGrade2(); System.out.print("Your first grade is a " + test.getLetterGrade()); System.out.print("Your second grade is a " + test.getLetterGrade2()); } }
Second time around, compiles fine. Runs fine. Allows me to input both test scores. then - BOOM.
java.lang.NoSuchMethodError: TestGrade2.<init>(II)V
at TestGrade2Demo.main(TestGrade2Demo.java:25)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknow n Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Un known Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.ru nCommand(JavacCompiler.java:272)
Could someone help me here? What am I doing wrong?