Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

trying to calculate fuel expenditures for travel between three cities  RSS feed

 
Anthony Camp
Greenhorn
Posts: 5
Firefox Browser Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone, I am new and just joined the website. I'm looking forward to spending some time on here now that I have a new interest in JAVA. Ok, I am having some trouble in my Java class. The teacher is pretty unorganized and get off topic really easily which makes learning this pretty rough. The assignment is to calculate fuel expenditures for travel between three cities, with the output looking something like this >

namecost@$2.80cost@$2.30#ofstops
brian$456.56$234.323
kyler$353.22$139.332


I just made the numbers up, but that's how the professor would like the format of the output. I am missing a lot but I have a foundation I believe. Also I do not know how to incorporate price per gallon.



Thank you for your time.
 
Stevens Miller
Bartender
Posts: 1444
30
C++ Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch, Anthony.

We'll need a bit more about the assignment to help you. What are the fuel costs based on? What's the full statement of the problem?
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Anthony, and welcome to JavaRanch! We're friendly to new Java developers here, so don't be afraid to ask questions. I do have a bit to say about this code though before I get to the meat of your question.

You do have a basic foundation here, but it is pretty rough (understandable considering a teacher who goes off on tangents -- I've had a few of those). First off, this code is pretty hard to read - there are standard Java conventions that you are not following here -- not a big deal in this short assignment, but you should probably learn them for larger-scale projects where you are working in a team. (Typically, method names and variable names start lower-case in Java, so "elmCity" and "public double distance(Point iCity)"). There are others, too, and they do make the code easier to read.

Second, you seem to have defined several classes inside of your main method. I will be honest -- I have never seen this before and was surprised that this would compile! There are several disadvantages to doing this, though. First, because this is very uncommon, it was difficult to follow what was going on (see "Coding conventions," above). Second, and more importantly, these classes only have scope within your main method. This means that you cannot create another method that takes, for example, a Vehicle object as a parameter -- the compiler will complain about that, because Vehicle objects are only valid within your main method.

Your Vehicle class is confusing. While you are defining points individually (elmCity, rokfordTown, etc.) you seem to be defining one vechicle with arrays of data, and then creating an array to hold 10 Vehicle objects. Consider defining the vehicles individually like you do the cities.

Now, to the meat of your question: how to take the price of fuel into account. I'm not going to answer the question directly (we don't do homework here, you understand, but we will help you understand how to do it, which is much more valuable in the long run). Instead, how would you do this if you were trying to calculate it on a piece of paper? If you can calculate the cost of fuel from elmCity to rokfordTown of someone driving your first SUV (MPG of 13) with fuel costing $2.80 a gallon, and then describe to the computer the steps you took to do it, you have solved that problem. (By the way, the answer is approx. $41.55. The answer is not important -- its how you get there that is.)

I think that's enough to think about to start with. See what you can do with that information.
 
Carey Brown
Bartender
Posts: 2979
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Something doesn't look right here. You go through all the trouble to set up and initialize the arrays, and then allow the constructor to overwrite them. Seems like you should have one or the other but not both.

Also, don't start all your variable names with 'a', but do start them with a lower case letter. E.g. 'passengers'.
 
Carey Brown
Bartender
Posts: 2979
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After looking at this some more, it seems that Vehicle should hold the information for a single Vehicle and not have arrays at all. What you do have though is an array of Vehicles. You will need to construct 10 Vehicle objects to initialize your Vehicle array.
 
Junilu Lacar
Sheriff
Posts: 11125
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another strange thing is that you have your Vehicle and Point classes defined inside your main method. While this is not syntactically illegal—the code you supplied compiles fine—that's not normal practice. That would make these classes local to the main method and inaccessible from anywhere else. Who does that? One piece of advice usually given to newbies around here is to step away from the keyboard and figure out, with paper and pencil, exactly WHAT it is you want to do. You are getting lost in the details of HOW you're going to do it. Only when you have a clear idea of WHAT will you be able to translate that to HOW.

EDIT: Ok, I see Joel already covered the strange location of the class declarations. So, tell us, in plain English without any references to arrays or strings or any Java language construct, WHAT it is you want this program to do.

 
Campbell Ritchie
Marshal
Posts: 55672
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome again
I see you are using Pythagoras to work out the distances. You don't need to do that because there is a method which does that already. It is intended for converting rectangular to polar coordinates but that is exactly what you are doing here.
 
Anthony Camp
Greenhorn
Posts: 5
Firefox Browser Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you for all of the replies. I will keep updated as I move along and ask questions on the way. I know there is more information you need to figure out what I'm asking exactly but what you guys have given me has helped.

ill be back haha
 
Anthony Camp
Greenhorn
Posts: 5
Firefox Browser Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, with your help and some help from classmates. This is what I have. I am getting everything I need for the output and most all of it makes sense to me. Can someone please explain to me how the constructors work, and why do I need them in the beginning under my Vehicle class? What's stopping me from deleting that and when doing the math equations, just using the declared values below. Is it just a tool saying that integers are going to be associated with those words?
Secondly, how would I go about making the outputs show in a table like in the original post. Lastly, any other tips on formatting. I read all of the comments and moved stuff around to what I think I needed to do.

Thank you all very much. Anthony

p.s. The random upper case characters are just how he had in the example when he was trying to teach, so I left them in there so I can try to follow along as best as I can.

@Campbell Ritchie
are you saying this is where I would use the method rather than using Pythagoras?



so i would replace it with this?







Thank you all again.
 
Campbell Ritchie
Marshal
Posts: 55672
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anthony Camp wrote:. . . @Campbell Ritchie
are you saying this is where I would use the method rather than using Pythagoras?
That method probably uses Pythagoras behind the scenes.
. . . so i would replace it with this?

. . .
No. You should try out the hypot method in a little app so you can see how it works. And read its documentation from the link I gave you.
 
Anthony Camp
Greenhorn
Posts: 5
Firefox Browser Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Campbell Ritchie

ahh thank you, I'll check it out
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!