015-004-002
Recipe Ingredients
Easy
Problem Description
In this problem, you will create a program that defines an Ingredient base class with Meat and Vegetable subclasses, reads ingredient data from standard input, stores instances in an Ingredient[] array, iterates through them calling the polymorphic methods getType() and getAmount() on each, and displays the results to standard output.
Input format:
- Line 1: number of ingredients n
- Next n lines: ingredient type (
MeatorVegetable) and amount (space-separated)
Create an Ingredient base class with Meat and Vegetable subclasses. Use Scanner to read input, store different ingredient types in an Ingredient[] array and iterate through them, calling the polymorphic methods getType() and getAmount() on each instance.
Test Cases
※ Output examples follow programming industry standards
Normal case
Input:
3 Meat 200 Vegetable 150 Meat 100
Expected Output:
Ingredients: 3 Meat: 200g Vegetable: 150g Meat: 100g
Normal case
Input:
2 Vegetable 100 Vegetable 80
Expected Output:
Ingredients: 2 Vegetable: 100g Vegetable: 80g
Your Solution
Current Mode:● My Code
Ingredient.java🔒
Meat.java🔒
Vegetable.java🔒
Main.java🔒
4/6 ファイル240B
9
1
2
›
⌄
class Ingredient {
}
0 B / 5 MB
You have 10 free executions remaining
