Google Answers Logo
View Question
 
Q: Java Program ( Answered,   0 Comments )
Question  
Subject: Java Program
Category: Computers > Programming
Asked by: math01-ga
List Price: $20.00
Posted: 09 Jan 2003 06:48 PST
Expires: 08 Feb 2003 06:48 PST
Question ID: 139810
Extend the program below to compute the average score for the whole
class. You will add the appropriate code so that the program will:

Ask the user to enter the total number of students in the class 

Ask the user to enter the scores (assignment and project) for each
student.

Print out the numeric score and the letter grade for each student 

Compute the average grade of the class 



Procedure

Modify the program using a "for" loop. 
 
Extend the program for multiple students using a "while" loop.
Although you could use either kind of loop.


// Program to manage the grades of students in a course.

public class ExamsApp {

    public static void main(String[] args) {
        Exams ex = new Exams();
        Ex.findSumAndCount();
        System.out.println(“Average score is “ + ex.findAve());
    }
}



// Program to manage the grades of students in a course.

import javax.swing.JOptionPane;

public class Exams {
    // Data fields {
    private int sum;          //sum of scores
    private int count;       // count of scores

   // methods
   public static int readInt(String prompt) {
       String numStr = JOptionPane.showInputDialog(prompt);
       return Integer.parseInt(numStr);
   }
   // preconditions: count and sum are zero
   // postconditions: data field sum stores the sum of  scores
   //   data field count stores the count of scores
   public void findSumAndCount() {
        int score;
        int SENTINEL = -1;         //sentinel value
        score = readInt(“Enter student’s first grade or “ + SENTINEL +
“ to stop”);
        while (score != SENTINEL) {
             sum += score;                // add current score to sum
             count++;                        // increase count by 1
             System.out.println(“score: “ + score);
             score = readInt(“Enter next grade or “ + SENTINEL + “ to
stop”);
         }
      }

   // precondition: sum and count are defined
   // postcondition: Returns the average score
   public double findAve() {
        double average;                   // average score

        if (count > 0)
            average = (double) sum / (double) count;
        else
             average = 0;
        return average;
   }
}

Request for Question Clarification by mathtalk-ga on 09 Jan 2003 14:23 PST
OFF TOPIC - sort of...

Hi, math01-ga:

I'd answered a few questions for you in the past, but never got a
rating.  No big deal, it's certainly your option - but I kind of got
the impression that my approach to answering just wasn't your cup of
tea, or something.  So I've stood back and let others be of
assistance.

However today I happened to look at all your questions (you've been a
pretty faithful customer!), and I think I noticed that you didn't rate
any of the answers.  So the good news is, I guess I shouldn't take it
personally!  Still, I wondered if you knew about the ratings and had
left so many responses unrated by design.

regards, mathtalk-ga
Answer  
Subject: Re: Java Program
Answered By: studboy-ga on 09 Jan 2003 15:26 PST
 
In file ExamApps.java:

// Program to manage the grades of students in a course. 
 
public class ExamsApp { 
 
    public static void main(String[] args) { 
        Exams ex = new Exams(); 
        ex.findSumAndCount(); 
        System.out.println("Average score for class is " +
ex.average);
	System.exit(0);
    } 
} 

In file Exams.java:

// Program to manage the grades of students in a course. 
 
import javax.swing.JOptionPane; 
 
public class Exams { 
    // Data fields { 
    public double average; 
    private double average_temp; 
    private int sum;          //sum of scores 
    private int count;       // count of scores 
    private int sum_temp;
    private int count_temp;
 
   // methods 
   public static int readInt(String prompt) { 
       String numStr = JOptionPane.showInputDialog(prompt); 
       return Integer.parseInt(numStr); 
   } 
   // preconditions: count and sum are zero 
   // postconditions: data field sum stores the sum of  scores 
   //   data field count stores the count of scores 
   public void findSumAndCount() { 
	int num;
        int score; 
        int SENTINEL = -1;         //sentinel value 
        num = readInt("Enter number of students");
	for (int i = 0; i < num; ++i) {
	sum_temp = 0;
	count_temp = 0;
        score = readInt("Enter student " + i + " 's first grade or " +
SENTINEL + " to stop");
        while (score != SENTINEL) { 
	     sum_temp += score;
             sum += score;                // add current score to sum
             count_temp++;                        // increase count by
1
             count++;                        // increase count by 1 
             System.out.println("score: " + score); 
             score = readInt("Enter next grade or " + SENTINEL + " to
stop");
         } 
	 average_temp = findAve(sum_temp, count_temp);
         System.out.println("Average score for student " + i + " is :
" + average_temp);
	 if (average_temp >= 90) {
         	System.out.println("Average score for student " + i + " is A
" );
	 } else if (average_temp >= 80) {
         	System.out.println("Average score for student " + i + " is B
" );
	 } else {
         	System.out.println("Average score for student " + i + " is
C, D, or F -- who cares... this student fails! " );
	 }
	 }
	 average = findAve(sum, count);
      } 
 
   // precondition: sum and count are defined 
   // postcondition: Returns the average score 
   public double findAve(int sum, int count) { 
        double average;                   // average score 
 
        if (count > 0) 
            average = (double) sum / (double) count; 
        else 
             average = 0; 
        return average; 
   } 
}
Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy