首页 > > 详细

Interfaces (More) & Inheritance讲解

Interfaces (More) & Inheritance
Artists
1.1 Introduction
The purpose of this lab is to give you more practice with the concepts of interfaces, inheritance, typing and subclassing. We'll also continue to use polymorphism as you build out more intricate class hierarchies.
1.2  What to do
A non-profit organization is creating a digital index of all artists who have some connection to the area. You'll help develop part of the solution. Here's the outline:
An Artist can be any of these:
All Artists maintain the following information:
name, a String containing information about an Artist’s first and last name (throw an IllegalArgumentException if the name is null or an empty string)
age, which is an integer in the range [0..128], containing information an Artist’s age (throw an IllegalArgument exception if the age is outside of this range)
genres, which is an array of Strings, representing an Artist’s genres
awards, which is an array of Strings, representing all awards that an Artist received
Additionally,
All Actors keep track of the movies they've acted in (a String array)
oAside from the shared attributes, this should be another parameter for the Actor constructor.
oAlso the toString method for Actor should follow the format provided below:   
My name is Samuel L. Jackson
My age is 73
I am an ACTOR
I make these types of movies: [Action, SciFi, Drama]
I have acted in these movies: [Star Wars, Captain America: Winter Soldier, Pulp Fiction]

All Musicians keep track of their Recording company (a String) and the title of their current album (a String)
oAside from the shared attributes, these should be added parameters to the Musician constructor, where the current album must precede the record company in the constructor.
oAlso the toString method for Musician should follow the format provided below:
My name is The Weeknd
My age is 32
I am an MUSICIAN
I make these types of music: [R&B, Pop]
My current album is: Dawn FM
My recording company is: XO

All Poets keep track of the publisher (a String) that publishes their books/work
oAside from the shared attributes, this should be the another parameter for the Poet constructor.
oAlso the toString method for Poet should follow the format provided below:
My name is Maya Angelou
My age is 86
I am an POET
I make these types of poems: [Autobiographical Fiction]
My publishing company is: Random House
Finally, all Artists can receive an award, which gets added to their current array of awards, and the method signature for that behavior is: receiveAward(String award).
All of your artists should implement the IArtist interface (given below). We're also giving you some "starter code" in the form of an AbstractArtist.
Be sure to place all of your solution assets in a package named artists
All of your test assets should be in the default package
package artists;

public interface IArtist {
void receiveAward(String award);
String [] getAwards();
}
 AbstractArtist code:
package artists;

import java.util.Arrays;

/**
* AbstractArtist class.
* This is the abstract superclass of all Artist concrete classes. It implements the
* IArtist interface and serves as the "repository" of reuse code for certain common services.
*/
public abstract class AbstractArtist implements IArtist {
private String name;
private int age;
private String [] genres;
private String [] awards;

/**
* AbstractArtist constructor.
* This class cannot be instantiated, but this creation mechanism ensures the superclass
* portion of all subclasses is appropriately initialized.
* @param name : name of the artist.
* @param age : age of the artist.
* @param genres : genres of art the artist develops.
* @param awards : awards earned by the artist.
*/
public AbstractArtist(String name, int age, String [] genres, String [] awards) throws IllegalArgumentException {
if(age > 128 || age < 0 || name == null || name.length() == 0) {
throw new IllegalArgumentException();
}
this.name = name;
this.age = age;
this.genres = genres;
this.awards = awards;
}

/**
* This method answers the age of the artist. This is a protected method and not intended to be
* used by clients outside of the hierarchy.
* @return (int)
*/
protected int getAge() {
return this.age;
}

/**
* This method answers the full name of the artist. It is a protected getter for subclass use
* @return (String)
*/
protected String getName() { return this.name; }

/**
* This method answers an array representing the genres the artist creates for.
* @return (String [])
*/
protected String [] getGenres() { return this.genres;}

/**
* This method answers the genres as a single string that is aggregated from the array.
* @return (String)
*/
protected String getGenresAsSingleString() {
if(this.genres == null || this.genres.length == 0) {
return "";
}
return Arrays.toString(this.genres); // convert the array to a string
}

/**
* Answers the awards earned by the artist.
* @return (String [])
*/
public String [] getAwards() { return this.awards;}

/**
* This method adds another award to the list of awards won by the artist.
* @param award (String)
*/
public void receiveAward(String award) {
int size = this.awards.length; // get the current size of the array

// Iterate through the current array, copy the values, then add the new value
String [] updatedAwards = new String[size + 1];
for (int i=0; i

联系我们
  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-21:00
  • 微信:codinghelp
热点标签

联系我们 - QQ: 99515681 微信:codinghelp
程序辅导网!