INSTANCE CHALLENGE

 Challenge.

 start with a base class of a Vehicle, then create a Car class that inherits from this base class.
 Finally, create another class, a specific type of Car that inherits from the Car class.
 you should be able to handle steering, change gears, and moving (speed in other words).
 you will want to decide where to put the appropriate state and behaviors (fields and method).
 as mentioned above, changing gears, increasing or decreasing speed should be included.
 for your specific type of vehicle, you will want to add something specifically for that type of car.

Soln:

main

package com.cnc;

public class Main {

public static void main(String[] args) {
BMW bmw = new BMW(36);
bmw.accelerate(30);
}
}

vehicle

package com.cnc;

public class Vehicle {
private String name;
private String size;
private int currentVelocity;
private int currentDirection;
public Vehicle(String name, String size) {
this.name = name;
this.size = size;
this.currentVelocity = 0;
this.currentDirection = 0;
}
public void steer(int direction){
this.currentDirection += direction;
System.out.println("Vehicle.steer() : steering at "+ currentDirection +"degrees");
}
public void move(int velocilty, int direction){
currentVelocity = velocilty;
currentDirection=direction;
System.out.println("Vehicle.move() : Moving at "+currentVelocity +" in direction "+currentDirection);
}

public String getName() {
return name;
}

public String getSize() {
return size;
}

public int getCurrentVelocity() {
return currentVelocity;
}

public int getCurrentDirection() {
return currentDirection;
}
public void stop(){
this.currentVelocity = 0;
}

}


car

package com.cnc;

public class Car extends Vehicle{
private int wheels;
private int doors;
private int gears;
private boolean isManual;

private int currentGear;

public Car(String name, String size, int wheels, int doors, int gears, boolean isManual) {
super(name, size);
this.wheels = wheels;
this.doors = doors;
this.gears = gears;
this.isManual = isManual;
this.currentGear = currentGear;
}
public void changeGear(int currentGear){
this.currentGear = currentGear;
System.out.println("Car.setCurrentGear() : Changed to "+ this.currentGear + "gear");
}
public void changeVelocity(int speed, int direction){
move(speed,direction);
System.out.println("Car.changeVelocity() : velocity "+speed+ " directon"+ direction);
}


}


B.M.W

package com.cnc;

public class BMW extends Car{
private int roadServiceMonth;

public BMW(int roadServiceMonth) {
super("B.M.W", "XUD", 5, 5, 6, false);
this.roadServiceMonth = roadServiceMonth;
}
public void accelerate(int rate){
int newVelocity = getCurrentVelocity() + rate;
if(newVelocity == 0){
stop();
changeGear(1);
}else if(newVelocity >0 && newVelocity<=10){
changeGear(1);
}else if(newVelocity >10 && newVelocity<=20){
changeGear(2);
}else if(newVelocity >20 && newVelocity<=30){
changeGear(3);
}else {
changeGear(4);
}
if (newVelocity>0){
changeVelocity(newVelocity,getCurrentDirection());
}
}
}

OUTCOME:

Car.setCurrentGear() : Changed to 3gear
Vehicle.move() : Moving at 30 in direction 0
Car.changeVelocity() : velocity 30 directon0


v

Popular posts from this blog

NUMBER PALINDROME CHALLENGE

ENCAPSULATION CHALLENGE

USER INPUT PRINT ORIGINAL,REVERSE ARRAY AND SORTED ARRAY