Question

Assignment Overview This programming exercise introduces generics and interfaces. The students must create methods that accept...

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.).
You should check this site to understand which character is bigger ("A" or "Z") and then work from there.

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.

Homework Answers

Answer #1

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:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Assignment #4 – Student Ranking : In this assignment you are going to write a program...
Assignment #4 – Student Ranking : In this assignment you are going to write a program that ask user number of students in a class and their names. Number of students are limited to 100 maximum. Then, it will ask for 3 test scores of each student. The program will calculate the average of test scores for each student and display with their names. Then, it will sort the averages in descending order and display the sorted list with students’...
In c++ create a class to maintain a GradeBook. The class should allow information on up...
In c++ create a class to maintain a GradeBook. The class should allow information on up to 3 students to be stored. The information for each student should be encapsulated in a Student class and should include the student's last name and up to 5 grades for the student. Note that less than 5 grades may sometimes be stored. Your GradeBook class should at least support operations to add a student record to the end of the book (i.e., the...
In Programming Exercise 13 (Chapter 8), you are asked to write a program to calculate students’...
In Programming Exercise 13 (Chapter 8), you are asked to write a program to calculate students’ average test scores and their grades. Improve this programming exercise by adding a function to sort students’ names so that students’ data is output into ascending order according to their name. *Need the answer in C++*
Please write the code in Python. Write a program/function in any Object-Oriented programming language that will...
Please write the code in Python. Write a program/function in any Object-Oriented programming language that will implement Queue Abstract Data Type with the following functions/methods.  Any build-in/pre-defined Queue function/library (e.g., java.util.Queue in Java) is NOT allowed to use in your code. push(Element):  insert the input Element (e.g., String or Integer in Java) to the end of the queue. pop(): remove the head element of the queue and print the head element on screen. count():  return the total number of elements in the queue...
Please Use your keyboard (Don't use handwriting) Thank you.. I need new and unique answers, please....
Please Use your keyboard (Don't use handwriting) Thank you.. I need new and unique answers, please. (Use your own words, don't copy and paste) Write a Java program that: Asks the user to enter his/her first name and ID. Prints the user's first name and ID in reverse order with a space between them recursively. Important notes: You should have to copy and paste the Java as your answer for this question. DON’T take screen shot for your Java Code....
C++ needs to output all of the name of the students not just first Write a...
C++ needs to output all of the name of the students not just first Write a program which uses an array of string objects to hold the five students' full names (including spaces), an array of five characters to hold each student's letter grade, a multi-dimensional array of integers to hold each of the five student's four test scores, and an array of five doubles to hold each student's average test score. The program will use a function to allow...
I cannot upload the needed text files to this question for your benefit of double checking...
I cannot upload the needed text files to this question for your benefit of double checking your solution, but I need help on this multi part question. Thanks! Write a Python program that will open the ‘steam.csv’ file. Your program should load the data as col- umns corresponding to the variable names located in the first row (You can hard code this – i.e open the file and use the names in the first row as variable names). You should...
Write a code in c++ using linear insertion following the steps below. Comment your work. 1....
Write a code in c++ using linear insertion following the steps below. Comment your work. 1.    Ask the user for the name of a file containing data. If it does not exist, the program should display an error, then ask for a new file name. Entering an asterisk (*) as the first and only character on a line should terminate the program. 2.     You can use a statically-allocated one-dimensional array of doubles for this with length 100. You...
n this lab, you use what you have learned about parallel arrays to complete a partially...
n this lab, you use what you have learned about parallel arrays to complete a partially completed C++ program. The program should: Either print the name and price for a coffee add-in from the Jumpin’ Jive Coffee Shop Or it should print the message Sorry, we do not carry that. Read the problem description carefully before you begin. The file provided for this lab includes the necessary variable declarations and input statements. You need to write the part of the...
Using C++, Python, or Java, write a program that: In this programming exercise you will perform...
Using C++, Python, or Java, write a program that: In this programming exercise you will perform an empirical analysis of the QuickSort algorithm to study the actual average case behavior and compare it to the mathematically predicted behavior. That is, you will write a program that counts the number of comparisons performed by QuickSort on an array of a given size. You will run the program on a large number of arrays of a certain size and determine the average...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT