I need to make the following (X and Y are only examples):
I have an X class. In that class I have objects, what I made. I have an another (Y) class, what extends the X class and implements Cloneable. In the X class, which objects I made, need to put them in an array. Then I need to clone it to the Y class. How can I do it?
Here is the code:
(X) class
import java.util.Scanner;
class Vehicle {
static int speed;
double direction;
protected String ownerName;
static int ID_number = 0;
int initID_number;
int ID;
Scanner s = new Scanner (System.in);
private final static String TURN_LEFT = "left";
private final static String TURN_RIGHT = "right";
Vehicle() {
ID = ID_number++;
}
public Vehicle(String initOwner) {
ownerName = initOwner;
}
public static void highestID() {
System.out.println("The highest ID is: " + ID_number);
}
public static void changeSpeed(int speedNow) {
speed = speedNow;
}
public static void stop(){
speed = 0;
}
public static void turn(int degree) {
System.out.println("The car turn in "+ degree);
}
public static void turn(String turn) {
if (turn == TURN_LEFT) {
System.out.println("The car turn left");
}
else if (turn == TURN_RIGHT) {
System.out.println("The car turn right");
}
}
public static void main(String[] args) {
Vehicle Alfa = new Vehicle();
Alfa.speed = 120;
Alfa.direction = 12.11;
Alfa.ownerName = "Alfa";
Alfa.initID_number = 1;
Vehicle BMW = new Vehicle();
BMW.speed = 100;
BMW.direction = 12.1411;
BMW.ownerName = "BMW";
BMW.initID_number = 2;
Vehicle Chrysler = new Vehicle();
Chrysler.speed = 90;
Chrysler.direction = 131.1210;
Chrysler.ownerName = "Chyrsler";
Chrysler.initID_number = 3;
System.out.println("Speed: " + Alfa.speed);
System.out.println("direction: " + Alfa.direction);
System.out.println("ownerName: " + Alfa.ownerName);
System.out.println("ID_number: " + Alfa.ID);
System.out.println("initID_Number: " + Alfa.initID_number);
System.out.println();
System.out.println("Speed: " + BMW.speed);
System.out.println("direction: " + BMW.direction);
System.out.println("ownerName: " + BMW.ownerName);
System.out.println("ID_number: " + BMW.ID);
System.out.println("initID_Number: " + BMW.initID_number);
System.out.println();
System.out.println("Speed: " + Chrysler.speed);
System.out.println("direction: " + Chrysler.direction);
System.out.println("ownerName: " + Chrysler.ownerName);
System.out.println("ID_number: " + Chrysler.ID);
System.out.println("initID_Number: " + Chrysler.initID_number);
Vehicle Dacia = new Vehicle("Dacia");
System.out.println();
System.out.println("ownerName: " + Dacia.ownerName);
highestID();
changeSpeed(50);
stop();
turn(50);
turn("right");
}
}
(Y) class
class Garage extends Vehicle implements Cloneable {
// need to make here the clone of the Vehicle's class array
}