Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Further develop the classes Vehicle, Car, Bicycle and TestVehicles to include followin g: ( Codes for classes are found in the bottom of post) Vehicle:

Further develop the classes Vehicle, Car, Bicycle and TestVehicles to include following: ( Codes for classes are found in the bottom of post) Vehicle:

- Add new datamember, CalendarBuyingDate which specifies when vehicle is bought ( current date in constructor for simplicity).

- Implement Comparable and Clonable interfaces in Vehicle class. Comparable should sort vehicles by price.

- Cloneable should call super.clone() to get a shallow copy of the object, and clone all "mutable" objects the class contains to get a deep copy.

(Mutable objects are objects wh can be changed without it beeing created a new copy. String is immutable, while Calendar is mutable.)

Car and Bicycle:

- Create your own version of clone method to make a deep copy.

TestVehicles:

- Should be given an extra choice, "Test clone method" to test the clone method. This test can be hardcoded, and should create new car, make a copy of it, and be able to edit date on one of the objects without affecting the other. Print both car object with date as verification.

UML diagram: image text in transcribed

Output example:

image text in transcribed

Car class

public class Car extends Vehicle{

private int power;

private Calendar productionDate;

public Car(){

productionDate = Calendar.getInstance();

}

public Car(String name, String color, int price, int model, String serialNr, int direction, int power){

super(name, color, price, model, serialNr, direction);

this.power = power;

productionDate = Calendar.getInstance();

}

public void setAllFields(){

super.setAllFields();

System.out.printf("Power:");

this.setPower(input.nextInt());

}

public void turnRight(int degrees){

if(degrees 0) {

this.setDirection(this.getDirection() + degrees);

if(this.getDirection()

this.setDirection(this.getDirection() + 360);

}

else if(this.getDirection() > 360){

this.setDirection(this.getDirection() - 360);

}}

}

public void turnLeft(int degrees){

if(degrees 0) {

this.setDirection(this.getDirection() - degrees);

if(this.getDirection()

this.setDirection(this.getDirection() + 360);

}

else if(this.getDirection() > 360){

this.setDirection(this.getDirection() - 360);

}}

}

public int getPower() {

return power;

}

public Calendar getProductionDate() {

return productionDate;

}

public void setPower(int power) {

this.power = power;

}

public void setProductionDate(Calendar productionDate) {

this.productionDate = productionDate;

}

public String toString(){

return String.format("%s Power: %d Production date: %s", super.toString(), this.getPower(), sdf.format(productionDate.getTime()));

}

}

Bicycle Class

public class Bicycle extends Vehicle{ private int gears; private Calendar productionDate;

public Bicycle(){ productionDate = Calendar.getInstance(); } public Bicycle(String name, String color, int price, int model, String serialNr, int direction, int gears){

super(name, color, price, model, serialNr, direction); this.gears = gears; productionDate = Calendar.getInstance(); }

public void setAllFields(){ super.setAllFields(); System.out.printf("Gears: ");

this.setGears(input.nextInt()); }

public void turnRight(int degrees){ System.out.println(degrees); }

public void turnLeft(int degrees){ System.out.println(degrees); }

public int getGears() { return gears; }

public Calendar getProductionDate() { return productionDate; }

public void setGears(int gears) { this.gears = gears; }

public void setProductionDate(Calendar productionDate) { this.productionDate = productionDate; }

public String toString(){ return String.format("%s Gears: %d Production date: %s", super.toString(), this.getGears(), sdf.format(productionDate.getTime())); } }

Vehicle Class

public abstract class Vehicle {

private String colour, name, serialNr;

private int model, price, direction = 0;

private double speed;

protected Scanner input;

public static final DateFormat sdf = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");

public Vehicle(){

input = new Scanner(System.in);

}

public Vehicle(String name, String color, int price, int model, String serialNr, int direction){

this.name = name;

this.colour = color;

this.serialNr = serialNr;

this.model = model;

this.price = price;

this.direction = direction;

input = new Scanner(System.in);

}

public void setAllFields(){

System.out.printf("Name: ");

this.setName(input.nextLine());

System.out.printf("Colour: ");

this.setColour(input.nextLine());

System.out.printf("Serial #: ");

this.setSerialNr(input.nextLine());

System.out.printf("Price: ");

this.setPrice(input.nextInt());

System.out.printf("Model: ");

this.setModel(input.nextInt());

}

public abstract void turnLeft(int degrees);

public abstract void turnRight(int degrees);

public String getColour() {

return colour;

}

public String getName() {

return name;

}

public String getSerialNr() {

return serialNr;

}

public int getModel() {

return model;

}

public int getPrice() {

return price;

}

public int getDirection() {

return direction;

}

public double getSpeed() {

return speed;

}

public void setColour(String colour) {

this.colour = colour;

}

public void setName(String name) {

this.name = name;

}

public void setSerialNr(String serialNr) {

this.serialNr = serialNr;

}

public void setModel(int model) {

this.model = model;

}

public void setPrice(int price) {

this.price = price;

}

public void setDirection(int direction) {

this.direction = direction;

}

public void setSpeed(double speed) {

this.speed = speed;

}

public String toString() {

return String.format("Name: %s Colour: %s Price: %d Model: %d Serial#: %s Direction: %d Speed: %.2f", this.getName(), this.getColour(), this.getPrice(), this.getModel(), this.getSerialNr(), this.getDirection(), this.getSpeed());

}

}

TestVehicles Class

public class TestVehicle {

ArrayList vehicles = new ArrayList();

public static void main(String[] args) {

TestVehicle vtest = new TestVehicle();

try {

vtest.menuLoop();

} catch (InputMismatchException e) {

System.out.println("InputMismatchException!");

System.out.println(e.getMessage());

System.exit(1);

}

}

private void menuLoop() throws InputMismatchException {

Scanner input = new Scanner(System.in);

Vehicle vehicle;

vehicles.add(new Car("Volvo 740", "blue", 85000, 1985, "1010-11", 0, 120));

vehicles.add(new Car("Ferrari Testarossa", "red", 1200000, 1996, "A112", 0, 350));

vehicles.add(new Bicycle("Monark 1", "yellow", 4000, 1993, "BC100", 0, 10));

vehicles.add(new Bicycle("DBS 2", "pink", 5000, 1994, "42", 0, 10));

while (true) {

System.out.println("1...................................New car");

System.out.println("2...............................New bicycle");

System.out.println("3......................Find vehicle by name");

System.out.println("4..............Show data about all vehicles");

System.out.println("5.......Change direction of a given vehicle");

System.out.println("6..............................Exit program");

System.out.print(".............................Your choice? ");

int choice = input.nextInt();

switch (choice) {

case 1:

System.out.println();

System.out.println("Input car data:");

vehicle = new Car();

vehicle.setAllFields();

vehicles.add(vehicle);

System.out.println();

break;

case 2:

System.out.println();

System.out.println("Input bicycle data:");

vehicle = new Bicycle();

vehicle.setAllFields();

vehicles.add(vehicle);

System.out.println();

break;

case 3:

System.out.println();

System.out.printf("Name of vehicle: ");

input.nextLine();

String vehicleName = input.nextLine();

for(int i = 0; i

Vehicle V = vehicles.get(i);

if(V.getName().equals(vehicleName)) {

System.out.printf(V.toString());

}

}

System.out.println();

System.out.println();

break;

case 4:

System.out.println();

for

(int i = 0; i

Vehicle V = vehicles.get(i);

System.out.printf("%s ", V.toString());

}

System.out.println();

break;

case 5:

System.out.println();

System.out.printf("Name of vehicle: ");

input.nextLine();

String vehicleName2 = input.nextLine();

for

(int i = 0; i

Vehicle V = vehicles.get(i);

if(V.getName().equals(vehicleName2)) {

System.out.printf("Direction [R/L]: ");

String directionString = input.nextLine();

System.out.printf("Degrees [0-360]: ");

int degreesToTurn = input.nextInt();

if(directionString.equals("R"))

V.turnRight(degreesToTurn);

else

V.turnLeft(degreesToTurn);

}}

System.out.println();

break;

case 6:

input.close();

System.exit(0);

default:System.out.println("Invalid option!");

}}

}

}

Vehicle ?. Cloneable o colour Oa name O a serialNr a model a price String String String 0 Comparable compareTo(T) int int int Int double Calendar Scanner direction O speed G Bicycle 0 gears O productionDate O Bicycle) Bicycle(String, String, int, int, String, int, int) buyingDate input Vehicle() Vehicle(String, String, int, int, String, int) SetAlIFields G Car O a power 0 a productionDate int Calendar int Calendar void vold void String String Int int String int ?'a turnLeft(int) Car(String, String, int, int, String, int, int) turnRight(int) getName() getcolouro void vold vold int Calendar vold vold setAllFields() turnRight(int) SetAlIFields0 OturnRight(int) void O turnLeft(int) turnLeft(int) O getPower) O getProductionDate) O setPower(int) DgetPrice) vold getGears) getProductionDate) getModel() getSerialNro Calendar O setGears(int) O setProductionDate(Calendar) DgetDirection) vold setProductionDate(Calendar) clone) getspeed) getBuyingDate) setName(String) setColour(String) setPrice(int) setModel(int) setSerialNr(String) setDirection(int) setspeed(double) setBuyingDate(Calendar) compareTo(Vehicle) clone0 toString double cloneo toStringo Object String String void void void vold vold void void void int Object String String O toString) Powered by yFiles

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases Illuminated

Authors: Catherine M Ricardo, Susan D Urban

3rd Edition

1284056945, 9781284056945

Students also viewed these Databases questions

Question

How would the optimal bid change if you had 12 competitors?

Answered: 1 week ago

Question

=+a) Write the null and alternative hypotheses.

Answered: 1 week ago