The following are the instructions and code for a Java program I have to complete. I am stuck and do not know how to continue. I'm trying to figure this out. I feel like I have no idea what I'm doing. All help, direction, and explanation would be very much appreciated.
Write a class named
Car
that has the following fields:
yearModel
: TheyearModel
field is an int that holds the car’s year model.
make
: Themake
field references a String object that holds the make of the car.
speed
: Thespeed
field is an int that holds the car’s current speed.In addition, the class should have the following constructor and other methods:
Constructors: One constructor should accept the car's year model, make, and speed as arguments. These values should be assigned to the object's
yearModel
,make
, andspeed
fields. Another constructor will have no arguments and will assign 0 as the car's year model and speed and an empty string ("") as the make.Accessors: Appropriate accessor methods should get the values stored in an object's
yearModel
,make
, andspeed
fields.Mutators: Appropriate mutator methods should store values in an object's
yearModel
,make
, andspeed
fields.
accelerate
: The accelerate method should add 5 to thespeed
field each time it is called.
brake
: The brake method should subtract 5 from thespeed
field each time it is called.Demonstrate the class in a program that asks the user to input data and then creates a
Car
object. It then calls theaccelerate
method five times. After each call to theaccelerate
method, get the currentspeed
of the car and display it. Then call thebrake
method five times. After each call to thebrake
method, get the currentspeed
of the car and display it.The output from running this program will appear similar to:
Enter the car's year model: 1965 Enter the car's make: Mustang Enter the car's speed: 30 Current status of the car: Year model: 1965 Make: Mustang Speed: 30 Accelerating... Now the speed is 35 Accelerating... Now the speed is 40 Accelerating... Now the speed is 45 Accelerating... Now the speed is 50 Accelerating... Now the speed is 55 Braking... Now the speed is 50 Braking... Now the speed is 45 Braking... Now the speed is 40 Braking... Now the speed is 35 Braking... Now the speed is 30
This is what I have so far:
public class Car {
// Declaration of variables.
private int yearModel;
private String make;
private int speed;
// Constructor that accepts arguements.
public static void acceptor(int yearModelIn, String makeIn, int speedIn){
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter the car's year model: ");
yearModelIn = keyboard.nextInt();
System.out.println("Enter the car's make: ");
makeIn = keyboard.next();
System.out.println("Enter the car's speed: ");
speedIn = keyboard.nextInt();
}
// Constructor that zeroes fields.
public void zeroer()
{
yearModel = 0;
speed = 0;
make = ("");
}
// Accessor Methods
public int getYearModel()
{
return yearModel;
}
public String getMake()
{
return make;
}
public int getSpeed()
{
return speed;
}
// Accelerate method for adding 5 to speed.
public void Accelerate()
{
speed += 5;
}
// Brake method for reducing speed.
public void Brake()
{
speed-=5;
}
First, get rid of the acceptor
method, it's not doing what you think it should...I'd probably drop the zeroer
method as well, as it doesn't provide any useful functionality, other than to screw you up
Constructors. One constructor should accept the car’s year model, make, and speed as arguments. These values should be assigned to the object’s yearModel, make, and speed fields. Another constructor will have no arguments and will assign 0 as the car’s year model and speed and an empty string (“”) as the make.
To start with, you're missing this...
public Car(int yearModel, String make, int speed) {
this.yearModel = yearModel;
this.make = make;
this.speed = speed;
}
From this, you can create an instance of the car...
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter the car's year model: ");
int year = keyboard.nextInt();
keyboard.nextLine();
System.out.println("Enter the car's make: ");
String make = keyboard.nextLine();
System.out.println("Enter the car's speed: ");
int speedIn = keyboard.nextInt();
Car car = new Car(year, make, speedIn);
}
Then, all you need to do is call the appropriate methods to change and report the state, for example...
car.Accelerate();
System.out.println(car.getSpeed());
Consult your notes and the tutorials when you're stuck, for example Providing Constructors for Your Classes and Passing Information to a Method or a Constructor
You might also like to have a read through Code Conventions for the Java TM Programming Language, it will make it easier for people to read your code and for you to read others