In: Computer Science
In Java Please!!
6.21 LAB: Artwork label (modules) Define the Artist class in Artist.py with a constructor to initialize an artist's information. The constructor should by default initialize the artist's name to "None" and the years of birth and death to 0. Define the Artwork class in Artwork.py with a constructor to initialize an artwork's information. The constructor should by default initialize the title to "None", the year created to 0, and the artist to use the Artist default constructor parameter values. Add an import statement to import the Artist class. Add import statements to main.py to import the Artist and Artwork classes. Ex: If the input is: Pablo Picasso 1881 1973 Three Musicians 1921 the output is: Artist: Pablo Picasso (1881-1973) Title: Three Musicians, 1921 If the input is: Brice Marden 1938 -1 Distant Muses 2000 the output is: Artist: Brice Marden, born 1938 Title: Distant Muses, 2000
Thanks for the question. Below is the code you will be needing. Let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please leave a +ve feedback : ) Let me know for any help with any other questions. Thank You! =========================================================================== public class Artist { private String artistName; private int yearOfBirth; private int yearOfDeath; public Artist(String artistName, int yearOfBirth, int yearOfDeath) { this.artistName = artistName; this.yearOfBirth = yearOfBirth; this.yearOfDeath = yearOfDeath; } public Artist() { artistName = "None"; yearOfBirth = 0; yearOfDeath = 0; } public String getArtistName() { return artistName; } public int getYearOfBirth() { return yearOfBirth; } public int getYearOfDeath() { return yearOfDeath; } public void printInfo() { if (yearOfDeath == -1) { System.out.println("Artist: " + getArtistName() + ", born " + getYearOfBirth()); } else { System.out.println("Artist: " + getArtistName() + " (" + getYearOfBirth() + "-" + getYearOfDeath() + ")"); } } }
============================================================
public class Artwork { private Artist artist; private String title; private int yearCreated; public Artwork(Artist artist, String title, int yearCreated) { this.artist = artist; this.title = title; this.yearCreated = yearCreated; } public Artwork(String title, int yearCreated) { this.title = title; this.yearCreated = yearCreated; artist = new Artist(); } public Artwork() { title = "None"; yearCreated = 0; artist = new Artist(); } public String getTitle() { return title; } public int getYearCreated() { return yearCreated; } public Artist getArtist() { return artist; } public void printInfo() { artist.printInfo(); System.out.println("Title: " + getTitle() + ", " + getYearCreated()); } }
============================================================
public class TestArtwork { public static void main(String[] args) { Artist picaso = new Artist("Pablo Picaso",1881,1973); Artwork musicians = new Artwork(picaso,"Three Musicians",1921); musicians.printInfo(); Artist brice = new Artist("Brice Marden",1938,-1); Artwork muses = new Artwork(brice,"Distant Muses",2000); muses.printInfo(); } }
============================================================