2009年5月25日 星期一

Lab Magic Parking Tower

A parking tower is out of order someday. If you park a Benz, you will end up with a Torben. Write a program to simulate this scenario. First create a class called CarParked which has a static method called outOfOrder. Name an object called yourCar, which happens to be a Benz. Your program should contain a class called CarParked and a test program called CarParkedDemo which test the method by CarParked.outOfOrder(yourCar).

Hint: You may study Display 5.14 to get some ideas.

class程式碼

------------------------------------------------<程式碼開始>--------------------------------------
public class CarParked {
private String name;
public CarParked(String YourCar) {
this.name = YourCar;
}
void setCar (String YourCar) {
this.name = YourCar;
}
static CarParked outOfOrder(CarParked name) {
name.setCar("Torben");
return name;
}
public String toString() {
return name;
}
}
------------------------------------------------<程式碼結束>--------------------------------------

主程式碼
------------------------------------------------<程式碼開始>--------------------------------------
import java.util.*;
public class CarParkedDemo {
public static void main (String args[]) {
CarParked Car = new CarParked("Benz");
        System.out.println("parking " + Car);
CarParked.outOfOrder(Car);
System.out.println("out " + Car );
    }
}
------------------------------------------------<程式碼結束>--------------------------------------
附註:

沒有留言:

張貼留言