oday we are using Inheritance to create a subclass. We have provided a Frult.java class for you, and we want you to create two subciasss of that, calle ome statistics about them. hese are the details of the Frult.java, Apple.java, and Orange.java class. Frult nt: weight nt: age Constructor : Fruit (weight, age] nt: getWeight|) nt: getAge() old : setWeight|[int) old : setAge[int) tring : toString() Apple (extends Frult) nt: weight

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

Java.

 

**Inheritance in Java: Creating Subclasses**

Today we are using **Inheritance** to create a subclass. We have provided a `Fruit.java` class for you, and we want you to create two subclasses of that, called `Apple.java` and `Orange.java`. We will then read in some apple objects and orange objects, and print some statistics about them.

### Class Details

The class details for `Fruit.java`, `Apple.java`, and `Orange.java` are provided below.

#### Fruit.java
```java
class Fruit {
  int weight;
  int age;

  // Constructor
  Fruit(int weight, int age) {
    this.weight = weight;
    this.age = age;
  }

  // Getter for weight
  int getWeight() {
    return weight;
  }

  // Getter for age
  int getAge() {
    return age;
  }

  // Setter for weight
  void setWeight(int weight) {
    this.weight = weight;
  }

  // Setter for age
  void setAge(int age) {
    this.age = age;
  }

  @Override
  public String toString() {
    return "Weight: " + weight + ", Age: " + age;
  }
}
```

#### Apple.java (extends Fruit)
```java
class Apple extends Fruit {
  // Constructor
  Apple(int weight, int age) {
    super(weight, age);
  }

  @Override
  public String toString() {
    return "Apple [Weight: " + weight + ", Age: " + age + "]";
  }
}
```

#### Orange.java (extends Fruit)
```java
class Orange extends Fruit {
  // Constructor
  Orange(int weight, int age) {
    super(weight, age);
  }

  @Override
  public String toString() {
    return "Orange [Weight: " + weight + ", Age: " + age + "]";
  }
}
```

### Details

#### Input
The input for this problem has been handled for you. It will be 3 apples followed by 3 oranges, and each line follows this format:

```
[weight in grams] [days since picked]
```

#### Example
Here is an example of how you might create and use these classes:
```java
public class Main {
  public static void main(String[] args) {
    // Creating apple objects
    Apple apple1 = new Apple(150, 10);
Transcribed Image Text:**Inheritance in Java: Creating Subclasses** Today we are using **Inheritance** to create a subclass. We have provided a `Fruit.java` class for you, and we want you to create two subclasses of that, called `Apple.java` and `Orange.java`. We will then read in some apple objects and orange objects, and print some statistics about them. ### Class Details The class details for `Fruit.java`, `Apple.java`, and `Orange.java` are provided below. #### Fruit.java ```java class Fruit { int weight; int age; // Constructor Fruit(int weight, int age) { this.weight = weight; this.age = age; } // Getter for weight int getWeight() { return weight; } // Getter for age int getAge() { return age; } // Setter for weight void setWeight(int weight) { this.weight = weight; } // Setter for age void setAge(int age) { this.age = age; } @Override public String toString() { return "Weight: " + weight + ", Age: " + age; } } ``` #### Apple.java (extends Fruit) ```java class Apple extends Fruit { // Constructor Apple(int weight, int age) { super(weight, age); } @Override public String toString() { return "Apple [Weight: " + weight + ", Age: " + age + "]"; } } ``` #### Orange.java (extends Fruit) ```java class Orange extends Fruit { // Constructor Orange(int weight, int age) { super(weight, age); } @Override public String toString() { return "Orange [Weight: " + weight + ", Age: " + age + "]"; } } ``` ### Details #### Input The input for this problem has been handled for you. It will be 3 apples followed by 3 oranges, and each line follows this format: ``` [weight in grams] [days since picked] ``` #### Example Here is an example of how you might create and use these classes: ```java public class Main { public static void main(String[] args) { // Creating apple objects Apple apple1 = new Apple(150, 10);
## Details

### Input
The input for this problem has been handled for you. It will be 3 apples followed by 3 oranges, and each line follows this format:
```
[weight in grams] [days since picked]
```

### Processing
There is not much processing involved in this problem - only subclassing!

Any processing will be handled for you. All you need to do is create the `Apple.java` class and the `Orange.java` class. Remember, these need to extend the `Fruit` class! The `super` keyword could be useful here.

The program will then decide which group of fruit is the heaviest (in grams), either apples or oranges.

### Output
The output has been handled for you, too!

### Sample Input/Output
| Sample Input            | Sample Output          |
|-------------------------|------------------------|
| 145 10                  |                        |
| 200 5                   |                        |
| 230 12                  | The oranges are heavier|
| 122 5                   |                        |
| 310 1                   |                        |
| 270 6                   |                        |

**Diagram Explanation:**

- The table shows a sample input and output. 
- Under "Sample Input", six entries are listed:
  - Three are for apples with weights and days since they were picked: 
    - Apple 1: 145 grams, 10 days
    - Apple 2: 200 grams, 5 days
    - Apple 3: 230 grams, 12 days
  - Three are for oranges with weights and days since they were picked:
    - Orange 1: 122 grams, 5 days
    - Orange 2: 310 grams, 1 day
    - Orange 3: 270 grams, 6 days
- The "Sample Output" column states "The oranges are heavier," indicating that the combined weight of the oranges is higher than that of the apples.
Transcribed Image Text:## Details ### Input The input for this problem has been handled for you. It will be 3 apples followed by 3 oranges, and each line follows this format: ``` [weight in grams] [days since picked] ``` ### Processing There is not much processing involved in this problem - only subclassing! Any processing will be handled for you. All you need to do is create the `Apple.java` class and the `Orange.java` class. Remember, these need to extend the `Fruit` class! The `super` keyword could be useful here. The program will then decide which group of fruit is the heaviest (in grams), either apples or oranges. ### Output The output has been handled for you, too! ### Sample Input/Output | Sample Input | Sample Output | |-------------------------|------------------------| | 145 10 | | | 200 5 | | | 230 12 | The oranges are heavier| | 122 5 | | | 310 1 | | | 270 6 | | **Diagram Explanation:** - The table shows a sample input and output. - Under "Sample Input", six entries are listed: - Three are for apples with weights and days since they were picked: - Apple 1: 145 grams, 10 days - Apple 2: 200 grams, 5 days - Apple 3: 230 grams, 12 days - Three are for oranges with weights and days since they were picked: - Orange 1: 122 grams, 5 days - Orange 2: 310 grams, 1 day - Orange 3: 270 grams, 6 days - The "Sample Output" column states "The oranges are heavier," indicating that the combined weight of the oranges is higher than that of the apples.
Expert Solution
steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Knowledge Booster
Introduction to computer system
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.
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