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

Thread: Average score assignment

  1. #1
    Junior Member
    Join Date
    Sep 2014
    Location
    San Diego
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Average score assignment

    I've been working on this same assignment using netbeans. I've completed the exercise and when I run it, it works. However, netbeans still had errors listed in the left hand numerical column and it asks me if I still want to run the program with errors when I hit run? Should I submit this, or does anyone see any errors I should fix? Here is what I have so far:

     package TestScores;
     
    import java.util.Scanner;
    // Name: Joe 
    // Date: ........
    // Desc: Test Score Averages
     
    public class TestScoresAverage
    {
    public class TestScores 
    //create 
    {
    private double score1;
    private double score2;
    private double score3;
     
    public TestScores(double score1, double score2, double score3) 
    {
    this.score1 = score1;
    this.score2 = score2;
    this.score3 = score3;
    }
    public void setScore1(double score) 
    {
    score1 = score;
    }
    public void setScore2(double score) 
    {
    score2 = score;
    }
    public void setScore3(double score) 
    {
    score3 = score;
    }
    public double getScore1() 
    {
    return score1;
    }
    public double getScore2() 
    {
    return score2;
    }
    public double getScore3() 
    {
    return score3;
    }
    public double getAverageScore() 
    {
    return (score1 + score2 + score3) / 3;
    }
    }
    public static void main(String[] args) 
    {
    double test1;
    double test2;
    double test3;
     
    // Create a scanner for keyboard score input.
     
    Scanner keyboard = new Scanner(System.in);
     
    System.out.print("Enter test score: ");
    test1 = keyboard.nextDouble();
    System.out.print("Enter test score: ");
    test2 = keyboard.nextDouble();
    System.out.print("Enter test score: ");
    test3 = keyboard.nextDouble();
     
    // Close Scanner
     
    keyboard.close();
     
    TestScoresAverage classProgram = new TestScoresAverage();
    TestScores scores = classProgram.new TestScores(test1, test2, test3);
     
    // Output Display Average Score
     
    System.out.println("The average test score: "
    + scores.getAverageScore());
    }
    }
    Thank you, I found this thread while doing a google search trying to find out the error in my code.
    Last edited by Wreckingball; September 27th, 2014 at 02:39 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Average score assignment

    Try using the javac command to check for errors. Be sure to use the -Xlint option also. It will show bad code also.

    post moved to own thread.

    One problem with the code is it has lost its formatting/indentations for nested statements.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Average score assignment

    Why are you using this inner class construction:

    TestScores scores = classProgram.new TestScores(test1, test2, test3);

    Why not eliminate the class TestScores and fall back to TestScoresAverage as the only class?

  4. #4
    Junior Member
    Join Date
    Sep 2014
    Location
    San Diego
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Average score assignment

    Quote Originally Posted by Norm View Post
    Try using the javac command to check for errors. Be sure to use the -Xlint option also. It will show bad code also.

    post moved to own thread.

    One problem with the code is it has lost its formatting/indentations for nested statements.
    Ok, Thank you.

    --- Update ---

    Quote Originally Posted by GregBrannon View Post
    Why are you using this inner class construction:

    TestScores scores = classProgram.new TestScores(test1, test2, test3);

    Why not eliminate the class TestScores and fall back to TestScoresAverage as the only class?
    Because I'm bulldogging my way through the code. Taking my Java course online and I have very little comp experience since I've been making my living as a mechanic for the last 16 years (currently). I'm lucky I know how to use Microsoft Word.... Excel = forget it

  5. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Average score assignment

    forget it
    No, that's not appropriate. Sometimes instructors ask for or require weird things to have that "teachable moment" . . . I was just trying to figure out where your weird thing was coming from.

    Don't give up on you or us, come back with additional questions if you need help.

  6. #6
    Junior Member
    Join Date
    Sep 2014
    Location
    San Diego
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Average score assignment

    Quote Originally Posted by GregBrannon View Post
    No, that's not appropriate.
    Since I'm not a regular computer guy, I think my attempt at sarcasm failed. I do quiet a bit of GIS in school and I export my attribute tables into an .Xlxs when I have a lot of data that needs to be sorted quickly then imported back into my .shp. I should have just said my excel skills are limited.

    So far, when I've been stuck trying to figure out a problem, I've been pretty successful using the search function to read through other posts. If I get stuck and need help, I'll be sure to ask. Thank you

  7. #7
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Average score assignment

    Sarcasm just doesn't work well in this format, and programmers tend to be literal.

    Figuring it out yourself will likely result in better learning, anyway, but don't hesitate to ask for help if you need to.

    Completing my point about the inner class design, rather than using the inner class, I suggest moving it outside like this (two existing lines were commented out and a replacement line was added, all commented):
    import java.util.Scanner;
    // Name: Joe 
    // Date: ........
    // Desc: Test Score Averages
     
    public class TestScoresAverage
    {
        public static void main(String[] args) 
        {
            double test1;
            double test2;
            double test3;
     
            // Create a scanner for keyboard score input.
     
            Scanner keyboard = new Scanner(System.in);
     
            System.out.print("Enter test score: ");
            test1 = keyboard.nextDouble();
            System.out.print("Enter test score: ");
            test2 = keyboard.nextDouble();
            System.out.print("Enter test score: ");
            test3 = keyboard.nextDouble();
     
            // Close Scanner
     
            keyboard.close();
     
            // moving TestScores from inside TestScoresAverage changes the
            // following two lines to the third:
            // TestScoresAverage classProgram = new TestScoresAverage();
            // TestScores scores = classProgram.new TestScores(test1, test2, test3);
     
            TestScores scores = new TestScores(test1, test2, test3);
     
            // Output Display Average Score
     
            System.out.println("The average test score: "
                    + scores.getAverageScore());
        }
    }
     
    // this class was moved from being an inner class to the top level. note that
    // only one top-level class in the same file can be 'public'
    class TestScores 
    //create 
    {
        private double score1;
        private double score2;
        private double score3;
     
        public TestScores(double score1, double score2, double score3) 
        {
            this.score1 = score1;
            this.score2 = score2;
            this.score3 = score3;
        }
        public void setScore1(double score) 
        {
            score1 = score;
        }
        public void setScore2(double score) 
        {
            score2 = score;
        }
        public void setScore3(double score) 
        {
            score3 = score;
        }
        public double getScore1() 
        {
            return score1;
        }
        public double getScore2() 
        {
            return score2;
        }
        public double getScore3() 
        {
            return score3;
        }
        public double getAverageScore() 
        {
            return (score1 + score2 + score3) / 3;
        }
    }

Similar Threads

  1. [SOLVED] Average Rainfall Main Class Using nested for loops,input validation - Average is off
    By CyberOps in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 24th, 2014, 05:36 AM
  2. How to display each score in a competition?
    By seaofFire in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 26th, 2012, 06:10 PM
  3. Dice score program
    By ksahakian21 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: April 23rd, 2012, 05:25 PM
  4. How to get the list of highest score
    By pogi in forum Java Theory & Questions
    Replies: 4
    Last Post: July 1st, 2010, 09:50 PM
  5. Help with score board!
    By vlan in forum Java Applets
    Replies: 0
    Last Post: June 2nd, 2010, 04:34 AM