Problem:
Write a program that represents that Gradebook of the class
Csc243.. The program takes the Grades of the students in the class till the
user enters -1 to quit.. The main class must have a constructor for the course
name and set and get methods, a displayMessage() to display the message and
determineClassAverage() to calculate the average.. Although each grade is an
integer, the averaging calculation is likely to produce a number with a decimal
point, so this class uses a type double to do so. Then write a GradeBookTest
and create GradeBook object and invoke its determineClassAverage method.
Output:
Welcome to the Grade book forCsc 243 Introduction to Object Oriented Programming
Enter grade or -1 to quit: 97
Enter grade or -1 to quit: 88
Enter grade or -1 to quit: 72
Enter grade or -1 to quit: -1
Total of the 3 grades entered is 257
Class average is 85.65
Solution:
import java.util.Scanner; public class GradeBook { private String name; private double sum,grade,average; private int count; public GradeBook (String name) { this.name = name; } public void setName(String name) { this.name = name; } public String getName() { return name; } public String displayMessage() { String S = "Total of the " + count + "grades entered is " + sum + "\n"; S += "Class average is " + average; return S; } public double determineClassAverage() { while (grade != -1) { Scanner scan = new Scanner (System.in); System.out.print("Enter grade or -1 to quit: "); grade = scan.nextDouble(); if (grade != -1) { sum += grade; count++; } } average = sum / count; return average; } }
public class GradeBookTest { public static void main (String[] args) { double grade = 0; double sum = 0; int count = 0; GradeBook grd1 = new GradeBook ("Csc 243 Introduction to Object Oriented Programming"); System.out.println("Welcome to the Grade book for "); System.out.println(grd1.getName()); System.out.println(); grd1.determineClassAverage(); System.out.println(); System.out.println(grd1.displayMessage()); } }
No comments :
Post a Comment