|
|
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 students 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; } } | |
|
|
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; } } |
|
There are no comments at this time. |
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 Home - Answers FAQ - Terms of Service - Privacy Policy |