Results 1 to 16 of 16
  1. #1
    Hadsvich is offline Member
    Join Date
    Apr 2012
    Posts
    8
    Rep Power
    0

    Default Desperately Need Help With Java Programming Assignment

    I have attached what I've coded here since I couldn't figure out how to attach anything in this post.
    I believe I haven't coded as well as I could have.
    I don't know how to use the continue statement I require.
    And don't really understand why baseDiameter, baseThickness, and baseRadius won't display in the end when I input certain dimensions for them.
    And in some situations I have a NullPointerException and I don't know how to fix that.

    I've also attached what my requirements for the assignment is.
    Any help would be deeply appreciated.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,616
    Rep Power
    25

    Default Re: Desperately Need Help With Java Programming Assignment

    how to attach anything in this post.
    Copy and paste the code here. Put code tags around the code:
    BB Code List - Java Programming Forum

    Post the full text of any error messages here.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Hadsvich is offline Member
    Join Date
    Apr 2012
    Posts
    8
    Rep Power
    0

    Default Re: Desperately Need Help With Java Programming Assignment

    Java Code:
    import java.text.DecimalFormat;
    import javax.swing.*;
    
    public class Orders
    {
    	private static Rocks orders[];
    	private static  DecimalFormat twoDigits = new DecimalFormat("0.00");
    	private static JTextArea outputArea = new JTextArea(10, 35);
    
    	public static void main(String args[])
    	{
    		orders = new Rocks[getDimension()];
    
    		for (int index = 0; index < orders.length; index++)
    		{
    			int selection = 0;
    			selection = getSelection();
    
    			int quantity = 0;
    			double width = 0;
    			double thickness = 0.0;
    			double height = 0.0;
    
    				quantity = (int)getNumericInput("Enter Number of" +
    				" Monuments Desired: ");
    				width = getNumericInput("Enter the Width of Monuments: "
    				);
    				thickness = getNumericInput("Enter the Thickness of " +
    				"Monuments: ");
    				height = getNumericInput("Enter the Height of " +
    				"Monuments:");
    
    				if (selection == 1)
    				orders[index] = new Monument(quantity, width, thickness,
    				height);
    				else
    				if (selection == 2)
    				{
    					double baseThickness = 0, baseDiameter = 0;
    
    					baseThickness = getNumericInput(
    						"Enter the Thickness of Monument's Bases: ");
    					baseDiameter = getNumericInput(
    						"Enter the Diameter of Monument's Bases: ");
    						System.out.println(baseDiameter);
    					orders[index] = new MonumentBase(
    						quantity, width, thickness, height, baseThickness,
    						baseDiameter);
    				}
    				else
    				{
    					//continue;
    				}
    		}
    		displayOutput();
    		System.exit(0);
    	}
    
    	private static int getDimension()
    	{
    		return Integer.parseInt(JOptionPane.showInputDialog(null,"How many " +
    		"orders would you like to make?"));
    	}
    
    	private static int getSelection()
    	{
    
    		String stringSelection = "";
    		int numericSelection = 0;
    
    		stringSelection = JOptionPane.showInputDialog(null,
    			"1:   Make A Monument Order\n" +
    			"2:   Make A Monument & Base Order\n",
    			"Enter 1 or 2 ONLY!", JOptionPane.QUESTION_MESSAGE);
    		numericSelection = Integer.parseInt(stringSelection);
    
    		if(numericSelection == 1 || numericSelection == 2)
    			return numericSelection;
    		else
    			JOptionPane.showMessageDialog(null, "An Incorrect " +
    				"Choice Was Entered\n"+
    				"Please Choose 1 or 2", "****ERROR****",JOptionPane.
    				ERROR_MESSAGE);
    				getSelection();
    
    		return numericSelection;
    	}
    
    	private static void displayOutput()
    	{
    		outputArea.append("Total Orders: " + orders.length + "\n"
    		+ "--------------------------------------------------------");
    
    		for (int count = 0; count < orders.length; count++)
    		{
    
    			String output = "Number of Monuments: " + orders
    			[count].getQuantity() + "\n" +	"Width of Monuments: " + twoDigits.
    			format(orders[count].getWidth()) + " Inches\n" + "Height of " +
    			"Monuments: " + twoDigits.format(orders[count].getHeight()) +
    			" Inches\n" + "Thickness of Monuments: " + twoDigits.format
    			(orders[count].getThickness()) + " Inches\n" + "Cubic Inches of " +
    			"WunderRock Required: " + twoDigits.format(orders[count].
    			getCubicInches()) + "\n" + "Cubic Feet of WunderRock Required: " +
    			twoDigits.format(orders[count].getCubicFeet()) + " \n" +
    			"Retail Price of Order: $" + twoDigits.format(orders[count].
    			getTotalPrice()) + "\n";
    
    			System.out.println(orders[count].getBaseDiameter());
    
    			outputArea.append("\n");
    
    			if(orders[count] instanceof MonumentBase)
    			{
    				output += "Radius of Base: " + twoDigits.format(orders[count].
    				getBaseRadius()) + " Inches\n" + "Diameter of Base: " +
    				twoDigits.format(orders[count].getBaseDiameter()) + " Inches\n"
    				+ "Thickness of Base: " + twoDigits.format(orders[count].
    				getBaseThickness()) + " Inches\n";
    				System.out.println(orders[count].getBaseDiameter());
    
    				outputArea.append(output);
    			}
    			else
    				outputArea.append(output);
    
    		}
    		JScrollPane scroller = new JScrollPane(outputArea);
    		JOptionPane.showMessageDialog(null, scroller);
    	}
    
    	private static double getNumericInput(String prompt)
    	{
    		return Double.parseDouble(JOptionPane.showInputDialog(null, prompt));
    	}
    }
    I put in 1 when I am asked how many orders I want.
    Then I put something other than 1 or 2 to test if it will go back to the menu where I make a selection.
    It does, but I'm supposed to use a continue statement instead of calling the getSelection method.
    And even though I go back to the selection and can input all the dimensions, the program doesn't display the output.
    And shows this error in the console.

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,616
    Rep Power
    25

    Default Re: Desperately Need Help With Java Programming Assignment

    Why is the continue statement commented out?

    the program doesn't display the output.
    Can you post what the program does display?

    To copy the contents of the command prompt window:
    Click on Icon in upper left corner
    Select Edit
    Select 'Select All' - The selection will show
    Click in upper left again
    Select Edit and click 'Copy'

    Paste here.

    There are some classes missing. The code does not compile without errors.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    Hadsvich is offline Member
    Join Date
    Apr 2012
    Posts
    8
    Rep Power
    0

    Default Re: Desperately Need Help With Java Programming Assignment

    I thought I might need to use the continue where I commented it out but instead I think I actually need to use it where I called the getSelection method inside the getSelection method itself.

    It doesn't display anything. The program crashes. And a NullPointerException is shown:

    Exception in thread "main" java.lang.NullPointerException
    at Orders.displayOutput(Orders.java:110)
    at Orders.main(Orders.java:68)

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,616
    Rep Power
    25

    Default Re: Desperately Need Help With Java Programming Assignment

    What variable is null at line 110? Find the variable and then backtrack in the code to see why that variable does not have a valid non-null value.
    If you can't tell which variable is null, add a println statement that prints out the values of all the variables on line 110.

    You use a continue inside a loop. I don't see a loop in the getSelection() method.

    You do need a loop inside of getSelection() to continue asking the user for valid input if invalid input is entered.
    Last edited by Norm; 04-20-2012 at 02:47 AM.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    Hadsvich is offline Member
    Join Date
    Apr 2012
    Posts
    8
    Rep Power
    0

    Default Re: Desperately Need Help With Java Programming Assignment

    It seems to be my output String that is null at line 97 in my previous post. I don't understand why it's null though.

    I don't know what sort of loop I should use that will help me return the numericSelection or where to start it and what condition it should have.

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,616
    Rep Power
    25

    Default Re: Desperately Need Help With Java Programming Assignment

    Is that the long statement from line 97 to line 107? Which variable in that long statement has the null value?

    Use a while loop that does not exit until the user enters a valid number. For example define a boolean outside the loop with a value of true then use its value to keep looping. When the user's input is good, set the boolean to false. That will allow execution to exit the loop.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    Hadsvich is offline Member
    Join Date
    Apr 2012
    Posts
    8
    Rep Power
    0

    Default Re: Desperately Need Help With Java Programming Assignment

    Yep, that's right. I think all of them had the null value, actually.
    But now that I've done the loop, the exception never happens. So I think that's all fine. Thanks so much for the help.

    There's still one more problem I'm having though and I'm not sure how to state this.
    When I give certain dimensions that should work well with the data (at least I think so) it doesn't display them, it just displays 0.
    And this only happens with the baseDiameter, baseRadius, and baseThickness I display.

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,616
    Rep Power
    25

    Default Re: Desperately Need Help With Java Programming Assignment

    it just displays 0.
    Where is/are the variable(s) given a value (other than 0)? Check through your logic to see where the data you expect to be in the variable(s) is going.
    Add some printlns to print out the values of the variables every time they are changed. If they are set to 0 somewhere, the printlns will show where.
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    Hadsvich is offline Member
    Join Date
    Apr 2012
    Posts
    8
    Rep Power
    0

    Default Re: Desperately Need Help With Java Programming Assignment

    All I know is that when it goes to the displayOutput method, the variable becomes 0. It has a value prior to that (I think. I did use a println.).
    It's just I don't understand, sometimes it accepts/keeps the value and other times it just sets it to 0.

  12. #12
    Hadsvich is offline Member
    Join Date
    Apr 2012
    Posts
    8
    Rep Power
    0

    Default Re: Desperately Need Help With Java Programming Assignment

    Someone please help me! I feel so close to being done.

  13. #13
    kraigballa is offline Senior Member
    Join Date
    Oct 2011
    Posts
    115
    Rep Power
    0

    Default Re: Desperately Need Help With Java Programming Assignment

    Did you do what Norm said?

  14. #14
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,616
    Rep Power
    25

    Default Re: Desperately Need Help With Java Programming Assignment

    I did use a println.).
    Post the the debug output from your program that shows when the variable has been set to a value and when it has a value of 0. Also post the code that shows where your println statements are located in the program.
    If you don't understand my response, don't ignore it, ask a question.

  15. #15
    Hadsvich is offline Member
    Join Date
    Apr 2012
    Posts
    8
    Rep Power
    0

    Default Re: Desperately Need Help With Java Programming Assignment

    This is the debug output I got when I made two MonumentBase orders:

    In main, baseThickness = 9.0
    In main, baseDiameter = 49.0
    In main, baseThickness = 13.0
    In main, baseDiameter = 68.0

    In displayOutput, baseThickness = 0.0
    In displayOutput, baseDiameter = 49.0
    In displayOutput, baseThickness = 0.0
    In displayOutput, baseDiameter = 0.0

    I put println statements at lines 13, 14 and lines 77, 78.
    I don't know if I should put ones anywhere else.

    Java Code:
    if (selection == 2)
    				{
    					double baseThickness = 0, baseDiameter = 0;
    
    					baseThickness = getNumericInput(
    						"Enter the Thickness of Monument's Bases: ");
    					baseDiameter = getNumericInput(
    						"Enter the Diameter of Monument's Bases: ");
    					orders[index] = new MonumentBase(
    						quantity, width, thickness, height, baseThickness,
    						baseDiameter);
    
    					System.out.println("In main, baseThickness = " + baseThickness);
    					System.out.println("In main, baseDiameter = " + baseDiameter);
    				}
    		}
    		displayOutput();
    		System.exit(0);
    	}
    
    	private static int getDimension()
    	{
    		return Integer.parseInt(JOptionPane.showInputDialog(null,"How many " +
    		"orders would you like to make?"));
    	}
    
    	private static int getSelection()
    	{
    
    		String stringSelection = "";
    		int numericSelection = 0;
    		boolean invalidIn = true;
    
    		while (invalidIn = true)
    		{
    			stringSelection = JOptionPane.showInputDialog(null,
    			"1:   Make A Monument Order\n" +
    			"2:   Make A Monument & Base Order\n",
    			"Enter 1 or 2 ONLY!", JOptionPane.QUESTION_MESSAGE);
    			numericSelection = Integer.parseInt(stringSelection);
    
    			if(numericSelection == 1 || numericSelection == 2)
    			{
    				invalidIn = false;
    				return numericSelection;
    			}
    			else
    				JOptionPane.showMessageDialog(null, "An Incorrect " +
    					"Choice Was Entered\n"+
    					"Please Choose 1 or 2", "****ERROR****",JOptionPane.
    					ERROR_MESSAGE);
    					continue;
    		}
    
    		return numericSelection;
    	}
    
    	private static void displayOutput()
    	{
    
    		outputArea.append("Total Orders: " + orders.length + "\n"
    		+ "--------------------------------------------------------");
    
    		for (int count = 0; count < orders.length; count++)
    		{
    			String output = "Number of Monuments: " + orders
    			[count].getQuantity() + "\n" +	"Width of Monuments: " + twoDigits.
    			format(orders[count].getWidth()) + " Inches\n" + "Height of " +
    			"Monuments: " + twoDigits.format(orders[count].getHeight()) +
    			" Inches\n" + "Thickness of Monuments: " + twoDigits.format
    			(orders[count].getThickness()) + " Inches\n";
    
    			outputArea.append("\n");
    
    			if(orders[count] instanceof MonumentBase)
    			{
    				System.out.println("In displayOutput, baseThickness = " + orders[count].getBaseThickness());
    				System.out.println("In displayOutput, baseDiameter = " + orders[count].getBaseDiameter());
    
    				output += "Radius of Base: " + twoDigits.format(orders[count].
    				getBaseRadius()) + " Inches\n" + "Diameter of Base: " +
    				twoDigits.format(orders[count].getBaseDiameter()) + " Inches\n"
    				+ "Thickness of Base: " + twoDigits.format(orders[count].
    				getBaseThickness()) + " Inches\n";
    			}

  16. #16
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,616
    Rep Power
    25

    Default Re: Desperately Need Help With Java Programming Assignment

    The variable: double baseThickness defined on line 3 will not exist after line 15. Its definition exists only inside or the enclosing {}. When execution crosses line 15, the variable and its value are gone. What does the MonumentBase class's constructor do with the value it receives?

    Where is the variable whose value is returned by the method: getBaseThickness() called on line 77?
    Where is the print out for when that variable is given a value? You need to find where that method gets its value from and add printlns to all the places where the variable is given a value.
    Last edited by Norm; 04-21-2012 at 02:41 AM.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. help..stuck w/ a java programming assignment
    By clemsontigers in forum New To Java
    Replies: 16
    Last Post: 02-19-2014, 05:16 AM
  2. Replies: 2
    Last Post: 01-19-2012, 08:43 PM
  3. assistance w/ java programming assignment
    By clemsontigers in forum New To Java
    Replies: 4
    Last Post: 04-07-2011, 09:07 PM
  4. Replies: 9
    Last Post: 03-06-2011, 04:00 AM
  5. Replies: 1
    Last Post: 10-02-2009, 02:35 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
  •