0
public abstract class Instrument { 
   private String id;
   protected String maker; 
   private static int count = 0;
   
   public Instrument(String maker) 
   {
       count++;
       id = "S" + count;
       this.maker = maker; 
   }
   
   public String getMaker() 
   {
       return maker; 
   }
   
   public String toString()
   {
       return "ID: " + id + ", Maker: " + maker;
   }
    
   abstract public void play(String music);
} 
public class Piano extends Instrument
{
    // instance variables - replace the example below with your own
    private int year;

    /**
     * Constructor for objects of class Piano
     */
    public Piano(int year, String maker)
    {
        // initialise instance variables
        super(maker);
        this.year = year;
        
    }

     /**
     * An example of a method - replace this comment with your own
     *
     * @param  y  a sample parameter for a method
     * @return    the sum of x and y
     */
    public int getyear()
    {
        // put your code here
        return year;
    }
    
    public String toString()
    {
        return super.toString() + " Year: " + year;
    }
    
    
    
    public void play(String music)
    {
        System.out.println("Playing piano: " + music);
    }
    
    
}

so basically I need to create class MusicShop that stores the Instrument objects in an ArrayList named instruments. And then define a method to print out the details of all pianos made by a given maker before a given year.

This is what ive done so far

public class MusicShop
{
    // instance variables - replace the example below with your own
    private ArrayList<Instrument> Instruments;
    
    /**
     * Constructor for objects of class MusicShop
     */
    public MusicShop()
    {
        Instruments = new ArrayList<Instrument>();
    }
    
    public int getTotal() 
    {
        return Instruments.size();
    } 

    /**
     * An example of a method - replace this comment with your own
     *
     * @param  y  a sample parameter for a method
     * @return    the sum of x and y
     */
    public void printDetailsOfPiano()
    {
        // put your code here
       
        for(Instrument aInstrument : Instruments) {
             System.out.println(Instruments.getTotal);
        }
    }
}

Not sure how to store objects from abstract class into arraylist and print it details.

YoungOne
  • 29
  • 2

1 Answers1

1

An object of type Piano can be added to the instruments without problems:

Piano piano = new Piano(1940, "Belarus");
List<Instrument> Instruments = new ArrayList<>();
Instruments.add(piano);

To print the details of an instrument, you need to do something similar to what you have done for play, you need a method print in Instrument. Piano will override it.

For example:

public abstract class Instrument { 
...
   abstract public void print();
}

public class Piano extends Instrument {
  ...
  @Override
  public void print() {
     System.out.println(getMaker() + " " + getYear() );
  }
}

When you want to print the instruments, one way could be:

public void printDetailsOfPiano() {
     Instruments.forEach( Instrument::print );
}

By the way, in Java the convention is to have the name of the variable not capitalized:

List<Instrument> instruments = new ArrayList<>();
Davide D'Alto
  • 7,421
  • 2
  • 16
  • 30