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’...
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....
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...
Note: Do not use classes or any variables of type string to complete this assignment Write...
Note: Do not use classes or any variables of type string to complete this assignment Write a program that reads in a sequence of characters entered by the user and terminated by a period ('.'). Your program should allow the user to enter multiple lines of input by pressing the enter key at the end of each line. The program should print out a frequency table, sorted in decreasing order by number of occurences, listing each letter that ocurred along...
Project 2 statement Please write this in JAVA. Please read this entire statement carefully before you...
Project 2 statement Please write this in JAVA. Please read this entire statement carefully before you start doing anything… This project involves implementing a simple university personnel management program. The program contains two different kinds of objects: students and faculty. For each object, the program stores relevant information such as university ID, name, etc. Different information is stored depending on the type of the object. For example, a student has a GPA, while a faculty has a title and department...
IN JAVA!! You may be working with a programming language that has arrays, but not nodes....
IN JAVA!! You may be working with a programming language that has arrays, but not nodes. In this case you will need to save your BST in a two dimensional array. In this lab you will write a program to create a BST modelled as a two-dimensional array. The output from your program will be a two-dimensional array.   THEN: practice creating another array-based BST using integers of your choice. Once you have figured out your algorithm you will be able...
Please answer in JAVA IDS 401 Assignment 4 Deadline In order to receive full credit, this...
Please answer in JAVA IDS 401 Assignment 4 Deadline In order to receive full credit, this assignment must be submitted by the deadline on Blackboard. Submitting your assignment early is recommended, in case problems arise with the submission process. Late submissions will be accepted (but penalized 10pts for each day late) up to one week after the submission deadline. After that, assignments will not be accepted. Assignment The object of this assignment is to construct a mini-banking system that helps...
The language is Java. Using a singly-linked list, implement the four queue methods enqueue(), dequeue(), peek(),...
The language is Java. Using a singly-linked list, implement the four queue methods enqueue(), dequeue(), peek(), and isEmpty(). For this assignment, enqueue() will be implemented in an unusual manner. That is, in the version of enqueue() we will use, if the element being processed is already in the queue then the element will not be enqueued and the equivalent element already in the queue will be placed at the end of the queue. Additionally, you must implement a circular queue....
1. you will just write your name in bytes - once in UTF8 bytes, once in...
1. you will just write your name in bytes - once in UTF8 bytes, once in UTF16 bytes. Name :Andrew Submit: 1. Your .java file 2. A screenshot showing your code ran and gave the right output. ---- /** * In this program you will write your first name in bytes in 2 different encodings. * Then convert the byte array to a String and print it out. * * TODO in lecture: show students how I google to find...