Assignment Overview This programming exercise introduces generics and interfaces. The students must create methods that accept generic parameters and perform operation on them. Deliverables A listing of the fully commented, working source code of the Java program Test data for the code A screen shot of the application in execution |
Step 1 Create a new project. Name it "Assignment_2_1". Step 2 Build a solution. Write the Java source code necessary to build a solution for the problem below:You have just taken a new "Teach for America" job in a very rural, impoverished neighborhood. The school system does not provide you with any tools. You decide to write a new gradebook program. You need your new gradebook to allow you to sort the students by NAME or by highest grade on a test so that you can fill out all of the many district reports on your students' progress. Remembering that sometimes Java lets you write one method and use it many times, you decide to write a generic method that will return the maximum and minimum element in a two-dimensional array (your gradebook, NAME, SCORE). Test the program using an array of random integers and again using an array of random names. In order to do the comparison of names you need to understand
the Java Character encoding scheme (Links to an external
site.). Print the array sorted by SCORE, then use it again and print the array sorted by NAME. MUST USE GENERICS OR I WILL NOT RATE Step 3 Compile and execute your code. Be sure to test the solution using a set of input data that demonstrates your solution is correct. Take a screen shot of your NetBeans® IDE showing the output from your Java program. |
import java.util.*;
public class StudentSortApp {
public static void main(String[] args) {
System.out.println("Welcome to the Student Scores Application." +
"\n");
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());
}
}
-------------------------Validator.java---------------------
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.java---------------------
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;
}
}
-----------------------StudentScore.java------------------
public class StudentScore implements Comparable<StudentScore> {
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;
}
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:
Get Answers For Free
Most questions answered within 1 hours.