-
Notifications
You must be signed in to change notification settings - Fork 1
/
GradeBook2.java
83 lines (66 loc) · 1.76 KB
/
GradeBook2.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import java.util.Scanner;
public class GradeBook2
{
private String courseName;
// default constructor.
public GradeBook2()
{
courseName = "";
}
// parametrized constructor.
public GradeBook2(String courseName)
{
this.courseName = courseName;
}
// setter method.
public void setCouseName(String courseName)
{
this.courseName = courseName;
}
// getter method.
public String getCourseName()
{
return (this.courseName);
}
// display method.
public void displayMessage()
{
System.out.println("Welcome to the grade book for " + getCourseName());
}
// average finder method.
public void determineClassAverage()
{
var input = new Scanner(System.in);
int total = 0;
int grade = 0;
int gradeCounter = 0;
double average = 0.0;
System.out.print("Enter grade or -1 to quit : ");
grade = input.nextInt();
while (grade != -1)
{
total += grade;
gradeCounter++;
System.out.print("Enter grade or -1 to quit : ");
grade = input.nextInt();
}
input.close();
if (gradeCounter != 0)
{
average = (double) total / gradeCounter;
System.out.println("Total of " + gradeCounter + " grades are : " + total);
System.out.printf("Average grades are : %.2f\n", average);
}
else
{
System.out.println("No grades were entered.");
}
}
// main driven function
public static void main(String[] args)
{
var gradebook = new GradeBook2("CS 50");
gradebook.displayMessage();
gradebook.determineClassAverage();
}
}