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

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

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 :/.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have a good look at line 18
 
Jeremy Flexer
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 43081
77
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The formula is fine, but in programming details matter - every single character is important :-)
 
Bartender
Posts: 5465
212
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Haha, oh wow how could I miss something that simple .
 
Think of how stupid the average person is. And how half of them are stupider than that. But who reads this tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic