I have my program that does what it needs to but I want it to be able to do one more thing and i can't seem to figure out how. This program ask what make,year,speed of a car, I also have where you can have the car accelerate or brake. The program works if you accelerate or brake once. I want to be able to accelerate or break 5 times then show the speed. Can somebody point me into the right direction? Thanks for the help!
Code:import java.util.Scanner; public class Car{ Scanner kb = new Scanner(System.in); private int yearModel; private String make; private int speed; private int acc; private int count = 0; public Car() { this.yearModel = yearModel; this.make = make; this.speed = speed; } public void SetMake(){ this.make = kb.next(); } public void SetYearModel(){ this.yearModel = kb.nextInt(); } public void SetSpeed(){ this.speed = kb.nextInt(); } public void Accel() { this.acc = kb.nextInt(); while (count <= 5) count++; { if (this.acc == 5) { this.speed = speed + 5; } else if (this.acc == -5) { this.speed = speed - 5; } else if (this.acc == 0) { this.speed = speed; } else { System.out.println("Goodbye"); System.exit(0); } } } // public String getMake() { // return make; // // } // public void setMake(String make) { // this.make = make; // } // public int getSpeed() { // return speed; // } // public void setSpeed(int speed) { // this.speed = speed; // } // public int getYearModel() { // return yearModel; // } // public void setYearModel(int yearModel) { // this.yearModel = yearModel; // } @Override public String toString() { return "Car {" + "Year Model: " + yearModel + ", Make: " + make + ", Speed=" + speed + '}'; } }
Code:public class CarDemo { public static void main(String[] args) { Car car1 = new Car(); System.out.println("Enter Make:\t"); car1.SetMake(); System.out.println("Enter Year:\t"); car1.SetYearModel(); System.out.println("Enter Speed:\t"); car1.SetSpeed(); System.out.println("Enter 5 to accelerate Vehicle or Enter -5 to brake by 5 mph"); System.out.println("Enter Acceleration:\t"); car1.Accel(); System.out.println(car1);




Reply With Quote