I am trying to figure out this one out. The directions:
Write a class named car in the following fields:
- yearModel - The yearModel field is an int that holds the car's yea model.
- make - The make field references a String object that holds the make of the car.
- speed - The speed field is an int that holds the car's current speed.
In addition, the class should have the following constructor and other methods: - Constructor - The constructor should accept the car's year model and make as arguments. These values should be assigned to the object's yearModel and make fields. The constructor should also assign 0 to speed field. - Accessor (getters). Appropriate getter methods should get the values stored in an object's yearModel, make and speed fields. - accelerate. The accelerate method should add 5 to the speed each time it is called. - brake. The brake method should subtract 5 from the speed field each time it is called.
Demonstrate that class in a program that creates a Car object and then calls the accelerate method five times. After each accelerate method, get the current speed of the car and display it. Then call the brake method five times. After each call to the brake method, get the current speed of the car and display it.
I get a "CarDemo.java:47: reached end of file while parsing" when I compile it the CarDemo.java program.
The code is as follows (Car.java) and (CarDemo.java):
// Car.java
// This is a class called Car with 3 different fields, holds data about a car.
// Programmer: X
// Chapter 6, Programming Challenge #2 Car Class.
// 3/10/2012
public class Car
{
private int yearModel; // The car's year model
private String make; // The car's make
private int speed; // The current speed
Car(int year, String carMake, int newSpeed)
{
yearModel = year;
make = carMake;
speed = newSpeed;
}
public void setYearModel(int y)
{
yearModel = y;
}
public void setMake(String m)
{
make = m;
}
public void setSpeed(int s)
{
speed = s;
}
public int getYearModel()
{
return yearModel;
}
public String getMake()
{
return make;
}
public int getSpeed()
{
return speed;
}
public void accelerate()
{
speed += 5;
}
public void brake()
{
speed -= 5;
}
}
// CarDemo.java
// This is a program called CarDemo that tells the person running the program that the car is going 0 mph,
// then accelerates to 5 mph, then brakes 5mph to 0mph again.
// Programmer: X
// CISC115
// Chapter 6, Programming Challenge #2 Car Class.
// 3/10/2012
public class CarDemo
{
public static void main(String[] args)
{
int s = speed;
int accelerate;
int brake;
String c;
{
Car c = new Car(2012, "Mercedes-Benz S55 AMG");
int s = 0;
s = c.getSpeed();
for(int i = 0; i < 5; i++)
{
System.out.println("The " + c.getYearModel() + " " + c.getMake()
+ "\n is going " + s(accelerate));
System.out.println("Now the " + c.getYearModel() + " " + c.getMake()
+ "\n is going " + s(brake));
}
// other code to accelerate or decelerate
{
Car c = new Car(2012, "Mercedes-Benz S55 AMG");
int s = 0;
s = c.getSpeed();
for(int i = 0; i < 5; i++)
{
System.out.println("The car's current speed is " + s);
}
}