-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
worked on Student.java and StudentTester.java, not Student2.java or S…
…tudentTester2.java
- Loading branch information
Showing
7 changed files
with
125 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,34 @@ | ||
public class Student { | ||
private String name; | ||
private int totalQuizScore; | ||
private int numberOfQuizzes; | ||
private int[] quizScores; | ||
|
||
public Student(String name) { | ||
public Student(String name, int[] quizScores) { | ||
this.name = name; | ||
this.totalQuizScore = 0; | ||
this.numberOfQuizzes = 0; | ||
this.quizScores = quizScores; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
public int getQuizScore(int quizNumber) { | ||
return quizScores[quizNumber]; | ||
} | ||
|
||
public void setQuizScore(int quizNumber, int newScore) { | ||
quizScores[quizNumber] = newScore; | ||
} | ||
|
||
public void addQuiz(int score) { | ||
totalQuizScore += score; | ||
numberOfQuizzes++; | ||
public String getName() { | ||
return name; | ||
} | ||
|
||
public int getTotalScore() { | ||
return totalQuizScore; | ||
public void setName(String newName) { | ||
name = newName; | ||
} | ||
|
||
public int getAverageScore() { | ||
return totalQuizScore / numberOfQuizzes; | ||
@Override | ||
public String toString() { | ||
String result = name + ": "; | ||
for (int score : quizScores) { | ||
result += score + " "; | ||
} | ||
return result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
public class Student2 { | ||
private String name; | ||
private int totalQuizScore; | ||
private int numberOfQuizzes; | ||
|
||
public Student2(String name) { | ||
this.name = name; | ||
this.totalQuizScore = 0; | ||
this.numberOfQuizzes = 0; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void addQuiz(int score) { | ||
totalQuizScore += score; | ||
numberOfQuizzes++; | ||
} | ||
|
||
public int getTotalScore() { | ||
return totalQuizScore; | ||
} | ||
|
||
public int getAverageScore() { | ||
return totalQuizScore / numberOfQuizzes; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,64 @@ | ||
import java.util.ArrayList; | ||
|
||
public class StudentTester { | ||
public static void main(String... args) { | ||
Student student = new Student("John Doe"); | ||
student.addQuiz(10); | ||
student.addQuiz(20); | ||
student.addQuiz(30); | ||
System.out.println("Student name: " + student.getName()); | ||
System.out.println("Student total score: " + student.getTotalScore()); | ||
System.out.println("Student average score: " + student.getAverageScore()); | ||
ArrayList<Student> students = new ArrayList<>(); | ||
students.add(new Student("Aubrey Graham", new int[] { 46, 57, 68, 79, 90 })); | ||
students.add(new Student("Shéyaa Joseph", new int[] { 98, 87, 76, 65, 54 })); | ||
students.add(new Student("Navy Wilburn", new int[] { 82, 93, 74, 85, 96 })); | ||
students.add(new Student("Symere Woods", new int[] { 31, 41, 51, 61, 71 })); | ||
students.add(new Student("Kiari Cephus", new int[] { 54, 65, 76, 87, 98 })); | ||
|
||
System.out.println("Name \t\t\t Quiz 1 \t Quiz 2 \t Quiz 3 \t Quiz 4 \t Quiz 5"); | ||
System.out.println("-".repeat(100)); | ||
for (Student student : students) { | ||
System.out.printf("%s \t|\t %d \t|\t %d \t|\t %d \t|\t %d \t|\t %d \n", student.getName(), | ||
student.getQuizScore(0), | ||
student.getQuizScore(1), student.getQuizScore(2), student.getQuizScore(3), student.getQuizScore(4)); | ||
} | ||
System.out.println(); | ||
|
||
// Changing the name of a student | ||
students.get(1).setName("Jaques Webster"); | ||
|
||
System.out.println("Name \t\t\t Quiz 1 \t Quiz 2 \t Quiz 3 \t Quiz 4 \t Quiz 5"); | ||
System.out.println("-".repeat(100)); | ||
for (Student student : students) { | ||
System.out.printf("%s \t|\t %d \t|\t %d \t|\t %d \t|\t %d \t|\t %d \n", student.getName(), | ||
student.getQuizScore(0), | ||
student.getQuizScore(1), student.getQuizScore(2), student.getQuizScore(3), student.getQuizScore(4)); | ||
} | ||
System.out.println(); | ||
|
||
// Setting the quiz score of a student | ||
students.get(2).setQuizScore(3, 100); | ||
|
||
System.out.println("Name \t\t\t Quiz 1 \t Quiz 2 \t Quiz 3 \t Quiz 4 \t Quiz 5"); | ||
System.out.println("-".repeat(100)); | ||
for (Student student : students) { | ||
System.out.printf("%s \t|\t %d \t|\t %d \t|\t %d \t|\t %d \t|\t %d \n", student.getName(), | ||
student.getQuizScore(0), | ||
student.getQuizScore(1), student.getQuizScore(2), student.getQuizScore(3), student.getQuizScore(4)); | ||
} | ||
System.out.println(); | ||
|
||
// Replace a student with a new student | ||
students.set(2, new Student("Kanye West", new int[] { 85, 61, 77, 93, 100 })); | ||
|
||
System.out.println("Name \t\t\t Quiz 1 \t Quiz 2 \t Quiz 3 \t Quiz 4 \t Quiz 5"); | ||
System.out.println("-".repeat(100)); | ||
for (Student student : students) { | ||
System.out.printf("%s \t|\t %d \t|\t %d \t|\t %d \t|\t %d \t|\t %d \n", student.getName(), | ||
student.getQuizScore(0), | ||
student.getQuizScore(1), student.getQuizScore(2), student.getQuizScore(3), student.getQuizScore(4)); | ||
} | ||
System.out.println(); | ||
|
||
System.out.println("ToString Demonstration:"); | ||
System.out.println(students.get(0).toString()); | ||
System.out.println(students.get(1).toString()); | ||
System.out.println(students.get(2).toString()); | ||
System.out.println(students.get(3).toString()); | ||
System.out.println(students.get(4).toString()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
public class StudentTester2 { | ||
public static void main(String... args) { | ||
Student2 student = new Student2("John Doe"); | ||
student.addQuiz(10); | ||
student.addQuiz(20); | ||
student.addQuiz(30); | ||
System.out.println("Student name: " + student.getName()); | ||
System.out.println("Student total score: " + student.getTotalScore()); | ||
System.out.println("Student average score: " + student.getAverageScore()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -121,5 +121,6 @@ public static void main(String[] args) { | |
System.out.println(number1); | ||
} | ||
} | ||
in.close(); | ||
} | ||
} |