I am new to Java and I have a work to do which put below here.
[]
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();
}