PHW6: ITunes

So far we’ve been working with composing shape objects with GUI Java classes. Now we’re going to practice composing objects that only output to the console.

Instructions

Part I: Song & ITunes basic

Rx: <15 min Av: 15-30 min Sd: 30-45 min DNF: 45+ min

  1. Create a new Java Project called ITunes_uLogin
  2. Create a new Song class with:
    • 4 Fields: for a song’s title, artist, album, & price
    • 2 Constructors: a default with no parameters and one with a parameter for each field
    • 9 methods: getters & setters for each field, and toString.
      • You can automatically generate getters & setters in Eclipse by right-clicking anywhere in the class and going to Source > Generate Getters & Setters and clicking Select All.
      • toString is a special Java method that returns a string representation of an object. When we don’t define a toString method and we try to print an object, we get its location in memory instead!
  3. Create an ITunes class that:
    • Stores a list of songs as an ArrayList, and initializes the list to be empty.
    • Has a method addSong to the ITunes class that adds a song to the ArrayList.
    • Has a print method that prints the entire song list using a for each loop.
    • Has a main method that adds 3 songs to the iTunes class without using the default constructor, and prints them out by calling the print method.

If you don’t feel confident in Part I, STOP. Do not move on to Part II.

Part II: ITunes challenge methods

Rx: <10 min Av: 10-20 min Sd: 20-30 min DNF: 31+ min

Add the following methods to the ITunes class and test them in main:

Demonstration

In class.