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
Keyboard 5000 15
Product: Keyboard Price: 5000yen Discount: 15% Final: 4250yen
Monitor 50000 30
Product: Monitor Price: 50000yen Discount: 30% Final: 35000yen
Your Solution
You have 10 free executions remaining
