I am completely new to Java and I need some DESPERATE help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kingmathyou
    New Member
    • Dec 2008
    • 3

    I am completely new to Java and I need some DESPERATE help

    I have been trying to figure it out this program for the passed two hours with no luck. It is probably very easy, but I can't figure it out.
    Here is what I have to do:

    Write a program that asks the user to enter name and score of students via a dialog box.
    Output the number of students, their names, and if their grade is above (or equal to) the average scores.
    • User may enter information about any number of students.
    • The user must enter “Q” (lower case or upper case) to stop entering the input and
    retrieve the results


    I need to do it in two separate classes, a User class and a UserDialog class. I don't even know where to begin. Is there anyone out there who can help me?
  • Nepomuk
    Recognized Expert Specialist
    • Aug 2007
    • 3111

    #2
    Sure, we're here to help.

    First of all: You're supposed to have two classes called "User" and "UserDialog ". What should these classes do?

    My guess is, that the User class should simply save the information of one student. What information is that? Right, the name and the score.

    The UserDialog class should read the input (name and score) with a dialog box. I guess, that should be a graphical dialog box, so you should have a look at this Swing tutorial.

    Now, you don't know how many students you'll have at the beginning. There are two possibilities to cope with that: either you say, "OK, I'll save a maximum of n users and save them in an array" or you use a resizeable container like an ArrayList for example.

    Oh, and you'll have to read the input until "Q" is entered. That sounds like a do ... while loop. Do you know about those?

    Try to do something with that information and come back here with more questions, when they arise!

    Greetings,
    Nepomuk

    Comment

    • kingmathyou
      New Member
      • Dec 2008
      • 3

      #3
      Thank you for the fast reply! It is greatly appreciated!

      Comment

      • kingmathyou
        New Member
        • Dec 2008
        • 3

        #4
        This is all I can figure out with it still compiling. As you can see there are some problems:
        • Everything is only in 1 class (I need to make them in two).
        • To exit the input screen I need to use a String, but all I can seem to do is use double.
        • I can't figure out how to make the student name and grade connected, so in the ArrayList, for example, name=Mike and score=90 stay together.



        Code:
        import java.util.ArrayList;;
        import javax.swing.*;
        
        public class AverageGreaterArrayList
        {
          public static void main(String[] args) {
         ArrayList<Double> data = new ArrayList<Double>();
        
            double number = 0.00;
            double sum = 0;
            String name = "";
        
            while (number !=-1)
            {
              String s = JOptionPane.showInputDialog("Enter student grade: or -1 to calculate average and exit");
              number = Double.parseDouble(s);
        
              if (number !=-1) {
              data.add(number);
              sum = sum + number;
              }
            }
         double average = sum / data.size();
            int countGreater = 0;
        
            for (int i = 0; i < data.size(); i++) {
              if (data.get(i) >= average)
               countGreater=countGreater + 1;
            }
        
        	System.out.println ("The entered grades are:");
            for (int i = 0; i < data.size(); i++) {
        		System.out.println (""+ data.get(i));
        	}
        
        	System.out.println ("");
        
        
        
                System.out.println("The average is " + average);
                System.out.println("The number of students scoring greater than the average is " + countGreater);
          }
        }

        Comment

        • Nepomuk
          Recognized Expert Specialist
          • Aug 2007
          • 3111

          #5
          OK, that's not bad for starters. Now, as you said yourself, you only have one class so far and you haven't connected the names with the scores yet. That is exactly what that class "User" should do. And then you'd have an ArrayList of Users, not of Doubles.

          About the exiting: You can compare Strings with
          Code:
          String s = "whatever";
          //...
          if(s.equals("q")) {
            //do something
          }
          Now, that comparison is case sensitive, but maybe you can find something in the String API that will help you there?

          Greetings,
          Nepomuk

          Comment

          Working...