הלינקייה: מגזין חודשי למפתחים

רוצה לשמוע על כל האירועים, המדריכים, הקורסים והמאמרים שנכתבו החודש ?
הלינקייה הינו מגזין חופשי בעברית שמשאיר אותך בעניינים.
בלי ספאם. בלי שטויות. פעם בחודש אצלך בתיבה.

Syntax Exercise 3

For each of the following problems, create a class hirerchy that models the problem. Add a test class for every class you choose to implement, and a main method for the program's functionality.

Music Box

Create a system to manage a personal music collection. Each item in the collection is a full album and we need to keep track of its name and performing artist. Each album should also provide a "play" method, that will play the album (by displaying the album's artist and title). In addition, the music box should provide a "favorite album" method that returns the album that was played the largest number of times.

Implement all the required classes so that the following main clause will work as expected:

public static void main(String [] args) {
  MusicBox box = new MusicBox();
 
  Album a1 = new Album("The Who", "Tommy");
  Album a2 = new Album("Tom Waits", "Closing Time");
  Album a3 = new Album("John Cale", "Paris 1919");
 
  box.addAlbum(a1);
  box.addAlbum(a2);
  box.addAlbum(a3);
 
  a1.play() ; // prints "Playing The Who - Tommy"
  a2.play(); // prints "Playing John Cale - Paris 1919"  
  a1.play(); // prints "Playing The Who - Tommy"
 
  String favorite = box.favoriteAlbum(); 
 
  // prints "favorite album is The Who - Tommy"
  System.out.println("favorite album is " + favorite); 
}

Photo Album

Write a program that keeps tracks of tagged photo albums. The system should allow anyone to create a new photograph and tag friends in the photo (by their names). After tagging is done, we should allow printing all the friends tagged in a photo, or a list of all the photos a friend is tagged in.

The program should allow the following main method to work:

public static void main(String [] args) {
  PhotoAlbum album = new PhotoAlbum();
 
  Photo p = new Photo("Paris Trip 1");
  p.tag("Jimmy");
  p.tag("Jane");
  p.tag("Jeff");
 
  album.addPicture(p);
 
  p = new Photo("Look the Eiffel");
  p.tag("Jimmy");
  p.tag("Max");
  album.addPicture(p);
 
  p = new Photo("OMG it's so high");
  p.tag("Jimmy");
  p.tag("Jane");
 
  // prints "Jimmy, Jane"
  p.showTags();
 
  album.addPicture(p);
 
  // prints "Paris Trip 1, Look the Eiffel, OMG it's so high"
  album.showPictures("Jimmy");
 
  // prints "Paris Trip 1, OMG it's so high"
  album.showPictures("Jane");
}
course: