
02-08-2010, 06:58 PM
|
|
Member
|
|
Join Date: Feb 2010
Posts: 11
Rep Power: 0
|
|
java programing homework help!
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!
import javax.swing.JOptionPane;
public class Lunch
{
public static void main(String[] args)
{
String lunch;
lunch=JOptionPane.showInputDialog
(null,"Enter your budget", "Budget", JOptionPane.QUESTION_MESSAGE);
double apple = .25;
double cheese = 3.99;
double bread = 1.19;
JOptionPane.showMessageDialog(null, "Lunch consists of:3 apples at $0.25 a piece 0.5lb of cheese at $3.99 a pound;1 loaf(s) of bread at
$1.19");
total = apple + cheese + bread;
JOptionPane.showMessageDialog(null, "The total is: "
+ total,JOptionPane.PLAIN_MESSAGE);
}
}
|
|

02-08-2010, 08:13 PM
|
|
Senior Member
|
|
Join Date: Nov 2009
Posts: 205
Rep Power: 1
|
|
|
Looks good so far, except you need to change your message boxes around so that they look like the ones in the pictures(title, line breaks, plain or information message). Now you need to turn the String lunch into and integer and figure out if it is greater than the total.
|
|

02-08-2010, 08:38 PM
|
|
Member
|
|
Join Date: Feb 2010
Posts: 11
Rep Power: 0
|
|
Originally Posted by collin389
|
|
Looks good so far, except you need to change your message boxes around so that they look like the ones in the pictures(title, line breaks, plain or information message). Now you need to turn the String lunch into and integer and figure out if it is greater than the total.
|
How do i make line breaks? i cant seem to figure it out.
|
|

02-08-2010, 08:45 PM
|
|
Senior Member
|
|
Join Date: Sep 2008
Location: Voorschoten, the Netherlands
Posts: 1,268
Rep Power: 3
|
|
Originally Posted by mxrider
|
|
How do i make line breaks? i cant seem to figure it out.
|
Ripped straight from the API documentation:
|
Code:
|
A descriptive message to be placed in the dialog box. In the most common usage, message is just a String or String constant.
However, the type of this parameter is actually Object. Its interpretation depends on its type:
Object[]
An array of objects is interpreted as a series of messages (one per object) arranged in a vertical stack. The interpretation is
recursive -- each object in the array is interpreted according to its type.
Component
The Component is displayed in the dialog.
Icon
The Icon is wrapped in a JLabel and displayed in the dialog.
others
The object is converted to a String by calling its toString method. The result is wrapped in a JLabel and displayed. |
... so a simple array of Strings could do the job ...
kind regards,
Jos
Last edited by JosAH; 02-08-2010 at 08:57 PM.
|
|

02-08-2010, 09:46 PM
|
|
Senior Member
|
|
Join Date: Nov 2009
Posts: 205
Rep Power: 1
|
|
|
Or you could just insert and '\n' into your string where you want a line break.
|
|

02-08-2010, 09:54 PM
|
|
Member
|
|
Join Date: Feb 2010
Posts: 11
Rep Power: 0
|
|
Originally Posted by collin389
|
|
Or you could just insert and '\n' into your string where you want a line break.
|
Thats worked perfectly.
I dont know how i would get a boolean to output true or false on the message box, can anyone lead me in the right direction?
this is my code so far, i just need to put in the boolean to see if the price is in the budget and then round and im done.
heres my code
|
Code:
|
import javax.swing.JOptionPane;
public class Lunch
{
public static void main(String[] args)
{
String lunch;
lunch=JOptionPane.showInputDialog
(null,"Enter your budget", "Budget", JOptionPane.QUESTION_MESSAGE);
double apple = .25;
double cheese = 3.99;
double bread = 1.19;
double total;
JOptionPane.showMessageDialog(null, "Lunch consists of:\n3 apples at $0.25 a piece\n0.5lb of cheese at $3.99/lb\n1 loaf(s) of bread at $1.19");
JOptionPane.showMessageDialog(null, "The total is " + (total = (3 * apple) + (.5 * cheese) + bread));
}
} |
|
|

02-08-2010, 10:01 PM
|
|
Senior Member
|
|
Join Date: Nov 2009
Posts: 205
Rep Power: 1
|
|
|
you declare variables with one equals =
you do a boolean test with two equals ==
hope that helps
Also, make sure your message boxes look exactly like the ones in the picture.
|
|

02-08-2010, 10:02 PM
|
|
Member
|
|
Join Date: Feb 2010
Posts: 11
Rep Power: 0
|
|
Originally Posted by collin389
|
you declare variables with one equals =
you do a boolean test with two equals ==
hope that helps
Also, make sure your message boxes look exactly like the ones in the picture.
|
so like this?
boolean == (total > 3.94);
|
|

02-08-2010, 10:10 PM
|
|
Senior Member
|
|
Join Date: Nov 2009
Posts: 205
Rep Power: 1
|
|
|
no
if total = 5.89 then total >= 3.98 will return true
|
|

02-09-2010, 02:59 AM
|
|
Member
|
|
Join Date: Feb 2010
Posts: 11
Rep Power: 0
|
|
Originally Posted by collin389
|
no
if total = 5.89 then total >= 3.98 will return true
|
How do i set it up that the boolean is on the same message as the total?
|
|

02-09-2010, 03:13 AM
|
 |
Member
|
|
Join Date: Feb 2010
Posts: 26
Rep Power: 0
|
|
I believe you have to would set up a String variable, which I would call
"strLunch", to get budget amount.
Then parse the "strLunch" into a double variable named "lunch", so that you can
use it to compare your budget versus the total of the items.
Then if your budget is a higher number than total items cost, return "true" to
your boolean variable, otherwise return "false".
Not sure if thats what you are asking, but that's my take on it.
__________________
The biggest room in the world, is room for improvement.
|
|

02-09-2010, 03:31 AM
|
|
Senior Member
|
|
Join Date: Nov 2009
Posts: 205
Rep Power: 1
|
|
take a look at this:
|
Code:
|
JOptionPane.showMessageDialog(null, "The total is: " + total + "\nWithin your budget? " + (Double.parseDouble(lunch) >= total), "Message", JOptionPane.INFORMATION_MESSAGE); |
|
|

02-09-2010, 03:35 AM
|
 |
Member
|
|
Join Date: Feb 2010
Posts: 26
Rep Power: 0
|
|
This is the way I would write the program:
|
Code:
|
import javax.swing.JOptionPane;
public class Lunch {
public static void main(String[] args) {
String strLunch;
boolean withinBudget = false;
double total = (3 * .25) + (.5 * 3.99) + 1.19;
strLunch = JOptionPane.showInputDialog(null, "Enter your budget",
"Budget", JOptionPane.QUESTION_MESSAGE);
Double lunch = Double.parseDouble(strLunch);
JOptionPane.showMessageDialog(null, "Lunch consists of:\n3 apples at "
+ "$0.25 a piece\n0.5lb of cheese at $3.99/lb\n1 loaf(s) of bread"
+ " at $1.19");
if (lunch > total) {
withinBudget = true;
} else {
withinBudget = false;
}
JOptionPane.showMessageDialog(null, "The total is " + total + "\nWithin"
+ " your budget? " + withinBudget);
}
} |
__________________
The biggest room in the world, is room for improvement.
|
|

02-09-2010, 03:56 AM
|
|
Member
|
|
Join Date: Feb 2010
Posts: 11
Rep Power: 0
|
|
Originally Posted by collin389
|
take a look at this:
|
Code:
|
JOptionPane.showMessageDialog(null, "The total is: " + total + "\nWithin your budget? " + (Double.parseDouble(lunch) >= total), "Message", JOptionPane.INFORMATION_MESSAGE); |
|
Thank you sooo much.
one last questions and then im done, i have to round my answer to two decimal places. my teacher gave us some instructions but i dont know what im doing wrong.
Create a DecimalFormat object: “DecimalFormat df = new DecimalFormat();”
Use the setMaximumFractionDigits() method to set the desired number of digits: “df.setMaximumFractionDigits(2);”
When you would like to format a number, call the format() method of the DecimalFormat object with the number as an argument: “df.format(total);”
and this is what i tried but i get errors
|
Code:
|
import javax.swing.JOptionPane;
public class Lunch
{
public static void main(String[] args)
{
df.setMaximumFractionDigits(2)
{
String lunch;
lunch=JOptionPane.showInputDialog
(null,"Enter your budget", "Budget", JOptionPane.QUESTION_MESSAGE);
double apple = .25;
double cheese = 3.99;
double bread = 1.19;
double total;
DecimalFormat df = new DecimalFormat();
JOptionPane.showMessageDialog(null, "Lunch consists of:\n3 apples at $0.25 a piece\n0.5lb of cheese at $3.99/lb\n1 loaf(s) of bread at $1.19");
total = ((3 * apple) + (.5 * cheese) + (bread));
JOptionPane.showMessageDialog(null, "The total is: $" + df.format(total) + "\nWithin your budget? " + (Double.parseDouble(lunch) >= total), "Message", JOptionPane.INFORMATION_MESSAGE);
}
}
} |
|
|

02-09-2010, 04:02 AM
|
 |
Member
|
|
Join Date: Feb 2010
Posts: 26
Rep Power: 0
|
|
I belive you have to declare it as:
|
Code:
|
DecimalFormat df = new DecimalFormat("#.##"); |
And don't forget to import:
|
Code:
|
import java.text.DecimalFormat; |
__________________
The biggest room in the world, is room for improvement.
Last edited by twiggy62; 02-09-2010 at 04:04 AM.
Reason: Forgot the parentheses.
|
|

02-09-2010, 04:09 AM
|
|
Member
|
|
Join Date: Feb 2010
Posts: 11
Rep Power: 0
|
|
Originally Posted by twiggy62
|
I belive you have to declare it as:
|
Code:
|
DecimalFormat df = new DecimalFormat("#.##"); |
And don't forget to import:
|
Code:
|
import java.text.DecimalFormat; |
|
When I declare it like that it gives me errors for the # signs. it also gives me an error with the
|
Code:
|
df.setMaximumFractionDigits(2) |
it wants a ;
and when i put a ; it has an error with the df
|
|

02-09-2010, 04:14 AM
|
 |
Member
|
|
Join Date: Feb 2010
Posts: 26
Rep Power: 0
|
|
I have used this one in the past, not sure if you have to use the DecimalFormat
or not, but this is another option I have used in the past:
|
Code:
|
System.out.printf("%.2f", calculate(4.0, 9.0)); |
Making sure you use "printf" instead of "println".
__________________
The biggest room in the world, is room for improvement.
|
|

02-09-2010, 04:23 AM
|
|
Member
|
|
Join Date: Feb 2010
Posts: 11
Rep Power: 0
|
|
Originally Posted by twiggy62
|
I have used this one in the past, not sure if you have to use the DecimalFormat
or not, but this is another option I have used in the past:
|
Code:
|
System.out.printf("%.2f", calculate(4.0, 9.0)); |
Making sure you use "printf" instead of "println".
|
I got the decimal format to work, thanks for all the help though! im finally finished
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT +2. The time now is 01:40 AM.
|
|