018-002-002
継承の基本:レシピの拡張
初級
問題説明
継承の基本:レシピの拡張
学習目標: 継承で親クラスにフィールドを追加し、機能を拡張する
この問題では: Recipe親クラスと、それを継承したSpecialRecipe子クラスを実装し、各クラスのgetTypeName()メソッドが"Regular recipe"または"Special recipe"を返すプログラムを作成します。
Recipeクラスを継承してSpecialRecipeクラスを作成します。親クラスの機能に加えて、子クラス独自のフィールドを追加する方法を学びましょう。
具体例で理解する
例1: 通常レシピ(親クラス)
入力: new Recipe("Pasta", 15)
出力: Regular recipe
例2: 特別レシピ(子クラス)
入力: new SpecialRecipe("Truffle Pasta", 15, "Truffle")
出力: Special recipe
(説明: トリュフを使った特別なパスタ)
例3: 短時間でも特別(境界値)
入力: new SpecialRecipe("Caviar Pasta", 10, "Caviar")
出力: Special recipe
(説明: 10分でもキャビアで特別なレシピ)
課題内容
以下の仕様でRecipeとSpecialRecipeクラスを作成してください:
Recipeクラス(親クラス)
- フィールド:
dishName(String型, protected): 料理名cookingTime(int型, protected): 調理時間
- コンストラクタ: dishName, cookingTimeを受け取る
- メソッド:
getTypeName()- "Regular recipe"を返す
SpecialRecipeクラス(子クラス)
- 継承: Recipeクラスを継承
- 追加フィールド:
specialIngredient(String型, private): 特別な材料 - コンストラクタ: dishName, cookingTime, specialIngredientを受け取り、super()で親クラスを初期化し、specialIngredientを設定
- メソッド:
getTypeName()- "Special recipe"を返す(オーバーライド)
テストケース例
※ 出力例はプログラミングの国際標準に準拠し英語で表示しています
正常系
入力:
{"recipeType":"Recipe","dishName":"Pasta","cookingTime":15}期待される出力:
Dish: Pasta Time: 15 min === Special Recipe === Dish: Truffle Pasta Time: 30 min Special Ingredient: Black Truffle Difficulty: 4/5
正常系
入力:
{"recipeType":"SpecialRecipe","dishName":"Truffle Pasta","cookingTime":15,"specialIngredient":"Truffle"}期待される出力:
Dish: Pasta Time: 15 min === Special Recipe === Dish: Truffle Pasta Time: 30 min Special Ingredient: Black Truffle Difficulty: 4/5
あなたの解答
現在のモード:● 自分のコード
Recipe.java🔒
SpecialRecipe.java🔒
Main.java🔒
3/6 ファイル75B
⚠️警告
- mainメソッドが見つかりません
9
1
2
›
⌄
class Recipe {
}
0 B / 5 MB
残り 10 回実行可能
