• Post Reply Bookmark Topic Watch Topic
  • New Topic

Deitel Java How to Program Exercise 4.17 (Gas Mileage) Application  RSS feed

 
Kim Cook
Greenhorn
Posts: 5
Android Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Everyone, I am working my way through Deitel Java How to Program 10th Edition and have prepared a gas mileage app that will The program should calculate and display the miles per gallon obtained for each tankful and print the combined miles per gallon obtained for all tankfuls up to this point. I would like to ensure I am on the right track.  All feedback is welcomed and appreciated.  Thank you.





 
Carey Brown
Saloon Keeper
Posts: 3315
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This

will perform the divide using integers because both totalMiles and totalGallons are ints. This cast will perform a floating point divide.


Also

will work but  if you use logical AND (&&) you will give the compiler a chance to optimize the statement, as in
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What should happen if the user enters -1 on line 36?  I'd expect the program to exit without the user having to enter any more responses.
 
Knute Snortum
Sheriff
Posts: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And what happens if the user enters "aksjdf"?
 
Junilu Lacar
Sheriff
Posts: 11485
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The comments you have received so far are valid but the following are meant to help you step back from those details and take a higher level view:

1. Getting away from putting all your code in main() is a good step. However, you still have most of your logic jammed into a single method. What's more, that method name says that it's purpose is to display miles per gallon but it's actually doing way more than just that. That makes your code misleading. Misleading code is not as clear as it could be. Lack of clarity in code causes many problems including bugs, unnecessary complexity, intractability, and general lack of quality.

2. Your program does not meet the requirement to display miles per gallon for each tank full. That is, each time gallons and miles are entered, you should display the MPG for that particular set as well. Your code only calculates and displays the total MPG for all the data entered.

3. Because you misinterpreted the requirements, your line 54 is checking the wrong variables.

4. You could choose better names for some elements of your program. In particular, the miles variable in the driver main() method is not a very good name.
 
Junilu Lacar
Sheriff
Posts: 11485
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My comment about good names may seem trivial but choosing good names is a very important part of writing good programs. In high-level languages like Java, good names can make a big difference in making your program clear and concise or complicated and confusing.

If you think of your classes as things that you can talk to and methods as the messages that these things understand, then it makes sense to choose names that make your code read like a conversation.  Say you go to a gas station that has an attendant.  Would you say to the attendant, "Hey gas, fill 'er up, please."  Likewise, related to my comment about your displayMilesPerGallon() method doing more than what its name says it does, if the attendant went ahead and started changing your tires while the gas was pumping, you'd be a little surprised, right?

To find a good name, you need to think about what exactly the code that's related to the name does. The name should fit that purpose/action/responsibility as closely as possible.

Your program tracks gas mileage, right? So if the guy who tends pumps at the gas station is called a "gas station attendant" and the guy who manages the gas station is a "gas station manager" and a person who fixes cars is called a "car mechanic", then what should a program that tracks gas mileage be called? Should it be called "miles"? Does that even make sense?

On the other hand, if you find that a chunk of code does way more than what the name that represents it implies, then your code would probably benefit from a little bit of breaking up into smaller chunks. This is called "decomposition". You break up a chunk of code that does multiple things so that you have many smaller chunks of code, each one doing a single logical thing.
 
Junilu Lacar
Sheriff
Posts: 11485
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Regarding my comment about checking the wrong variables on line 54, try entering -1 for miles and -1 for gallons right away as your very first set of data. That is, you basically tell the program to quit without giving it any valid data to use. What does your program do? Why does it do that? What should it do?
 
Kim Cook
Greenhorn
Posts: 5
Android Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you everyone for all of the great feedback . @Junilu thank you very much for the detailed response. I will incorporate your suggestions into my code. I really appreciate the feedback everyone.
 
Knute Snortum
Sheriff
Posts: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And post your new code back here so we can see how you're doing.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!