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;       boolean bowed;       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();       bowed = scnr.nextBoolean();       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.setIsBowed(bowed);       myStringInstrument.printInfo();       System.out.println("   Number of strings: " + myStringInstrument.getNumOfStrings());       System.out.println("   Number of frets: " + myStringInstrument.getNumOfFrets());       System.out.println("   Is bowed: " + myStringInstrument.getIsBowed());    } }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

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;
      boolean bowed;

      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();
      bowed = scnr.nextBoolean();

      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.setIsBowed(bowed);
      myStringInstrument.printInfo();

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

```java
// TODO: Define a class: StringInstrument that is derived from the Instrument class
public class Instrument {
}

public class StringInstrument extends Instrument {
    // TODO: Declare private fields
    private {
        setNumOfStrings(int);
        setNumOfFrets(int);
        setIsBowed(bool);
    }

    getNumOfStrings();
    getNumOfFrets();
    getIsBowed();

    // TODO: Define mutator methods -
    //     setNumOfStrings(), setNumOfFrets(), setIsBowed()
    // TODO: Define accessor methods -
    //     getNumOfStrings(), getNumOfFrets(), getIsBowed()
}
```

### Explanation

This code snippet defines a Java class `StringInstrument` which is derived from a base class `Instrument`. The class structure includes:

1. **Class Declaration:**

   - `StringInstrument extends Instrument`: This indicates that `StringInstrument` is a subclass of `Instrument`, inheriting its properties and behavior.

2. **Private Fields:**

   - Private fields are intended for values such as the number of strings, number of frets, and whether the instrument is bowed. However, the code uses methods to set these values, which is an unusual syntax for declaring variables. These fields should typically be declared with type and variable names.

3. **Accessor and Mutator Methods:**

   - **Mutator (Setter) Methods:** 
     - `setNumOfStrings(int)`: Sets the number of strings.
     - `setNumOfFrets(int)`: Sets the number of frets.
     - `setIsBowed(bool)`: Sets whether the instrument is bowed.
   
   - **Accessor (Getter) Methods:**
     - `getNumOfStrings()`: Returns the number of strings.
     - `getNumOfFrets()`: Returns the number of frets.
     - `getIsBowed()`: Returns whether the instrument is bowed.

4. **TODO Comments:**

   - These indicate tasks that need to be completed, such as defining mutator and accessor methods.

This code serves as a template for further development of a class representing string instruments, highlighting object-oriented programming concepts in Java, such as inheritance and encapsulation.
Transcribed Image Text:```java // TODO: Define a class: StringInstrument that is derived from the Instrument class public class Instrument { } public class StringInstrument extends Instrument { // TODO: Declare private fields private { setNumOfStrings(int); setNumOfFrets(int); setIsBowed(bool); } getNumOfStrings(); getNumOfFrets(); getIsBowed(); // TODO: Define mutator methods - // setNumOfStrings(), setNumOfFrets(), setIsBowed() // TODO: Define accessor methods - // getNumOfStrings(), getNumOfFrets(), getIsBowed() } ``` ### Explanation This code snippet defines a Java class `StringInstrument` which is derived from a base class `Instrument`. The class structure includes: 1. **Class Declaration:** - `StringInstrument extends Instrument`: This indicates that `StringInstrument` is a subclass of `Instrument`, inheriting its properties and behavior. 2. **Private Fields:** - Private fields are intended for values such as the number of strings, number of frets, and whether the instrument is bowed. However, the code uses methods to set these values, which is an unusual syntax for declaring variables. These fields should typically be declared with type and variable names. 3. **Accessor and Mutator Methods:** - **Mutator (Setter) Methods:** - `setNumOfStrings(int)`: Sets the number of strings. - `setNumOfFrets(int)`: Sets the number of frets. - `setIsBowed(bool)`: Sets whether the instrument is bowed. - **Accessor (Getter) Methods:** - `getNumOfStrings()`: Returns the number of strings. - `getNumOfFrets()`: Returns the number of frets. - `getIsBowed()`: Returns whether the instrument is bowed. 4. **TODO Comments:** - These indicate tasks that need to be completed, such as defining mutator and accessor methods. This code serves as a template for further development of a class representing string instruments, highlighting object-oriented programming concepts in Java, such as inheritance and encapsulation.
**Educational Resource: Understanding Object-Oriented Programming with Derived Classes**

In this example, you will learn how to define a derived class, `StringInstrument`, in an object-oriented programming context. This class will extend the functionality of an existing `Instrument` class by introducing additional attributes and methods.

### Class Definition and Attributes

1. **Derived Class: `StringInstrument`**

   * **Attributes:**
     - `int`: Number of strings
     - `int`: Number of frets
     - `boolean`: Whether the instrument is bowed

   * **Methods:**
     - Set and get methods for each private field to ensure encapsulation.

### Example

#### Input

The example input consists of data for different musical instruments, provided in the following format:

```
Drums
Zildjian
2015
2500
Guitar
Gibson
2002
1200
6
19
false
```

- **Drums**: Details include the name, manufacturer, year built, and cost.
- **Guitar**: Details include the name, manufacturer, year built, cost, number of strings, number of frets, and whether it is bowed.

#### Output

The program processes the input to produce a structured output, detailing the instrument information:

```
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
Is bowed: false
```

### Explanation

- The output displays detailed information for each instrument.
- For instruments like the guitar, which fall under `StringInstrument`, additional details such as the number of strings, number of frets, and whether the instrument is bowed are included. 

This example demonstrates the use of derived classes to extend functionality, showcasing the principles of inheritance and encapsulation in object-oriented programming.
Transcribed Image Text:**Educational Resource: Understanding Object-Oriented Programming with Derived Classes** In this example, you will learn how to define a derived class, `StringInstrument`, in an object-oriented programming context. This class will extend the functionality of an existing `Instrument` class by introducing additional attributes and methods. ### Class Definition and Attributes 1. **Derived Class: `StringInstrument`** * **Attributes:** - `int`: Number of strings - `int`: Number of frets - `boolean`: Whether the instrument is bowed * **Methods:** - Set and get methods for each private field to ensure encapsulation. ### Example #### Input The example input consists of data for different musical instruments, provided in the following format: ``` Drums Zildjian 2015 2500 Guitar Gibson 2002 1200 6 19 false ``` - **Drums**: Details include the name, manufacturer, year built, and cost. - **Guitar**: Details include the name, manufacturer, year built, cost, number of strings, number of frets, and whether it is bowed. #### Output The program processes the input to produce a structured output, detailing the instrument information: ``` 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 Is bowed: false ``` ### Explanation - The output displays detailed information for each instrument. - For instruments like the guitar, which fall under `StringInstrument`, additional details such as the number of strings, number of frets, and whether the instrument is bowed are included. This example demonstrates the use of derived classes to extend functionality, showcasing the principles of inheritance and encapsulation in object-oriented programming.
Expert Solution
steps

Step by step

Solved in 4 steps with 4 images

Blurred answer
Knowledge Booster
Unreferenced Objects
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education