• Post Reply Bookmark Topic Watch Topic
  • New Topic

Can't figure out how to finish this java exercise.  RSS feed

 
Jeremy Flexer
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi so I was doing some java excersies, then I came across this one:
Different packages of ground beef have different percentages of fat and different costs per pound. Write a program that asks the user for:

The price per pound of package "A"
The percent lean in package "A"
The price per pound of package "B"
The percent lean in package "B"
The program then calculates the cost per pound of lean (non-fat) beef for each package and writes out which is the best value.

Price per pound package A:
2.89
Percent lean package A:
85
Price per pound package B:
3.49
Percent lean package B:
93

Package A cost per pound of lean:3.4
Package B cost per pound of lean:3.752688
Package A is the best value
Assume that the two packages will not come out equal in value.


I came up with this: (Sorry for my noob code I am new to java coming from python.)


I don't think it is working right because when I enter what the guy entered in the exercise, my results come out different.
Don't know what I am doing I am probably overlooking something small :/.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66306
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

Don't worry about being a n00b. Everyone's a n00b at one time.

The first thing that I would do is to use integer pennies rather than double dollars. doubles are not a good way to express currency.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a good look at line 18
 
Jeremy Flexer
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your replies, I was thinking about representing the currency using integers and cents, maybe I should considering the exercise didn't state I couldn't unlike others. I also looked at line 18, could you maybe tell me what formula to use because I have always been extremely lacking in mathematics, and I am talking extremely bad xD.
Also I don't know if you are wondering but if you are, I know I didn't need the parenthesis on line 18 It just helps me somehow hehe.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66306
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeremy Flexer wrote:Thank you for your replies, I was thinking about representing the currency using integers and cents, maybe I should considering the exercise didn't state I couldn't unlike others.

I would highly recommend it.

I also looked at line 18, could you maybe tell me what formula to use

No, no. Really look at line 18. Carefully.

Also I don't know if you are wondering but if you are, I know I didn't need the parenthesis on line 18 It just helps me somehow hehe.

Code clarity is a goal. If the parens make the code clearer, then just because they are "not needed" doesn't mean they should be omitted.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The formula is fine, but in programming details matter - every single character is important :-)
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmm... maybe it's the late time, but what about multiply or division?
(apart from this letterthingy in line 18, of course).

Greetz,
Piet
 
Jeremy Flexer
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Haha, oh wow how could I miss something that simple .
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!