• Post Reply Bookmark Topic Watch Topic
  • New Topic

Write a class that represents a car object.  RSS feed

 
Karen Barlow
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I was hoping that someone may have some insight for me on the following question.

Write a class that represents a car object. A Car has the following data fields:
• yearModel – The model year of the car
• make – The manufacturer of the car
• speed – The speed the car is currently travelling
A Car should have the following as well:
• A Constructor that takes as arguments the car’s year and make. The speed of the care should be set to 0.
• Accessors – there should be an appropriate accessor method for each of the data fields.
• Mutators – there should be an accelerate method and a brake method. Each of these methods adds or subtracts 5
from the speed of the car respectively. Note: The speed of the car should never be less than 0 and never more than
210 (assume km/h). Your methods should make sure these limits are respected.
• ***Write JavaDoc comments for this class.***
Write a tester class that:
• Prompts the user for the make and model of the Car
• Constructs a Car with the input given
• Accelerate once and show the speed of the Car
• Use a loop to accelerate 5 (or better yet a random number of) times and show the speed of the Car.
• Use a loop to decelerate until the Car is stopped. Display the number of times the brakes were applied.
The following output is what is expected.

Please enter the make and model of the car: Tiberon 2006
New car created! Tiberon, 2006
The current speed is 5.
The current speed is 30.
The current speed is 0, and the brakes were applied 6 times.


The work that I have done so far is as follows:
class code:


Code tester:

The output after it runs is as follows:

Please enter the make and model of the car: Tiberon 2006
New car created! Tiberon, 2006
The current speed is 5
The current speed is 10
The current speed is 15
The current speed is 20
The current speed is 25
The current speed is 20
The current speed is 15
The current speed is 10
The current speed is 5
The current speed is 0
The current speed is 0, and the brakes were applied 6 times.

The question I have is how do I get rid of the line that is bolded in the output, the "The current speed is 0" line.

If anyone has a simple quick fix as soon as possible it would be greatly appreciated, as this is due in less than 3 hrs from now...lol..
 
Partheban Udayakumar
Ranch Hand
Posts: 499
AngularJS Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Karen Barlow,

Change this condition

to


I have a small doubt. You are receiving the input from the user but you are not feeding it to car object. In that case, no matter what the input may be, you will always get the output as

New car created! Tiberon, 2006

because you have initialized the object with the value. Is this really what you want?
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Start by indenting all your code. A getter method squashed into a single line is illegible. And there is no reason so to do because you can scroll and there is no limit to the size of your keyboard. You are rsiking confusing yourself dangerously with your inconsistent indentation.

Why have you got setXXX methods when you have not been told to do so? The car is not going to change its make ever, is it? You have methods to speed up and slow down, but I do n't think you should call them mutators. You should only call setXXX methods mutators.
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. A bit weird looks no argument constructor which constructs sort of default car, probably it shouldn't be like that. At least requirements doesn't ask you do in that way.

2. speed += 5 it looks nice, but it could be dangerous and cause harm one day. speed = speed + 5 is safer. I know this because I was instructed by Campbell Ritchie to read the book Java Puzzlers by Joshua Bloch. I shall leave you to find out why and when it could cause a harm.

3. You got some comments in your code, but these are not considered as JavaDoc comments. Please double check how these should be looking like. You can check this.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think
speed += 5;
will work all right. You are applying the operator to an int, so there will be no narrowing conversions and the right operator is an int too.
Suggest you confine the compound assignment operators to cases where …
  • 1: Both operands (left and right) are the same type.
  • and 2: Both operands' types occupy at least 32 bits.
  • That way there will be no risk of confusion. You can easily confuse yourself if you write
    speed += 5.9999999999;
    Liutauras will know exactly what that will do.

    Edit: added no before risk.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!