Hello, I need some help writing 2 classes for a car program. Honestly, I'm kinda lost. Any help would be appreciated. Here's the outline that I was given:
Fields: yearModel - an integer that holds the car's year model make - a string that holds the make of the car speed - an integer that holds the car's current speed
//constructor the constructor should accept the car's year model and make an arguments. these values should be assigned to the object's yearModel and make fields. The constructor should also assign 0 to the speed field.
Make appropriate accessor methods to get the values and store them in an object's yearModel, make, and speed fields.
*now this is the part i'm truely lost with*
accelerate. the accelerate method should add 5 to the speed field each time it is called
brake. should subtract 5 every time it is called
Demonstrate the class in a program that creates a Car object, and then calls the accelerate method 5 times. After each call to the accelerate method, get the current speed of the car and display it. Then, call the brake method 5 times. After each call to the brake method, get the current speed of the car and display it. ------------------- I think i have the first half okay- this is what i have:
public class Car { public int yearModel; public String make; public int speed;
public Car() { yearModel=0; speed=0; make="2004 Toyota Echo"; }
public Car(int yearModel, String make, int speed) { this.yearModel=yearModel; this.make=make; }
public int getYear() { return yearModel; }
public String getMake() { return make; }
public int getSpeed() { return speed }
public int accelerate() { speed=speed+5; return speed; }
public int break() { speed=speed-5; return speed; }
public void setYear(int yearValue) { this.yearModel=yearValue }
public void setMake(String make) { this.make=make; }
//not sure how to display twice - telling the person using the program that the car is going 0 mph, then accelerates to 5, then brakes 5mph to 0mph again
public String toString() { return "The "+yearModel + make + " is currently going " + speed + "mph."; }
public static void main(String[]args) { Car car1 = new Car(yearModel, make, speed); } }//end class Car
Thank you for your help. :) I am getting one error message I don't understand:
Car.java --
public class Car { public int yearModel; public String make; public int speed;
public Car() { yearModel=0; speed=0; make="2004 Toyota Echo"; }
public Car(int yearModel, String make, int speed) { this.yearModel=yearModel; this.make=make; }
public int getYear() { return yearModel; }
public String getMake() { return make; }
public int getSpeed() { return speed }
public int accelerate() { speed=speed+5; return speed; }
public int brake() { speed=speed-5; return speed; }
public void setYear(int yearValue) { this.yearModel=yearValue }
public void setMake(String make) { this.make=make; }
}//end class Car
and I have CarDemo.java --
//This is a CarDemo Class
public class CarDemo
{ //Begin Class
public static void main (String args [])
{ //Begin Main Method //not sure how to display twice - telling the person using the program that the car is going 0 mph, then accelerates to 5, then brakes 5mph to 0mph again
create two peddles,one for acceleration and one for braking.when the accelerator is pressed,the wheel is speed up inrotation and when the break is pressed the wheels declerate and stop the peddles should work n pressing a and b keys on keyboard for acceleration and break respectively.when a key s pressed,it should lead to corresponding view on the two paddles