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 5 of 5

Thread: Constructor Undefined?

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

    Default Constructor Undefined?

    Hello,

    I am very new to Java, and I keep running into problems with constructors. Specifically, I am having trouble with this class & driver:

    <
    public class TestScores
    {
      private int score1;
      private int score2;
      private int score3;
    // Declares scores 1, 2, and 3 as private integer attributes
     
      public TestScore(int s1, int s2, int s3) // Creates a constructor that accepts 3 arguments
      {
        score1 = s1;
        score2 = s2;
        score3 = s3;
      }
     
      public void setScore1(int s1) // Creats a mutator method that sets score1 (s1)
      {
        score1 = s1;
      }
     
      public void setScore2(int s2) // Creats a mutator method that sets score2 (s2)
      {
        score2 = s2;
      }
     
      public void setScore3(int s3) // Creats a mutator method that sets score3 (s3)
      {
        score3 = s3;
      }
     
      public int getScore1() // Creates an accessor method that gets score1
      {
        return score1;
      }
     
      public int getScore2() // Creates an accessor method that gets score2
      {
        return score2;
      }
     
      public int getScore3() // Creates an accessor method that gets score3
      {
        return score3;
      }
     
      public char getLetterGrade() /* This method determines a letter grade average
                                    * based on the 3 scores entered */
      {
        char grade;
        int score;
        score = (score1*score2*score3) / 3
     
        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;
     
      }
    }
    >


    <
    import java.util.Scanner;  // Imports the Scanner class
     
    public class TestScoreDriver
     
    /* This program uses the TestScores class to determine
     * the letter grade calculated by the average of
     * the three test scores entered*/
     
    {
      public static void main(String[] args)
      {
        int testScore1, testScore2, testScore3; // Each variable holds 1 test score
        char letterGrade; // Holds a letter grade
     
        // Creates a Scanner object to read input
        Scanner keyboard = new Scanner(System.in);
     
        // Get the first test score
        System.out.println("Enter your first numeric test score");
        testScore1 = keyboard.nextInt();
     
        // Get the second test score
        System.out.println("Enter your second numeric test score");
        testScore2 = keyboard.nextInt();
     
        // Get the third test score
        System.out.println("Enter your third numeric test score");
        testScore3 = keyboard.nextInt();
     
        // Create a TestScores object with the numeric scores
        TestScores test = new TestScores(testScore1, testScore2, testScore3);
     
        // Get the letter grade of the test average
        letterGrade = test.getLetterGrade();
     
        // Display the grade
        System.out.println("Your test average grade is " + test.getLetterGrade());
      }
    }
    >

    The error message says "The constructor TestScores(int, int, int) is undefined"

    I don't understand this. Please help?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Constructor Undefined?

    Please edit your post and wrap your code with code tags:
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.

    constructor TestScores(int, int, int) is undefined
    The compiler can not find the definition for a constructor in the TestScores that takes 3 int values.
    Look at the TestScores class and look at its constuctors to see what are available.

    A class can have many different constructors each of which take different arguments.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    JessicaCouture95 (October 20th, 2013)

  4. #3
    Junior Member
    Join Date
    Oct 2013
    Posts
    8
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Constructor Undefined?

    The class has the constructor
    <
    public TestScore(int s1, int s2, int s3) 
      {
        score1 = s1;
        score2 = s2;
        score3 = s3;
      }
    >


    --- Update ---

    I just found the error in my program. It was simply a syntax error in my constructor! Thank you.

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Constructor Undefined?

    What error messages do you get when you compile the class? Do you get more than the one that you posted? I get lots of errors from the posted code. Did you post the current version of the code for the class?
    If you don't understand my answer, don't ignore it, ask a question.

  6. The Following User Says Thank You to Norm For This Useful Post:

    JessicaCouture95 (October 20th, 2013)

  7. #5
    Junior Member
    Join Date
    Oct 2013
    Posts
    8
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Constructor Undefined?

    After fixing the constructor error, I found many more problems within my code. Luckily, I was able to quickly figure them out. They were just minor errors. The code now works. But thank you!

Similar Threads

  1. Method getCurrentSession() is Undefined for SessionFactory
    By freakycoder in forum Web Frameworks
    Replies: 2
    Last Post: August 21st, 2012, 11:26 AM
  2. which class has a default constructor? (Req. guidance on constructor)
    By DragBall in forum Java Theory & Questions
    Replies: 1
    Last Post: June 27th, 2012, 04:42 PM
  3. method undefined error
    By Herah in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 29th, 2011, 08:38 PM
  4. constructor X is undefined
    By ober0330 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: August 24th, 2011, 01:51 PM
  5. Undefined Array Problem
    By oakheart in forum Other Programming Languages
    Replies: 2
    Last Post: April 18th, 2011, 09:19 AM

Tags for this Thread