• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

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

 
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 :/.
 
Marshal
Posts: 67340
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • 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.
 
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
Marshal
Posts: 67340
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • 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.
 
Rancher
Posts: 43011
76
  • 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 :-)
 
Bartender
Posts: 3674
151
  • 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 .
 
rubbery bacon. crispy tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!