import java.util.Scanner; // "import" means "put me on a first name basis with this class" class Song3 extends Object { private String artist; private String title; private double length; // running time, in s. private boolean isCopyrighted; public static final double MB_PER_SEC = 3.0; // the "constructor": public Song3( String a, String t, double l, boolean isC ) { // Song3 this = /* a Song3 object w/ all fields "zero-ish" */; // implicit -- don't write it! this.artist = a; this.title = t; this.length = l; this.isCopyrighted = isC; // return this; // implicit -- don't write it! } public Song3(Scanner scan) { // to call one constructor from another, use syntax 'this'. this( scan.nextLine(), scan.nextLine(), scan.nextDouble(), (scan.next()).equalsIgnoreCase("yes") ); /* System.out.println( "Who is the song's artist? " ); String a = scan.nextLine(); System.out.println( "What is the song's name? " ); String t = scan.nextLine(); System.out.println( "What is the song's length, in seconds? " ); double l = scan.nextDouble(); System.out.println( "Is the song copyrighted? (yes/no) " ); boolean b = (scan.next()).equalsIgnoreCase("yes"); */ } // we will "override" the `toString` method defined for class `Object`. public String toString( /* Song3 this */ ) { return this.title + (this.isCopyrighted ? "©" : "") + " by " + this.artist; } /** return whether `s` can be stored using `space` disk-space. * @param s the song to consider * @param space the amount of available space it has to fit into, in B. */ public boolean willFit( /*Song3 thiss,*/ int space ) { return this.getDiskSpace() <= space; } /** @return how much space this song takes on disk, as an mp3, in B. */ public double getDiskSpace( /* Song3 this */ ) { return (this.getLength()/60.0) * MB_PER_SEC * Math.pow(2,20); } /** return whether one song is shorter than another. * @param that The song to compare this Song to. * @return whether s1 is shorter than s2. */ public boolean isShorter( /*Song3 this,*/ Song3 that ) { return (this.getLength() < that.getLength()); } String getArtist( /*Song3 this */ ) { return this.artist; } String getTitle( /*Song3 this */ ) { return this.title; } /** @return the length of this song, in seconds. */ public double getLength( /*Song3 this */ ) { return this.length; } public boolean getIsCopyrighted( /*Song3 this */ ) { return this.isCopyrighted; } private void setArtist( /* Song3 this, */ String newArtist ) { this.artist = newArtist; } private void setTitle( /* Song3 this, */ String newTitle ) { this.title = newTitle; } /** @param newLength -- must be >= 0 */ private void setLength( /* Song3 this, */ double newLength ) { if (newLength < 0) { throw new IllegalArgumentException("length can't be negative!"); } this.length = newLength; } public void setIsCopyrighted( /* Song3 this, */ boolean newIsCopyrighted ) { this.isCopyrighted = newIsCopyrighted; } /** return a remix of a song. * @return `orig` remixed by D.J. `dj`, who added `newMaterialLength` seconds of ill beatz. */ Song3 remix( /* Song3 this, */ String dj, double newMaterialLength ) { return new Song3( this.getArtist() + " vs " + dj, this.getTitle() + " (" + dj + " remix)", this.getLength() + newMaterialLength, false ); } static void testAll() { // some examples of Song3 objects: Song3 s1 = new Song3("Lil Nas X", "Old Town Road", 120, true); Song3 s2 = new Song3("Abba", "Ring Ring", 180, true); Song3 s2a = new Song3("Abba", "Ring Ring", 180, true); Object120.assertEquals( s1.toString(), "Old Town Road© by Lil Nas X" ); Object120.assertEquals( s1.isShorter(s2), true ); Object120.assertEquals( s2.isShorter(s1), false ); Object120.assertEquals( s1.isShorter(s1), false ); Object120.assertEquals( s1.willFit(4000), false); Object120.printTestMsg("remix"); Object120.assertEquals( new Song3( "Lil Nas X vs ibarland", "Old Tow Road (ibarland remix)", 150, false ), s1.remix("ibarland", 30) ); Object120.assertEquals( new Song3("Abba vs saltman", "Ring Ring (saltman remix)", 180, false), s2.remix("saltman", 0) ); Object120.assertEquals( s1.getIsCopyrighted(), true ); s1.setIsCopyrighted(false); Object120.assertEquals( s1.getIsCopyrighted(), false ); Object120.printTestSummary(); } public static void main( String[] args ) { testAll(); } }