Win a copy of Zero to AI - A non-technical, hype-free guide to prospering in the AI era this week in the Artificial Intelligence and Machine Learning forum!
  • 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
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Java programing homework help

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i really need help with my homework


this is what i have to do

You are buying a picnic lunch to share with friends in the park. Write, compile, and test a Java class that will 1) ask for a budget amount 2) display the cost of each item for a lunch for three and 3) calculate and display the total price of all the items and determine and display whether you have exceeded your budget.

Lunch items will consist of apples, cheese, and bread. Use six class variables of the appropriate data type to hold the price (cost) and amount (quantity) of each item as indicated in the following table:

ITEM
COST
QUANTITY

Apple
$0.25/each
3

Cheese
$3.99/lb
0.5 lb

Bread
$1.19/loaf
1



You will need additional class variables to hold the budget and total amounts.

Your program will ask the user for the budget amount through an input dialog box and then display the results in message dialog boxes. Hint: it might be easier to first create a String variable to hold all or part of the second parameter to the JOptionPane.showMessageDialog() method.


Your output should resemble the pictures below:









this is what i have so far, i need a little help with it. thanks!


 
Marshal
Posts: 70625
288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch

Please use the Code button; it makes your code that much easier to read. Since you are new I shall edit your post with the Code button.
You will doubtless have realised we don't do other people's homework.
You seem to have made a start, so I suggest you write down with pencil and paper exactly how you are going to change the entry "5.32" into a number, and how you intend to change the number of apples into a price, etc. You want it in very simple terms, not using any long words. Once you have got that written down, you can call it pseudocode and that is easy to turn into real code.
 
Campbell Ritchie
Marshal
Posts: 70625
288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need some way of entering and recording how many apples and how much cheese you plan to eat. Don't hard-code 3 or 0.5 or 1 for 3 apples ½lb cheese and 1 loaf.
 
todd stolz-schroeder
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I figured out most of it, i just need help with a boolean to tell if the budget is over 3.94 for true and if its under its false.

Can someone send me in the right direction on how to write a boolean variable?

this is the code i have so far


 
author
Posts: 23886
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

todd stolz-schroeder wrote:I figured out most of it, i just need help with a boolean to tell if the budget is over 3.94 for true and if its under its false.

Can someone send me in the right direction on how to write a boolean variable?



Well, do you have the budget value stored somewhere? Is it a number? And if not can you convert it to a number? And finally, have you looked at the java comparison operators?

Henry
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you know how to get a numeric value from a String representation? What kind of numeric value are you
expecting the user to input?

Also, I think lunch is a confusing name for your variable. I read it and I think it should contain a menu item or
or some type of food. I don't think this is your intention. What input exactly are you expecting from the user?
 
Campbell Ritchie
Marshal
Posts: 70625
288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

todd stolz-schroeder wrote: . . . tell if the budget is over 3.94 for true and if its under its false. . . .

Believe it or not, that will come out very simply if you use the very high-tech equipment I recommended yesterday. You may find something helpful in the Java™ Tutorials, too.

I also think you need some way to enter how many apples and how much cheese you intend to buy. You would use a very similar technique to entering your budget.

Another thing: why have you got all your code in the main method?
 
I don't always make ads but when I do they're tiny
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic