0

I implemented the comparable interface. I don't know how Arrays.sort internally calls the comparable interface too. Can someone go step by step on how the Arrays.sort secretly uses the compareTo method? I get this error:

Exception in thread "main" java.lang.ClassCastException: Lab8.Appliance cannot be cast to java.lang.Comparable
    at java.util.ComparableTimSort.countRunAndMakeAscending(ComparableTimSort.java:290)
    at java.util.ComparableTimSort.sort(ComparableTimSort.java:157)
    at java.util.ComparableTimSort.sort(ComparableTimSort.java:146)
    at java.util.Arrays.sort(Arrays.java:472)
    at Lab8.TestAppliance.main(TestAppliance.java:52) 

I tried to debug it and when I go into the Arrays.sort method, then it goes into this:

  public static void sort(Object[] a) {
    if (LegacyMergeSort.userRequested)
        legacyMergeSort(a);
    else
        ComparableTimSort.sort(a);
}

But I still don't get it.

public class Appliance implements Comparable<Appliance> {
public static int currentID = 0;
private int ID;
private boolean on;
private int currentWatts;
private int onW;
private int offW;

public Appliance(Appliance a1) {
    ID = currentID;
    on = a1.on;
    onW = a1.onW;
    offW = a1.offW;
}
public Appliance (int newOnWatts, int newOffWatts)
{
    currentID++;
    ID = currentID;
    onW = newOnWatts;
    offW = newOffWatts;
}
public int getID()
{
    return ID;
}
public void setID(int newID)
{
    ID = newID;
}
public int getCurrentWatts() {
    return currentWatts;
}
public void setCurrentWatts(int newCurrentWatts) {
    if (currentWatts>0)
    {
        currentWatts = newCurrentWatts;
    }
}
public int getOnWatts()
{
    return onW;
}
public void setOnWatts(int newOnW)
{
    if (onW>0)
    {
        onW = newOnW;
    }
}
public int getOffWatts()
{
    return offW;
}
public void setOffWatts(int newOffW)
{
    if (offW>0)
    {
        offW = newOffW;
    }
}
public void turnOn()
{
    on = true;
    currentWatts = onW;
}
public void turnOff()
{
    on = false;
}
public int compareTo(Appliance that)
{
    if (this.onW > that.onW) return 1;
    else if (this.onW < that.onW) return -1;
    else return 0;
}
public String toString()
{
    return ID + " on?=" + on + " OnW=" + onW + " OffW=" + offW;
}

}

   public class SmartAppliance extends Appliance implements Comparable<Appliance> {
private double percentSaving;
private boolean smartOn;

public SmartAppliance(SmartAppliance a2) {
    super(a2);
    smartOn = a2.smartOn;
}
public SmartAppliance(int newOnWatts, int newOffWatts, double      newPercentSaving) {
    super(newOnWatts, newOffWatts);
    setPercentSaving(newPercentSaving);
    smartOn = false;
}
public double getPercentSaving() {
    return percentSaving;
}
public void setPercentSaving(double newPercentSaving) {
    if (newPercentSaving>0)
    {
        percentSaving = newPercentSaving;
    }
}
public boolean smartOn() {
    smartOn = true;
    setCurrentWatts((int) (getCurrentWatts()-             (getCurrentWatts()*percentSaving)));
    return smartOn;
}
public int compareTo(Appliance that)
{
    if (getOnWatts() > that.getOnWatts()) return 1;
    else if (getOnWatts() < that.getOnWatts()) return -1;
    else return 0;
}
public String toString()
{
    return super.toString() + "; PercentSaving=" + percentSaving + " smartOn=" + smartOn;
     }
  }

 public interface Comparable<Appliance> {
public int compareTo(Appliance that);
  }

import java.util.*;

public class TestAppliance {
public static void main(String[] args) {
    Appliance a1 = new Appliance(200,0);
    System.out.println(a1);
    System.out.println("appliance off watts "+a1.getCurrentWatts());    
    a1.turnOn();
    System.out.println(a1);
    System.out.println("appliance on watts "+a1.getCurrentWatts()); 

    // test Appliance copy constructor
    System.out.println("Appliance Copy Constructor");
    Appliance a1Copy = new Appliance(a1);
    a1Copy.setOnWatts(150);
    System.out.println("ORIGINAL "+a1);
    System.out.println("COPY "+a1Copy);

    SmartAppliance s1 = new SmartAppliance(783,50,0.25);
    System.out.println(s1);

    // test SmartAppliance reduce current wattage
    s1.turnOn();
    System.out.println("smartOff watts "+s1.getCurrentWatts());
    s1.smartOn();
    System.out.println("smartOn watts "+s1.getCurrentWatts());

    // test SmartAppliance copy constructor
    System.out.println("SmartAppliance Copy Constructor");
    SmartAppliance s1Copy = new SmartAppliance(s1);
    s1Copy.setPercentSaving(.5);
    System.out.println("ORIGINAL "+s1);
    System.out.println("COPY "+s1Copy);

    // sorted array 
    Appliance a = new Appliance(100,0);
    Appliance b = new Appliance(125,0);
    Appliance c = new Appliance(150,0);
    Appliance [] appliance = {a,b,c};
    int i;

    for (i=0;i<appliance.length;i++)
    {
        //int random = (int) (Math.random()*100);
        //appliance[i]=new Appliance(random,zero);
        //appliance[i+1]=new SmartAppliance(random,random2,random3);
        System.out.println(appliance[i]);
    }

    Arrays.sort(appliance);
    System.out.println("Sorted");
    for (i =0;i<appliance.length;i++)
        System.out.println(appliance[i]);
    }
}
2
  • 1
    Narrow it down a little... I don't think you need to post your entire program to figure out Arrays.sort Commented Mar 13, 2017 at 21:15
  • The stack track tells us there is an issue with: ComparableTimSort. This is the class that you need to include in your description. Commented Mar 13, 2017 at 21:19

2 Answers 2

1

Delete your interface of Comparable and use system one. Implement it in your Appliance class and don't implement in SmartAppliance. If you need to overwrite it - just do it, don't implement the same interface once again. It's needed because Arrays.sort use this interface inside, not your own

Sign up to request clarification or add additional context in comments.

Comments

0

I see that your SmartAppliance should implement

public class SmartAppliance implements Comparable<SmartAppliance >

and

public int compareTo(SmartAppliance that)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.