2013年2月8日 星期五

TQC+ JAVA 608 食物熱量計算

TQC+ JAVA6 試題總覽:LINK
Github 備份:LINK

/* TQC+ JAVA6 - 608_1 */
//建立抽象的食物類別
abstract class Food{
int amount;
int calorie;
//建構子初始化份量
Food(int i){
amount=i;
}
//建立設定卡洛里方法
void setCaloriePerGram(int i){
calorie=i;
}
//建立取得份量的方法
int getAmount(){
return amount;
}
//建立取得份量的抽象方法,待繼承者去實作
abstract int getCalorie();
}
//繼承食物
class Rice extends Food{
Rice(int i){
super(i);
calorie=1;
}
int getCalorie(){
return getAmount()*calorie;
}
}
//繼承食物
class Egg extends Food{
Egg(int i){
super(i);
calorie=2;
}
int getCalorie(){
return getAmount()*calorie;
}
}
//繼承食物
class Cabbage extends Food{
Cabbage(int i){
super(i);
calorie=1;
}
int getCalorie(){
return getAmount()*calorie;
}
}
//繼承食物
class PorkRib extends Food{
PorkRib(int i){
super(i);
calorie=10;
}
int getCalorie(){
return getAmount()*calorie;
}
}
//繼承食物
class Carrot extends Food{
Carrot(int i){
super(i);
calorie=1;
}
int getCalorie(){
return getAmount()*calorie;
}
}
class JPA06_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.");
}
}
/* TQC+ JAVA6 - 608_2 */
import java.util.Iterator;
import java.util.Vector;
abstract class Food{
int amount;
int calorie;
Food(int i){
amount=i;
}
void setCaloriePerGram(int i){
calorie=i;
}
int getAmount(){
return amount;
}
abstract int getCalorie();
}
class Rice extends Food{
Rice(int i){
super(i);
calorie=1;
}
int getCalorie(){
return getAmount()*calorie;
}
}
class Egg extends Food{
Egg(int i){
super(i);
calorie=2;
}
int getCalorie(){
return getAmount()*calorie;
}
}
class Cabbage extends Food{
Cabbage(int i){
super(i);
calorie=1;
}
int getCalorie(){
return getAmount()*calorie;
}
}
class PorkRib extends Food{
PorkRib(int i){
super(i);
calorie=10;
}
int getCalorie(){
return getAmount()*calorie;
}
}
class Carrot extends Food{
Carrot(int i){
super(i);
calorie=1;
}
int getCalorie(){
return getAmount()*calorie;
}
}
//建立便當盒的物件
class LunchBox{
int calorie;
Vector content;
//建構子初始為向量的物件
LunchBox(){
content=new Vector();
}
//建立增加物件的方法
void add(Food f){
content.add(f);
}
//建立取得整個便當盒卡洛里的方法
int getCalorie(){
int i =0;
for(Iterator iterator = content.iterator();iterator.hasNext();){
Food f = (Food)iterator.next();
i+=f.getCalorie();
}
return i;
}
}
class JPA06_2{
public static void main(String args[])
{
//建立economy的便當盒物件
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() +".");
//建立economy的便當盒物件
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()+".");
}
}
/* TQC+ JAVA6 - 608_3 */
import java.util.Iterator;
import java.util.Vector;
abstract class Food{
int amount;
int calorie;
//新增單位成本
int unitCost;
Food(int i){
amount=i;
}
void setCaloriePerGram(int i){
calorie=i;
}
//設定單位成本方法
void setUnitCost(int i){
unitCost=i;
}
//取得成本方法
int getCost(){
return unitCost*amount;
}
int getAmount(){
return amount;
}
abstract int getCalorie();
}
class Rice extends Food{
Rice(int i){//在子類別中設定好單位價錢
super(i);
calorie=1;
unitCost=1;
}
int getCalorie(){
return getAmount()*calorie;
}
}
class Egg extends Food{
Egg(int i){//在子類別中設定好單位價錢
super(i);
calorie=2;
unitCost=2;}
int getCalorie(){
return getAmount()*calorie;
}
}
class Cabbage extends Food{
Cabbage(int i){//在子類別中設定好單位價錢
super(i);
calorie=1;
unitCost=3;
}
int getCalorie(){
return getAmount()*calorie;
}
}
class PorkRib extends Food{
PorkRib(int i){//在子類別中設定好單位價錢
super(i);
calorie=10;
unitCost=8;}
int getCalorie(){
return getAmount()*calorie;
}
}
class Carrot extends Food{
Carrot(int i){
super(i);
calorie=1;
unitCost=3;
}
int getCalorie(){
return getAmount()*calorie;
}
}
class LunchBox{
int calorie;
Vector content;
LunchBox(){
content=new Vector();
}
//新增一個變數,該變數乘以成本可以得到售價
double priceRatio;
void setPriceRatio(double d){
priceRatio=d;
}
double getPrice(){
double i =0;
for(Iterator iterator = content.iterator();iterator.hasNext();){
Food f = (Food)iterator.next();
i+=f.getCost()*priceRatio;
}
return i;
}
void add(Food f){
content.add(f);
}
int getCalorie(){
int i =0;
for(Iterator iterator = content.iterator();iterator.hasNext();){
Food f = (Food)iterator.next();
i+=f.getCalorie();
}
return i;
}
}
class JPA06_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());
}
}
/* TQC+ JAVA6 - 608_4 */
import java.util.Iterator;
import java.util.Vector;
abstract class Food{
int amount;
int calorie;
int unitCost;
Food(int i){
amount=i;
}
void setCaloriePerGram(int i){
calorie=i;
}
void setUnitCost(int i){
unitCost=i;
}
int getCost(){
return unitCost*amount;}
int getAmount(){
return amount;
}
abstract int getCalorie();
}
class Rice extends Food{
Rice(int i){
super(i);
calorie=1;
unitCost=1;
}
int getCalorie(){
return getAmount()*calorie;
}
}
class Egg extends Food{
Egg(int i){
super(i);
calorie=2;
unitCost=2;
}
int getCalorie(){
return getAmount()*calorie;
}
}
class Cabbage extends Food{
Cabbage(int i){
super(i);
calorie=1;
unitCost=3;
}
int getCalorie(){
return getAmount()*calorie;
}
}
class PorkRib extends Food{
PorkRib(int i){
super(i);
calorie=10;unitCost=8;
}
int getCalorie(){
return getAmount()*calorie;
}
}
class Carrot extends Food{
Carrot(int i){
super(i);
calorie=1;
unitCost=3;
}
int getCalorie(){
return getAmount()*calorie;
}
}
class LunchBox{
int calorie;
Vector content;
LunchBox(){
content=new Vector();
}
double priceRatio;
void setPriceRatio(double d){
priceRatio=d;
}
//新增一個比較價位高低方法
String isCheaperThan(LunchBox lb){
//lb.getPrice()是指外部傳入那個便當盒
if(getPrice()<lb.getPrice())
return "YES";
else
return "NO";
}
double getPrice(){
double i =0;
for(Iterator iterator = content.iterator();iterator.hasNext();){
Food f = (Food)iterator.next();
i+=f.getCost()*priceRatio;
}
return i;
}
void add(Food f){
content.add(f);
}
int getCalorie(){
int i =0;
for(Iterator iterator = content.iterator();iterator.hasNext();){
Food f = (Food)iterator.next();
i+=f.getCalorie();
}
return i;
}
}
class JPA06_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));
}
}
/* TQC+ JAVA6 - 608_5 */
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Vector;
abstract class Food{
int amount;
int calorie;
int unitCost;
Food(int i){
amount=i;
}
void setCaloriePerGram(int i){
calorie=i;
}
void setUnitCost(int i){
unitCost=i;
}
int getCost(){
return unitCost*amount;
}
int getAmount(){
return amount;
}
abstract int getCalorie();
}
class Rice extends Food{
Rice(int i)
{super(i);calorie=1;unitCost=1;}
int getCalorie(){return getAmount()*calorie;}
}
class Egg extends Food{
Egg(int i){
super(i);
calorie=2;
unitCost=2;
}
int getCalorie(){
return getAmount()*calorie;
}
}
class Cabbage extends Food{
Cabbage(int i){
super(i);
calorie=1;
unitCost=3;}
int getCalorie(){
return getAmount()*calorie;
}
}
class PorkRib extends Food{
PorkRib(int i){
super(i);
calorie=10;
unitCost=8;
}
int getCalorie(){
return getAmount()*calorie;
}
}
class Carrot extends Food{
Carrot(int i){
super(i);
calorie=1;
unitCost=3;
}
int getCalorie(){
return getAmount()*calorie;
}
}
class LunchBox{
int calorie;
Vector content;
LunchBox(){
content=new Vector();
}
double priceRatio;
void setPriceRatio(double d){
priceRatio=d;
}
String isCheaperThan(LunchBox lb){
if(getPrice()<lb.getPrice())
return "YES";
else
return "NO";
}
double getPrice(){
double i =0;
for(Iterator iterator = content.iterator();iterator.hasNext();){
Food f = (Food)iterator.next();i+=f.getCost()*priceRatio;
}
return i;
}
int getCost(){
int i =0;
for(Iterator iterator = content.iterator();iterator.hasNext();){
Food f = (Food)iterator.next();i+=f.getCost();
}
return i;
}
void add(Food f){
content.add(f);
}
int getCalorie(){
int i =0;
for(Iterator iterator = content.iterator();iterator.hasNext();){
Food f = (Food)iterator.next();
i+=f.getCalorie();
}
return i;
}
}
//建立銷售報告類別
class SaleReport{
ArrayList ss;
int mun=0;
//建構子初始化為ArrayList
SaleReport(){
ss=new ArrayList();
}
//將便當盒加入至陣列中
void add(LunchBox ff){
ss.add(ff);
mun++;//每增加一次,便當盒數量會+1
}
//建立取得數量的方法
int getNumberOfLunchBox(){
return mun;
}
//建立取得利潤的方法
int getProfit(){
int i =0;
for(Iterator iterator = ss.iterator();iterator.hasNext();){
LunchBox lbb = (LunchBox)iterator.next();
i+=lbb.getPrice()-lbb.getCost();
}
return i;
}
}
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() + ".");
}
}


TQC+ JAVA6 試題總覽:LINK
Github 備份:LINK

本篇教學的程式碼皆由筆者編輯,歡迎轉貼本教學,但請全文轉貼,謝啦~

沒有留言:

張貼留言