I am trying to fulfil the requirements of the homework... "Include at least one interface that contains at least one method that implementing classes must implement."
When I try to instantiate the interface it says it cannot instantiate the interface. I am not sure what I am doing wrong.
I have tried several ways to make it work.
//main class
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
Ford mustang = new Ford("Mustang", 135, 125);
Chevrolet camero = new Chevrolet("Camero", 202, 100);
Dodge challenger = new Dodge("Challenger", 203, 75);
Nitrous nitro = new Nitrous();//problem code
mustang.start();
camero.start();
challenger.start();
}
}
//Abstract class
public abstract class Vehicle extends Thread implements Nitrous {
private String model;
private int speed;
private int boost;
public Vehicle(String model, int speed, int boost) {
this.model = model;
this.speed = speed;
this.boost = boost;
}
public String getmodel() {
return model;
}
public void setmodel(String model) {
this.model = model;
}
public int getspeed() {
return speed;
}
public void setspeed(int speed) {
this.speed = speed;
}
public int getboost() {
return boost;
}
public void setboost(int boost) {
this.boost = boost;
}
@Override
public void run() {
try {
go();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private void go() throws InterruptedException {
int trackLength = 5000;
int checkPointPassed = 0;
for (int i = 0; i < trackLength; i += (speed + boost)) {
if (checkPointPassed * 1000 < i) {
checkPointPassed++;
System.out.println("The " + this.model + " has passed check point " + checkPointPassed);
// System.out.println(nos);
Thread.sleep(10);
}
}
}
}
//subclass one of three
public class Ford extends Vehicle {
public Ford (String model, int speed, int boost) {
super(model, speed, boost);
}
@Override
public void nos() {
// TODO Auto-generated method stub
System.out.println("The cars have Nitro!");
}
}
public class Chevrolet extends Vehicle{
public Chevrolet(String model, int speed, int boost) {
// TODO Auto-generated constructor stub.
super(model, speed, boost);
}
@Override
public void nos() {
// TODO Auto-generated method stub
System.out.println("The cars have Nitro!");
}
}
public class Dodge extends Vehicle{
public Dodge(String model, int speed, int boost) {
// TODO Auto-generated constructor stub
super(model, speed, boost);
}
@Override
public void nos() {
// TODO Auto-generated method stub
System.out.println("The cars have Nitro!");
}
}
//Interface
public interface Nitrous {
public void nos();
}
It is a race with three vehicles that have a nitrous boost. I have chosen to make the Nitrous the interface. You can see in my code where I have tried different ways to make it work and none have been successful. I don't even know if I am close or way off with how to do this.