练习
本帖最后由 匿名 于 2023-4-16 12:58 编辑要求如下
和运行结果再图片上
Exercises 1
Given the Ingredients Interface class as follows:
public interface IPrice {
public double getPrice();
}
Given a UML diagram as follows, write the Java code to implement the class Ingredient that implements the interface IPrice. An ingredient has a name and a price and a method to get the name of the ingredient and the price. The constructor of the class receives the name and the price of the ingredient as parameters.
Exercises 2
Given the Ingredients Interface class as follows:
public interface Ingredients {
public void addIngredient(Ingredient ingredient);
public ArrayList<Ingredient> getIngredients();
}
Implement the class Meal. A Meal has a name and a list of ingredients and a price. The class implements the Ingredients and IPrice interfaces. The price of the meal is calculated by adding the prices of all the ingredients. The Ingredient class is the one implemented in question 2 and the link between Ingredient and IPrice has been omitted in this diagram.
Exercises 3
Write one main class Main.java to use Meal and Ingredient based on the UML above. The requirements as below:
Create one meal instance with name “Dinner”, ask user to add Ingredients to the meal until the input is “stop”. The ingredients can be one or more than one.
Print out the meal information and calculate the total bill after the user added the Ingredients.
页:
[1]