Question

In: Computer Science

I'm very confused on how to complete this code. There are 5 total classes. I have...

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,

  • an if then else statement
  • a case statement
  • an array which contains all the valid values and you search whether the type is one of these values, etc. The last one is more challenging but you will learn more about arrays.

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:

  • Must instrument all the constructors
  • Must instrument all the methods.
  • Create an instrument with the values make:”gibson” type:”failedguitar” year:2019, yearIntroduced: 1965, model:”les paul” This must write an error.
  • Create a musician who plays 5 instruments. Create an additional valid instrument. Add the instrument to the musician. It should not add and print an error. See the definition for the format of the error.

Tips and Tricks:

  • Please attempt Instrument and Person objects first, then Musician! Then attempt Band, and Manager in this order.
  • Keep track of the size of the array in Instruments assigned to a musician. For example, if you create a default array with 5 elements, there are no instruments assigned yet. Every time you add an Instrument, you have to add it to the NEXT empty spot in the array. In order to do this, use a private class variable numInstruments.
  • The instruments may be "set" by a setter method externally for the musician. In this case, the array size is fixed by the parameter as you are replacing the default array created. In the body of the array method, find the number of the instruments by the length of the array. For example, if a person plays only three instruments and a setInstruments method was used, it is not possible to extend it beyond capacity. Consequent addInstrument method must return the error.
  • All the variables are private. Don't redeclare the variables in the subclasses AGAIN! To construct a subclass, use the superclass's super method with the exact number of parameters for the super class. For example, Musician's constructor can call Person's constructor by using super but the signature of the call must match the signature of the super class' constructor method.

Solutions

Expert Solution

/*************************************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 :)


Related Solutions

Making a program in Python with the following code types. I'm very confused and would love...
Making a program in Python with the following code types. I'm very confused and would love the actual code that creates this prompt and a brief explanation. Thanks! A comment on the top line of your program containing your name. A comment on the second line containing your section number. A comment on the third line containing the date. A comment on the fourth line containing your email address. A comment with the lab number and purpose of this lab....
I'm really confused Junit test case Here is my code. How can I do Junit test...
I'm really confused Junit test case Here is my code. How can I do Junit test with this code? package pieces; import java.util.ArrayList; import board.Board; public class Knight extends Piece {    public Knight(int positionX, int positionY, boolean isWhite) {        super("N", positionX, positionY, isWhite);    }    @Override    public String getPossibleMoves() {        ArrayList<String> possibleMoves = new ArrayList<>();               // check if squares where knight can go are available for it       ...
I'm having a very hard time getting this c++ code to work. I have attached my...
I'm having a very hard time getting this c++ code to work. I have attached my code and the instructions. CODE: #include using namespace std; void printWelcome(string movieName, string rating, int startHour, int startMinute, char ampm); //menu function //constants const double TICKET_PRICE = 9.95; const double TAX_RATE = 0.095; const bool AVAILABLE = true; const bool UNAVAILABLE = false; int subTotal,taxAdded, totalCost; // bool checkAvailability( int tickets, int& seatingLeft){ //checks ticket availability while(tickets > 0 || tickets <= 200) //valid...
Hello, I'm confused on part 'C' on how to graph the answer I received in part...
Hello, I'm confused on part 'C' on how to graph the answer I received in part 'B' B. A steam boiler is required as part of the design of a new plant. The types of fuel that can be used to ignite the boiler are natural gas, fuel oil, and coal. The cost of installation including all required controls is $40,000 for natural gas, $50,000 for fuel oil, and $120,000 for coal. In addition, the annual cost of fuel oil...
I am very confused about how to find the critical value of the test statistic. I...
I am very confused about how to find the critical value of the test statistic. I have found the test statistic of 5.636, with 1% significance level and two degrees of freedom. How do I calculate the critical value? Geneticists examined the distribution of seed coat color in cultivated amaranth grains, Amaranthus caudatus. Crossing black-seeded and pale-seeded A. caudatus populations gave the following counts of black, brown, and pale seeds in the second generation. Seed Coat Color black brown pale...
The source code I have is what i'm trying to fix for the assignment at the...
The source code I have is what i'm trying to fix for the assignment at the bottom. Source Code: #include <iostream> #include <cstdlib> #include <ctime> #include <iomanip> using namespace std; const int NUM_ROWS = 10; const int NUM_COLS = 10; // Setting values in a 10 by 10 array of random integers (1 - 100) // Pre: twoDArray has been declared with row and column size of NUM_COLS // Must have constant integer NUM_COLS declared // rowSize must be less...
ANSYS Fluent I'm a beginner in the field. Now I am confused about some settings. I'm...
ANSYS Fluent I'm a beginner in the field. Now I am confused about some settings. I'm doing a analysis job to compare the difference between a car with rear wing and the other one without rear wing. First, I don't sure which car (the one with rear wing or the one without rear wing)should have a bigger Cd value, somebody says that cars with rear wings will increase drag as well as downforce, is that right ? Second, I always...
Trying to make sure I'm answering these questions correctly. I'm confused on question d. I can...
Trying to make sure I'm answering these questions correctly. I'm confused on question d. I can explain why in terms of concentration gradient and the fact it's in a hypotonice solution which would cause water to go into the blood vessel, but don't know how to answer it in terms of hydrostatic pressure. 6. Imagine blood in a vessel that has an osmolarity of 300 mEq (a measure of the concentration of particles). Now, imagine that the IF around the...
What are the similarities and differences between Mendelian and non-Mendelian inheritance? I'm very confused by what...
What are the similarities and differences between Mendelian and non-Mendelian inheritance? I'm very confused by what im reading online and wanted to know the most significant details that distinguish/relate the two.
Multistep problem that I'm confused on the entire thing: A) The addition of 5E-3 total moles...
Multistep problem that I'm confused on the entire thing: A) The addition of 5E-3 total moles of Zn2+ to a 1.0L solution of NaCN gives a solution of the complex ion [Zn(CN)4-2](Kf=4.2E19). What is the concentration of uncomplexed Zn2+ ions if the concentration of cyanide ions in the final solution is 0.5M? B) ZnCO3 is sparaingly soluble salt with a Ksp=1.0E-7. The addition of CN- (aq) to ZnCO3(s) yields the complex ion [Zn(Cn)42-] (aq) with the Kf mentioned before in...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT