// Representation of a musical track public class Tune { private String artist; // Performer of the piece private String title; // Name of the track private int year; // Release year public Tune() { this.setArtist("Thomas Alva Edison"); this.setTitle("Mary had a little lamb"); this.setYear(1877); } public void setArtist(String performer) { this.artist = performer; } public void setTitle(String track) { this.title = track; } public void setYear(int date) { this.year = date; } public String toString() { String performer = this.getArtist(); String track = this.getTitle(); int date = this.getYear(); String result = "Tune( " + performer + ", " + track + ", " + date + " )"; return result; } public static void main(String[] args) { Tune tune1 = new Tune(); Tune tune2 = new Tune(); tune2.setArtist("Paul McCartney"); tune2.setYear(1972); System.out.println( "tune1: " + tune1 ); System.out.println( "tune2: " + tune2 ); System.out.println( "Same artist: " + tune1.sameArtist(tune2) ); System.out.println( "Same title: " + tune1.sameTitle(tune2) ); } // comparators public boolean sameArtist(Tune that ) { String s1 = this.getArtist(); String s2 = that.getArtist(); if ( s1.equals(s2) ) { return true; } else { return false; } } public boolean sameTitle(Tune that ) { String s1 = this.getTitle(); String s2 = that.getTitle(); if ( s1.equals(s2) ) { return true; } else { return false; } } // inspectors public String getArtist() { return this.artist; } public String getTitle() { return this.title; } public int getYear() { return this.year; } }