-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTests.java
More file actions
111 lines (106 loc) · 3.44 KB
/
Tests.java
File metadata and controls
111 lines (106 loc) · 3.44 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package module1.introduction.testmanager;
import java.util.Scanner;
/**
* A data-type which holds a student's name and their test scores.
*/
class Tests {
private String firstName, lastName;
private int[] scores;
/**
* Create a Tests object using an array of scores
* @param firstName The student's first name
* @param lastName The student's last name
* @param scores An array of the student's test scores (0-100%)
*/
public Tests(String firstName, String lastName, int ...scores) {
this.firstName = firstName;
this.lastName = lastName;
this.scores = scores.clone();
}
/**
* @return The student's first name
*/
public String getFirstName() {
return this.firstName;
}
/**
* @return The student's last name
*/
public String getLastName() {
return this.lastName;
}
/**
* @return The student's first and last name, separated by a space
*/
public String getFullName() {
return this.firstName + " " + this.lastName;
}
/**
* @return the scores array as a simple string
* i.e. "85 50 76 32 99"
*/
public String getScoreList() {
String list = "";
for (int i = 0; i < this.scores.length; i++) {
list += this.scores[i] + " ";
}
return list.trim();
}
/**
* @return the average of the student's test scores
*/
public double getAverage() {
int avg = 0;
for (int i = 0; i < scores.length; i++) {
avg += scores[i];
}
return avg / scores.length;
}
/**
* @return the letter grade corresponding to the student's average
*/
public String getLetterGrade() {
double avg = getAverage();
if (avg < 60)
return "F";
if (60 <= avg && avg < 70)
return "D";
if (70 <= avg && avg < 80)
return "C";
if (80 <= avg && avg < 90)
return "B";
if (90 <= avg)
return "A";
return "";
}
/**
* A string formatted like:
* "John Smith - SCORES: [0 5 1 2 3 ] - AVG: 2.0 - GRADE: F"
*/
public String toString() {
String name = this.getFirstName() + " " + this.getLastName();
String scoreList = "SCORES: [" + this.getScoreList() + "]";
String average = "AVG: " + getAverage();
String letterGrade = "GRADE: " + getLetterGrade();
return String.join(" - ",
name,
scoreList,
average,
letterGrade);
}
public static void main(String[] args) {
System.out.println("===Tests Class Demo - Example students ====");
Tests stephenStrange = new Tests("Stephen", "Strange", new int[] { 98, 99, 95, 94, 90 });
Tests magnusBurnsides = new Tests("Magnus", "Burnsides", new int[] { 43, 47, 62, 45, 64 });
Tests barryBluejeans = new Tests("Barry", "Bluejeans", new int[] { 86, 89, 85, 85, 83 });
System.out.println(stephenStrange.toString());
System.out.println(magnusBurnsides.toString());
System.out.println(barryBluejeans.toString());
System.out.println();
// Input student via console
Scanner console = new Scanner(System.in);
Tests customStudent = TestManager.inputStudent(console, "===Custom Student - User input ====");
System.out.println(customStudent.toString());
System.out.println();
}
}