-
음식 정보 출력(package)코딩/java 2019. 3. 3. 16:00반응형
package Fruit;
public class Food {
int calory;
protected int price;
protected double weight;
public int getCalory() {return calory;}; //NoFruit에도 쓸 수 있게 메소드 추가하였다.
public int getPrice() {return price;};
public double getWeight() {return weight;};
public void setCalory(int calory) {this.calory = calory;};
public void setPrice(int price) {this.price = price;};
public void setWeight(double weight) {this.weight = weight;};
public String toString() {
return "칼로리: " + calory +"\n가격: " + price + "\n무게: " + weight;
}
public Food() {
this.calory = 0;
this.price = 0;
this.weight = 0;
}
}
package Fruit;
public class Melon extends Food{
String farmInfo;
public String getFarmInfo() {return farmInfo;};
public void setFarmInfo(String farmInfo) {this.farmInfo = farmInfo;};
public String toString() {
return "칼로리: " + calory +"\n가격: " + price + "\n무게: " + weight + "\n재배농장: " + farmInfo;
}
}
---------------------------------------------------------------------------------------------------------------------------------------------------------
package NoFruit;
import Fruit.Food; //package가 달라 추가하였다.
public class Potato extends Food{
public int getUnitPrice() {
double Unitprice = price / weight;
return (int)Unitprice;
}
public String toString() {
return "칼로리: " + getCalory() +"\n가격: " + price + "\n무게: " + weight + "\n단위가격: " + getUnitPrice();
//calory를 가져올 수 없으므로 getCalory를 추가하였다.
}
}
package NoFruit;
import Fruit.*;
public class Practice_68 {
public static void main(String[] args) {
Food f0 = new Food();
Melon f1 = new Melon();
Potato f2 = new Potato();
f0.setCalory(100);
f0.setPrice(100);
f0.setWeight(12.3);
f1.setCalory(200);
f1.setPrice(200);
f1.setWeight(5.5);
f1.setFarmInfo("좋은 이웃들");
f2.setCalory(300);
f2.setPrice(200);
f2.setWeight(50.0);
System.out.println("음식의 정보입니다.");
System.out.println(f0.toString() + "\n");
System.out.println("멜론의 정보입니다.");
System.out.println(f1.toString() + "\n");
System.out.println("감자의 정보입니다.");
System.out.println(f2.toString() + "\n");
System.out.println("감자의 단위가격은 " + f2.getUnitPrice() + "입니다.");
}
}
결과:
반응형'코딩 > java' 카테고리의 다른 글
상자 정보 (상속 super) (0) 2019.03.04 사각형, 도형 정보 출력 (0) 2019.03.03 음식 정보 출력 (0) 2019.03.03 주사위 던지기2 (0) 2019.03.03 랜덤 주사위 (같은 수가 나올 때까지) (0) 2019.03.03