• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Write a class that represents a car object.

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..
 
Ranch Hand
Posts: 499
Spring AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Marshal
Posts: 76862
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 8523
615
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 76862
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
     
    Greenhorn
    Posts: 24
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    In order to actually change the instance variable one would need to the following (in CarTester):
    "
    System.out.print("Please enter the make: ");
    car.setMake(keyb.nextLine());
    System.out.print("Please enter model year of the car: ");
    car.setYearModel(Integer.parseInt(keyb.nextLine()));
    System.out.println("New car created! " + car.getMake() + ", " + car.getYearModel());
    "

    instead of:
    "
    System.out.print("Please enter the make and model of the car: ");
    String m = keyb.nextLine();
    System.out.println("New car created! " + car.getMake() + ", " + car.getYearModel());
    "
    -Lazarus Wald
     
    Campbell Ritchie
    Marshal
    Posts: 76862
    366
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Welcome to the Ranch
     
    Lazarus Wald
    Greenhorn
    Posts: 24
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:Welcome to the Ranch



    Thanks a lot!
     
    Arch enemy? I mean, I don't like you, but I don't think you qualify as "arch enemy". Here, try this tiny ad:
    the value of filler advertising in 2021
    https://coderanch.com/t/730886/filler-advertising
    reply
      Bookmark Topic Watch Topic
    • New Topic