• 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 ...
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
  • Mikalai Zaikin

Challange to write a program

Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i was wondering if any one can help me in writing this program. your help would be appreciated. The instructions for the program are below

Create a simple Car class that represents a car with certain characteristics such as name, gas tank
capacity, distance traveled, and fuel efficiency (miles per gallon). Your class will include methods to
add gas to the car and to drive it a certain distance (mutators) and a few simple accessor methods to
return field values.
What You Need to Know
This assignment uses information in Chapters 1-3 from the Horstmann textbook as well as a little
information from Chapter 6 (section 6.1) that I will preview in lecture. To complete this assignment
successfully, you will need to understand how to design, implement, document, and test a simple
Java class. You will also need to understand how to use fields, local variables and parameters of
type double and now to do simple numeric operations like addition, subtraction, multiplication and
division on floating-point (double) values. You will need to know how to write and call methods that
accept parameters, alter field values, and return results.
You will also be asked to use simple if and if-else statements in Java to include some simple error
checking logic in your methods. Finally, you will need to design a tester class to verify that you have
implemented each method properly.
Your resulting Car and CarTester classes will be similar to the CashRegister and CashRegisterTester
classes described on pages 85-87.
Implementing Your Car Class
Your Car class will include methods to do the following (use the method names below):
� addGas: add a specified amount of gas (given in gallons)
� drive: drive a specified distance (given in miles)
� getName: return the name of the current car (as a String)
� getEfficiency: return the fuel efficiency of the current car (in miles per gallon)
� getCapacity: return the gas tank capacity of the current car (in gallons)
� getDistanceTraveled: return the distance traveled (in miles)
� getGas: return the amount of gas currently available in the tank (in gallons)
� getSpace: return the amount of gas that can currently be added to the tank (in gallons)
You will also need a constructor that accepts as parameters a name, the fuel efficiency, and the
tank capacity.
You will need at least five fields in your Car class, all of type double, except for the car name which
is a String:
2 / 3
� car name
� fuel efficiency
� tank capacity
� distance traveled
� available gas
You should select appropriate names and declare these fields properly at the bottom of the class.
Make sure they are all properly initialized by the constructor. Note that the first three fields should
never change after they are initialized while the other two fields may be altered by the addGas() and
drive() methods.
Error Checking
There are several error conditions that you need to detect and handle. In later programs you will
generate error messages (exceptions). For this assignment, you need to simply detect invalid
parameters and do something �reasonable� despite the erroneous method parameters.
In your addGas() method, if 0 or a negative amount is specified, simply return 0 and do nothing else.
If the amount specified is greater than the space currently available in the tank, add as much as you
can and return that amount. In all other cases, add (and return) the requested amount.
In the drive() method, if the specified distance is 0 or negative, simply return 0 and do nothing else.
Otherwise, determine how much gas is required to drive the requested distance. If the amount
required is less than or equal to the current amount of gas in the tank, then �drive� that distance (up
date the distance traveled and available gas fields) and return the distance driven. If the requested
distance requires more gas than is available, drive as far as you can (returning that distance).
Here is an example of what is called �pseudo-code� (half English, half Java) for the drive() method
with error checking:
if (distance <= 0)
return 0.0;
compute gas needed
if (gas needed <= gas available)
drive requested distance
return requested distance
drive as far as possible
return actual distance (available gas * efficiency)
Style and Documentation
You must include a javadoc header comment for the class as a whole and for each method. Your
method headers should document each parameter and return values. Follow the stylistic guidelines
used in the book and outlined in Appendix A. That means use appropriate variables names, indent
properly, space around operators, make curlies line up, and use blank lines as appropriate. We will
only take off a few points for incorrect style on this assignment but subsequent assignments will
require proper style.
3 / 3
Testing Your Car Class
Use incremental development. Add the basic field definitions and the outlines of the methods (without
bodies) to your class first. Make sure it compiles before proceeding. Then implement the constructor.
You can now write a very simple main() in CarTester.java that just instantiates a few Car objects.
Then implement the simple accessor methods. Make sure everything compiles. Add some more tests
to main(). Next implement simple versions of the addGas() and drive() methods that don�t worry about
error checking. Try a variety of tests before proceedings. Once you have a class that works for the
�normal cases�, then add some additional error checking logic to addGas(). Add more tests to main()
to check for all the different possible error conditions you are trying to handle. Once you have that
much working add the error checking logic to drive().
Posts: 23951
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

i was wondering if any one can help me in writing this program. your help would be appreciated. The instructions for the program are below

Unfortunately, JavaRanch is a learning site. And as such, we prefer that the users don't do other user's homework. However...

If you tell us what you did so far, what you tested so far, what problems you are running into, and have very specific questions about the problems, we can give you some hints in the right direction.

    Bookmark Topic Watch Topic
  • New Topic