Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-08-2010, 06:58 PM
Member
 
Join Date: Feb 2010
Posts: 11
Rep Power: 0
mxrider is on a distinguished road
Default 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);

}

}
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 02-08-2010, 08:13 PM
Senior Member
 
Join Date: Nov 2009
Posts: 205
Rep Power: 1
collin389 is on a distinguished road
Default
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.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 02-08-2010, 08:38 PM
Member
 
Join Date: Feb 2010
Posts: 11
Rep Power: 0
mxrider is on a distinguished road
Default
Originally Posted by collin389 View Post
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.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 02-08-2010, 08:45 PM
Senior Member
 
Join Date: Sep 2008
Location: Voorschoten, the Netherlands
Posts: 1,268
Rep Power: 3
JosAH is on a distinguished road
Default
Originally Posted by mxrider View Post
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.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 02-08-2010, 09:46 PM
Senior Member
 
Join Date: Nov 2009
Posts: 205
Rep Power: 1
collin389 is on a distinguished road
Default
Or you could just insert and '\n' into your string where you want a line break.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 02-08-2010, 09:54 PM
Member
 
Join Date: Feb 2010
Posts: 11
Rep Power: 0
mxrider is on a distinguished road
Default
Originally Posted by collin389 View Post
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));

		
	}

}
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 02-08-2010, 10:01 PM
Senior Member
 
Join Date: Nov 2009
Posts: 205
Rep Power: 1
collin389 is on a distinguished road
Default
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.
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 02-08-2010, 10:02 PM
Member
 
Join Date: Feb 2010
Posts: 11
Rep Power: 0
mxrider is on a distinguished road
Default
Originally Posted by collin389 View Post
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);
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 02-08-2010, 10:10 PM
Senior Member
 
Join Date: Nov 2009
Posts: 205
Rep Power: 1
collin389 is on a distinguished road
Default
no
if total = 5.89 then total >= 3.98 will return true
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 02-09-2010, 02:59 AM
Member
 
Join Date: Feb 2010
Posts: 11
Rep Power: 0
mxrider is on a distinguished road
Default
Originally Posted by collin389 View Post
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?
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 02-09-2010, 03:13 AM
twiggy62's Avatar
Member
 
Join Date: Feb 2010
Posts: 26
Rep Power: 0
twiggy62 is on a distinguished road
Default
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.
Bookmark Post in Technorati
Reply With Quote
  #12 (permalink)  
Old 02-09-2010, 03:31 AM
Senior Member
 
Join Date: Nov 2009
Posts: 205
Rep Power: 1
collin389 is on a distinguished road
Default
take a look at this:
Code:
JOptionPane.showMessageDialog(null, "The total is: " + total + "\nWithin your budget? " + (Double.parseDouble(lunch) >= total), "Message", JOptionPane.INFORMATION_MESSAGE);
Bookmark Post in Technorati
Reply With Quote
  #13 (permalink)  
Old 02-09-2010, 03:35 AM
twiggy62's Avatar
Member
 
Join Date: Feb 2010
Posts: 26
Rep Power: 0
twiggy62 is on a distinguished road
Default
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.
Bookmark Post in Technorati
Reply With Quote
  #14 (permalink)  
Old 02-09-2010, 03:56 AM
Member
 
Join Date: Feb 2010
Posts: 11
Rep Power: 0
mxrider is on a distinguished road
Default
Originally Posted by collin389 View Post
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);
	
		}
		
	}

}
Bookmark Post in Technorati
Reply With Quote
  #15 (permalink)  
Old 02-09-2010, 04:02 AM
twiggy62's Avatar
Member
 
Join Date: Feb 2010
Posts: 26
Rep Power: 0
twiggy62 is on a distinguished road
Default
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.
Bookmark Post in Technorati
Reply With Quote
  #16 (permalink)  
Old 02-09-2010, 04:09 AM
Member
 
Join Date: Feb 2010
Posts: 11
Rep Power: 0
mxrider is on a distinguished road
Default
Originally Posted by twiggy62 View Post
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
Bookmark Post in Technorati
Reply With Quote
  #17 (permalink)  
Old 02-09-2010, 04:14 AM
twiggy62's Avatar
Member
 
Join Date: Feb 2010
Posts: 26
Rep Power: 0
twiggy62 is on a distinguished road
Default
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.
Bookmark Post in Technorati
Reply With Quote
  #18 (permalink)  
Old 02-09-2010, 04:23 AM
Member
 
Join Date: Feb 2010
Posts: 11
Rep Power: 0
mxrider is on a distinguished road
Default
Originally Posted by twiggy62 View Post
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
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
java homework help jenniferrlie New To Java 5 09-22-2009 09:12 PM
Having trouble with java homework... purinlove88 New To Java 3 03-02-2009 04:02 PM
Java homework please Indulgence New To Java 1 11-03-2008 03:48 AM
LF: Homework help with Java excurssion New To Java 2 10-17-2008 07:00 AM
Hi, I am new to programing! Zrob Introductions 1 09-14-2008 06:38 AM


All times are GMT +2. The time now is 01:40 AM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org