Objectives Design and use generic classes and interfaces Des

Objectives

Design and use generic classes and interfaces

Design and write the method to use wildcard generic types

Assignment Overview

This programming exercise introduces generics and interfaces. The students must create methods that accept generic parameters and perform operation on them.

Step 1 Create a new project.

Name it \"Assignment_2_1\".

MUST USE JAVA GENERICS IN CODE

DO NOT USE PLAGERIZED MATERIAL


Step 2 Build a solution.

Write the Java source code necessary to build a solution for the problem below (YOU MUST USE JAVA GENERICS):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.

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.

Objectives

Design and use generic classes and interfaces

Design and write the method to use wildcard generic types

Assignment Overview

This programming exercise introduces generics and interfaces. The students must create methods that accept generic parameters and perform operation on them.

Step 1 Create a new project.

Name it \"Assignment_2_1\".

MUST USE JAVA GENERICS IN CODE

DO NOT USE PLAGERIZED MATERIAL


Step 2 Build a solution.

Write the Java source code necessary to build a solution for the problem below (YOU MUST USE JAVA GENERICS):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.


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.

Solution

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<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;
   }
}

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;

   }

}

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);
       for (StudentScore i : studentScores)
           System.out.println(i.getlastName() + \", \" + i.getfirstName() + \": \"
                   + i.getScore());

   }
}

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 : 36

Student 2 Last name: david
Student 2 First name: m
Student 2 score : 65

Student 3 Last name: ramu
Student 3 First name: bandla
Student 3 score : 10

Student 4 Last name: murali
Student 4 First name: p
Student 4 score : 5

Last Name Sort
david, m: 65
mark, smith: 36
murali, p: 5
ramu, bandla: 10

Score Sort
murali, p: 5
ramu, bandla: 10
mark, smith: 36
david, m: 65

Objectives Design and use generic classes and interfaces Design and write the method to use wildcard generic types Assignment Overview This programming exercise
Objectives Design and use generic classes and interfaces Design and write the method to use wildcard generic types Assignment Overview This programming exercise
Objectives Design and use generic classes and interfaces Design and write the method to use wildcard generic types Assignment Overview This programming exercise
Objectives Design and use generic classes and interfaces Design and write the method to use wildcard generic types Assignment Overview This programming exercise
Objectives Design and use generic classes and interfaces Design and write the method to use wildcard generic types Assignment Overview This programming exercise

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site