0

I am new to Java and I have a work to do which put below here. [Task]

And i have written code below and i get the error:

Exception in thread "main" java.lang.ClassCastException: class CSE_241_PA7.Image cannot be cast to class CSE_241_PA7.Playable (CSE_241_PA7.Image and CSE_241_PA7.Playable are in unnamed module of loader 'app') at CSE_241_PA7.Main.main(Main.java:33)

How could i fix this, i couldn't find my fault. Please help.

I am giving the code classes that written :

package CSE_241_PA7;

public class Main {
    public static void main(String[] args) {
        Dataset ds = new Dataset();
        //different observers
        Player p1 = new Player();
        Player p2 = new Player();
        Viewer v1 = new Viewer();
        Viewer v2 = new Viewer();

        ds.register(p1);
        ds.register(p2);
        ds.register(v1);
        ds.register(v2);

        ds.add(new Image("imagename1", "dimension info1", "other info1"));
        ds.add(new Image("imagename2", "dimension info2", "other info2"));
        ds.add(new Image("imagename3", "dimension info3", "other info3"));
        ds.add(new Image("imagename4", "dimension info4", "other info4"));
        ds.add(new Image("imagename5", "dimension info5", "other info5"));
        ds.add(new Audio("audioname1", "duration1", "other info1"));
        ds.add(new Audio("audioname2", "duration2", "other info2"));
        ds.add(new Audio("audioname3", "duration3", "other info3"));
        ds.add(new Video("videoname1", "duration1", "other info1"));
        ds.add(new Video("videoname2", "duration2", "other info2"));
        ds.add(new Video("videoname3", "duration3", "other info3"));
        ds.add(new Text("textname1", "other info1"));
        ds.add(new Text("textname2", "other info2"));
        ds.add(new Text("textname3", "other info3"));


        p1.updatePlaylist((Playable) ds.getDatasetObjects().get(0)); //33.line
        p1.updatePlaylist((Playable) ds.getDatasetObjects().get(2));
        p1.updatePlaylist((Playable) ds.getDatasetObjects().get(4));


        v1.updateViewList((NonPlayable) ds.getDatasetObjects().get(1));
        v1.updateViewList((NonPlayable) ds.getDatasetObjects().get(3));
        v1.updateViewList((NonPlayable) ds.getDatasetObjects().get(5));


        Playable po = p1.currentlyPlaying();
        if (po != null) {
            po.info();
            ds.remove((DatasetObject) po);
        } else {
            System.out.println("No currently playing item.");
        }
        NonPlayable np = v1.currentlyViewing();
        if (np != null) {
            np.info();
        }

        p1.showList();
        p2.showList();
        v1.showList();
        v2.showList();
    }
}

Now i am adding Image class:

package CSE_241_PA7;

public class Image extends DatasetObject implements NonPlayable {
    private String dimensionInfo;

    public Image(String name, String dimensionInfo, String otherInfo) {
        super(name, otherInfo);
        this.dimensionInfo = dimensionInfo;
    }

    public void play() {
        System.out.println("Playing image: " + getName());
    }

    @Override
    public void info() {
        System.out.println("Image Name: " + getName());
        System.out.println("Dimension Info: " + dimensionInfo);
        System.out.println("Other Info: " + getOtherInfo());
    }

    public void view() {
        System.out.println("Viewing image: " + getName());
    }

    public String getDimensionInfo() {
        return dimensionInfo;
    }
    public void showList() {
        System.out.println("This is the showList() method implementation in Audio class.");
    }
}

Now i am adding Playable interface:

package CSE_241_PA7;

public interface Playable {
    void play();
    void info();
    void showList();


}
berry.11
  • 88
  • 7
  • 2
    Please reduce your code to a [mcve]. For example, if the error occurs on line 33, is anything after that relevant to show? Do you need to show adding 14 objects to the dataset, or does it still occur if you only add 1 thing? – Andy Turner May 31 '23 at 09:54
  • 1
    The actual error seems relatively straightforward: you are trying to cast an `Image` to `Playable`, but `Image` implements just the `NonPlayable` interface. It's hard to advise you what to do to correct it, because it's not clear what you're trying to do. – Andy Turner May 31 '23 at 09:56

1 Answers1

3

The first thing you add to your DataSet is

new Image("imagename1", "dimension info1", "other info1")

In line 33, you are retrieving that image again with

ds.getDatasetObjects().get(0)

and in the same line, you cast that image to Playable:

(Playable) ds.getDatasetObjects().get(0)

However, Image is not a Playable, i.e. Image does not implement the Playable interface. Quite the opposite is true:

class Image ... implements NonPlayable

You probably need to check if the items you get from your DataSet are indeed Playable:

Object o = ds.getDatasetObjects().get(0);
if (o instanceof Playable) {
  p1.updatePlaylist((Playable) o); 
}

Even better would be an iterative apporach, if DataSet offers one:

for (Object data : ds.getDatasetObjects()) {
  if (data instanceof Playable) {
    p1.updatePlaylist((Playable) data);
  }
  if (data instanceof NonPlayable) {
    v1.updateViewList((NonPlayable) data);
  }
}
Thomas Behr
  • 761
  • 4
  • 10