Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Beginner assignment - constructor issue / error

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Beginner assignment - constructor issue / error

    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?


  2. #2
    Member
    Join Date
    Apr 2012
    Posts
    42
    Thanks
    8
    Thanked 4 Times in 4 Posts

    Default Re: Beginner assignment - constructor issue / error

    I'm actually having a hard time pin-pointing whats wrong with your code myself. It looks fine to me, but you may want to add a .nextLine() command in between the two commands that get the users integers. This clears the buffer of any possible input and may help you. Also, while not completely related to your question, I noticed that in the last lines of your main method you have this code:

    System.out.print("Your first grade is a " + test.getLetterGrade());
      System.out.print("Your second grade is a " + test.getLetterGrade2());

    calling the .getLetterGrade() methods here is redundant, because you already called them above this code and assigned their return values to the two letterGrade variables. Calling the methods multiple times adds to the systems memory, and could potentially slow your program down. Instead of this, you should add your letterGrade variables to the end of your println() statements.

Similar Threads

  1. Help with beginner assignment
    By Jarruda in forum Object Oriented Programming
    Replies: 4
    Last Post: October 8th, 2012, 03:40 PM
  2. constructor issue
    By Reakwind in forum Java Theory & Questions
    Replies: 1
    Last Post: June 29th, 2012, 10:17 AM
  3. [SOLVED] Bad operand types for binary operator '<' error issue (beginner)
    By mju516 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: January 27th, 2012, 09:33 PM
  4. [SOLVED] Error in constructor
    By hello_world in forum What's Wrong With My Code?
    Replies: 9
    Last Post: August 14th, 2011, 07:46 PM
  5. Issue with beginner program
    By suxen in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 5th, 2011, 08:55 AM