608 食物熱量計算

第一題

abstract class Food{
int  amount=0;
int calorie=0;
Food(){}
Food(int x){ amount=x; }
void setCaloriePerGram(int x){
calorie = x;
}
int getAmount(){
return amount;
}
int getCalorie(){
return amount*calorie;
}
}
class Rice extends Food{
Rice(){}
Rice(int i){
super(i);
setCaloriePerGram(1);
}
}
class Egg extends Food{
Egg(){}
Egg(int i){
super(i);
setCaloriePerGram(2);
}
}
class Cabbage extends Food{
Cabbage(){}
Cabbage(int i){
super(i);
setCaloriePerGram(1);
}
}
class PorkRib extends Food{
PorkRib(){}
PorkRib(int i){
super(i);
setCaloriePerGram(10);
}
}
class Carrot extends Food{
Carrot(){}
Carrot(int i){
super(i);
setCaloriePerGram(1);
}
}

class JPD06_1
{
    public static void main(String args[])
    {
        Rice rice = new Rice(100);
        System.out.println( rice.getAmount() + " grams of rice has " + rice.getCalorie() + " calories.");

        Egg egg = new Egg(30);
        System.out.println( egg.getAmount() + " grams of egg has " + egg.getCalorie() + " calories.");

        Cabbage cabbage = new Cabbage(50);
        System.out.println( cabbage.getAmount() + " grams of cabbage has " + cabbage.getCalorie() + " calories.");

        PorkRib porkRib = new PorkRib(300);
        System.out.println( porkRib.getAmount() + " grams of pork rib has " + porkRib.getCalorie() + " calories.");

        Carrot carrot = new Carrot(100);
        System.out.println( carrot.getAmount() + " grams of carrot has " + carrot.getCalorie() + " calories.");
    }
}


第二題

import java.util.*;

class LunchBox{
int calorie=0;
Vector <Food> content;
LunchBox(){
content=new Vector<Food>();
}
void add(Food f){
content.add(f);
}
int getCalorie(){
for(Food f:content){
calorie+=f.getCalorie();
}
return calorie;
}

}

class JPA06_2
{
    public static void main(String args[])
    {
        LunchBox economy = new LunchBox();
        economy.add(new Rice(200));
        economy.add(new Cabbage(100));
        economy.add(new PorkRib(250));
        System.out.println("Total calories of an economy lunch box are " + economy.getCalorie() +".");

        LunchBox valuedChoice = new LunchBox();
        valuedChoice.add(new Rice(200));
        valuedChoice.add(new Egg(30));
        valuedChoice.add(new Carrot(100));
        valuedChoice.add(new PorkRib(300));
        System.out.println("Total calories of a valued-choice lunch box are " + valuedChoice.getCalorie()+".");

    }
}


第三題

import java.util.*;

class LunchBox{
int calorie=0;
Vector <Food> content;
LunchBox(){
content=new Vector<Food>();
}
void add(Food f){
content.add(f);
}
int getCalorie(){
for(Food f:content){
calorie+=f.getCalorie();
}
return calorie;
}

double getCost(){
double cost=0;
for(Food f:content){
cost+=f.getCost();
}
return cost;
}
double priceRatio=1;
void setPriceRatio(double x){
priceRatio=x;
}
double getPrice(){
return getCost()*priceRatio;
}

}
abstract class Food{

int amount=0;
int calorie=0;
Food(){}
Food(int x){ amount=x; }
void setCaloriePerGram(int x){
calorie = x;
}
int getAmount(){
return amount;
}
int getCalorie(){
return amount*calorie;
}

int unitCost=0;
void setCost(int x){
unitCost=x;
}
int getCost(){
return unitCost*amount;
}
}
class Rice extends Food{
Rice(){}
Rice(int i){
super(i);
setCaloriePerGram(1);
setCost(1);
}
}
class Egg extends Food{
Egg(){}
Egg(int i){
super(i);
setCaloriePerGram(2);
setCost(2);
}
}
class Cabbage extends Food{
Cabbage(){}
Cabbage(int i){
super(i);
setCaloriePerGram(1);
setCost(3);
}
}
class PorkRib extends Food{
PorkRib(){}
PorkRib(int i){
super(i);
setCaloriePerGram(10);
setCost(8);
}
}
class Carrot extends Food{
Carrot(){}
Carrot(int i){
super(i);
setCaloriePerGram(1);
setCost(3);
}
}

class JPD06_3
{
    public static void main(String args[])
    {
        LunchBox economy = new LunchBox();
        economy.add(new Rice(200));
        economy.add(new Cabbage(100));
        economy.add(new PorkRib(250));
        economy.setPriceRatio(1.2);
        System.out.println("Total calories of an economy lunch box are " + economy.getCalorie());
        System.out.println("The price of an economy lunch box is " + economy.getPrice());

        LunchBox valuedChoice = new LunchBox();
        valuedChoice.add(new Rice(200));
        valuedChoice.add(new Egg(30));
        valuedChoice.add(new Carrot(100));
        valuedChoice.add(new PorkRib(300));
        valuedChoice.setPriceRatio(1.3);
        System.out.println("Total calories of a valued-choice lunch box are " + valuedChoice.getCalorie());
        System.out.println("The price of a valued-choice lunch box is " + valuedChoice.getPrice());
    }
}


第四題

import java.util.*;

class LunchBox{
int calorie=0;
Vector <Food> content;

LunchBox(){
content=new Vector<Food>();
}

void add(Food f){
content.add(f);
}

int getCalorie(){
for(Food f:content){
calorie+=f.getCalorie();
}
return calorie;
}

double getCost(){
double cost=0;
for(Food f:content){
cost+=f.getCost();
}
return cost;
}

double priceRatio=1;

void setPriceRatio(double x){
priceRatio=x;
}

double getPrice(){
return getCost()*priceRatio;
}


String isCheaperThan(LunchBox x){

if(getPrice()<x.getPrice()){
return "YES!";
}
else{
return "NO!";
}
}

}
class JPD06_4
{
    public static void main(String args[])
    {
        LunchBox economy = new LunchBox();
        economy.add(new Rice(200));
        economy.add(new Cabbage(100));
        economy.add(new PorkRib(250));
        economy.setPriceRatio(1.2);

        LunchBox valuedChoice = new LunchBox();
        valuedChoice.add(new Rice(200));
        valuedChoice.add(new Egg(30));
        valuedChoice.add(new Carrot(100));
        valuedChoice.add(new PorkRib(300));
        valuedChoice.setPriceRatio(1.3);

        System.out.println("Is the economy lunch box cheaper than the valued-choice? " + economy.isCheaperThan(valuedChoice));

    }
}


第五題

import java.util.*;

class SaleReport{

ArrayList <LunchBox> list;

int count;

SaleReport(){
list=new ArrayList<LunchBox>();
count=0;
}

void add(LunchBox l){
System.out.println("Profit is " + l.getPrice() + ".");
System.out.println("Profit is " + l.getCost() + ".");
list.add(l);
count++;
}

int getNumberOfLunchBox(){
return count;
}

int profit;

int getProfit(){
profit=0;
for(LunchBox l : list){
System.out.println("Profit is " + l.getPrice() + ".");
System.out.println("Profit is " + l.getCost() + ".");
profit+=(int)(l.getPrice()-l.getCost());
}
return profit;
}
}

class JPA06_5
{
    public static void main(String args[])
    {
        LunchBox economy = new LunchBox();
        economy.add(new Rice(200));
        economy.add(new Cabbage(100));
        economy.add(new PorkRib(250));
        economy.setPriceRatio(1.2);

        LunchBox valuedChoice = new LunchBox();
        valuedChoice.add(new Rice(200));
        valuedChoice.add(new Egg(30));
        valuedChoice.add(new Carrot(100));
        valuedChoice.add(new PorkRib(300));
        valuedChoice.setPriceRatio(1.3);

        SaleReport sr = new SaleReport();
        sr.add(economy);
        sr.add(valuedChoice);
        System.out.println( sr.getNumberOfLunchBox() + " lunch boxes have been sold.");
        System.out.println("Profit is " + sr.getProfit() + ".");

    }
}

沒有留言:

張貼留言