參考程式碼:
第一題
package JPA603.JP06_1;
//建立一個共用的方法,以供其他方法繼承
class Unit
{
double cost,price;
Unit()
{
cost = 0.0;
price = 0.0;
}
public double getCost(){return cost;}
public double getPrice(){return price;}
}
//建立每個原料的方法,並且繼承Unit,繼承其方法和成員
class Apple extends Unit{Apple(){cost = 6.0;price=10.0;}}
class Banana extends Unit{Banana(){cost = 2.0;price=5.0;}}
class Pudding extends Unit{Pudding(){cost = 3.0;price=5.0;}}
class Strawberry extends Unit{Strawberry(){cost = 1.0;price=5.0;}}
class Mango extends Unit{Mango(){cost = 2.0;price=5.0;}}
class JPD06_1 {
public static void main(String args[]) {
//產生個原料的物件
Apple ab = new Apple();
Banana bb = new Banana();
Pudding pt = new Pudding();
System.out.println("Apple cost:" + ab.getCost());
System.out.println("Apple price:" + ab.getPrice());
System.out.println("Banana cost:" + bb.getCost());
System.out.println("Banana price:" + bb.getPrice());
System.out.println("Pudding cost:" + pt.getCost());
System.out.println("Pudding price:" + pt.getPrice());
}
}
第二題
package JPA603.JP06_2;
class Unit
{
double cost,price;
Unit()
{
cost = 0.0;
price = 0.0;
}
public double getCost(){return cost;}
public double getPrice(){return price;}
}
class Apple extends Unit{Apple(){cost = 6.0;price=10.0;}}
class Banana extends Unit{Banana(){cost = 2.0;price=5.0;}}
class Pudding extends Unit{Pudding(){cost = 3.0;price=5.0;}}
class Strawberry extends Unit{Strawberry(){cost = 1.0;price=5.0;}}
class Mango extends Unit{Mango(){cost = 2.0;price=5.0;}}
//建立一個抽象的 product方法,供A、B套餐繼承
abstract class product{
product(){}
//抽象取得成本
abstract double getCost();
//抽象取得售價
abstract double getPrice();
//建立利潤方法
double getProfit(){return getPrice()-getCost();}
}
//A套餐方法
class A extends product{
Unit a1,a2;
//建構子,傳入兩個參數,供初始化
A(Unit b1,Unit b2)
{a1=b1;a2=b2;}
double getCost(){return a1.getCost()+a2.getCost();}
double getPrice(){return a1.getPrice()+a2.getPrice();}
}
//B套餐方法
class B extends product{
Unit a1,a2,a3;
//建構子,傳入三個參數,供初始化
B(Unit b1,Unit b2,Unit b3)
{a1=b1;a2=b2;a3=b3;}
double getCost(){return a1.getCost()+a2.getCost()+a3.getCost();}
double getPrice(){return a1.getPrice()+a2.getPrice()+a3.getPrice();}
}
class JPD06_2 {
public static void main(String args[]) {
//產生A套餐的物件,並傳入兩個原料物件
A t1 = new A(new Apple(), new Banana());
//產生B套餐的物件,並傳入三個原料物件
B t2 = new B(new Banana(), new Pudding(), new Strawberry());
B t3 = new B(new Apple(), new Banana(), new Mango());
System.out.println("t1 price:" + t1.getPrice());
System.out.println("t1 profit:" + t1.getProfit());
System.out.println("t2 price:" + t2.getPrice());
System.out.println("t2 profit:" + t2.getProfit());
System.out.println("t3 price:" + t3.getPrice());
System.out.println("t3 profit:" + t3.getProfit());
}
}
第三題
package JPA603.JP06_3;
class Unit
{
double cost,price;
Unit()
{
cost = 0.0;
price = 0.0;
}
public double getCost(){return cost;}
public double getPrice(){return price;}
}
class Apple extends Unit{Apple(){cost = 6.0;price=10.0;}}
class Banana extends Unit{Banana(){cost = 2.0;price=5.0;}}
class Pudding extends Unit{Pudding(){cost = 3.0;price=5.0;}}
class Strawberry extends Unit{Strawberry(){cost = 1.0;price=5.0;}}
class Mango extends Unit{Mango(){cost = 2.0;price=5.0;}}
abstract class product{
product(){}
abstract double getCost();
abstract double getPrice();
double getProfit(){return getPrice()-getCost();}
}
//建立C套餐的方法,由A套餐改造而來的,僅修改成本部分
class C extends product{
Unit a1,a2;
C(Unit b1,Unit b2)
{a1=b1;a2=b2;}
double getCost(){return a1.getCost()+a2.getCost()+2;}
double getPrice(){return (a1.getPrice()+a2.getPrice())*1.5;}
}
//建立D套餐的方法,由B套餐改造而來的,僅修改成本部分
class D extends product{
Unit a1,a2,a3;
D(Unit b1,Unit b2,Unit b3)
{a1=b1;a2=b2;a3=b3;}
double getCost(){return a1.getCost()+a2.getCost()+a3.getCost()+2;}
double getPrice(){return (a1.getPrice()+a2.getPrice()+a3.getPrice())*1.5;}
}
class JPD06_3 {
public static void main(String args[]) {
//建立套餐的物件,將原料物件傳入套餐中
C t1 = new C (new Apple(), new Banana());
D t2 = new D (new Banana(), new Pudding(), new Strawberry());
D t3 = new D (new Apple(), new Banana(), new Mango());
System.out.println("t1 cost:" + t1.getCost());
System.out.println("t1 price:" + t1.getPrice());
System.out.println("t1 profit:" + t1.getProfit());
System.out.println("t2 cost:" + t2.getCost());
System.out.println("t2 price:" + t2.getPrice());
System.out.println("t2 profit:" + t2.getProfit());
System.out.println("t3 cost:" + t3.getCost());
System.out.println("t3 price:" + t3.getPrice());
System.out.println("t3 profit:" + t3.getProfit());
}
}
第四題
package JPA603.JP06_4;
import java.util.*;
class Unit
{
double cost,price;
Unit()
{
cost = 0.0;
price = 0.0;
}
public double getCost(){return cost;}
public double getPrice(){return price;}
}
class Apple extends Unit{Apple(){cost = 6.0;price=10.0;}}
class Banana extends Unit{Banana(){cost = 2.0;price=5.0;}}
class Pudding extends Unit{Pudding(){cost = 3.0;price=5.0;}}
class Strawberry extends Unit{Strawberry(){cost = 1.0;price=5.0;}}
class Mango extends Unit{Mango(){cost = 2.0;price=5.0;}}
abstract class product{
product(){}
abstract double getCost();
abstract double getPrice();
double getProfit(){return getPrice()-getCost();}
}
class A extends product{
Unit a1,a2;
A(Unit b1,Unit b2) {a1=b1;a2=b2;}
double getCost(){return a1.getCost()+a2.getCost();}
double getPrice(){return a1.getPrice()+a2.getPrice();}
}
class B extends product{
Unit a1,a2,a3;
B(Unit b1,Unit b2,Unit b3){a1=b1;a2=b2;a3=b3;}
double getCost(){return a1.getCost()+a2.getCost()+a3.getCost();}
double getPrice(){return a1.getPrice()+a2.getPrice()+a3.getPrice();}
}
class Deliver{
//建立一個LinkedList來存放套餐的物件
LinkedList ap;
//利用建構子產生LinkedList的物件
Deliver()
{ap=new LinkedList();}
//建立加入套餐的物件
void addProduct(product p)
{ap.add(p);}
double getTotalPrice()
{
double d=0.0;
for(Iterator iterator=ap.iterator();iterator.hasNext();)
{
product p = (product)iterator.next();
d+=p.getPrice();//累加售價
}
return d;
}
double getTotalCost()
{
double d=0.0;
for(Iterator iterator=ap.iterator();iterator.hasNext();)
{
product p = (product)iterator.next();
d+=p.getCost();//累加成本
}
return d;
}
double getTotalProfit()
{
double d=0.0;
for(Iterator iterator=ap.iterator();iterator.hasNext();)
{
product p = (product)iterator.next();
d+=(p.getPrice()-p.getCost());//累加利潤
}
return d;
}
}
class JPD06_4 {
public static void main(String args[]){
//產生外送物件
Deliver d1 = new Deliver();
//將A套餐的物件放入,A套餐又放入兩個原料來組成
d1.addProduct(new A(new Apple(), new Banana()));
//將B套餐的物件放入,B套餐又放入三個原料來組成
d1.addProduct(new B(new Banana(), new Pudding(), new Strawberry()));
System.out.println("a Price: " + d1.getTotalPrice());
System.out.println("a Cost: " + d1.getTotalCost());
System.out.println("a Profit: " + d1.getTotalProfit());
Deliver d2 = new Deliver();
d2.addProduct(new B(new Apple(), new Banana(), new Mango()));
d2.addProduct(new A(new Apple(), new Banana()));
d2.addProduct(new B(new Banana(), new Pudding(), new Strawberry()));
d2.addProduct(new B(new Apple(), new Banana(), new Mango()));
System.out.println("b Price: " + d2.getTotalPrice());
System.out.println("b Cost: " + d2.getTotalCost());
System.out.println("b Profit: " + d2.getTotalProfit());
}
}
第五題
package JPA603.JP06_5;
import java.util.*;
class Unit
{
double cost,price;
Unit()
{
cost = 0.0;
price = 0.0;
}
public double getCost(){return cost;}
public double getPrice(){return price;}
}
class Apple extends Unit{Apple(){cost = 6.0;price=10.0;}}
class Banana extends Unit{Banana(){cost = 2.0;price=5.0;}}
class Pudding extends Unit{Pudding(){cost = 3.0;price=5.0;}}
class Strawberry extends Unit{Strawberry(){cost = 1.0;price=5.0;}}
class Mango extends Unit{Mango(){cost = 2.0;price=5.0;}}
abstract class product{
product(){}
abstract double getCost();
abstract double getPrice();
double getProfit(){return getPrice()-getCost();}
}
class A extends product{
Unit a1,a2;
A(Unit b1,Unit b2){a1=b1;a2=b2;}
double getCost(){return a1.getCost()+a2.getCost();}
double getPrice(){return a1.getPrice()+a2.getPrice();}
}
class B extends product{
Unit a1,a2,a3;
B(Unit b1,Unit b2,Unit b3){a1=b1;a2=b2;a3=b3;}
double getCost(){return a1.getCost()+a2.getCost()+a3.getCost();}
double getPrice(){return a1.getPrice()+a2.getPrice()+a3.getPrice();}
}
class Deliver{
LinkedList ap;
Deliver(){ap=new LinkedList();}
void addProduct(product p){ap.add(p);}
//建立一個檢查的方法,若有符合特定條件,則丟出Exception來警示
double checkOut() throws notenoughorder
{
double d = getTotalPrice();
if(d<50)
throw new notenoughorder(this);
else
return d;
}
double getTotalPrice()
{
double d=0.0;
for(Iterator iterator=ap.iterator();iterator.hasNext();)
{
product p = (product)iterator.next();
d+=p.getPrice();
}
return d;
}
double getTotalCost()
{
double d=0.0;
for(Iterator iterator=ap.iterator();iterator.hasNext();)
{
product p = (product)iterator.next();
d+=p.getCost();
}
return d;
}
double getTotalProfit()
{
double d=0.0;
for(Iterator iterator=ap.iterator();iterator.hasNext();)
{
product p = (product)iterator.next();
d+=(p.getPrice()-p.getCost());
}
return d;
}
}
//建立一個Exception的方法,並傳入外送物件
class notenoughorder extends Exception{
static Deliver d;
notenoughorder(Deliver deliver){d=deliver;}
//在裡面再建立一個order的方法,回傳外送總價
static double order(){return d.getTotalPrice();}
}
class JPD06_5 {
public static void main(String args[]) {
//利用try-catch包住整個main的程式
try{
Deliver d1 = new Deliver();
d1.addProduct(new A(new Apple(), new Banana()));
d1.addProduct(new B(new Banana(), new Pudding(), new Strawberry()));
d1.addProduct(new B(new Banana(), new Pudding(), new Strawberry()));
d1.addProduct(new B(new Apple(), new Banana(), new Mango()));
System.out.println("a Price: " + d1.getTotalPrice());
System.out.println("a Cost: " + d1.getTotalCost());
System.out.println("a Profit: " + d1.getTotalProfit());
System.out.println("");
//進行外送的檢查
d1.checkOut();
Deliver d2 = new Deliver();
d2.addProduct(new B(new Apple(), new Banana(), new Mango()));
d2.addProduct(new A(new Apple(), new Banana()));
System.out.println("b Price: " + d2.getTotalPrice());
System.out.println("b Cost: " + d2.getTotalCost());
System.out.println("b Profit: " + d2.getTotalProfit());
//進行外送的檢查
d2.checkOut();
}
catch(notenoughorder e)//抓取錯誤的catch
{
//抓取到錯誤後,會進來此block,執行程式
System.out.println("Not enough order for carry out:"+
notenoughorder.order());
}
}
}
TQC+ JAVA6 試題總覽:
連結
聲明:
本篇教學的程式碼皆由作者群編輯,歡迎轉貼本教學,但請全文轉貼,尊重一下作者群的心血喔 ^_^