Java question: Below are three classes I created where you type a first name, la
ID: 3790871 • Letter: J
Question
Java question:
Below are three classes I created where you type a first name, last name, and score. The code sorts by last name. I am having trouble adding the enhancement below.
Add the ability to sort the list by score. The easiest solution is probably to create a second student class (perhaps called StudentScore) that implements the IComparable interface to sort by score rather than by name.
StudentSortApp
import java.util.*;
public class StudentSortApp
{
public static void main(String[] args)
{
System.out.println("Welcome to the Student Scores Application."+" ");
//instantiate Scanner Object
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of students to enter: ");
//input stream takes in an integer value
int numberofStudents = sc.nextInt();
//array to store student name and score
Student[] students = new Student[numberofStudents];
//code which prints array of students name and score
int t = 0;
for( int i = 1; i <= numberofStudents; i++)
{
System.out.println("");
String studentLastName = Validator.lastName(sc, "Student " + i + " Last name: ");
String studentFirstName = Validator.lastName(sc , "Student " + i + " First name: ");
int studentScore= Validator.vScore(sc,"Student " + i + " score : ");
students [t] = new Student(studentFirstName, studentLastName , studentScore);
// increament the array index
t = t + 1;
}
System.out.println("");
Arrays.sort(students, 0, numberofStudents);
for(Student i: students)
System.out.println(i.getlastName()+", "+i.getfirstName()+": "+i.getScore());
}
}
Student Class
public class Student implements Comparable
{
private String firstName = "";
private String lastName = "";
private int score = 0;
// Student Class constructor
public Student(String firstName, String lastName, int score)
{
this.firstName = firstName;
this.lastName = lastName;
this.score = score;
}
@Override
public int compareTo(Object nextStudent)
{
Student student =(Student) nextStudent;
if(lastName.equals(student.lastName))
{
return firstName.compareToIgnoreCase(student.firstName);
}
return lastName.compareToIgnoreCase(student.lastName);
}
// returns first Name
public String getfirstName()
{
return firstName;
}
// Returns Last Name
public String getlastName()
{
return lastName;
}
// Returns Student Score
public int getScore()
{
return score;
}
}
Validator
import java.util.Scanner;
public class Validator
{
public static int vScore(Scanner sc, String prompt)
{
int studentScore = 0;
boolean isValid = false;
while (isValid == false)
{
System.out.print (prompt);
studentScore = sc.nextInt();
if (studentScore > 100 || studentScore < 0)
{
System.out.println("Error! You have to enter a score between 0 and 100");
}
else
{
isValid = true;
}
}
return studentScore;
}
Explanation / Answer
StudentSortApp.java
import java.util.*;
public class StudentSortApp
{
public static void main(String[] args)
{
System.out.println("Welcome to the Student Scores Application."+" ");
//instantiate Scanner Object
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of students to enter: ");
//input stream takes in an integer value
int numberofStudents = sc.nextInt();
//array to store student name and score
Student[] students = new Student[numberofStudents];
StudentScore[] studentScores = new StudentScore[numberofStudents];
//code which prints array of students name and score
int t = 0;
for( int i = 0; i < numberofStudents; i++)
{
System.out.println("Student " + (i+1) + " last name : ");
String studentLastName = sc.next();
System.out.println("Student " + (i+1) + " first name : ");
String studentFirstName = sc.next();
int studentScore= Validator.vScore(sc,"Student " + (i+1) + " score : ");
students [t] = new Student(studentFirstName, studentLastName , studentScore);
studentScores [t] = new StudentScore(studentFirstName, studentLastName , studentScore);
// increament the array index
t = t + 1;
}
System.out.println("");
Arrays.sort(students, 0, numberofStudents);
for(Student i: students)
System.out.println(i.getlastName()+", "+i.getfirstName()+": "+i.getScore());
System.out.println();
Arrays.sort(studentScores, 0, numberofStudents);
for(StudentScore i: studentScores)
System.out.println(i.getlastName()+", "+i.getfirstName()+": "+i.getScore());
}
}
Student.java
public class Student implements Comparable<Student>
{
private String firstName = "";
private String lastName = "";
private int score = 0;
// Student Class constructor
public Student(String firstName, String lastName, int score)
{
this.firstName = firstName;
this.lastName = lastName;
this.score = score;
}
public int compareTo(Student nextStudent)
{
Student student = nextStudent;
if(lastName.equals(student.lastName))
{
return firstName.compareToIgnoreCase(student.firstName);
}
return lastName.compareToIgnoreCase(student.lastName);
}
// returns first Name
public String getfirstName()
{
return firstName;
}
// Returns Last Name
public String getlastName()
{
return lastName;
}
// Returns Student Score
public int getScore()
{
return score;
}
}
Validator.java
import java.util.Scanner;
public class Validator
{
public static int vScore(Scanner sc, String prompt)
{
int studentScore = 0;
boolean isValid = false;
while (isValid == false)
{
System.out.print (prompt);
studentScore = sc.nextInt();
if (studentScore > 100 || studentScore < 0)
{
System.out.println("Error! You have to enter a score between 0 and 100");
}
else
{
isValid = true;
}
}
return studentScore;
}
}
StudentScore.java
public class StudentScore implements Comparable<StudentScore>{
private String firstName = "";
private String lastName = "";
private int score = 0;
// Student Class constructor
public StudentScore(String firstName, String lastName, int score)
{
this.firstName = firstName;
this.lastName = lastName;
this.score = score;
}
public int compareTo(StudentScore nextStudent)
{
StudentScore student = nextStudent;
if(score > student.score ){
return score;
}
return student.score;
}
// returns first Name
public String getfirstName()
{
return firstName;
}
// Returns Last Name
public String getlastName()
{
return lastName;
}
// Returns Student Score
public int getScore()
{
return score;
}
}
Output:
Welcome to the Student Scores Application.
Enter number of students to enter: 3
Student 1 last name :
Murapaka
Student 1 first name :
Suresh
Student 1 score : 10
Student 2 last name :
Murapaka
Student 2 first name :
Sekhar
Student 2 score : 20
Student 3 last name :
Kella
Student 3 first name :
Revathi
Student 3 score : 30
Kella, Revathi: 30
Murapaka, Sekhar: 20
Murapaka, Suresh: 10
Murapaka, Suresh: 10
Murapaka, Sekhar: 20
Kella, Revathi: 30
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.