Question: Write the Music class in Java (including the equals method shown on page 3 of Lesson 1). The toString method should return a String with
Write the Music class in Java (including the equals method shown on page 3 of Lesson 1). The toString method should return a String with the values of each instance variable.
| Music |
| -title: String -composer: String -opus: int |
| +Music() +Music(ti: String, comp: String, op: int) +getTitle(): String +getComposer(): String +getOpus(): int +setTitle( newTitle: String ) +setComposer( newComp: String) +setOpus( newOpus: int ) +equals(other: Object): boolean +toString(): String |
// EXAMPLE OF THE equals METHOD IN THE Music class: public boolean equals(Object obj) // overrides Object's equals method { Music other = (Music)obj; return title.equals(other.title) && composer.equals(other.composer); } // GENERIC METHOD EXAMPLE: (inside another class) public static void displayArray(T []myArray) { for( int i =0; i < myArray.length; ++i ) { System.out.println(myArray[i]); } } public static void main( String [] args ) { Music [] musicArray = { new Music("Moonlight Sonata", "Beethoven", 27), new Music("Brandenburg Concerto #3", "Bach", 1048), new Music("Prelude in e minor", "Chopin", 28) }; LList musicList = new LList(); // creates LinkedList! Music tempMusic; System.out.println(" The list as displayed in LList.java: "); displayArray(musicArray);// template is Music here for( int i=0; i < 3; ++i ) musicList.add(musicArray[i]); musicList.display(); tempMusic = musicList.getEntry(2); if( tempMusic != null ) System.out.println(" Found " + tempMusic.getTitle() ); else System.out.println( " Unable to find " + musicArray[1].getTitle() ); // you'll need to test more in an exercise! } // end main
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
