Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9295703
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T21:40:09+00:00 2026-06-18T21:40:09+00:00

When I use the array sort function in my tester class I’ve been getting

  • 0

When I use the array sort function in my tester class I’ve been getting an error.

import java.util.Arrays;
public class tester implements EmployeeInfo
{

public static void main(String[] args) throws CloneNotSupportedException
{
        Employee[]emps = new Employee[9];
        double sum=0,total=0;

        emps[0]= new Staff("Chan, Scott","123",'M',1959,2,23,35.00);
        emps[1]= new Staff("Salinas, Brian","456",'F',1964,7,12,30.00);
        emps[2]= new Staff("Weir, Allen","789",'M',1970,6,2,22.00);
        emps[3]= new Faculty("Im, Lee", "243", 'F',1962,4,27, "Full","Ph.d",
                "Engineering", 3);
        emps[4]= new Faculty("Bui, Trung","791" , 'F', 1975,3,14, 
                "Associate","Ph.d","English", 1);
        emps[5] = new Faculty("Monreno, Maria", "623", 'F', 1980,5,22, 
                "Assistant","MS","Physical Education", 0);
        emps[6]= new Partime("Lee, Chesong", "455", 'F', 
                1977,8,10,35,20);
        emps[7]= new Partime("Garcia, Frank", "678", 'F', 
                1987,9,15,30,25);
        emps[8] = new Partime("Alquilo, Roscoe", "945", 'M', 
                1988,9,15,20,30);
        /**
         * Display Staff, faculty, and Part-time
         */
        for(int i = 0; i<emps.length;i++)
        {
            if(emps[i] instanceof Staff)
            {
                System.out.println("\n"+emps[i]);
            }//end of if statement
            if(emps[i] instanceof Faculty)
            {
                System.out.println("\n"+emps[i]);
            }//end of if statement

        }// end of for loop

        //b
        System.out.println("\nTotal montly Salary for all employees");
        for(int i = 0; i<emps.length; i++)
        { 
            sum = emps[i].monthlyEarning()+sum;
        }
        System.out.println("$"+sum);
        //c
        System.out.println("\nTotal monthly salary for all faculuty");
        for(int i = 0; i<emps.length;i++)
        {
            if(emps[i] instanceof Faculty)
            {
                total = emps[i].monthlyEarning()+total;
            }
        }
        System.out.println("$"+total);

        //d Duplicate a faculty object. test the duplication
        Faculty f1 = (Faculty)emps[4];
        Faculty f2 = (Faculty)f1.clone();
        Education dupl = new Education("Bachelor",
                "Airforce",2);
        f2.setEducation(dupl);
        System.out.println("\nD Duplicate a Faculty Object"
                +"\n"+f2.toString());

        //E Verify two staff objects are the same

        System.out.println("\nE.Verify two staff objects ");
        Staff s1 = (Staff)emps[6];
        Staff s2 = (Staff)s1.clone();
        Staff s3 = new Staff("Victor Tran", "456", 'M', 
        1993, 5, 20,60);
        if(s1.getbirthday()==s2.getbirthday())
        {
            System.out.print("\nThe two staff objects " +
                    " birthdays"+ " are the same "
                    +"therefore "+true+"\n");
        }
        if(s3.getbirthday()==s1.getbirthday())
        {
            System.out.print(true);
        }

        //F Sort employees by ascending employee ID
        System.out.println("\nSort employees by ID");
        Arrays.sort(emps);
        for(int i=0;i<emps.length;i++)
        {
            System.out.println("\n"+emps[i]);
        }

The error is outputted as

Exception in thread “main” java.lang.ClassCastException: Staff cannot be cast to java.lang.Comparable
at java.util.Arrays.mergeSort(Arrays.java:1144)
at java.util.Arrays.mergeSort(Arrays.java:1155)
at java.util.Arrays.sort(Arrays.java:1079)
at tester.main(tester.java:93)

My staff class looks something like this:

public class Staff extends Employee implements EmployeeInfo,Cloneable
{
    private double hourlyRate;

/**
 * Default Constructor
 */
public Staff()
{
    super();
    hourlyRate = 0.0;
}
/**
 * Argument Constructor
 * @param name
 * @param number
 * @param g
 * @param date
 * @param rate
 */
public Staff(String name, String number, char g, 
        int year, int month, int day,double rate)
{
    super(name,number,g,year,month,day);
    hourlyRate = rate; 
}
/**
 * Sets the hourly rate
 * @param rate
 */
public void setHourlyRate(double rate)
{
    hourlyRate = rate; 
}
/**
 * Get the hourly rate
 * @return hourlyRate
 */
public double getHourlyRate()
{
    return hourlyRate;
}
public double monthlyEarning()
{
    double salary = STAFF_MONTHLY_HOURS_WORKED* hourlyRate;
    return salary;
}

public String toString()
{
    return super.toString() +"\nFull Time"+"\nMonthly Salary: $"
                + monthlyEarning()+"\nHourly Rate: $"+hourlyRate;
}
public Object clone() throws CloneNotSupportedException
{
    Staff s = (Staff)super.clone();
    return s;
}
}// End of Staff Class
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T21:40:10+00:00Added an answer on June 18, 2026 at 9:40 pm

    Make your objects (Staff etc) implement Comparable interface and use ID for comparison. Check for usage of Comparable interface if needed , it is pretty simple.Without that Arrays.sort will not know how to sort your objects

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I use array_multisort() function to sort an array by a field value. what I
I tried to use lambda function with sort , but was getting Segmentation fault
Which algorithm does the JavaScript Array#sort() function use? I understand that it can take
I'm attempting to write a sort function to use with Array.sort(). I'm a bit
I'm trying to use the bubble sort method to sort an array of only
I was wondering how I could sort this array, when I use asort right
To access the array indice at the xth position we can use some sort
I'm trying to use array_filter on an array of objects, and using a public
How can I use usort to sort an associative array inside a symfony2 controller?
Trying to arrange an array based on the order of another array function Sort(old,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.