Skip to content

Instantly share code, notes, and snippets.

@gmhafiz
Created November 14, 2016 01:28
Show Gist options
  • Save gmhafiz/64b0cb89d5097bd111b6477138ede050 to your computer and use it in GitHub Desktop.
Save gmhafiz/64b0cb89d5097bd111b6477138ede050 to your computer and use it in GitHub Desktop.
import java.util.Scanner;
class EnterTestScores {
public static void main (String[] args){
Scanner scanner = new Scanner (System.in);
String reply;
int n = 0;
double score;
Student[] students = new Student[50];
for (int i=0; i<50; i++){
students[i] = new Student();
}
do {
System.out.print("Enter student’s name: ");
students[n].enterName(scanner.next());
scanner.nextLine(); //Refer to part (c)
System.out.print("Enter student’s score: ");
students[n].enterScore(students[n].getName(),
scanner.nextDouble());
scanner.nextLine(); //Refer to part (d)
n++;
System.out.print("Any more student (y or n)?");
reply = scanner.next();
} while (reply.charAt(0) == 'y');
for (int i=0;i<n;i++){
System.out.println(students[i].getName()+ " – " + students[i].getScore());
}
System.out.println("Average score in this class : " + Student.averageScoreInClass());
System.out.println("Top student in this class: " + Student.topStudentInClass() + " - " + Student.topScoreInClass());
}
}
public class Student {
private static int totalStudentNumber = 0;
private static double totalScore = 0;
private static double topScore = 0;
private static String topStudent;
private String name;
private double score = 0;
public Student() {
}
public static int getTotalStudentNumber() {
return totalStudentNumber;
}
public static void setTotalStudentNumber(int totalStudentNumber) {
Student.totalStudentNumber = totalStudentNumber;
}
public static double getTotalScore() {
return totalScore;
}
public static void setTotalScore(double totalScore) {
Student.totalScore = totalScore;
}
public static double getTopScore() {
return topScore;
}
public static void setTopScore(double topScore) {
Student.topScore = topScore;
}
public static String getTopStudent() {
return topStudent;
}
public static void setTopStudent(String topStudent) {
Student.topStudent = topStudent;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
public void enterName(String name) {
this.name = name;
}
public void enterScore(String name, double score) {
this.name = name;
this.score = score;
this.totalScore += score;
this.totalStudentNumber++;
if (score > topScore) {
topScore = score;
topStudent = name;
}
}
public static double averageScoreInClass() {
return totalScore / totalStudentNumber;
}
public static String topStudentInClass() {
return topStudent;
}
public static double topScoreInClass() {
return topScore;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment