Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Error in Java Exception in thread \"main\" java.lang.ClassCastException: namesor

ID: 666978 • Letter: E

Question

Error in Java

Exception in thread "main" java.lang.ClassCastException: namesort.NameSort cannot be cast to java.lang.Comparable
   at java.util.ComparableTimSort.countRunAndMakeAscending(ComparableTimSort.java:316)
   at java.util.ComparableTimSort.sort(ComparableTimSort.java:184)
   at java.util.Arrays.sort(Arrays.java:1246)
   at namesort.NameSort.main(NameSort.java:87)
Java Result: 1

CODE:

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

package namesort;
      
import java.util.Arrays;
import java.util.Scanner;

public class NameSort {
String firstName,lastName;
int score;
//constructor
public NameSort(String firstName, String lastName, int score) {
this.firstName = firstName;
this.lastName = lastName;
this.score = score;
}
//getter setter methods
public String getFirstName() {
return firstName;
}

public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public int getScore() {
return score;
}

public void setScore(int score) {
this.score = score;
}
//sorting method
public int compareTo(NameSort compareStudent) { //java internally implement as comparable
int res = String.valueOf(this.getLastName()).compareTo(String.valueOf(compareStudent.getLastName()));
if(res == 0){
res = this.getFirstName().compareTo(compareStudent.getFirstName()); //checkcing if last name equals
}
return res;
}
public static void main(String args[]){
int num = 0,score;
String first,last;
Scanner sc = new Scanner(System.in);

System.out.println ("Welcome to the Student Scores Application ");
//reading number of students until correct ale enters
while(true){
System.out.print ("Enter number of students to enter: ");

num = sc.nextInt();
if(num>0){
break;
}
}
NameSort[] students = new NameSort[num]; //array to hold objects
for(int i=0;i<num;i++){ //reading student details
System.out.print (" Student "+(i+1)+" Last name: ");
first = sc.nextLine();
sc.nextLine();
System.out.print ("Student "+(i+1)+" First name: ");
last = sc.nextLine();
while(true){
System.out.print ("Student "+(i+1)+" Score: ");
score = sc.nextInt();
if((int)score >= 0 && (int)score <= 100){ //checking input score is valid or not
break;
}
}
NameSort student = new NameSort(first,last,score); //creating object
students[i] = student; //storing into array
}
Arrays.sort(students); //sorting...it will trigger comparable method
int i=0;
for(NameSort temp: students){ //printing back detials
System.out.println("student " + ++i + " : Last Name: " + temp.getLastName() + " First Name: "+temp.getFirstName()+
", Score : " + temp.getScore());
}
}
}
  
  


SPECS:

Welcome to the Student Scores Application.

Enter number of students to enter: 4

Student 1 last name: Steelman

Student 1 first name: Andrea

Student 1 score: 95

Student 2 last name: Michaels

Student 2 first name: Joe

Student 2 score: 92

Student 3 last name: Lowe

Student 3 first name: Doug

Student 3 score: 82

Student 4 last name: Michaels

Student 4 first name: Mike

Student 4 score: 93

  Lowe, Doug: 82

Michaels, Joe: 92

Michaels, Mike: 93

Steelman, Andrea: 95

Operation

This application accepts the last name, first name, and score for one or more students and stores the results in an array. Then, it prints the students and their scores in alphabetical order by last name.

Specifications

The program should implement a class named Student that stores the last name, first name, and score for each student. This class should implement the IComparable interface so the students can be sorted by name. If two students have the same last name, the first name should be used to determine the final sort order.

The program should use an array to store the Student objects. Then, it should sort the array prior to printing the student list.

Validate the input so the user can enter only a positive integer for the number of students, the last or first name can’t be an empty string, and the score is an integer from 0 to 100.

Explanation / Answer

working java code:

package student.scores;
import java.util.Scanner;
import java.util.Arrays;
import java.util.Comparator;
import java.util.ArrayList;
import java.util.*;


public class StudentScores{


    public static void main(String[] args)
    {
        System.out.println("Welcome to the Student Scores Application.");

        Scanner input = new Scanner (System.in);
        System.out.println("Enter number of students: ");
        int numStudents = input.nextInt();
        ArrayList<String> lastName = new ArrayList<>();
        ArrayList<String> firstName = new ArrayList<>();
        ArrayList<Integer> scores = new ArrayList<>();

        for (int i = 0; i < numStudents; i++)
        {
            input.nextLine();
            System.out.println("Enter Student Last Name: ");
            lastName.add(input.next());

            System.out.println("Enter Student First Name: ");
            firstName.add(input.next());   

            System.out.println("Enter Student Score: ");
            scores.add(input.nextInt());

        }
        System.out.println("Would you like to sort by name or score?");
        String answer = input.next();

        if(answer.equalsIgnoreCase("score"))
        {


         System.out.println("Sorted by Scores:");
       for(int in: scores){
            System.out.println(in);
           }
        }

         else if (answer.equalsIgnoreCase("name"))
    {

    }


}


/* static class Student implements Comparable {


   private String lastName;
   private String firstName;
   private int scores;

    public Student (String lastName, String firstName, int score)
    {
        this.lastName = lastName;
        this.firstName = firstName;
        this.scores = score;
    }

    public int getScores()
    {
        return scores;
    }

    public String getLastName()
    {
        return lastName;
    }

    public String getFirstName()
    {
        return firstName;
    }

    @Override
    public int compareTo(Student s)
    {
       if (s.lastName.equals(lastName))
       {
           return firstName.compareToIgnoreCase(s.firstName);
       }
      return lastName.compareToIgnoreCase(s.lastName);
    }*/


}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote