Page 1 of 3 123 LastLast
Results 1 to 20 of 48

Thread: Need help

  1. #1
    Join Date
    Apr 2011
    Posts
    30
    Rep Power
    0

    Default Need help

    i'm writing a program thats gonna ask the user to enter 5 test scores and calculate the avg and view the highest and lowest grade.


    public class LabAssig8 {

    public static void main(String[] args) {
    getUserInput();
    }

    public static double[] getUserInput() {

    int numT =4;
    double[] scores;

    scores = new double[numT];
    do {
    for (int i = 0; i <= numT; i++)

    JOptionPane.showInputDialog("Enter score " + (i + 1) + ": ");

    } while (scores[numT]>= 0 && scores[numT] <= 100);



    return (scores);
    }

    The code above has no error but It's not looping. the score has to be between 0 and 100.

    public static void calculateAverage(double[] scores) {

    avg = scores[numT] / 5;
    JOptionPane.showInputDialog("The average of 5 " + "test scores"

    + " is " + avg);


    }

    Is the code about right? I'm trying to calc the avg and I'm not able to do that since i'm still new to java


    public static void findHighLow(double[] scores) {

    Above is a method that I have to use to get the highest and lowest grade. Can any one help me PLEAAAASEE. I've been trying to figure out
    something that works for like the past 3 days but nothing
    Thanks

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    There are some problems. I will point out first that you may want to consider a for loop to get the input and no do while loop.

    Since you know how many times you want to loop a for loop is ideal. Your goal will be to loop from 0 - array.length - 1, like this

    Java Code:
    int[] blah = new int[10];
    for(int i = 0; i < blah.length; i++){
      //do stuff
    }
    This will loop x amount of times, where x is the length of the list. This will allow you do do something with each item in the array. With this you can get the user input and return the scores in the array correctly. The validation can also happen in the for loop with a while loop.

    To find the average you will want to add the scores up, again, a for loop here is very ideal. Once you have added all the scores in the array you will need to divide it by the length of the array.

    Finally, for finding the high and low variables you will also want to use for loops. Try correcting these two things and then post back. Be sure to use code tags when posting code.
    [ code] //no space
    YOUR CODE HERE
    [/code]

  3. #3
    Join Date
    Apr 2011
    Posts
    30
    Rep Power
    0

    Default

    is this right?
    Java Code:
    //
     public static void main(String[] args) {
            getUserInput();
    }
    
        public static double[] getUserInput() {
    
            
            double[] scores;
    
            int[] numT = new int[4];
    do {
            for (int i = 0; i <= numT.length; i++)
    
                    JOptionPane.showInputDialog("Enter score " + (i + 1) + ": ");
    
            } while (scores[numT>= 0 && scores[numT] <= 10)
    
            return (scores);
            }

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    It's close. Here is some pseudo code of this method
    Java Code:
    declare scores array
    loop
      get user input
      loop if input is not from 0 - 100
        get user input
      end loop
      store input in array
    end loop
    return scores array

  5. #5
    Join Date
    Apr 2011
    Posts
    30
    Rep Power
    0

    Default

    my problem is I know the steps but I'm not being able to write them as code :/

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You don't want to use a do while loop. If your user enters a number that isn't 1 - 100 you want to start a loop which continues until the user enters a correct number. A for loop lets you loop x times, so you can loop through the array quite easily.
    Java Code:
    for loop
      while(input < 0 || input > 100){
        prompt
        store input
      }
    end for loop

  7. #7
    Join Date
    Apr 2011
    Posts
    30
    Rep Power
    0

    Default

    I hope this is right
    Java Code:
    \\
    double[] scores;
            int[] numT = new  int[4];
    
    
            for (int i = 0; i <= numT.length; i++){
    
                while(scores >= 0 || scores <= 10)
                {
                    JOptionPane.showInputDialog("Enter score " + (i + 1) + ": ");
                }
            }
    
            return (scores);
            }

  8. #8
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    It's closer, you need to remember to test yourself as well. Your code has pseudo code like this
    Java Code:
    declare scores
    declare and initialize numT
    for loop
      while loop
        prompt
      end while
    end for
    return scores
    You are missing actually storing the input in the array, it should look more like this
    Java Code:
    declare scores and initialize
    for loop
      declare input and initialize with user input
      while loop //only entered if input is out of range
        store user input in input variable
      end while
      store in scores array
    end for
    return scores

  9. #9
    Join Date
    Apr 2011
    Posts
    30
    Rep Power
    0

    Default

    so declare input would be int = numT; right?
    and how do you initialize it with user input?
    I'm sorry I just don;t understand programming language :(

  10. #10
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I don't mind helping you out. The input you want named inside the loop and it will be re created each time through the loop, and destroyed once it leaves the loop.

    Java Code:
    JOptionPane.showInputDialog("Enter score " + (i + 1) + ": ");
    will return a string argument, so you want to capture it in a string, and then parse it to get the double grade.

    Java Code:
    for loop
    String s = JOptionPane.showInputDialog("Enter score " + (i + 1) + ": "); //stores the user input
    double score = Double.parseDouble(s); //parses the string above into a double
    while(score is not in correct range){
      repeat above
    }
    store score in array
    end for

  11. #11
    Join Date
    Apr 2011
    Posts
    30
    Rep Power
    0

    Default

    Thanks soo much for wanting to help me.
    Java Code:
    //
    public static double[] getUserInput() {
            int[] scores = new  int[4];
    
    
            for (int i = 0; i <= scores.length; i++){
           String numT =   JOptionPane.showInputDialog("Enter score " + (i + 1) + ": ");
           
                  double scores = Double.parseDouble(numT);
                  
                while(scores <= 0 || scores >= 100)
                {
                  
                }
            }
    
            return (scores);
            }
    I have a red line under double scores = Double.parseDouble(numT);

  12. #12
    Join Date
    Apr 2011
    Posts
    30
    Rep Power
    0

    Default

    Java Code:
    public static double[] getUserInput() {
            int[] scores = new  int[4];
    
    
            for (int i = 0; i <= scores.length; i++){
           String numT =   JOptionPane.showInputDialog("Enter score " + (i + 1) + ": ");
           
                  double scores = Double.parseDouble(numT);
                  
                while(scores <= 0 || scores >= 100){
                    JOptionPane.showInputDialog("Enter score " + (i + 1) + ": ");
               }
            }
    
            return (scores);
            }
    I guess it should be something like this?

  13. #13
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    This is close, but still a bit off, first the local variable in the loop should not have the same name as the array variable at the beginning, change scores inside the loop to score.

    In the for loop you need to do the same thing to capture the value again(re use numT to store the input as a string, then use score to parse the string(numT), finally, in the for loop, at the end you must add the score to the scores array. Also, declare scores at the top as a double[].

    You said in the method header that the method will return a double array, not an integer array, the compiler will complain if you don't match what the method header says.

  14. #14
    Join Date
    Apr 2011
    Posts
    30
    Rep Power
    0

    Default

    Java Code:
    public static double[] getUserInput() {
            int[] scores = new  int[4];
            
    
            for (int i = 0; i <= scores.length; i++){
           String numT =   JOptionPane.showInputDialog("Enter score " + (i + 1) + ": ");
    
                  double score = Double.parseDouble(numT);
    
                while(score <= 0 || score >= 100){
                    JOptionPane.showInputDialog("Enter score " + (i + 1) + ": ");
                     double score = Double.parseDouble(numT);
               }
            }
    
            return (scores);
            }
    ok I still have a red line under double score = Double.parseDouble(numT);

  15. #15
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Java Code:
    public static double[] getUserInput() {
            int[] scores = new  int[4];
            
    
            for (int i = 0; i <= scores.length; i++){
           String numT =   JOptionPane.showInputDialog("Enter score " + (i + 1) + ": ");
    
                  double score = Double.parseDouble(numT);
    
                while(score <= 0 || score >= 100){
                    numT = JOptionPane.showInputDialog("Enter score " + (i + 1) + ": ");//ADDED THE "numT =" PART HERE
                     double score = Double.parseDouble(numT);
               }
            }
    
            return (scores);
            }
    Again, this is close, but whats the whole goal of this method? Is it to simply get user input and throw it away, or do you want to store it somewhere? What do you want to store it in? How do you store it?

  16. #16
    Join Date
    Apr 2011
    Posts
    30
    Rep Power
    0

    Default

    hmmm I'm confused. what do you mean by store it?

  17. #17
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    At the end of your method you are returning the array named scores, but what does this array contain? When did you add values to it?

  18. #18
    Join Date
    Apr 2011
    Posts
    30
    Rep Power
    0

    Default

    Am I suppose to return getUserInput(); ?
    and Its only repeating once. If I enter 101 and it asks me again to reenter if I re enter a smaller number it ends.

  19. #19
    Join Date
    Apr 2011
    Posts
    30
    Rep Power
    0

    Default

    Help! Anyone? please

  20. #20
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    No, the method should return an array. In the for loop you are doing the following
    Java Code:
    declare array initialize array
    for loop
      declare string initialize with input
      declare double initialize with parsed string
        while(not in range)
          repeat lines 3 and 4
        end while
    end for
    You are missing the most important set
    Java Code:
    store input in array index i
    where should this line go in my pseudo code above?

Page 1 of 3 123 LastLast

Posting Permissions

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