#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2008
    Posts
    5
    Rep Power
    0

    Grades.Java using methods


    I am working on this program that continuously requests a numeric score to be entered. If the score is less than 0 or greater than 100, your program should display an appropriate message informing the user that an invalid score has been entered. If the score is valid, the score should be added to a total. When a score of 999 is entered, the program should exit and compute the display of the average of the valid grades entered.
    I have been working on this for a while now and I think I am stuck.
    It is accepting the input but that is it. If I enter 3333, or xyz which should be considered invalid it keeps running. If I enter 999 which is supposed to exit it keeps running.
    I am not sure where everything is going wrong. It might be something simple or I might be way off. I am very new to this. I tried to keep everything as close to the project guidelines as possible so I cant deviate far from the structure that I have.


    Code:
    import java.io.*;
    import javax.swing.JOptionPane;
    
    public class Grades
    {
    	public static void main(String [] args) throws IOException
    	{
    		addScore();
    		} //declare, initialize variables
    	public static void addScore()
    	{
    		double grade = 0;
            double total = 0;
            int count = 0;
            boolean done = false;
            
            while(!done)
            	do
            	{
            		promptUser(grade);
            		
            		if (grade == 999);
                            {
                            	done = true;
                            	}
                            
                            if (grade >= 0 && grade <= 100)
                            {
                            	count = count +1;
                                total = total + grade;
                                }
                            else
                            	
                            {
                            	JOptionPane.showMessageDialog(null,
                            			"Please enter a number from 0 through 100." +
                            			"\nEnter 999 to display the average and quit.", "Error",
                            			JOptionPane.ERROR_MESSAGE);
                            	}
                            }
            	while (true);
            {
            	average(total,count);
            	}
            }
    	public static double promptUser(double grade)
    	{
    		JOptionPane.showInputDialog(null,
    				"Enter the grade from 0 to 100,using only numerals." +
    				"\nEnter 999 to display the average of all grades entered." +
    				"\nThis will also quit the program:");
    		return grade;
    		}
      
    	public static void average(double total, int count)
    	{
    		total = total/count;
    		}
    	
    	public static void displayResult(double result)
    	{
    		JOptionPane.showMessageDialog(null,"The average of all" +" grades entered is "+ result);
    		}
      }
  2. #2
  3. Daniel Schildsky
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Mar 2004
    Location
    KL, Malaysia.
    Posts
    1,555
    Rep Power
    1621

    logical error


    Originally Posted by cocolask
    I am working on this program that continuously requests a numeric score to be entered. If the score is less than 0 or greater than 100, your program should display an appropriate message informing the user that an invalid score has been entered. If the score is valid, the score should be added to a total. When a score of 999 is entered, the program should exit and compute the display of the average of the valid grades entered.
    I have been working on this for a while now and I think I am stuck.
    It is accepting the input but that is it. If I enter 3333, or xyz which should be considered invalid it keeps running. If I enter 999 which is supposed to exit it keeps running.
    I am not sure where everything is going wrong. It might be something simple or I might be way off. I am very new to this. I tried to keep everything as close to the project guidelines as possible so I cant deviate far from the structure that I have.


    Code:
    import java.io.*;
    import javax.swing.JOptionPane;
    
    public class Grades
    {
    	public static void main(String [] args) throws IOException
    	{
    		addScore();
    		} //declare, initialize variables
    	public static void addScore()
    	{
    		double grade = 0;
            double total = 0;
            int count = 0;
            boolean done = false;
            
            while(!done)
            	do
            	{
            		promptUser(grade);
            		
            		if (grade == 999);
                            {
                            	done = true;
                            	}
                            
                            if (grade >= 0 && grade <= 100)
                            {
                            	count = count +1;
                                total = total + grade;
                                }
                            else
                            	
                            {
                            	JOptionPane.showMessageDialog(null,
                            			"Please enter a number from 0 through 100." +
                            			"\nEnter 999 to display the average and quit.", "Error",
                            			JOptionPane.ERROR_MESSAGE);
                            	}
                            }
            	while (true);
            {
            	average(total,count);
            	}
            }
    	public static double promptUser(double grade)
    	{
    		JOptionPane.showInputDialog(null,
    				"Enter the grade from 0 to 100,using only numerals." +
    				"\nEnter 999 to display the average of all grades entered." +
    				"\nThis will also quit the program:");
    		return grade;
    		}
      
    	public static void average(double total, int count)
    	{
    		total = total/count;
    		}
    	
    	public static void displayResult(double result)
    	{
    		JOptionPane.showMessageDialog(null,"The average of all" +" grades entered is "+ result);
    		}
      }

    There are 2 errors:

    1. You brought up the dialog box to get the input, but you never returned the value from the input. you simply returned the variable 'grade' which is always 0.

    2. You compared the value 999 with the variable 999, but you never assigned the value from the user input to 'grade', therefore it will never match the condition you intended to execute.


    Solution:

    1. return the value from JOptionPane. Read the javadoc for JOptionPane for more information.

    2. assign the value returned from promptUser() to grade.
    When the programming world turns decent, the real world will turn upside down.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2008
    Posts
    5
    Rep Power
    0
    Originally Posted by tvc3mye
    There are 2 errors:

    1. You brought up the dialog box to get the input, but you never returned the value from the input. you simply returned the variable 'grade' which is always 0.

    2. You compared the value 999 with the variable 999, but you never assigned the value from the user input to 'grade', therefore it will never match the condition you intended to execute.


    Solution:

    1. return the value from JOptionPane. .

    2. assign the value returned from promptUser() to grade.
    Thanks for the help. I still don't understand though. Could you possibly break it down a little more? In my method when I use " return grade;" , shouldn't that return the input to the addScore method? I feel like a first grader learning to read.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2008
    Posts
    5
    Rep Power
    0
    I have completely reworked everything but I am still having the problem using 999 to calculate the average and exit. Can anyone tell me what is going wrong please?
    Code:
    import javax.swing.JOptionPane;
    
    public class Grades
    {
       public static void main (String[] args)
       {
        addScore();
       }
    
       public static void addScore()
       {
        int grade = 0;
        int count = 0;
        int total = 0;
    
    
        do // Loop to keep total of test scores
        {
        	promptUser(grade);
        
    
         if (grade == 999)
          break;
         else
          if (grade >=0 && grade <=100)
          {
           count = count + 1;
           total = total + grade;
          }
          else
           JOptionPane.showMessageDialog(null,"Number must be between 0 and 100");
        }while (true);
    
        average(total,count);
    
       }
    
       public static double promptUser(double grade)
       {
    	   return grade = Integer.parseInt(JOptionPane.showInputDialog("Enter test score"));
       }
       public static void average(int total, int count) //will calculate the average of all test scores entered
       {
        int result;
        result = (total / count);
        displayResult(result);
    
       }
    
       public static void displayResult(int result) // used to display the results
       {
        System.out.println(result);
       }
    }

IMN logo majestic logo threadwatch logo seochat tools logo