944,147 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 5157
  • Java RSS
Oct 18th, 2004
0

Need some help with my do-while and while loops

Expand Post »
I’m having problems with my do-while loops and my while loop. Also my switch statement ends and does not loop to ask for another input. I’m confused. Any suggestion that can help me or send me one the right track I would appreciate it.
This is the assignment:

The Miskatonic University’s math department wants you to write a computer program that will find all of the Rectangular Prisms with integer sides whose volume is equal to an inputted integer volume of cubic units (units can be feet, inches, meters, or whatever according to your preference).

The Miskatonic mathematicians realize that nested repetition will be required to solve this problem, and never missing a chance to test their statistical skills, they have an additional specification to this problem. The mathematicians have heard of while, for, and do-while loops and are interested in seeing how well they perform compared to one another. As such once you have discovered an algorithm to solve their Rectangular Prism Options problem, they want you to code the solution three times: once using only while loops, once using only for loops, and once using only do-while loops. The mathematicians want all of this code in one Java program which will allow the user to select which version of the algorithm to use (i.e., “Press 1 for while loops, Press 2 for for loops, Press 3 for do-while loops�). They require that you use a switch statement to accomplish this selection.

If the user inputs any non-positive values for the volume of the rectangular prism, then the program should display an error message and prompt the user for input again. Your list of Rectangular Prism Options should be displayed in a JScrollPane within a dialog box as shown in the example output below.


Here is my code so far:
Java Syntax (Toggle Plain Text)
  1. import javax.swing.*;
  2.  
  3. public class RectangularPrism
  4. {
  5. public static void main(String args[])
  6. {
  7. int counter = 0; //Sets counter to 1
  8. int a, b, c, volume; //Volume of rectangular prism
  9. int i; //Case counter
  10. int choice; //User's choice of method to solve
  11. String inputFeet, inputChoice; //Value entered by user
  12. String output = "Solution #\tL*W*H\n";
  13.  
  14. //Input Dialog Box for volume of recangular prism
  15. inputFeet = JOptionPane.showInputDialog("Enter volume of a rectangular prism in feet");
  16. volume = Integer.parseInt(inputFeet);
  17.  
  18. //Input Dialog Box for choice of repetition method
  19. inputChoice = JOptionPane.showInputDialog("Enter 1 to solve by While loops\nEnter 2 to solve by For loops\nEnter 3 to solve by a Do-While loop");
  20. choice = Integer.parseInt(inputChoice);
  21.  
  22. //Start user choice selection
  23. switch (choice)
  24. {
  25. case 1: //While loop solution
  26. a = 0; b = 0; c = 0; i = 0;
  27. while (a <= volume)
  28. {
  29. a++;
  30. while (b <= volume)
  31. {
  32. b++;
  33. while (c <= volume)
  34. {
  35. c++;
  36. while (a * b * c == volume)
  37. {
  38. counter++;
  39. output += counter + ".\t" + a + ", " + b + ", " + c + "\n";
  40. a = 0;
  41. }
  42. }
  43. }
  44. }
  45.  
  46. break;
  47.  
  48. case 2: //For loop solution (Finished)
  49. for (a = 0; a <= volume; a++)
  50. {
  51. for (b = 0; b <= volume; b++)
  52. {
  53. for (c = 0; c <= volume; c++)
  54. {
  55. if ( a * b * c == volume)
  56. {
  57. counter++;
  58. output += counter + ".\t" + a + ", " + b + ", " + c + "\n";
  59. }
  60. }
  61. }
  62. }
  63. break;
  64.  
  65. case 3: //Do-while loop solution
  66. a = 0; b = 0; c = 0;
  67. do
  68. {
  69. do
  70. {
  71. do
  72. {
  73. do
  74. {
  75. c++;
  76. }while(c <= volume);
  77. b++;
  78. }while(b <= volume);
  79. b = 0;
  80. b++;
  81. }while(c <= volume);
  82. counter++;
  83. output += counter + ".\t" + a + ", " + b + ", " + c + "\n";
  84. }while(a * b * c == volume);
  85. break;
  86.  
  87. default: //Message diplayed if not a valid choice is chosen
  88. JOptionPane.showMessageDialog(null,"Please enter a valid choice!");
  89. }
  90.  
  91. //Output += counter + ".\t" + volume;
  92. //Output Dialog Box Outside informatio
  93. JTextArea area = new JTextArea(15, 10);
  94. area.setText(output);
  95. JScrollPane scroller = new JScrollPane(area);
  96. JOptionPane.showMessageDialog(null, scroller, "Volume of " + volume, JOptionPane.INFORMATION_MESSAGE);
  97. System.exit(0);//End Program
  98. }//End Main
  99. }//End Class Rectangular Prism
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
SonicSlayer is offline Offline
5 posts
since Oct 2004
Oct 18th, 2004
0

Re: Need some help with my do-while and while loops

where is your while loop ?? I didnt find any in your code.
Team Colleague
Reputation Points: 45
Solved Threads: 56
Unauthenticated Liar
nanosani is offline Offline
1,767 posts
since Jul 2004
Oct 18th, 2004
0

Re: Need some help with my do-while and while loops

Case 1 and 2 I cannot come up with the solution.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
SonicSlayer is offline Offline
5 posts
since Oct 2004
Oct 18th, 2004
0

Re: Need some help with my do-while and while loops

To solve the problem with not looping to ask for a new input, you need to include the entire Dialog, Switch statements in a Do-while loop too.
In essance your main app, only asks for input once, and then processes the switch statment, and then exits the program.
Reputation Points: 11
Solved Threads: 1
Junior Poster in Training
jerbo is offline Offline
84 posts
since Sep 2004
Oct 18th, 2004
0

Re: Need some help with my do-while and while loops

I have updated my solution to work better...I need now to find a answer to my solutions because they are not all listed.

Java Syntax (Toggle Plain Text)
  1. import javax.swing.*;
  2.  
  3. public class RectangularPrism
  4. {
  5. public static void main(String args[])
  6. {
  7. int counter = 0; //Sets counter to 1
  8. int a, b, c, volume; //Volume of rectangular prism
  9. int choice; //User's choice of method to solve
  10. String inputFeet, inputChoice; //Value entered by user
  11. String output = "Solution #\tL*W*H\n";
  12.  
  13. //Input Dialog Box for volume of recangular prism
  14. inputFeet = JOptionPane.showInputDialog("Enter volume of a rectangular prism in feet");
  15. volume = Integer.parseInt(inputFeet);
  16.  
  17. //Input Dialog Box for choice of repetition method
  18. inputChoice = JOptionPane.showInputDialog("Enter 1 to solve by While loops\nEnter 2 to solve by For loops\nEnter 3 to solve by a Do-While loop");
  19. choice = Integer.parseInt(inputChoice);
  20.  
  21. //Start user choice selection
  22. switch (choice)
  23. {
  24. case 1: //While loop solution
  25. a = 0; b = 0; c = 0; i = 0;
  26. while (a <= volume)
  27. {
  28. b = a + 1;
  29. while (b <= volume)
  30. {
  31. c = 1;
  32. if (a * b * c == volume)
  33. {
  34. counter++;
  35. output += counter + ".\t" + a + ", " + b + ", " + c + "\n";
  36. }
  37. c++;
  38. b++;
  39. }
  40. a++;
  41. }
  42.  
  43. break;
  44.  
  45. case 2: //For loop solution
  46.  
  47. for (a = 0; a <= volume; a++)
  48. {
  49. for (b = a + 1; b <= volume; b++)
  50. {
  51. c = 1;
  52. if (a * b * c == volume)
  53. {
  54. counter++;
  55. output += counter + ".\t" + a + ", " + b + ", " + c + "\n";
  56. }
  57. c++;
  58. }
  59. }
  60. break;
  61.  
  62. case 3: //Do-while loop solution
  63. a = 0; b = 0; c = 0;
  64. do
  65. {
  66. b = a + 1;
  67. do
  68. {
  69. c = 1;
  70. if (a * b * c == volume)
  71. {
  72. counter++;
  73. output += counter + ".\t" + a + ", " + b + ", " + c + "\n";
  74. }
  75. c++;
  76. b++;
  77. }while(b <= volume);
  78. a++;
  79. }while(a <= volume);
  80. break;
  81.  
  82. default: //Message diplayed if not a valid choice is chosen
  83. JOptionPane.showMessageDialog(null,"Please enter a valid choice!");
  84. }
  85.  
  86. //Output += counter + ".\t" + volume;
  87. //Output Dialog Box Outside informatio
  88. JTextArea area = new JTextArea(15, 10);
  89. area.setText(output);
  90. JScrollPane scroller = new JScrollPane(area);
  91. JOptionPane.showMessageDialog(null, scroller, "Volume of " + volume, JOptionPane.INFORMATION_MESSAGE);
  92. System.exit(0);//End Program
  93. }//End Main
  94. }//End Class Rectangular Prism
Reputation Points: 10
Solved Threads: 0
Newbie Poster
SonicSlayer is offline Offline
5 posts
since Oct 2004
Oct 18th, 2004
0

Re: Need some help with my do-while and while loops

Almost perfect!!! One more thing to do is get my while loop solutions right (case 1). Everything else is great!!! This should not be hard for someone to figure out.

Java Syntax (Toggle Plain Text)
  1. import javax.swing.*;
  2.  
  3. public class RectangularPrism
  4. {
  5. public static void main(String args[])
  6. {
  7. int counter = 0; //Sets counter to 1
  8. int a, b, c, volume; //Volume of rectangular prism
  9. int choice; //User's choice of method to solve
  10. String inputFeet, inputChoice; //Value entered by user
  11. boolean run = false; //Choice Selection is wrong
  12. String output = "Solution #\tL*W*H\n";
  13.  
  14. //Input Dialog Box for volume of recangular prism
  15. inputFeet = JOptionPane.showInputDialog("Enter volume of a rectangular prism in feet");
  16. volume = Integer.parseInt(inputFeet);
  17. while(!run)
  18. {
  19. //Input Dialog Box for choice of repetition method
  20. inputChoice = JOptionPane.showInputDialog("Enter 1 to solve by While loops\nEnter 2 to solve by For loops\nEnter 3 to solve by a Do-While loop");
  21. choice = Integer.parseInt(inputChoice);
  22. if ((choice <= 3) && (choice >= 1))
  23. {
  24. //Start user choice selection
  25. switch (choice)
  26. {
  27. case 1: //While loop solution
  28. a = 0;
  29. while (a <= volume)
  30. {
  31. b = a;
  32. while (b <= volume)
  33. {
  34. c = a;
  35. while (c <= volume)
  36. {
  37. if (a * b * c == volume)
  38. {
  39. counter++;
  40. output += counter + ".\t" + a + ", " + b + ", " + c + "\n";
  41. }
  42. c++;
  43. }
  44. b++;
  45. }
  46. a++;
  47. }
  48.  
  49. break;
  50.  
  51. case 2: //For loop solution
  52. for (a = 0; a <= volume; a++)
  53. {
  54. for (b = a; b <= volume; b++)
  55. {
  56. for (c = b; c <= volume; c++)
  57. {
  58. if (a * b * c == volume)
  59. {
  60. counter++;
  61. output += counter + ".\t" + a + ", " + b + ", " + c + "\n";
  62. }
  63. }
  64. }
  65. }
  66. break;
  67.  
  68. case 3: //Do-while loop solution
  69. a = 0;
  70. do
  71. {
  72. b = a;
  73. do
  74. {
  75. c = b;
  76. do
  77. {
  78. if (a * b * c == volume)
  79. {
  80. counter++;
  81. output += counter + ".\t" + a + ", " + b + ", " + c + "\n";
  82. }c++;
  83. }while(c <= volume);
  84. b++;
  85. }while(b <= volume);
  86. a++;
  87. }while(a <= volume);
  88. break;
  89.  
  90. default: //Message diplayed if not a valid choice is chosen
  91. JOptionPane.showMessageDialog(null,"Please enter a valid choice!");
  92. }
  93.  
  94. //Output Dialog Box Outside information
  95. JTextArea area = new JTextArea(15, 10);
  96. area.setText(output);
  97. JScrollPane scroller = new JScrollPane(area);
  98. JOptionPane.showMessageDialog(null, scroller, "Volume of " + volume, JOptionPane.INFORMATION_MESSAGE);
  99. run = true;
  100. }
  101. else//Message displayed if not a valid choice is chosen
  102. {
  103. JOptionPane.showMessageDialog(null,"Please enter a valid choice!");
  104. }//End If/Else Statement
  105. }//End While Statement
  106. System.exit(0);//End Program
  107. }//End Main
  108. }//End Class Rectangular Prism
Reputation Points: 10
Solved Threads: 0
Newbie Poster
SonicSlayer is offline Offline
5 posts
since Oct 2004
Oct 19th, 2004
0

Re: Need some help with my do-while and while loops

Finnaly finish it...In case 1 it is c = b not c = a. Thanks to everyone who tryed to help.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
SonicSlayer is offline Offline
5 posts
since Oct 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: twenty integers in an array
Next Thread in Java Forum Timeline: Help! Erorr in my Switch Statement





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC