In: Computer Science
I'm very confused on how to complete this code. There are 5 total classes. I have some written but it isn't quite working and the Band class especially is confusing me. I posted the entire thing since the band and manager class refer to the other classes' constructors and thought it would be helpful to post those as well.
Person class is a base class:
Private variables
String firstname, String lastname, int age
Constructor has the parameters String firstname, String lastname, int age.
Add Getter and setter methods for all of the member variables.
Instrument:
It has private member variables: an int year, a String make, int yearIntroduced, String model, String type.
String type must be one of the valid values ‘guitar’, ‘drum’, ‘bass’, ‘keyboard’, ‘wind’, ‘bass’, ‘keyboard’. You must check the valid value in the constructor to be one of these values or print out an error that "Wrong type of instrument:" with the type that is given as a parameter in the constructor.
Constructor has the parameters String make, String model, String type, int yearIntroduced and an int year. You can check the validity of the type by one of three methods,
Methods:
toString() prints the values of the Instruments as follows
"Instrument type guitar with make Gibson with model Les Paul in year 2019 was first introduced in 1962".
Musician
A musician is a Person. It has additional member variables a list of Instruments.
It has two Constructors.
1. First constructor has has String firstname, String last name, int age, and Instruments[] instruments.
2. Second constructor has String firstname, String last name, int age. It sets a default array with 5 elements as instruments for the musician which are initially empty.
Consider the additional member variable you need to keep track the number of instruments and add it to the class.
Methods:
getInstruments() method returns the list of instruments the musician plays. What is the type that this method return?
addInstrument(Instrument ins) adds an instrument to the list of instruments that the musician plays. If the caller adds another instrument beyond 5 elements, it prints an error.
“The musician can only play at most 5 instruments”. Here 5 is the size of the array. If the array size is a different integer, print that integer.
setInstrument(Instrument[] instruments) sets the instruments that a musician plays to an array given by the caller of the method.
Band
This class has String name, a list of Musicians, and a Manager manager as variable.
It has two constructors
1. String name and a list of Musicians
2. String name, a list of Musicians and a Manager
Methods:
setManager(Manager m) sets the manager of the band.
getManager() returns the manager of the band (what should this return?)
calculateMax() returns the Musician which plays the largest number of instruments.
getName returns the name of the Band.
setName sets with the name of the Band.
Manager:
is a type of Person.
It has an additional variable bands, which is list of Bands( a Band array).
Constructor 1. must have String firstname, String lastname, int age. It sets up the bands variable to be an array of 5 Bands.
Methods:
addBand() takes a Band parameter and adds it to the contents of the bands variable.
getBands() returns a list of bands.
Test cases:
Tips and Tricks:
/*************************************Person.java*************************************/
package music;
/**
* The Class Person.
*/
public class Person {
/** The first name. */
private String firstName;
/** The last name. */
private String lastName;
/** The age. */
private int age;
/**
* Instantiates a new person.
*
* @param firstName the first name
* @param lastName the last name
* @param age the age
*/
public Person(String firstName, String lastName, int
age) {
super();
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}
/**
* Gets the first name.
*
* @return the first name
*/
public String getFirstName() {
return firstName;
}
/**
* Sets the first name.
*
* @param firstName the new first name
*/
public void setFirstName(String firstName) {
this.firstName = firstName;
}
/**
* Gets the last name.
*
* @return the last name
*/
public String getLastName() {
return lastName;
}
/**
* Sets the last name.
*
* @param lastName the new last name
*/
public void setLastName(String lastName) {
this.lastName = lastName;
}
/**
* Gets the age.
*
* @return the age
*/
public int getAge() {
return age;
}
/**
* Sets the age.
*
* @param age the new age
*/
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Name: " + firstName + " " +
lastName + ", Age:" + age + "\n";
}
}
/*****************************************************Musician.java*************************************/
package music;
/**
* The Class Musician.
*/
public class Musician extends Person {
/** The instruments. */
private Instrument[] instruments;
/** The number of instruments. */
private int numberOfInstruments;
/**
* Instantiates a new musician.
*
* @param firstName the first name
* @param lastName the last name
* @param age the age
* @param instruments the instruments
*/
public Musician(String firstName, String lastName, int
age, Instrument[] instruments) {
super(firstName, lastName,
age);
this.instruments =
instruments;
numberOfInstruments = 0;
}
/**
* Instantiates a new musician.
*
* @param firstName the first name
* @param lastName the last name
* @param age the age
*/
public Musician(String firstName, String lastName, int
age) {
super(firstName, lastName,
age);
instruments = new
Instrument[5];
numberOfInstruments = 0;
}
/**
* Gets the instruments.
*
* @return the instruments
*/
public Instrument[] getInstruments() {
return instruments;
}
/**
* Adds the instrument.
*
* @param instrument the instrument
*/
public void addInstrument(Instrument instrument)
{
if (numberOfInstruments < 5)
{
instruments[numberOfInstruments] = instrument;
numberOfInstruments++;
} else {
System.out.println("The musician can only play at most 5
instruments");
}
}
/**
* Sets the instruments.
*
* @param instruments the new instruments
*/
public void setInstruments(Instrument[] instruments)
{
this.instruments =
instruments;
}
@Override
public String toString() {
String s = "";
for (Instrument instrument :
instruments) {
s +=
instrument.toString() + "\n";
}
return s;
}
}
/***********************************************Instrument.java*********************************/
package music;
/**
* The Class Instrument.
*/
public class Instrument {
/** The year. */
private int year;
/** The make. */
private String make;
/** The year introduced. */
private int yearIntroduced;
/** The model. */
private String model;
/** The type. */
private String type;
/**
* Instantiates a new instrument.
*
* @param year the year
* @param make the make
* @param yearIntroduced the year introduced
* @param model the model
* @param type the type
*/
public Instrument(int year, String make, int
yearIntroduced, String model, String type) {
super();
this.year = year;
this.make = make;
this.yearIntroduced =
yearIntroduced;
this.model = model;
if (type.equalsIgnoreCase("Guitar")
|| type.equalsIgnoreCase("drum") ||
type.equalsIgnoreCase("bass")
|| type.equalsIgnoreCase("keyboard") ||
type.equalsIgnoreCase("wind")) {
this.type =
type;
} else {
System.out.println("Error! Default value set.");
}
}
/**
* Gets the year.
*
* @return the year
*/
public int getYear() {
return year;
}
/**
* Sets the year.
*
* @param year the new year
*/
public void setYear(int year) {
this.year = year;
}
/**
* Gets the make.
*
* @return the make
*/
public String getMake() {
return make;
}
/**
* Sets the make.
*
* @param make the new make
*/
public void setMake(String make) {
this.make = make;
}
/**
* Gets the year introduced.
*
* @return the year introduced
*/
public int getYearIntroduced() {
return yearIntroduced;
}
/**
* Sets the year introduced.
*
* @param yearIntroduced the new year introduced
*/
public void setYearIntroduced(int yearIntroduced)
{
this.yearIntroduced =
yearIntroduced;
}
/**
* Gets the model.
*
* @return the model
*/
public String getModel() {
return model;
}
/**
* Sets the model.
*
* @param model the new model
*/
public void setModel(String model) {
this.model = model;
}
/**
* Gets the type.
*
* @return the type
*/
public String getType() {
return type;
}
/**
* Sets the type.
*
* @param type the new type
*/
public void setType(String type) {
this.type = type;
}
@Override
public String toString() {
return "Instrument type " + type +
" with make" + make + " with model " + model + " in year " +
year
+ " was first introduced in" +
yearIntroduced;
}
}
/***************************************Band.java*********************************/
package music;
import java.util.Arrays;
/**
* The Class Band.
*/
public class Band {
/** The name. */
private String name;
/** The musicians. */
private Musician[] musicians;
/** The manager. */
private Manager manager;
/**
* Instantiates a new band.
*
* @param name the name
* @param musicians the musicians
*/
public Band(String name, Musician[] musicians) {
super();
this.name = name;
this.musicians = musicians;
}
/**
* Instantiates a new band.
*
* @param name the name
* @param musicians the musicians
* @param manager the manager
*/
public Band(String name, Musician[] musicians, Manager
manager) {
super();
this.name = name;
this.musicians = musicians;
this.manager = manager;
}
/**
* Gets the manager.
*
* @return the manager
*/
public Manager getManager() {
return manager;
}
/**
* Sets the manager.
*
* @param manager the new manager
*/
public void setManager(Manager manager) {
this.manager = manager;
}
/**
* Calculate max.
*
* @return the musician
*/
public Musician calculateMax() {
int max = 0;
Musician musician1 = null;
for (Musician musician : musicians)
{
if (max < musician.getInstruments().length) {
max = musician.getInstruments().length;
musician1 = musician;
}
}
return musician1;
}
/**
* Gets the name.
*
* @return the name
*/
public String getName() {
return name;
}
/**
* Sets the name.
*
* @param name the new name
*/
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Band name=" + name +
"\nmusicians=" + Arrays.toString(musicians) + "\nmanager=" +
manager.getFirstName()
+ " " + manager.getLastName();
}
}
/*************************************************Manager.java****************************/
package music;
/**
* The Class Manager.
*/
public class Manager extends Person {
/** The bands. */
private Band[] bands;
/** The number of bands. */
private int numberOfBands;
/**
* Instantiates a new manager.
*
* @param firstName the first name
* @param lastName the last name
* @param age the age
*/
public Manager(String firstName, String lastName, int
age) {
super(firstName, lastName,
age);
this.numberOfBands = 0;
bands = new Band[5];
}
/**
* Adds the band.
*
* @param band the band
*/
public void addBand(Band band) {
bands[numberOfBands] =
band;
numberOfBands++;
}
/**
* Gets the bands.
*
* @return the bands
*/
public Band[] getBands() {
return bands;
}
@Override
public String toString() {
String s = "";
for (Band band : bands) {
s +=
band.toString() + "\n";
}
return s;
}
}
/*******************************************Driver.java***************************/
package music;
/**
* The Class Driver.
*/
public class Driver {
/**
* The main method.
*
* @param args the arguments
*/
public static void main(String[] args) {
//create instruments
Instrument instrument = new
Instrument(2019, "gibson", 1965, "les paul", "guitar");
//
System.out.println("***********Test Instrument with failed
// guitar************");
// Instrument instrument1 = new
Instrument(2019, "gibson", 1965, "les paul",
// "failedguitar");
Musician musician = new
Musician("Virat", "Kohli", 35); //create musician
//create more instruments
Instrument instrument2 = new
Instrument(2019, "gibson", 1965, "les paul", "drum");
Instrument instrument3 = new
Instrument(2019, "gibson", 1965, "les paul", "bass");
Instrument instrument4 = new
Instrument(2019, "gibson", 1965, "les paul", "keyboard");
Instrument instrument5 = new
Instrument(2019, "gibson", 1965, "les paul", "wind");
/*
* assign instrument to
musician
*/
musician.addInstrument(instrument);
musician.addInstrument(instrument2);
musician.addInstrument(instrument3);
musician.addInstrument(instrument4);
musician.addInstrument(instrument5);
//create new musician array you
can add more musicians
Musician[] musicians = new
Musician[1];
musicians[0] = musician;//add
musician
Band band = new Band("ABC",
musicians); //crate band
Manager manager = new Manager("MS",
"Dhoni", 35); //create manager
band.setManager(manager); //set
manager of band
System.out.println(band.toString()); //print the band
}
}
/*****************************output*************************/
Band name=ABC
musicians=[Instrument type guitar with makegibson with model les
paul in year 2019 was first introduced in1965
Instrument type drum with makegibson with model les paul in year
2019 was first introduced in1965
Instrument type bass with makegibson with model les paul in year
2019 was first introduced in1965
Instrument type keyboard with makegibson with model les paul in
year 2019 was first introduced in1965
Instrument type wind with makegibson with model les paul in year
2019 was first introduced in1965
]
manager=MS Dhoni
Please let me know if you have any doubt or modify the answer, Thanks :)