015-003-001

Downcast: Product Class

Easy

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

Input:
Laptop
100000
20
Expected Output:
Product: Laptop
Price: 100000yen
Discount: 20%
Final: 80000yen
Input:
Mouse
3000
10
Expected Output:
Product: Mouse
Price: 3000yen
Discount: 10%
Final: 2700yen
Input:
A
100
0
Expected Output:
Product: A
Price: 100yen
Discount: 0%
Final: 100yen
Input:
Laptop
-1
-1
Expected Output:
Product: Laptop
Price: -1yen
Discount: -1%
Final: -1yen
❌ Some tests failed
❌ エラー発生

Your Solution

Current Mode: My Code
Product.java🔒
DiscountProduct.java🔒
Solution.java🔒
3/6 ファイル0B
⚠️警告
  • No main method found

0 B / 5 MB

You have 4 free executions remaining