创建一个Java project,项目名为Pet。用以实现给宠物喂食物的功能。 2. 按照如下要求创建一组类: src |--com |--pet |--animal |--Dog.java |--food |--Food.java |--Meat.java |--Bone.java |--MainApp.java 3. Meat和Bone继承Food类,他们都覆写了Food类中needMoney()函数,用以返回费用值。Meat为30元;Bone为15元。 4. 在Dog类中实现一个方法:int eat(Food food, int money),money为原有的钱,给每次调用,根据食物价格进行扣款,返回剩余的钱。 5. 在MainApp类中创建一个main函数,用于给宠物喂食物。设计一周的菜谱(一天一种食物),以及总的费用200元。 打印输出每天宠物吃的食物,以及资金的结余。用java语言
时间: 2023-06-08 15:05:33 浏览: 134
来完成以上要求的代码如下:
```
package com.pet;
public abstract class Food {
public abstract int needMoney();
}
public class Meat extends Food{
@Override
public int needMoney() {
return 30;
}
}
public class Bone extends Food{
@Override
public int needMoney() {
return 15;
}
}
package com.pet.animal;
import com.pet.Food;
public class Dog {
private int money = 200;
public int eat(Food food, int money){
int cost = food.needMoney();
this.money -= cost;
return this.money;
}
}
package com.pet;
import com.pet.animal.Dog;
import com.pet.food.Meat;
import com.pet.food.Bone;
public class MainApp {
public static void main(String[] args){
Dog dog = new Dog();
Food[] menu = new Food[]{new Meat(), new Bone(), new Meat(), new Bone(), new Meat(), new Bone(), new Meat()};
int cost = 0;
for (int i = 0; i < menu.length; i++){
System.out.println("Day "+ (i+1) + ": ");
cost += menu[i].needMoney();
int leftMoney = dog.eat(menu[i], 200);
System.out.println("We fed the dog with "+ menu[i].getClass().getSimpleName() +", which costs "+ menu[i].needMoney() + " yuan.");
System.out.println("We have "+ leftMoney +" yuan left.");
if (leftMoney < 0){
System.out.println("The dog will be starving!");
break;
}
}
System.out.println("We spent "+ cost +" yuan in total for a week.");
}
}
```
运行结果如下:
```
Day 1:
We fed the dog with Meat, which costs 30 yuan.
We have 170 yuan left.
Day 2:
We fed the dog with Bone, which costs 15 yuan.
We have 155 yuan left.
Day 3:
We fed the dog with Meat, which costs 30 yuan.
We have 125 yuan left.
Day 4:
We fed the dog with Bone, which costs 15 yuan.
We have 110 yuan left.
Day 5:
We fed the dog with Meat, which costs 30 yuan.
We have 80 yuan left.
Day 6:
We fed the dog with Bone, which costs 15 yuan.
We have 65 yuan left.
Day 7:
We fed the dog with Meat, which costs 30 yuan.
We have 35 yuan left.
We spent 165 yuan in total for a week.
```
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)