Results 1 to 16 of 16
- 04-19-2012, 11:06 PM #1
Member
- Join Date
- Apr 2012
- Posts
- 8
- Rep Power
- 0
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.
- 04-19-2012, 11:23 PM #2
Re: Desperately Need Help With Java Programming Assignment
Copy and paste the code here. Put code tags around the code:how to attach anything in this post.
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.
- 04-20-2012, 12:32 AM #3
Member
- Join Date
- Apr 2012
- Posts
- 8
- Rep Power
- 0
Re: Desperately Need Help With Java Programming Assignment
I put in 1 when I am asked how many orders I want.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)); } }
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.
- 04-20-2012, 01:29 AM #4
Re: Desperately Need Help With Java Programming Assignment
Why is the continue statement commented out?
Can you post what the program does display?the program doesn't display the output.
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.
- 04-20-2012, 01:42 AM #5
Member
- Join Date
- Apr 2012
- Posts
- 8
- Rep Power
- 0
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)
- 04-20-2012, 01:44 AM #6
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 01:47 AM.
If you don't understand my response, don't ignore it, ask a question.
- 04-20-2012, 02:34 AM #7
Member
- Join Date
- Apr 2012
- Posts
- 8
- Rep Power
- 0
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.
- 04-20-2012, 02:44 AM #8
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.
- 04-20-2012, 03:25 AM #9
Member
- Join Date
- Apr 2012
- Posts
- 8
- Rep Power
- 0
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.
- 04-20-2012, 03:33 AM #10
Re: Desperately Need Help With Java Programming Assignment
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.it just displays 0.
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.
- 04-20-2012, 04:01 AM #11
Member
- Join Date
- Apr 2012
- Posts
- 8
- Rep Power
- 0
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.
- 04-20-2012, 05:36 AM #12
Member
- Join Date
- Apr 2012
- Posts
- 8
- Rep Power
- 0
Re: Desperately Need Help With Java Programming Assignment
Someone please help me! I feel so close to being done.
- 04-20-2012, 08:08 AM #13
Senior Member
- Join Date
- Oct 2011
- Posts
- 115
- Rep Power
- 0
Re: Desperately Need Help With Java Programming Assignment
Did you do what Norm said?
- 04-20-2012, 12:42 PM #14
Re: Desperately Need Help With Java Programming Assignment
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.I did use a println.).If you don't understand my response, don't ignore it, ask a question.
- 04-21-2012, 12:15 AM #15
Member
- Join Date
- Apr 2012
- Posts
- 8
- Rep Power
- 0
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"; }
- 04-21-2012, 01:38 AM #16
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 01:41 AM.
If you don't understand my response, don't ignore it, ask a question.
Similar Threads
-
Java Programming University Assignment - Guidance needed
By jay89 in forum New To JavaReplies: 2Last Post: 01-19-2012, 07:43 PM -
assistance w/ java programming assignment
By clemsontigers in forum New To JavaReplies: 4Last Post: 04-07-2011, 08:07 PM -
Help w/ java programming assignment counting number of spaces
By clemsontigers in forum New To JavaReplies: 9Last Post: 03-06-2011, 03:00 AM -
help..stuck w/ a java programming assignment
By clemsontigers in forum New To JavaReplies: 15Last Post: 02-24-2011, 09:31 PM -
Help with implement class programming assignment
By ALH813 in forum EclipseReplies: 1Last Post: 10-02-2009, 01:35 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks