java inheritance programs of example music composition
import java.util.ArrayList;
public class Composition {
private static int ids;
private int id;
private String name, artist;
private double length;
public Composition(String n, String a, double l) {
name = n;
artist = a;
length = l;
id = ids++;
}
public void getInfo() {
System.out.println("Composition: " + name);
System.out.println("Artist (or group): " + artist);
System.out.println("Length: " + length + " minutes");
}
public static void main(String args[]) {
ArrayList<Composition> melodies = new ArrayList<>();
melodies.add(new Composition("Angels", "Morandi", 3.8));
melodies.add(new Composition("Porcelain", "Moby", 4));
melodies.add(new GenreComposition("Flash in the night", "Secret Service", "disco", 4.3));
melodies.add(new GenreComposition("Toccata and Fugue in D minor", "Johann Sebastian Bach", "classic", 8.5));
melodies.add(new GenreComposition("Hang on to you love", "Sade", "jazz", 4));
melodies.add(new GenreComposition("Dream of You", "Schiller", "electronic", 4));
melodies.add(new CountryGenreComposition("Orion", "Metallica", "metal", "USA", 8.5));
melodies.add(new CountryGenreComposition("Emerald Sword", "Rhapsody of fire", "metal", "Italy", 5));
melodies.add(new CountryGenreComposition("Hey You", "Pink Floyd", "rock", "Great Britain", 4.5));
melodies.add(new CountryGenreComposition("Dresden performance", "Klaus Schulze", "electronic", "Germany", 142));
melodies.add(new CountryGenreComposition("Blooming Day", "W Angel's Conquest", "metal", "Ukraine", 6.8));
for(Composition m: melodies)
m.getInfo();
}
}
class GenreComposition extends Composition {
private String genre;
public GenreComposition(String n, String a, String g, double l) {
super(n, a, l);
genre = g;
}
public void getInfo() {
super.getInfo();
System.out.println("Genre: " + genre);
}
}
class CountryGenreComposition extends GenreComposition {
private String country;
public CountryGenreComposition(String n, String a, String g, String c, double l) {
super(n, a, g, l);
country = c;
}
public void getInfo() {
super.getInfo();
System.out.println("Country: " + country);
}
}
Comments
Leave a comment