Create a Gradebook Class in Java

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 for
Csc 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

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact