Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I na addition to the below java code: The program should be developed gradually and in the second part you will: Vehicle: Add new data

Ina addition to the below java code: The program should be developed gradually and in the second part you will: Vehicle: Add new data member, Calendar buyingDate, who states when the vehicle was purchased (set it to the current date in the designer for simplicity). Implement Comparable and Cloneable Interfaces in the Vehicle Class. Comparable should compare vehicles by price. Cloneable should call super.clone () to get a shallow copy of the object and then it must clone all "mutable" objects the class contains to get a deep copy. Mutable objects are objects that can be changed without creating a new copy of them, String is immutable, while Calendar is mutable. TestVehicles: Must get an additional choice "Test clone method" to test the clone method. This test can be hard-coded, it must create a new car, make a copy of it and then change the date on one car object without changing the date in the other object. Print both car items with date fields as verification.

This are Java code:

abstract class Vehicle {

private String color, name, serialNumber; private int model,price, direction; private double speed; protected java.util.Scanner input; public Vehicle() { color = ""; name = ""; serialNumber = ""; model = 0; price = 0; direction = 0; speed = 0; } public Vehicle(String color,String name,String serialNumber,int model,int price,int direction) { this.color = color; this.name= name; this.serialNumber = serialNumber; this.model = model; this.price = price; this.direction = direction; speed = 0; } public void setAllFields () { System.out.println("Enter the color of vehicle : "); color = input.next(); System.out.println("Enter the name of vehicle : "); name = input.next(); System.out.println("Enter the serial number of vehicle : "); serialNumber = input.next(); System.out.println("Enter the model of vehicle : "); int model = input.nextInt(); System.out.println("Enter the price of vehicle : "); int price = input.nextInt(); direction = 0; speed = 0; } public abstract void turnLeft (int degrees); public abstract void turnRight (int degrees); //get and set methods for all members. public void setName(String name) { this.name = name; } public String getName() { return name; } public void setColor(String color) { this.color = color; } public String getColor() { return color; } public void setSerialNumber(String serialNumber) { this.serialNumber= serialNumber; } public String getSerialNumber() { return serialNumber; } public void setPrice(int price) { this.price = price; } public int getPrice() { return price; } public void setModel(int model) { this.model = model; } public double getSpeed() { return speed; } public int getDirection() { return direction; } public void setDirection(int direction) { this.direction = direction; } public String toString () { return "Vehicle name : "+name +" color : "+color +" Serial# : "+serialNumber +" Model : "+model +" Price : "+price+" Direction : "+direction +" Speed : "+speed; } } class Car extends Vehicle { private int power; public Car(){} public Car(String color,String name,String serialNumber,int model,int price,int direction,int power) { super(color,name,serialNumber,model,price,direction); this.power = power; } public void setAllFields () { super.setAllFields(); System.out.println("Enter the horse power of car : "); power = input.nextInt(); } public void turnRight (int degrees) { if(degrees >=0 && degrees <= 360) setDirection(degrees); else if(degrees > 360) setDirection(degrees - 360); } public void turnLeft (int degrees) { if(degrees >=0 && degrees <= 360) setDirection(degrees); if(degrees > 360) setDirection(degrees - 360); } public int getPower() { return power; } public void setPower(int power) { this.power = power; } public String toString() { return super.toString() + " Power : "+power; } } class Bicycle extends Vehicle { private int gears; public Bicycle(){} public Bicycle(String color,String name,String serialNumber,int model,int price,int direction,int gears) { super(color,name,serialNumber,model,price,direction); this.gears = gears; } public void setAllFields () { super.setAllFields(); System.out.println("Enter the number of gears of bicycle : "); gears = input.nextInt(); } public void turnRight (int degrees) { System.out.println("Bike has turned "+degrees+" degrees right"); } public void turnLeft (int degrees) { System.out.println("Bike has turned "+degrees+" degrees left"); } public int getGears() { return gears; } public void setGears(int power) { this.gears = gears; } public String toString() { return super.toString() + " Number of gears : "+gears; } } class TestVehicles { public static void main (String[] args) { Car c = new Car("blue","Hyundai","768787-55",2009,7787,34,300); c.turnLeft(390); System.out.println(c); Bicycle b = new Bicycle("black","Atlas","7111-523",2009,287,22,2); b.turnRight(75); System.out.println(b); } }

Output:

Vehicle name : Hyundai color : blue Serial# : 768787-55 Model : 2009 Price : 7787 Direction : 30 Speed : 0.0 Power : 300 Bike has turned 75 degrees right Vehicle name : Atlas color : black Serial# : 7111-523 Model : 2009 Price : 287 Direction : 22 Speed : 0.0 Number of gears : 2

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

Database Processing Fundamentals, Design, and Implementation

Authors: David M. Kroenke, David J. Auer

14th edition

133876705, 9781292107639, 1292107634, 978-0133876703

More Books

Students also viewed these Databases questions

Question

3-24. Was the message well timed?

Answered: 1 week ago