第一題
class Material{
double cost;
double price;
Material(double x,double y){
cost=x;
price=y;
}
double getCost(){
return cost;
}
double getPrice(){
return price;
}
}
class Apple extends Material{
Apple(){
super(6,10);
}
}
class Banana extends Material{
Banana(){
super(2,5);
}
}
class Pudding extends Material{
Pudding(){
super(3,5);
}
}
class Strawberry extends Material{
Strawberry(){
super(1,5);
}
}
class Mango extends Material{
Mango(){
super(2,5);
}
}
class JPA06_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());
}
}
第二題
abstract class product{
abstract double getPrice();
abstract double getCost();
double getProfit(){
return getPrice()-getCost();
}
}
class A extends product{
Material x1,x2;
A(){}
A(Material a,Material b){
x1=a;
x2=b;
}
double getPrice(){
return x1.getPrice()+x2.getPrice();
}
double getCost(){
return x1.getCost()+x2.getCost();
}
}
class B extends product{
Material x1,x2,x3;
B(){}
B(Material a,Material b,Material c){
x1=a;
x2=b;
x3=c;
}
double getPrice(){
return x1.getPrice()+x2.getPrice()+x3.getPrice();
}
double getCost(){
return x1.getCost()+x2.getCost()+x3.getCost();
}
}
class JPA06_2 {
public static void main(String args[]) {
A t1 = new A(new Apple(), new Banana());
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());
}
}
第三題
class C extends A{
C(){}
C(Material a,Material b){super(a,b);}
double getPrice(){
return (x1.getPrice()+x2.getPrice())*1.5;
}
double getCost(){
return x1.getCost()+x2.getCost()+2;
}
}
class D extends B{
D(){}
D(Material a,Material b,Material c){super(a,b,c);}
double getPrice(){
return (x1.getPrice()+x2.getPrice()+x3.getPrice())*1.5;
}
double getCost(){
return x1.getCost()+x2.getCost()+x3.getCost()+2;
}
}
class JPA06_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());
}
}
第四題
import java.util.*;
class Deliver{
LinkedList<product> list = new LinkedList<product>();
void addProduct(product x){
list.add(x);
}
double getTotalPrice(){
double total=0;
for(product i:list){
total+=i.getPrice();
}
return total;
}
double getTotalProfit(){
double total=0;
for(product i:list){
total+=i.getProfit();
}
return total;
}
double getTotalCost(){
double total=0;
for(product i:list){
total+=i.getCost();
}
return total;
}
}
class JPA06_4 {
public static void main(String args[]){
Deliver d1 = new Deliver();
d1.addProduct(new A(new Apple(), new Banana()));
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());
}
}
第五題
import java.util.*;
class Deliver{
LinkedList<product> list = new LinkedList<product>();
void addProduct(product x){
list.add(x);
}
double getTotalPrice(){
double total=0;
for(product i:list){
total+=i.getPrice();
}
return total;
}
double getTotalProfit(){
double total=0;
for(product i:list){
total+=i.getProfit();
}
return total;
}
double getTotalCost(){
double total=0;
for(product i:list){
total+=i.getCost();
}
return total;
}
void checkOut() throws notenoughException {
if(getTotalPrice()<50){
throw new notenoughException("Not enough order for carry out:"+getTotalPrice());
}
}
}
class notenoughException extends Exception{
notenoughException(String x){
System.out.println(x);
}
}
class JPA06_5 {
public static void main(String args[]) {
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(notenoughException e){
}
}
}
第一題
class Part{
int cost;
Part(){}
int getCost(){
return cost;
}
}
class LCD extends Part{
LCD(int l){
switch(l){
case 10:
cost=2000;break;
case 15:
cost=2500;break;
case 17:
cost=3000;break;
}
}
}
class CPU extends Part{
CPU(double c){
if(c==1.66){
cost=6000;
}
else if(c==2.2){
cost=8000;
}
else if(c==2.4){
cost=11000;
}
}
}
class HD extends Part{
HD(String h){
if(h.equals("120G")){
cost=2400;
}
else{
cost=2800;
}
}
}
class NoteBook{
LCD l;
CPU c;
HD h;
NoteBook(int i,double d,String s){
l = new LCD(i);
c = new CPU(d);
h = new HD(s);
}
double unit(){
return l.getCost()+c.getCost()+h.getCost();
}
double getPrice(){
return unit()*2;
}
double getCost(){
return unit()*1.4;
}
}
class MiniNote extends NoteBook{
MiniNote(){super(10,1.66,"120G"); }
}
class Note15 extends NoteBook{
Note15(){super(15,2.2,"160G");}
}
public class JPA06_1{
public static void main(String args[]){
MiniNote mininote = new MiniNote();
System.out.println("MiniNote cost:"+mininote.getCost()+", price:"+mininote.getPrice());
Note15 note15 = new Note15();
System.out.println("Note15 cost:"+note15.getCost()+", price:"+note15.getPrice());
}
}
第二題
abstract class PCandMultiPC{
int CPUcount=1;
int HDcount=1;
CPU c;
HD h;
PCandMultiPC(double d,String s,int x,int y){
c = new CPU(d);
h = new HD(s);
CPUcount=x;
HDcount=y;
}
PCandMultiPC(double d,String s){
c = new CPU(d);
h = new HD(s);
}
double unit(){
return c.getCost()*CPUcount+h.getCost()*HDcount;
}
abstract double getPrice();
abstract double getCost();
}
class PC extends PCandMultiPC{
PC(){super(2.4,"160G");}
double getCost(){
return unit()+500;
}
double getPrice(){
return unit()*1.8;
}
}
class MultiPC extends PCandMultiPC{
MultiPC(int c, int d){ super(2.4,"160G",c,d); }
double getCost(){
return unit()*1.2;
}
double getPrice(){
return unit()*1.8;
}
}
class JPA06_2 {
public static void main(String args[]){
PC pc = new PC();
System.out.println("PC cost:"+pc.getCost()+", price:"+pc.getPrice());
MultiPC multipc1 = new MultiPC(2, 4);
System.out.println("MultiPC: 2CPU, 4HD, cost:"+multipc1.getCost()+", price:"+multipc1.getPrice());
MultiPC multipc2 = new MultiPC(4, 8);
System.out.println("MultiPC: 4CPU, 8HD, cost:"+multipc2.getCost()+", price:"+multipc2.getPrice());
}
}
第三題
class AllPC{
AllPC(){}
boolean isExpensive(PCandMultiPC p,NoteBook n){
if( p.getPrice() > n.getPrice() ){
return true;
}
else{
return false;
}
}
}
class JPA06_3 {
public static void main(String args[]) {
PC pc = new PC();
Note15 note15 = new Note15();
AllPC compare=new AllPC();
if(compare.isExpensive(pc,note15)){
System.out.println("PC is more expensive than Note15");
}
else{
System.out.println("Note15 is more expensive than PC");
}
}
}
第四題
import java.util.*;
class NoteBook extends AllPC{
LCD l;
CPU c;
HD h;
NoteBook(int i,double d,String s){
l = new LCD(i);
c = new CPU(d);
h = new HD(s);
}
double unit(){
return l.getCost()+c.getCost()+h.getCost();
}
double getPrice(){
return unit()*2;
}
double getCost(){
return unit()*1.4;
}
}
abstract class PCandMultiPC extends AllPC{
int CPUcount=1;
int HDcount=1;
CPU c;
HD h;
PCandMultiPC(double d,String s,int x,int y){
c = new CPU(d);
h = new HD(s);
CPUcount=x;
HDcount=y;
}
PCandMultiPC(double d,String s){
c = new CPU(d);
h = new HD(s);
}
double unit(){
return c.getCost()*CPUcount+h.getCost()*HDcount;
}
abstract double getPrice();
abstract double getCost();
}
abstract class AllPC{
AllPC(){}
boolean isExpensive(PCandMultiPC p,NoteBook n){
if( p.getPrice() > n.getPrice() ){
return true;
}
else{
return false;
}
}
abstract double getPrice();
}
class Order{
LinkedList<AllPC> list =new LinkedList<AllPC>();
void in(AllPC c){
list.add(c);
}
double revenue(){
double total=0;
for(AllPC i:list){
total+=i.getPrice();
}
return total;
}
}
class JPD06_4 {
public static void main(String args[]){
Order ord = new Order();
ord.in(new MiniNote());
ord.in(new Note15());
ord.in(new PC());
System.out.println(ord.revenue());
}
}
第五題
import java.util.*;
class NoteBook extends AllPC{
LCD l;
CPU c;
HD h;
NoteBook(int i,double d,String s){
l = new LCD(i);
c = new CPU(d);
h = new HD(s);
}
double unit(){
return l.getCost()+c.getCost()+h.getCost();
}
double getPrice(){
return unit()*2;
}
double getCost(){
return unit()*1.4;
}
}
abstract class PCandMultiPC extends AllPC{
int CPUcount=1;
int HDcount=1;
CPU c;
HD h;
PCandMultiPC(double d,String s,int x,int y){
c = new CPU(d);
h = new HD(s);
CPUcount=x;
HDcount=y;
}
PCandMultiPC(double d,String s){
c = new CPU(d);
h = new HD(s);
}
double unit(){
return c.getCost()*CPUcount+h.getCost()*HDcount;
}
abstract double getPrice();
abstract double getCost();
}
abstract class AllPC{
AllPC(){}
boolean isExpensive(PCandMultiPC p,NoteBook n){
if( p.getPrice() > n.getPrice() ){
return true;
}
else{
return false;
}
}
abstract double getPrice();
abstract double getCost();
}
class Order{
LinkedList<AllPC> list =new LinkedList<AllPC>();
void in(AllPC c){
list.add(c);
}
double revenue(){
double total=0;
for(AllPC i:list){
total+=i.getPrice();
}
return total;
}
double costs(){
double total=0;
for(AllPC i:list){
total+=i.getCost();
}
return total;
}
double profit(){
return (revenue()-costs());
}
}
class JPA06_5 {
public static void main(String args[]){
Order ord = new Order();
ord.in(new MiniNote());
ord.in(new Note15());
ord.in(new PC());
if(ord.profit()>20000)
{System.out.println("This order exceeds 20000:"+ord.profit());}
}
}
第一題
class Unit{
int cost;
Unit(){}
int getCost(){
return cost;
}
}
class Engine extends Unit{
Engine(int x){
if(x==1600){
cost=20000;
}
else if(x==2000){
cost=25000;
}
}
}
class Aircond extends Unit{
Aircond(String s){
if(s.equals("Auto")){
cost=12000;
}
else if(s.equals("Manual")){
cost=10000;
}
}
}
class Sound extends Unit{
Sound(){
cost=2000;
}
}
public class JPA601_1 {
public static void main(String args[]){
Engine e1 = new Engine(1600);
System.out.println("1600 cost: " + e1.getCost());
Engine e2 = new Engine(2000);
System.out.println("2000 cost: " + e2.getCost());
Aircond a1 = new Aircond("Auto");
System.out.println("Auto: " + a1.getCost());
Aircond a2 = new Aircond("Manual");
System.out.println("Manual: " + a2.getCost());
Sound s1 = new Sound();
System.out.println("Stereo: " + s1.getCost());
}
}
第二題
abstract class Car{
Engine e;
Aircond a;
Car(int i,String s){
e = new Engine(i);
a = new Aircond(s);
}
public abstract double cost();
double price(){
return cost()*1.2;
}
}
class BasicCar extends Car{
BasicCar(int i,String s){
super(i,s);
}
public double cost(){
return e.getCost()+a.getCost()+5000;
}
}
class LuxCar extends Car{
LuxCar(int i,String s){
super(i,s);
}
public double cost(){
return e.getCost()+a.getCost()+10000;
}
}
public class JPA601_2 {
public static void main(String args[]){
BasicCar bc = new BasicCar(1600,"Manual");
System.out.println("Basic cost: " + bc.cost());
System.out.println("Basic price: " + bc.price());
LuxCar lc = new LuxCar(2000,"Auto");
System.out.println("Lux cost: " + lc.cost());
System.out.println("Lux price: " + lc.price());
}
}
第三題
class SLuxCar extends LuxCar{
SLuxCar(int i,String s){
super(i,s);
}
Sound s = new Sound();
public double cost(){
return e.getCost()+a.getCost()+10000+s.getCost();
}
String expensive(Car c){
if(c.price()<price()){
return "Yes!!";
}
else{
return "No!!";
}
}
}
public class JPA601_3 {
public static void main(String args[]) {
SLuxCar llc = new SLuxCar(2000,"Auto");
System.out.println("SLux cost: " + llc.cost());
System.out.println("SLux price: " + llc.price());
LuxCar lc = new LuxCar(2000,"Auto");
System.out.println("Is llc more expensive than lc? " + llc.expensive(lc));
}
}
第四題
import java.util.*;
import java.io.*;
class Warehouse{
ArrayList <Car>cars=new ArrayList<Car>();
void add(Car car){
cars.add(car);
}
double TotalCost(){
double total=0.0;
for(Car car:cars){
total=total+car.cost();
}
return total;
}
double TotalPrice(){
return TotalCost()*1.2;
}
}
public class JPA601_4 {
public static void main(String args[]) {
Scanner sc = null;
try {
sc = new Scanner(new File("data.txt"));
}
catch (FileNotFoundException e){
System.out.println("File not found!");
System.exit(0);
}
Warehouse wh = new Warehouse();
boolean si = true ;
do{
if(sc.hasNext()){
String s=sc.next();
int i=sc.nextInt();
String s1=sc.next();
if(s.charAt(0)=='B'){
wh.add(new BasicCar(i,s1));
}
if(s.charAt(0)=='L'){
wh.add(new LuxCar(i,s1));
}
if(s.charAt(0)=='S'){
wh.add(new SLuxCar(i,s1));
}
}
else{
si=false;
}
}while(si);
System.out.println("Total cost: " + wh.TotalCost());
System.out.println("Total price: " + wh.TotalPrice());
}
}
第五題
import java.util.*;
import java.io.*;
class WrongException extends Exception{
WrongException(String i){
System.out.println(i);
}
}
public class JPA601_5 {
public static void main(String args[]) {
Scanner sc = null;
try {
sc = new Scanner(new File("wrongdata.txt"));
} catch (FileNotFoundException e) {
System.out.println ("File not found!");
// Stop program if no file found
System.exit (0);
}
Warehouse wh = new Warehouse();
boolean si = true ;
try{
do{
if(sc.hasNext()){
String s=sc.next();
int i=sc.nextInt();
String s1=sc.next();
if(s.charAt(0)=='B'){
wh.add(new BasicCar(i,s1));
}
else if(s.charAt(0)=='L'){
wh.add(new LuxCar(i,s1));
}
else if(s.charAt(0)=='S'){
wh.add(new SLuxCar(i,s1));
}
else{
throw new WrongException("Incorrect input: "+s+" "+i+" "+s1);
}
}
else{
si=false;
}
}while(si);
}
catch(WrongException err){
}
System.out.println("Total cost: " + wh.TotalCost());
System.out.println("Total price: " + wh.TotalPrice());
}
}