課本 503 第6題
Create a class called Vehicle that has the manufacturer’s name (type String), number of cylinders in the engine (type int), and owner (type Person given below). Then, create a class called Truck that is derived from Vehicle and has the following additional properties: the load capacity in tons (type double since it may contain a fractional part) and towing capacity in pounds (type int). Be sure your class has a reasonable complement of constructors, accessor and mutator methods, and suitably defined equals and toString methods. Write a program to test all your methods.
The definition of the class Person is below. Completing the definitions of the methods
package Person;/** * @author Shiang-Yun Yang */public class Person { private String name; public Person() { this("No name"); } public Person(String theName) { name = theName; } public Person(Person theObject) { this(theObject.name); } public String getName() { return name; } public void setName(String theName) { name = theName; } public String toString() { return name; } @Override public boolean equals(Object other) { if (other == null) return false; if (!(other instanceof Person)) return false; return this.name.equals(((Person) other).name); }}
package Vehicle;import Person.*;/** * @author Shiang-Yun Yang */public class Vehicle { private String manufacturer; private int cylinder; private Person owner; public Vehicle() { this("None", 0, new Person()); } public Vehicle(String manufacturer, int cylinder, Person owner) { if(cylinder < 0) { System.out.println("Format Error"); System.exit(0); } this.manufacturer = manufacturer; this.cylinder = cylinder; this.owner = new Person(owner); } /** * Copy constructor by Truck Class * * @param other */ public Vehicle(Vehicle other) { this(other.manufacturer, other.cylinder, other.owner); } public String getManufacturer() { return manufacturer; } public int getCylinder() { return cylinder; } public Person getOwner() { return new Person(owner); } public void setManufacturer(String manufacturer) { this.manufacturer = manufacturer; } public void setCylinder(int cylinder) { if(cylinder < 0) { System.out.println("Format Error"); System.exit(0); } this.cylinder = cylinder; } public void setOwner(Person owner) { this.owner = new Person(owner); } public String toString() { return manufacturer + ", " + cylinder + " cylinders, owned by " + owner; } /* * "Object instanceof Class" * if Object == null, return false. but Class * can't be null. */ @Override public boolean equals(Object other) { /* if(other == null || other.getClass() != this.getClass()) return false; */ if (!(other instanceof Vehicle)) return false; return this.manufacturer.equals(((Vehicle) other).manufacturer) && this.cylinder == ((Vehicle) other).cylinder && this.owner.equals(((Vehicle) other).owner); }}
package Truck;import Vehicle.*;import Person.*;/** * @author Shiang-Yun Yang */public class Truck extends Vehicle { private double loadCapacity; private int towingCapacity; public Truck() { super(); } public Truck(String manufacturer, int cylinder, Person owner, double loadCapacity, int towingCapacity) { super(manufacturer, cylinder, owner); if(loadCapacity < 0 || towingCapacity < 0) { System.out.println("Format Error"); System.exit(0); } this.loadCapacity = loadCapacity; this.towingCapacity = towingCapacity; } /** * Copy constructor by Truck Class * * @param other */ public Truck(Truck other) { super(other); if(loadCapacity < 0 || towingCapacity < 0) { System.out.println("Format Error"); System.exit(0); } loadCapacity = other.loadCapacity; towingCapacity = other.towingCapacity; } public double getLoadCapacity() { return loadCapacity; } public int getTowingCapacity() { return towingCapacity; } public void setLoadCapacity(double load) { if(loadCapacity < 0) { System.out.println("Format Error"); System.exit(0); } loadCapacity = load; } public void setTowingCapacity(int towing) { if(towingCapacity < 0) { System.out.println("Format Error"); System.exit(0); } towingCapacity = towing; } public String toString() { return super.toString() + ", load capacity " + loadCapacity + " ton(s), towing capacity " + towingCapacity; } /* * "Object instanceof Class" * if Object == null, return false. but Class * can't be null. */ @Override public boolean equals(Object other) { /* if(other == null || other.getClass() != this.getClass()) return false; */ if (!(other instanceof Truck)) return false; return super.equals(other) && this.loadCapacity == ((Truck) other).loadCapacity && this.towingCapacity == ((Truck) other).towingCapacity; }}/* * 1. super() method should be written in first line * 2. || and && are special operators, and both strong logic operators. * 3. instanceof and Object.getClass() are different. */
import Person.*;import Truck.*;import Vehicle.*;/** * @author Shiang-Yun Yang */public class Demo { public static void main(String[] args) { System.out.println("Test Person Class:"); Person p1 = new Person(); System.out.println("p1: " + p1); p1.setName("Morris"); System.out.println("p1: " + p1.getName()); Person p2 = new Person("Ping-Lin"); System.out.println("p2: " + p2); Person p3 = new Person(p1); System.out.println("p3.equals(p1): " + p3.equals(p1)); System.out.println("p3.equals(p2): " + p3.equals(p2)); System.out.println("\nTest Vehicle Class:"); Vehicle v1 = new Vehicle(); System.out.println("v1: " + v1); v1.setManufacturer("Porsche"); v1.setCylinder(4); v1.setOwner(new Person("Dijkstra")); System.out.println("V1's Manufacturer is " + v1.getManufacturer()); System.out.println("V1's Cylinders is " + v1.getCylinder()); System.out.println("V1's Owner is " + v1.getOwner()); Vehicle v2 = new Vehicle("Porsche", 8, new Person("Ping")); System.out.println("v2: " + v2); Vehicle v3 = new Vehicle(v1); System.out.println("v3.equals(v1): " + v3.equals(v1)); System.out.println("v3.equals(v2): " + v3.equals(v2)); System.out.println("\nTest Truck Class:"); Truck t1 = new Truck(); System.out.println("t1: " + t1); t1.setLoadCapacity(10.20); t1.setTowingCapacity(5); System.out.println("t1's load capacity is " + t1.getLoadCapacity()); System.out.println("t1's lowing capacity is " + t1.getTowingCapacity()); Truck t2 = new Truck("NISSAN", 6, new Person("Ping-Lin"), 10.25, 9); System.out.println("t2: " + t2); Truck t3 = new Truck(t1); System.out.println("t3.equals(t1): " + t3.equals(t1)); System.out.println("t3.equals(t2): " + t3.equals(t2)); System.out.println("\nTest \"instanceOf\" and \"getClass()\" difference:"); System.out.println("I use \"instanceOf\" method to write equals(), the answer will be"); Vehicle v4 = new Vehicle(); Truck t4 = new Truck(); System.out.println("t4.equals(v4): "+ t4.equals(v1)); System.out.println("v4.equals(t4): "+ v4.equals(t4)); System.out.println("If \"getClass()\" method to write equals(), the answer will be"); System.out.println("t4.equals(v4): false"); System.out.println("v4.equals(t4): false"); }}
文章定位: