Help with Program


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: Help with Program

  1. #1
    Join Date
    Mar 2005
    Posts
    2

    Help with Program

    I need to get the minimum and maximum values from the input of grades and I cant seem to get it. Its all the way at the end of the code. PLEASE HELP.

    import TerminalIO.KeyboardReader;

    public class project64 { //Project 6-4

    public static void main (String [] args) {
    KeyboardReader reader = new KeyboardReader();

    int gradeTally; // sum of inputs of grades
    int numGrades; // Number of grades inputed
    int avg; // outputs class average
    int grade; // grade entered by the user.
    int minimum; // outputs class minimum
    int maximum; // outputs class maximum
    int gradeNum;
    char letter; // letter grade for computed average.
    int getMinimum;
    gradeTally = 0;
    numGrades = 0;

    System.out.print( "Enter the first grade (value -1 to stop): " );
    grade = reader.readInt();

    while( grade >= 0 ) {
    gradeTally = gradeTally + grade;
    numGrades++;

    System.out.print( "Enter the next grade (value -1 to stop): " );
    grade = reader.readInt();

    }

    avg = gradeTally / (int) numGrades;
    grade = avg;
    if( grade >= 89.5 )
    letter = 'A';
    else if( grade >= 79.5 )
    letter = 'B';
    else if( grade >= 69.5 )
    letter = 'C';
    else if( grade >= 59.5 )
    letter = 'D';
    else
    letter = 'F';


    System.out.println( "The average of the grades is:" + avg );
    System.out.println( "Letter Grade = " + letter );

    getMinimum = grade;
    System.out.println ("The class Minimum is " +getMinimum+" which is a: " + letter);
    maximum = Math.max(grade, grade);
    System.out.println ("The class Maximum is " +maximum+" which is a: " + letter);
    }
    }

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560

    Well well....

    I would say its not at the end of the code, more like all over, but no offense,
    I just have a feeling that you are quite new to algorithms and computing.

    Have a look at this extreme makeover that I have done to your code.
    The reason for me using the getKeyboardInt method is explained in the
    comment.


    Code:
    import java.io.*;
    
    public class project64 { //Project 6-4
      static BufferedReader keyboard=new BufferedReader(new InputStreamReader(System.in));
      /**
       * Show user prompt and get integer input
       * (I use this since I haven't got the TerminalIO.KeyboardReader)
       * @param prompt
       * @return
       */
      private int getKeyboardInt(String prompt) {
        String line=null;
        while (true) {
          System.out.println(prompt);
          try {
            line = keyboard.readLine();
            int ret = Integer.parseInt(line.trim());
            return ret;
          }
          catch (IOException ex) {
            System.out.println("An error occurred");
          }
          catch (NumberFormatException ex) {
            System.out.println(line+" is not an integer, please reenter");
          }
    
        }
      }
      /**
       * check a value and return the corresponding letter
       * @param d
       * @return
       */
      private char getLetter(double d) {
        if (d >= 89.5)
          return 'A';
        else if (d >= 79.5)
          return 'B';
        else if (d >= 69.5)
          return 'C';
        else if (d >= 59.5)
          return 'D';
        else
          return 'F';
      }
      /**
       * Do the user input and accumulation
       */
      public void doIt() {
        int gradeTally=0; // sum of inputs of grades
        int numGrades=0; // Number of grades inputed
        double avg; // outputs class average
        int grade; // grade entered by the user.
        int minimum=Integer.MAX_VALUE; // outputs class minimum
        int maximum=Integer.MIN_VALUE; // outputs class maximum
    
        while (true) {
          grade=getKeyboardInt("Enter a grade (value -1 to stop): ");
          if (grade == -1) break;
          gradeTally += grade;
          numGrades++;
          minimum=(grade < minimum) ?  grade : minimum;
          maximum=(grade > maximum) ?  grade : maximum;
        }
        if (numGrades==0) {
          System.out.println("No data was entered, program will terminate");
          return;
        }
    
        avg = (double)gradeTally/(double) numGrades;
    
        System.out.println("The average of the grades is:" + avg);
        System.out.println("Letter Grade = " + getLetter(avg));
    
    
        System.out.println("The class Minimum is " + minimum + " which is a: " +
                           getLetter((double)minimum));
    
        System.out.println("The class Maximum is " + maximum + " which is a: " +
                           getLetter((double)maximum));
      }
      /**
       * **************** MAIN ****************
       * @param args
       */
      public static void main(String[] args) {
        project64 p64=new project64 ();
        p64.doIt();
      }
    }
    eschew obfuscation

  3. #3
    Join Date
    Mar 2005
    Posts
    2

    Thanks

    Yes, I am new to java. Thanks ALOT for your help!!

  4. #4
    Join Date
    Mar 2005
    Posts
    4
    ei help me 2 create a java program about the game wordhunt tnx

  5. #5
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    ? pls be more specific
    eschew obfuscation

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center