0% found this document useful (0 votes)
2 views10 pages

Assign3-oop

The document outlines a media library application using an abstract class MediaItem to define common properties and enforce a play() method for all media types. It describes the implementation of optional behaviors through interfaces like Downloadable and Rateable, and demonstrates polymorphism with subclasses such as Song, Movie, and Podcast. Additionally, it covers concepts like generics for type-safe playlists, equality checks, and runtime type checks to manage media items effectively.

Uploaded by

anumsaeed095
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views10 pages

Assign3-oop

The document outlines a media library application using an abstract class MediaItem to define common properties and enforce a play() method for all media types. It describes the implementation of optional behaviors through interfaces like Downloadable and Rateable, and demonstrates polymorphism with subclasses such as Song, Movie, and Podcast. Additionally, it covers concepts like generics for type-safe playlists, equality checks, and runtime type checks to manage media items effectively.

Uploaded by

anumsaeed095
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Page 1 of 10

1. Core Abstraction
Answer:
Use an abstract class MediaItem to define common properties and enforce the play()
method for all media.
An abstract class is suitable because:

• All media share implementation of properties (id, title, duration).

• Enforcing a base behavior (play() must be implemented).

Code:
public abstract class MediaItem {
protected String id;
protected String title;
protected int duration; // seconds

public MediaItem(String id, String title, int duration) {


this.id = id;
this.title = title;
this.duration = duration;
}

public abstract void play();

public String getId() { return id; }


public String getTitle() { return title; }

@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (!(obj instanceof MediaItem)) return false;
if (this.getClass() != obj.getClass()) return false;

MediaItem other = (MediaItem) obj;


return id.equals(other.id) && title.equals(other.title);
}

@Override
public int hashCode() {
return id.hashCode() + title.hashCode();
}
}

Page 2 of 10
2. Optional Behaviors
Answer:
Use Downloadable and Rateables interfaces to define optional capabilities.
Interfaces avoid polluting Media Item with methods irrelevant to some subclasses.

Downloadable.java
public interface Downloadable {
void download();
}
Rateable.java
public interface Rateable {
void rate(int stars);
}

3. Polymorphism & Collections


Answer:
Polymorphism ensures that play() on each MediaItem calls the correct subclass version,
even from a shared collection like ArrayList<MediaItem>.

Subclasses:

Song.java
public class Song extends MediaItem implements Downloadable, Rateable {
private String artist;

public Song(String id, String title, int duration, String artist) {


super(id, title, duration);
this.artist = artist;
}

@Override
public void play() {
System.out.println("Playing song by " + artist + ": " + title);
}

@Override
public void download() {
System.out.println("Downloading song: " + title);
}

@Override
public void rate(int stars) {

Page 3 of 10
System.out.println("Rated song '" + title + "' with " + stars + "
stars.");
}
}
Movie.java
public class Movie extends MediaItem implements Rateable {
private String director;

public Movie(String id, String title, int duration, String director) {


super(id, title, duration);
this.director = director;
}

@Override
public void play() {
System.out.println("Playing movie directed by " + director + ": " +
title);
}

@Override
public void rate(int stars) {
System.out.println("Rated movie '" + title + "' with " + stars + "
stars.");
}
}
Podcast.java
public class Podcast extends MediaItem implements Downloadable {
private String host;

public Podcast(String id, String title, int duration, String host) {


super(id, title, duration);
this.host = host;
}

@Override
public void play() {
System.out.println("Streaming podcast by " + host + ": " + title);
}

@Override
public void download() {
System.out.println("Downloading podcast: " + title);
}
}

Page 4 of 10
4. Subtyping vs. Inheritance
Answer:

• A Podcast is-a MediaItem → Inheritance

• A Podcast behaves-as Downloadable → Subtyping

This allows behavior composition without forcing all MediaItem subclasses to implement
unrelated methods.

5. Generics & Playlists


Answer:
Use Playlist<T extends MediaItem> for type-safe playlists, and wildcards for mixed
playlists.

Playlist.java
import java.util.ArrayList;
import java.util.List;

public class Playlist<T extends MediaItem> {


private List<T> items = new ArrayList<>();

public void addItem(T item) {


if (!items.contains(item)) {
items.add(item);
}
}

public void playAll() {


for (T item : items) {
item.play();
}
}
}
Mixed Playlist Usage
Playlist<MediaItem> mixed = new Playlist<>();
mixed.addItem(new Song("1", "Chill Vibes", 200, "Lo-Fi"));
mixed.addItem(new Podcast("2", "TechTalk", 1800, "Alex"));
mixed.playAll();

Page 5 of 10
6. Equality & Downcasting
Answer:

• Use getClass() in equals() to ensure a Song ≠ Movie even with same ID/title.

• Downcasting risk: Causes ClassCastException.

Mitigation: Use instanceof before casting:

Code:
if (item instanceof Song) {
Song s = (Song) item;
s.rate(5);
}

7. Runtime Type Checks


Answer:
Use instanceof to find downloadable items safely:

Code:
for (MediaItem item : mediaList) {
if (item instanceof Downloadable) {
((Downloadable) item).download();
}
}

8. Complete code implementation:


Class Downloadable:
public interface Downloadable {
void download();
}

Class MediaItem:
public abstract class MediaItem {
protected String id;
protected String title;
protected int duration; // in seconds

public MediaItem(String id, String title, int duration) {


this.id = id;

Page 6 of 10
this.title = title;
this.duration = duration;
}

public abstract void play();

public String getId() { return id; }


public String getTitle() { return title; }

@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (!(obj instanceof MediaItem)) return false;
if (this.getClass() != obj.getClass()) return false;

MediaItem other = (MediaItem) obj;


return id.equals(other.id) && title.equals(other.title);
}

@Override
public int hashCode() {
return id.hashCode() + title.hashCode();
}
}

Class Rateable:
public interface Rateable {
void rate(int stars);
}

Class Song:
public class Song extends MediaItem implements Downloadable, Rateable {
private String artist;

public Song(String id, String title, int duration, String artist) {


super(id, title, duration);
this.artist = artist;
}

@Override
public void play() {
System.out.println("Playing song by " + artist + ": " + title);
}

@Override
public void download() {
System.out.println("Downloading song: " + title);

Page 7 of 10
}

@Override
public void rate(int stars) {
System.out.println("Rated song '" + title + "' with " + stars + "
stars.");
}
}

Class Movie:
public class Movie extends MediaItem implements Rateable {
private String director;

public Movie(String id, String title, int duration, String director) {


super(id, title, duration);
this.director = director;
}

@Override
public void play() {
System.out.println("Playing movie directed by " + director + ": " +
title);
}

@Override
public void rate(int stars) {
System.out.println("Rated movie '" + title + "' with " + stars + "
stars.");
}
}

Class Podcast:
public class Podcast extends MediaItem implements Downloadable {
private String host;

public Podcast(String id, String title, int duration, String host) {


super(id, title, duration);
this.host = host;
}

@Override
public void play() {
System.out.println("Streaming podcast by " + host + ": " + title);
}

@Override
public void download() {

Page 8 of 10
System.out.println("Downloading podcast: " + title);
}
}

Class Playlist:
import java.util.*;

public class Playlist<T extends MediaItem> {


private List<T> items = new ArrayList<>();

public void addItem(T item) {


if (!items.contains(item)) {
items.add(item);
}
}

public void playAll() {


for (T item : items) {
item.play();
}
}
}
Class MediaLibraryApp:
import java.util.*;

public class MediaLibraryApp {


public static void main(String[] args) {
List<MediaItem> mediaLibrary = new ArrayList<>();

Song song = new Song("S1", "Imagine", 210, "John Lennon");


Movie movie = new Movie("M1", "Inception", 8880, "Christopher
Nolan");
Podcast podcast = new Podcast("P1", "TechTalk", 1800, "Jane Doe");

mediaLibrary.add(song);
mediaLibrary.add(movie);
mediaLibrary.add(podcast);

System.out.println("Playing all media:");


for (MediaItem item : mediaLibrary) {
item.play();
}

System.out.println("\nDownloading items:");
for (MediaItem item : mediaLibrary) {
if (item instanceof Downloadable) {
((Downloadable) item).download();

Page 9 of 10
}
}

System.out.println("\nRating applicable items:");


for (MediaItem item : mediaLibrary) {
if (item instanceof Rateable) {
((Rateable) item).rate(5);
}
}

System.out.println("\nCreating a Song-only playlist:");


Playlist<Song> songPlaylist = new Playlist<>();
songPlaylist.addItem(song);
songPlaylist.playAll();

System.out.println("\nCreating a mixed-media playlist:");


Playlist<MediaItem> mixedPlaylist = new Playlist<>();
mixedPlaylist.addItem(song);
mixedPlaylist.addItem(movie);
mixedPlaylist.addItem(podcast);
mixedPlaylist.playAll();
}
}

Output:

Page 10 of 10

You might also like