Downcast: Product Class
Problem Description
Downcast: Product class
In this problem, you will create a program that stores a DiscountProduct object in a Product type variable, performs a type check with instanceof, downcasts it to the child class type, and displays the calculated discounted price to standard output.
Learning Objective: Understand downcast explicitly casting parent class reference to child class type
Create Product class representing products and DiscountProduct class inheriting from it. Check type with instanceof for DiscountProduct object stored in Product type variable, then cast to child class type (downcast) to call child class specific methods.
Input
Line 1: Product name (string)
Line 2: Price (integer)
Line 3: Discount rate (integer, percentage)
Output
Product: [name]
Price: [price]yen
Discount: [rate]%
Final: [discounted price]yen
Examples
Example 1: Laptop 20% OFF
Input:
Laptop
100000
20
Output:
Product: Laptop
Price: 100000yen
Discount: 20%
Final: 80000yen
Example 2: Mouse 10% OFF
Input:
Mouse
3000
10
Output:
Product: Mouse
Price: 3000yen
Discount: 10%
Final: 2700yen
Example 3: Boundary (0% discount)
Input:
A
100
0
Output:
Product: A
Price: 100yen
Discount: 0%
Final: 100yen
Test Cases
※ Output examples follow programming industry standards
Laptop 100000 20
Product: Laptop Price: 100000yen Discount: 20% Final: 80000yen
Mouse 3000 10
Product: Mouse Price: 3000yen Discount: 10% Final: 2700yen
A 100 0
Product: A Price: 100yen Discount: 0% Final: 100yen
Laptop -1 -1
Product: Laptop Price: -1yen Discount: -1% Final: -1yen
Your Solution
- No main method found
You have 4 free executions remaining
