601 汽車零件設計

第一題

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());
    }

}

沒有留言:

張貼留言