Results 1 to 5 of 5
  1. #1
    Estrex is offline Member
    Join Date
    Dec 2012
    Posts
    7
    Rep Power
    0

    Default Can someone help me with this array assignment?

    I'm supposed to make a class where the user inputs how many scores they want. That would start the array by letting them input values for score0, score1, score2, etc. They also have to input how many points are possible out of those respective scores. Then the program is supposed to take and print out the average of the scores and then say which score is the highest score and which score is the lowest. This part is the hardest to figure out in my opinion. This is what I have so far:

    Java Code:
    import java.util.Scanner;
    public class Grade
    {
        public static void main (String [] args)
        {
            Scanner scan = new Scanner (System.in);
            System.out.print ("How many scores, bitch?: ");
            int input = scan.nextInt();
            scan.nextLine();
            Grade[]scores = new Grade [input];
            int score, points;
            for (int i = 0; i < input; i++)
            {
                System.out.print ("Score"+i+": ");
                score = scan.nextInt();
                System.out.print ("Points possible: ");
                points = scan.nextInt();
            }
        }
    }
    I'm also not sure if you're supposed to use methods in this program so I made one just to be safe:

    Java Code:
    public class GradeMethod
    {
        int score1, point1, sum = 0, sum2 = 0;
        public GradeMethod (int score, int points)
        {
            score1 = score;
            point1 = points;
        }
        public int addScore()
        {
            score1 += sum;
            return score1;
        }
        public int addPoints()
        {
            point1 += sum2;
            return point1;
        }
    }
    I can't figure out how to get the average or the highest and lowest score from Grade class above. I tried using the GradeMethod class to add the array values to the variables sum and sum2, but it printed out values of 0 every time, so now I'm stuck.
    Last edited by Fubarable; 01-27-2013 at 04:33 AM. Reason: code tags added.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Can someone help me with this array assignment?

    I've added [code] [/code] tags to your post above to help make it readable. Please notice that without tags, code loses its formatting:

    import java.util.Scanner;
    public class Grade
    {
    public static void main (String [] args)
    {
    Scanner scan = new Scanner (System.in);
    System.out.print ("How many scores, bitch?: ");
    int input = scan.nextInt();
    scan.nextLine();
    Grade[]scores = new Grade [input];
    int score, points;
    for (int i = 0; i < input; i++)
    {
    System.out.print ("Score"+i+": ");
    score = scan.nextInt();
    System.out.print ("Points possible: ");
    points = scan.nextInt();
    }
    }
    }

    but with tags, it retains its formatting and becomes more readable:

    Java Code:
    import java.util.Scanner;
    public class Grade
    {
        public static void main (String [] args)
        {
            Scanner scan = new Scanner (System.in);
            System.out.print ("How many scores, bitch?: ");
            int input = scan.nextInt();
            scan.nextLine();
            Grade[]scores = new Grade [input];
            int score, points;
            for (int i = 0; i < input; i++)
            {
                System.out.print ("Score"+i+": ");
                score = scan.nextInt();
                System.out.print ("Points possible: ");
                points = scan.nextInt();
            }
        }
    }
    Now regarding your problem, you will want to break down the big problem into very small steps, and then solve each step one at a time. So first solve how to get the number of total scores from the user, and then use a for loop to ask the user for a score and get the input. Use System.out.println(...) methods to test that the code is in fact working (these methods will be removed from your finished code). Once you've got that working, then work on trying to sum all the scores entered. Once that code has been well tested and is working well, work on isolating the maximum and minimum score, once that has been tested and is working well,... etc.

    Note that in the second part of your question you state that you've created a "method" but have done nothing of the kind. You've instead created a class, one that really doesn't do much useful. I'd get rid of that code. Also in your first code block you create a Score array, Score[], but this makes little sense since you're not really using Score objects. Get rid of that array.

  3. #3
    Estrex is offline Member
    Join Date
    Dec 2012
    Posts
    7
    Rep Power
    0

    Default Re: Can someone help me with this array assignment?

    I wasn't sure if you even needed methods for this assignment, so I just put a class there so you can see my intentions if I do end up utilizing some methods. And I thought I needed the score array to loop the user inputting the scores. I can call the array whatever I want, so I can change into something more appropriate in this case.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Can someone help me with this array assignment?

    An array of objects of a class with nothing but a main method is pretty much useless, but even if it were useful, you need use no arrays for this program. All you need are a few variables: a sum to hold the sum of the input, a scoreCount to hold the number of scores entered, a highScore and a lowScore, and that's it.

  5. #5
    Estrex is offline Member
    Join Date
    Dec 2012
    Posts
    7
    Rep Power
    0

    Default Re: Can someone help me with this array assignment?

    Oh yeah, forgot to mention. For this particular assignment, I HAVE to use arrays. We're just learning about it and the teacher said that even though you might find another way, you still have to use arrays.

Similar Threads

  1. Please help me with my assignment
    By Shin in forum New To Java
    Replies: 3
    Last Post: 10-15-2012, 11:14 PM
  2. Problem with array assignment
    By ur2cdanger in forum New To Java
    Replies: 3
    Last Post: 04-03-2012, 10:45 AM
  3. My assignment array program will not run
    By BenH in forum New To Java
    Replies: 1
    Last Post: 01-29-2012, 03:00 AM
  4. Array Programming Assignment - Digit Counter
    By ayelleeeecks in forum New To Java
    Replies: 10
    Last Post: 11-01-2011, 08:45 PM
  5. Very annoying error for array assignment
    By DudeJericho in forum New To Java
    Replies: 2
    Last Post: 04-20-2011, 03:07 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •