Results 1 to 18 of 18
  1. #1
    mxrider is offline Member
    Join Date
    Feb 2010
    Posts
    18
    Rep Power
    0

    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);

    }

    }

  2. #2
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Posts
    235
    Rep Power
    5

    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.

  3. #3
    mxrider is offline Member
    Join Date
    Feb 2010
    Posts
    18
    Rep Power
    0

    Default

    Quote 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.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,679
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote 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:

    Java 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.

  5. #5
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Posts
    235
    Rep Power
    5

    Default

    Or you could just insert and '\n' into your string where you want a line break.

  6. #6
    mxrider is offline Member
    Join Date
    Feb 2010
    Posts
    18
    Rep Power
    0

    Default

    Quote 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
    Java 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));
    
    		
    	}
    
    }

  7. #7
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Posts
    235
    Rep Power
    5

    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.

  8. #8
    mxrider is offline Member
    Join Date
    Feb 2010
    Posts
    18
    Rep Power
    0

    Default

    Quote 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);

  9. #9
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Posts
    235
    Rep Power
    5

    Default

    no
    if total = 5.89 then total >= 3.98 will return true

  10. #10
    mxrider is offline Member
    Join Date
    Feb 2010
    Posts
    18
    Rep Power
    0

    Default

    Quote 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?

  11. #11
    twiggy62's Avatar
    twiggy62 is offline Member
    Join Date
    Feb 2010
    Posts
    26
    Rep Power
    0

    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. :D
    The biggest room in the world, is room for improvement.

  12. #12
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Posts
    235
    Rep Power
    5

    Default

    take a look at this:
    Java Code:
    JOptionPane.showMessageDialog(null, "The total is: " + total + "\nWithin your budget? " + (Double.parseDouble(lunch) >= total), "Message", JOptionPane.INFORMATION_MESSAGE);

  13. #13
    twiggy62's Avatar
    twiggy62 is offline Member
    Join Date
    Feb 2010
    Posts
    26
    Rep Power
    0

    Default

    This is the way I would write the program:

    Java 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.

  14. #14
    mxrider is offline Member
    Join Date
    Feb 2010
    Posts
    18
    Rep Power
    0

    Default

    Quote Originally Posted by collin389 View Post
    take a look at this:
    Java 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

    Java 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);
    	
    		}
    		
    	}
    
    }

  15. #15
    twiggy62's Avatar
    twiggy62 is offline Member
    Join Date
    Feb 2010
    Posts
    26
    Rep Power
    0

    Default

    I belive you have to declare it as:

    Java Code:
    DecimalFormat df = new DecimalFormat("#.##");
    And don't forget to import:

    Java Code:
    import java.text.DecimalFormat;
    Last edited by twiggy62; 02-09-2010 at 04:04 AM. Reason: Forgot the parentheses.
    The biggest room in the world, is room for improvement.

  16. #16
    mxrider is offline Member
    Join Date
    Feb 2010
    Posts
    18
    Rep Power
    0

    Default

    Quote Originally Posted by twiggy62 View Post
    I belive you have to declare it as:

    Java Code:
    DecimalFormat df = new DecimalFormat("#.##");
    And don't forget to import:

    Java 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
    Java Code:
    df.setMaximumFractionDigits(2)
    it wants a ;
    and when i put a ; it has an error with the df

  17. #17
    twiggy62's Avatar
    twiggy62 is offline Member
    Join Date
    Feb 2010
    Posts
    26
    Rep Power
    0

    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:

    Java 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.

  18. #18
    mxrider is offline Member
    Join Date
    Feb 2010
    Posts
    18
    Rep Power
    0

    Default

    Quote 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:

    Java 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

Similar Threads

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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •