Question:
car Class java issues?
Reave
2009-11-08 11:39:46 UTC
My assignment is write a class that has the following fields: - yearModel. The yearModel field is an int that holds the car's year model - make. The make field references a String object that holds the make of the car. - speed. The speed field in 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. The problem with my program below is, I don't think its using the accelerate of brake correctly. How can I fix this. Thanks

public class Car
{
private int yearModel; // The car's year model
private String make; // The car's make
private String speed; // The current speed


Car(int year, String carMake, String newSpeed)

{
yearModel = year;
make = carMake;
speed = newSpeed;
}


public void setYearModel(int y)
{
yearModel = y;
}



public void setMake(String m)
{
make = m;
}



public void setSpeed(String s)
{
speed = s;
}


public int getYearModel()
{
return yearModel;
}


public String getMake()
{
return make;
}


public String getSpeed()
{
return speed;
}


public void accelerate()
{
speed += 5;
}


public void brake()
{
speed -= 5;
}
}







public class carTest
{
public static void main(String[] args)
{


Car box = new Car( 1975, "Honda", "67");
Car box1 = new Car( 2004,"Toyota", "71");
Car box2 = new Car( 1989, "Ford", "100");

System.out.println(" yearModel Make Speed ");
System.out.println("_____________________________________________");
System.out.print(" " + box.getYearModel() + " " + box.getMake() + " " + box.getSpeed() + "\n");
System.out.print(" " + box1.getYearModel() + " " + box1.getMake() + " " + box1.getSpeed() + "\n");
System.out.print(" " + box2.getYearModel() + " " + box2.getMake() + " " + box2.getSpeed());

}
}
Four answers:
Mark aka jack573
2009-11-12 09:06:20 UTC
"The speed field in an int that holds the car's current speed"

private String speed; // The current speed

so this should be

private int speed; // The current speed





"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."

Car(int year, String carMake, String newSpeed)

{

yearModel = year;

make = carMake;

speed = newSpeed;

}

This should be:

Car(int year, String carMake)

{

yearModel = year;

make = carMake;

speed = 0;

}





If you change them, then your accelerate and brake methods are correct.





Then in your carTest class, and the main mehod, you are doing this:

Car box = new Car( 1975, "Honda", "67");

Car box1 = new Car( 2004,"Toyota", "71");

Car box2 = new Car( 1989, "Ford", "100");

it should be:

Car box = new Car( 1975, "Honda");

Car box1 = new Car( 2004,"Toyota");

Car box2 = new Car( 1989, "Ford");



You also need to do this:

"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."



So, you should just make 1 car, do a loop for 5 times. In this loop, call the accelerate method and print out the speed.

Then do another loop for 5 times, calling the brake and printing out the speed.



Good luck with it.
anonymous
2015-08-07 09:37:03 UTC
This Site Might Help You.



RE:

car Class java issues?

My assignment is write a class that has the following fields: - yearModel. The yearModel field is an int that holds the car's year model - make. The make field references a String object that holds the make of the car. - speed. The speed field in an int that holds the car's current speed....
Silent
2009-11-08 11:52:38 UTC
Your constructor does not work the way the assignment specifies. The assignment tells you that the constructor needs to set the speed to 0, but you're setting it to a parameter values.



Your test class does not call the accelerate or brake methods as the assignment specifies. These methods do seem to be implemented correctly, though.
The Phlebob
2009-11-08 11:50:26 UTC
I don't see it calling the accelerate() method at all. You have to call it explicitly.



Hope that helps.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...