Purpose:
To practice the design of classes, interfaces, and methods.
Part 1: Defining Methods for Classes
Here we'll define a class hierarchy to represent a collection of media (CDAlbum, DVDVideo, and VCRTape).
Exercise 1:
Start by designing a class to represent a music CD (CDAlbum). You must keep track of the title of the album, the artist, and the year it was published.
Make some examples of CDs in an examples class.Exercise 2:
Design an interface (for Media) and two new classes (DVDVideo and VCRTape) to represent the other types of media. The two new classes must also represent a title and the year it was published, but in addition DVDVideo must keep track of whether or not it is doubleSided, and a VCRTape has a playSpeed (a double).
Make some examples of each class.Exercise 3:
Design a method getTitle for your Media interface/classes that returns its title. Don't forget to test!
Exercise 4:
Design a method speed for your Media interface/classes that returns its playSpeed. For CDs/DVDs the play speed is always 1.0.
Exercise 5:
Design a method rerelease for your Media interface/classes that returns a new media with an updated year (as if the producer just re-released the album/movie).
Part 2: List Methods
Here we'll define a class hierarchy to represent the actual collection.
Exercise 6:
Design an interface (ILoM) and two new classes (MtLoM and ConsLoM) to represent the lists of media.
Make some examples of lists.Exercise 7:
Design a method averageSpeed for your list interface/classes that calculates the average speed of all the media in the list. Hint: You might need to create a helper method like length.
Exercise 8:
Design a method titleList for your list interface/classes that returns all the titles of all the media in the list separated by new-lines ("\n").
Exercise 9:
Design a method rereleaseAll for your list interface/classes that returns a new list of all the media after being re-released.
Part 3: Posns and Images... in Java
We've seen some image classes/methods in lectures, but now you get to mess around with them yourself.
Have a look through the JavaWorld image tutorial. It shows another way to run Java programs, but you should be able to see the basic concepts.
Setup:
The World library contains a class Posn that represents two dimensional positions. Posns (as you may have guessed) are created like so:Grab the JavaWorld library, save it somewhere you can find it, and add it to your Project's build-path.
Place import image.*; at the top of your file. If you don't see any errors, then you've done something right! You'll also need import world.*;, so you can add that too.new Posn(5, 7)Exercise 10:
We're going to go through the process you did for the first HW, except now we're in Java... right?Design an interface (ILoP) and classes (MtLoP and ConsLoP) to represent lists of Posns and create some examples. Make sure you have one with at least 5 nicely spaced Posns so we can draw them next.
Exercise 11:
Design a method (say justDots) for your list of posn interface/classes that takes a Scene and places dots at each of this list posns.
In order to draw your resulting Scene, take a look at what we did in the lecture notes on Monday, creating a dummy boolean so that World.display is called when an instance of your Examples class is created. You could also call display within a test method, since it returns a boolean once the popup window is closed.Exercise 12:
Design a method (say withLines) that takes a Scene, places dots, and draws lines between adjacent posns in this list posns.
Exercise 13:
Design a method (say withNumLines) that takes a Scene, places dots, draws lines, and numbers each of hte posns in this list.
Make sure the numbers go in order from left to right.
If you finish early, you can mess around with more recursive methods to create images... or finish your HW.