Java question
I am having issues returning the score sort in numerical order for more than 2 entries. I had to
develop an enhancement which allowed the application 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.\"+\"\ \");
Scanner sc = new Scanner(System.in);
System.out.print(\"Enter number of students to enter: \");
int numberofStudents = sc.nextInt();
//arrays storing student name and score
Student[] students = new Student[numberofStudents];
StudentScore [] studentScores = new StudentScore [numberofStudents];
int t = 0;
for (int i = 0; i < numberofStudents; i++)
{
System.out.println(\"\");
String studentLastName = Validator.lastName(sc, \"Student \" + (i+1) + \" Last name: \");
String studentFirstName = Validator.lastName(sc, \"Student \" + (i+1) + \" First name: \");
int studentScore = Validator.validScore(sc, \"Student \" + (i+1) + \" score : \");
students [t] = new Student(studentFirstName, studentLastName , studentScore);
studentScores [t] = new StudentScore(studentFirstName, studentLastName , studentScore);
t = t + 1;
}
//output
System.out.println(\"\");
System.out.println(\"Last Name Sort\");
Arrays.sort(students, 0, numberofStudents);
for(Student i: students)
System.out.println(i.getlastName()+\", \"+i.getfirstName()+\": \"+i.getScore());
System.out.println();
System.out.println(\"Score Sort\");
Arrays.sort(studentScores, 0, numberofStudents);
for(StudentScore i: studentScores)
System.out.println(i.getlastName()+\", \"+i.getfirstName()+\": \"+i.getScore());
}
}
Validator Class
import java.util.Scanner;
public class Validator
{
public static int validScore(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;
}
public static String lastName(Scanner sc, String prompt)
{
Scanner input = new Scanner(System.in);
String StudentName = \"\";
boolean isvalid = false;
while (isvalid == false)
{
System.out.print (prompt);
StudentName = input.nextLine();
if (StudentName == null || StudentName.equals(\"\"))
{
System.out.println(\"Error! You have to enter a name.\");
}
else
{
isvalid = true;
}
}
return StudentName;
}
}
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.
Application of Matrices in real life. Presentation on application of matrices
Java questionI am having issues returning the score sort in numeri.pdf
1. Java question
I am having issues returning the score sort in numerical order for more than 2 entries. I had to
develop an enhancement which allowed the application 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."+" ");
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of students to enter: ");
int numberofStudents = sc.nextInt();
//arrays storing student name and score
Student[] students = new Student[numberofStudents];
StudentScore [] studentScores = new StudentScore [numberofStudents];
int t = 0;
for (int i = 0; i < numberofStudents; i++)
{
System.out.println("");
String studentLastName = Validator.lastName(sc, "Student " + (i+1) + " Last name: ");
String studentFirstName = Validator.lastName(sc, "Student " + (i+1) + " First name: ");
int studentScore = Validator.validScore(sc, "Student " + (i+1) + " score : ");
students [t] = new Student(studentFirstName, studentLastName , studentScore);
studentScores [t] = new StudentScore(studentFirstName, studentLastName , studentScore);
t = t + 1;
}
//output
System.out.println("");
2. System.out.println("Last Name Sort");
Arrays.sort(students, 0, numberofStudents);
for(Student i: students)
System.out.println(i.getlastName()+", "+i.getfirstName()+": "+i.getScore());
System.out.println();
System.out.println("Score Sort");
Arrays.sort(studentScores, 0, numberofStudents);
for(StudentScore i: studentScores)
System.out.println(i.getlastName()+", "+i.getfirstName()+": "+i.getScore());
}
}
Validator Class
import java.util.Scanner;
public class Validator
{
public static int validScore(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;
}
}
3. return studentScore;
}
public static String lastName(Scanner sc, String prompt)
{
Scanner input = new Scanner(System.in);
String StudentName = "";
boolean isvalid = false;
while (isvalid == false)
{
System.out.print (prompt);
StudentName = input.nextLine();
if (StudentName == null || StudentName.equals(""))
{
System.out.println("Error! You have to enter a name.");
}
else
{
isvalid = true;
}
}
return StudentName;
}
}
Student Class
public class Student implements Comparable
{
4. 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
5. public String getlastName()
{
return lastName;
}
// Returns Student Score
public int getScore()
{
return score;
}
}
StudentScore Class
public class StudentScore implements Comparable
{
private String firstName = "";
private String lastName = "";
private int score = 0;
// StudentScore constructor
public StudentScore(String firstName, String lastName, int score)
{
this.firstName = firstName;
this.lastName = lastName;
this.score = score;
}
@Override
public int compareTo (StudentScore nextStudent)
{
StudentScore student = nextStudent;
if (score > student.score)
{
return score;
}
return student.score;
}
6. // 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
Last Name Sort
adams, jessica: 63
smith, amy: 75
smith, mike: 88
thomas, kirk: 98
Score Sort
smith, mike: 88
smith, amy: 75
adams, jessica: 63
thomas, kirk: 98
Solution
import java.util.*;
public class StudentSortApp {
public static void main(String[] args) {
7. System.out.println("Welcome to the Student Scores Application." + " ");
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of students to enter: ");
int numberofStudents = sc.nextInt();
// arrays storing student name and score
Student[] students = new Student[numberofStudents];
StudentScore[] studentScores = new StudentScore[numberofStudents];
int t = 0;
for (int i = 0; i < numberofStudents; i++) {
System.out.println("");
String studentLastName = Validator.lastName(sc, "Student "
+ (i + 1) + " Last name: ");
String studentFirstName = Validator.lastName(sc, "Student "
+ (i + 1) + " First name: ");
int studentScore = Validator.validScore(sc, "Student " + (i + 1)
+ " score : ");
students[t] = new Student(studentFirstName, studentLastName,
studentScore);
studentScores[t] = new StudentScore(studentFirstName,
studentLastName, studentScore);
t = t + 1;
}
// output
System.out.println("");
System.out.println("Last Name Sort");
Arrays.sort(students, 0, numberofStudents);
for (Student i : students)
System.out.println(i.getlastName() + ", " + i.getfirstName() + ": "
+ i.getScore());
System.out.println();
System.out.println("Score Sort");
Arrays.sort(studentScores);
for (StudentScore i : studentScores)
System.out.println(i.getlastName() + ", " + i.getfirstName() + ": "
+ i.getScore());
}
8. }
import java.util.Scanner;
public class Validator {
public static int validScore(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;
}
public static String lastName(Scanner sc, String prompt) {
Scanner input = new Scanner(System.in);
String StudentName = "";
boolean isvalid = false;
while (isvalid == false) {
System.out.print(prompt);
StudentName = input.nextLine();
if (StudentName == null || StudentName.equals("")) {
System.out.println("Error! You have to enter a name.");
}
else
{
isvalid = true;
}
}
return StudentName;
}
}
9. 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;
}
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;
}
}
public class StudentScore implements Comparable {
private String firstName = "";
private String lastName = "";
private int score = 0;
// StudentScore constructor
10. public StudentScore(String firstName, String lastName, int score) {
this.firstName = firstName;
this.lastName = lastName;
this.score = score;
}
public int compareTo(StudentScore nextStudent)
{
return Integer.compare(this.score, nextStudent.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: 4
Student 1 Last name: mark
Student 1 First name: smith
Student 1 score : 98
Student 2 Last name: koti
Student 2 First name: b
Student 2 score : 10
Student 3 Last name: john
Student 3 First name: abraham
Student 3 score : 65
Student 4 Last name: ramu
Student 4 First name: rma