Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

import java . io .*; import java . util . Calendar ; public class Person { priva

ID: 3576174 • Letter: I

Question

import java . io .*; import java . util . Calendar ; public class Person { private String name ; private String birthdate ; // stored as yyyy -mm -dd public Person () { this . init ("n/a", " 1900 -01 -01 ") ; } public Person ( String s1 , String s2 ) { this . init ( s1 , s2 ) ; } public void init ( String s1 , String s2 ) { this . setName ( s1 ) ; this . setBirthdate ( s2 ) ; } public String getName () { return this . name ; } public String getBirthdate () { return this . birthdate ; } public void setName ( String s ) { this . name = s ; } public void setBirthdate ( String s ) { this . birthdate = s ; } public int age () { Calendar now = Calendar . getInstance () ; // current date and time int curYear = now . get ( Calendar . YEAR ) ; // current year int curMonth = now . get ( Calendar . MONTH ) + 1; // current month ( indexed at 0) int curDay = now . get ( Calendar . DAY_OF_MONTH ) ; // current day String [] temp = this . birthdate . split ("-") ; int myYear = Integer . parseInt ( temp [0]) ; int myMonth = Integer . parseInt ( temp [1]) ; int myDay = Integer . parseInt ( temp [2]) ; int age = curYear - myYear ; // if the birthday hasn ’t happened yet , the age is one year younger if ( curMonth < myMonth || ( curMonth == myMonth && curDay < myDay ) ) age - -; return age ; } public void userEdit () throws IOException { BufferedReader cin = new BufferedReader (new InputStreamReader ( System . in ) ) ; System . out . print (" Person name : ") ; this . setName ( cin . readLine () ) ; System . out . print (" Birthdate (yyyy -mm -dd): ") ; this . setBirthdate ( cin . readLine () ) ; } public void print () { System . out . format ("%s (%d)", this . name , this . age () ) ; } } import java . io .*; public class Artist extends Person { private String firstAlbum ; private int nTopTenHits ; public Artist () { super () ; this . init ("n/a", 0) ; } public Artist ( String s1 , String s2 , String s3 , int n ) { this . init ( s1 , s2 , s3 , n ) ; } public void init ( String s , int n ) { this . setFirstAlbum ( s ) ; this . setNTopTenHits ( n ) ; } public void init ( String s1 , String s2 , String s3 , int n ) { super . init ( s1 , s2 ) ; this . setFirstAlbum ( s3 ) ; this . setNTopTenHits ( n ) ; } public String getFirstAlbum () { return this . firstAlbum ; } public int getNTopTenHits () { return this . nTopTenHits ; } public void setFirstAlbum ( String s ) { this . firstAlbum = s ; } public void setNTopTenHits (int n ) { this . nTopTenHits = n ; } @Override public void userEdit () throws IOException , NumberFormatException { BufferedReader cin = new BufferedReader (new InputStreamReader ( System . in ) ) ; super . userEdit () ; System . out . print (" First album : ") ; this . setName ( cin . readLine () ) ; System . out . print ("# top 10 hits : ") ; this . setNTopTenHits ( Integer . parseInt ( cin . readLine () ) ) ; } @Override public void print () { System . out . format ("%s: %s (%d)", this . getName () ,this . firstAlbum , this . nTopTenHits ) ; } } import java . io .*; public class Song { private Artist artist ; private String title ; private int year ; private boolean oldie ; public Song () { this . init (new Artist () , "n/a", -1) ; } public Song ( Artist A , String s , int x ) { this . init (A , s , x ) ; } Page 2 public void init ( Artist A , String title , int year ) { this . setArtist ( A ) ; this . setTitle ( title ) ; this . setYear ( year ) ; } public Artist getArtist () { return this . artist ; } public String getArtistName () { return this . artist . getName () ; } public String getTitle () { return this . title ; } public int getYear () { return this . year ; } public boolean isOldie () { return this . oldie ; } public void setArtist ( Artist A ) { this . artist = A ; } public void setArtistName ( String s ) { this . artist . setName ( s ) ; } public void setTitle ( String s ) { this . title = s ; } public void setYear (int x ) { this . year = x ; if ( this . year > 0 && this . year <= 1980) this . setOldie ( true ) ; else this . setOldie ( false ) ; } private void setOldie ( boolean b ) { this . oldie = b ; } public void userEdit () throws IOException , NumberFormatException { BufferedReader cin = new BufferedReader (new InputStreamReader ( System . in ) ) ; this . artist . userEdit () ; System . out . print (" Song title : ") ; this . setTitle ( cin . readLine () ) ; System . out . print (" Song year : ") ; this . setYear ( Integer . parseInt ( cin . readLine () ) ) ; } public void print () { String s = this . isOldie () ? " ( oldie )" : ""; System . out . format ("%s by %s (%d)%s", title , artist . getName () , year , s ) ; } public boolean isInDecade (int decade ) { return ( this . year >= decade && this . year < decade +10) ; } }

Problem 3 (35 points) Write a class called TeenyBopper to store music artists who are teenagers. The TeenyBopper class inherits from the Artist class, and additionally stores the artist’s legal guardian as a String. An example of the TeenyBopper class in use is below. You only have to write enough of the TeenyBopper class so that the code below works. Anything extra you write will not be graded. ArrayList < TeenyBopper > T = new ArrayList < TeenyBopper >() ; for (int i = 0; i < 100; i ++) { TeenyBopper temp = new TeenyBopper () ; T . add ( temp ) ; T . get ( i ) . userEdit () ; } // list all legal guardians for ( TeenyBopper temp : T ) { if ( temp . isValid () ) // check if teeny bopper is actually a teenager System . out . println ( temp . getLegalGuardian () ) ; }

Explanation / Answer

class TeenyBopper extends Artist
{
String legalGuardian;
public void userEdit () throws IOException , NumberFormatException
{
     BufferedReader cin = new BufferedReader (new InputStreamReader ( System . in ) ) ;
     super . userEdit () ;
     System . out . print (" Legal Guardian : ") ;
     this . setLegalGuardian ( cin . readLine () ) ;

}
public String getLegalGuardian()
{
     return legalGuardian;
}
public void setLegalGuardian(String lg)
{
      legalGuardian=lg;
}
public boolean isValid()
{
     if(this.age() > 9 && this.age() <20)
     return true;
     return false;
}
      
}