Question

LAB: Instrument information (derived classes)

10.14 LAB: Instrument information (derived classes)

Given main() and the Instrument class, define a derived class, StringInstrument, for string instruments.

Ex. If the input is:

Drums
Zildjian
2015
2500
Guitar
Gibson
2002
1200
6
19

the output is:

Instrument Information: 
   Name: Drums
   Manufacturer: Zildjian
   Year built: 2015
   Cost: 2500
Instrument Information: 
   Name: Guitar
   Manufacturer: Gibson
   Year built: 2002
   Cost: 1200
   Number of strings: 6
   Number of frets: 19


InstrumentInformation.java

import java.util.Scanner;

public class InstrumentInformation {
   public static void main(String[] args) {
      Scanner scnr = new Scanner(System.in);

      Instrument myInstrument = new Instrument();
      StringInstrument myStringInstrument = new StringInstrument();

      String instrumentName, manufacturerName, stringInstrumentName, stringManufacturer;
      int yearBuilt, cost, stringYearBuilt, stringCost, numStrings, numFrets;

      instrumentName = scnr.nextLine();
      manufacturerName = scnr.nextLine();
      yearBuilt = scnr.nextInt();
      scnr.nextLine();
      cost = scnr.nextInt();
      scnr.nextLine();
      stringInstrumentName = scnr.nextLine();
      stringManufacturer = scnr.nextLine();
      stringYearBuilt = scnr.nextInt();
      stringCost = scnr.nextInt();
      numStrings = scnr.nextInt();
      numFrets = scnr.nextInt();

      myInstrument.setName(instrumentName);
      myInstrument.setManufacturer(manufacturerName);
      myInstrument.setYearBuilt(yearBuilt);
      myInstrument.setCost(cost);
      myInstrument.printInfo();

      myStringInstrument.setName(stringInstrumentName);
      myStringInstrument.setManufacturer(stringManufacturer);
      myStringInstrument.setYearBuilt(stringYearBuilt);
      myStringInstrument.setCost(stringCost);
      myStringInstrument.setNumOfStrings(numStrings);
      myStringInstrument.setNumOfFrets(numFrets);
      myStringInstrument.printInfo();

      System.out.println("   Number of strings: " + myStringInstrument.getNumOfStrings());
      System.out.println("   Number of frets: " + myStringInstrument.getNumOfFrets());
   }
}


Instrument.java

public class Instrument {

    protected String instrumentName;
    protected String instrumentManufacturer;
    protected int yearBuilt, cost;

    public void setName(String userName) {
        instrumentName = userName;
    }

    public String getName() {
        return instrumentName;
    }

    public void setManufacturer(String userManufacturer) {
        instrumentManufacturer = userManufacturer;
    }

    public String getManufacturer(){
        return instrumentManufacturer;
    }

    public void setYearBuilt(int userYearBuilt) {
        yearBuilt = userYearBuilt;
    }

    public int getYearBuilt() {
        return yearBuilt;
    }

    public void setCost(int userCost) {
        cost = userCost;
    }

    public int getCost() {
        return cost;
    }

    public void printInfo() {
        System.out.println("Instrument Information: ");
        System.out.println("   Name: " + instrumentName);
        System.out.println("   Manufacturer: " + instrumentManufacturer);
        System.out.println("   Year built: " + yearBuilt);
        System.out.println("   Cost: " + cost);
    }

}


StringInstrument.java

// TODO: Define a class: StringInstrument that is derived from the Instrument class
public class StringInstrument extends Instrument {
   // TODO: Declare private fields: numStrings, numFrets

   // TODO: Define mutator methods - 
   //      setNumOfStrings(), setNumOfFrets()

   // TODO: Define accessor methods -
   //      getNumOfStrings(), getNumOfFrets()

}


1 0
Add a comment Improve this question Transcribed image text
✔ Recommended Answer
Answer #1

StringInstrument.java

public class StringInstrument extends Instrument {

   private int numStrings;
   private int numFrets;
   
   public void setNumOfStrings(int num) {
      numStrings = num;
   }
   
   public void setNumOfFrets(int numF) {
      numFrets = numF;
   }
   
   public int getNumOfStrings() {
      return numStrings;
   }
   
   public int getNumOfFrets() {
      return numFrets;
   }

}


Instrument.java

public class Instrument {

    protected String instrumentName;
    protected String instrumentManufacturer;
    protected int yearBuilt, cost;

    public void setName(String userName) {
        instrumentName = userName;
    }

    public String getName() {
        return instrumentName;
    }

    public void setManufacturer(String userManufacturer) {
        instrumentManufacturer = userManufacturer;
    }

    public String getManufacturer(){
        return instrumentManufacturer;
    }

    public void setYearBuilt(int userYearBuilt) {
        yearBuilt = userYearBuilt;
    }

    public int getYearBuilt() {
        return yearBuilt;
    }

    public void setCost(int userCost) {
        cost = userCost;
    }

    public int getCost() {
        return cost;
    }

    public void printInfo() {
        System.out.println("Instrument Information: ");
        System.out.println("   Name: " + instrumentName);
        System.out.println("   Manufacturer: " + instrumentManufacturer);
        System.out.println("   Year built: " + yearBuilt);
        System.out.println("   Cost: " + cost);
    }

}


InstrumentInformation.java

import java.util.Scanner;

public class InstrumentInformation {
   public static void main(String[] args) {
      Scanner scnr = new Scanner(System.in);

      Instrument myInstrument = new Instrument();
      StringInstrument myStringInstrument = new StringInstrument();

      String instrumentName, manufacturerName, stringInstrumentName, stringManufacturer;
      int yearBuilt, cost, stringYearBuilt, stringCost, numStrings, numFrets;

      instrumentName = scnr.nextLine();
      manufacturerName = scnr.nextLine();
      yearBuilt = scnr.nextInt();
      scnr.nextLine();
      cost = scnr.nextInt();
      scnr.nextLine();
      stringInstrumentName = scnr.nextLine();
      stringManufacturer = scnr.nextLine();
      stringYearBuilt = scnr.nextInt();
      stringCost = scnr.nextInt();
      numStrings = scnr.nextInt();
      numFrets = scnr.nextInt();

      myInstrument.setName(instrumentName);
      myInstrument.setManufacturer(manufacturerName);
      myInstrument.setYearBuilt(yearBuilt);
      myInstrument.setCost(cost);
      myInstrument.printInfo();

      myStringInstrument.setName(stringInstrumentName);
      myStringInstrument.setManufacturer(stringManufacturer);
      myStringInstrument.setYearBuilt(stringYearBuilt);
      myStringInstrument.setCost(stringCost);
      myStringInstrument.setNumOfStrings(numStrings);
      myStringInstrument.setNumOfFrets(numFrets);
      myStringInstrument.printInfo();

      System.out.println("   Number of strings: " + myStringInstrument.getNumOfStrings());
      System.out.println("   Number of frets: " + myStringInstrument.getNumOfFrets());
   }
}


Add a comment
Know the answer?
Add Answer to:
LAB: Instrument information (derived classes)
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
  • LAB: Pet information (derived classes)

    LAB: Pet information (derived classes)The base class Pet has private fields petName, and petAge. The derived class Dog extends the Pet class and includes a private field for dogBreed. Complete main() to:create a generic pet and print information using printInfo().create a Dog pet, use printInfo() to print information, and add a statement to print the dog's breed using the getBreed() method.Ex. If the input is:Dobby 2 Kreacher 3 German Schnauzerthe output is:Pet Information:     Name: Dobby    Age: 2 Pet Information:     Name: Kreacher    Age: 3    Breed: German SchnauzerPetInformation.javaimport java.util.Scanner; public class PetInformation {    public static void main(String[] args) {       Scanner scnr = new Scanner(System.in);       Pet myPet = new Pet();       Dog myDog = new Dog();              String petName, dogName, dogBreed;       int petAge, dogAge;       petName = scnr.nextLine();       petAge = scnr.nextInt();       scnr.nextLine();...

  • LAB: Book information (overriding member methods)

    10.15 LAB: Book information (overriding member methods)Given main() and a base Book class, define a derived class called Encyclopedia. Within the derived Encyclopedia class, define a printInfo() method that overrides the Book class' printInfo() method by printing not only the title, author, publisher, and publication date, but also the edition and number of volumes.Ex. If the input is:The Hobbit J. R. R. Tolkien George Allen & Unwin 21 September 1937 The Illustrated Encyclopedia of the Universe James W. Guthrie Watson-Guptill 2001 2nd 1the output is:Book Information:     Book Title: The Hobbit    Author: J. R. R. Tolkien    Publisher: George Allen & Unwin    Publication Date: 21 September 1937 Book Information:     Book Title: The Illustrated Encyclopedia of the Universe    Author: James W. Guthrie    Publisher: Watson-Guptill    Publication Date: 2001    Edition: 2nd    Number of Volumes: 1Note: Indentations use 3 spaces.BookInformation.javaimport java.util.Scanner; public class BookInformation {    public static void main(String[] args) {       Scanner scnr = new Scanner(System.in);       Book myBook = new Book();...

  • LAB: Plant information (ArrayList)

    10.16 LAB: Plant information (ArrayList)Given a base Plant class and a derived Flower class, complete main() to create an ArrayList called myGarden. The ArrayList should be able to store objects that belong to the Plant class or the Flower class. Create a method called printArrayList(), that uses the printInfo() methods defined in the respective classes and prints each element in myGarden. The program should read plants or flowers from input (ending with -1), adding each Plant or Flower to the myGarden ArrayList, and output each element in myGarden using the printInfo() method.Ex. If the input is:plant Spirea 10  flower Hydrangea 30 false lilac  flower Rose 6 false white plant Mint 4...

  • For Java please.Artwork. javaimport java.util.Scanner;public class ArtworkLabel {public static void main(String[] args)...

     Given main(). define the Artist class (in file Artist java) with constructors to initialize an artist's information, get methods, and a printlnfo() method. The default constructor should initialize the artist's name to "None' and the years of birth and death to 0. printinfo() should display Artist Name, born XXXX if the year of death is -1 or Artist Name (XXXX-YYYY) otherwise. Define the Artwork class (in file Artwork.java) with constructors to initialize an artwork's information, get methods, and a printinfo() method. The constructor should...

  • JAVA PROGRAMMING Given main(), complete the SongNode class to include the printSongInfo() method. Then write the...

    JAVA PROGRAMMING Given main(), complete the SongNode class to include the printSongInfo() method. Then write the Playlist class' printPlaylist() method to print all songs in the playlist. DO NOT print the dummy head node. Ex: If the input is: Stomp! 380 The Brothers Johnson The Dude 337 Quincy Jones You Don't Own Me 151 Lesley Gore -1 the output is: LIST OF SONGS ------------- Title: Stomp! Length: 380 Artist: The Brothers Johnson Title: The Dude Length: 337 Artist: Quincy Jones...

  • THE ENTIRE CODE SHOULD BE IN JAVA Playlist (output linked list) Given main(), complete the SongNode...

    THE ENTIRE CODE SHOULD BE IN JAVA Playlist (output linked list) Given main(), complete the SongNode class to include the printSongInfo() method. Then write the Playlist class' printPlaylist() method to print all songs in the playlist. DO NOT print the dummy head node. Ex: If the input is: Stomp! 380 The Brothers Johnson The Dude 337 Quincy Jones You Don't Own Me 151 Lesley Gore -1 the output is: LIST OF SONGS ------------- Title: Stomp! Length: 380 Artist: The Brothers...

  • 9.8 LAB: Finding the first and last occurrence of a value (doubly-linked list) Given main() and...

    9.8 LAB: Finding the first and last occurrence of a value (doubly-linked list) Given main() and a PeopleNode class, complete the PeopleList class by writing findFirst() and findLast() methods. The findFirst() method should find the first occurrence of an age value in the linked list and return the corresponding node. Similarly, the findLast() method should find the last occurrence of the age value in the linked list and return the corresponding node. For both methods, if the age value is...

  • Inventory (linked lists: insert at the front of a list)

    import java.util.Scanner;public class Inventory {   public static void main (String[] args) {      Scanner scnr = new Scanner(System.in);             InventoryNode headNode;                                                    InventoryNode currNode;      InventoryNode lastNode;      String item;      int numberOfItems;      int i;      // Front of nodes list           ...

  • 10.18 LAB: Plant information (vector) Given a base Plant class and a derived Flower class, complete...

    10.18 LAB: Plant information (vector) Given a base Plant class and a derived Flower class, complete main() to create a vector called myGarden. The vector should be able to store objects that belong to the Plant class or the Flower class. Create a function called PrintVector(), that uses the PrintInfo() functions defined in the respective classes and prints each element in myGarden. The program should read plants or flowers from input (ending with -1), adding each Plant or Flower to...

  • Answer in JAVA 1. Complete the method definition to output the hours given minutes. Output for...

    Answer in JAVA 1. Complete the method definition to output the hours given minutes. Output for sample program: 3.5 import java.util.Scanner; public class HourToMinConv {    public static void outputMinutesAsHours(double origMinutes) {       /* Your solution goes here */    }    public static void main (String [] args) {       Scanner scnr = new Scanner(System.in);       double minutes;       minutes = scnr.nextDouble();       outputMinutesAsHours(minutes); // Will be run with 210.0, 3600.0, and 0.0.       System.out.println("");    } } 2....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT