Results 1 to 9 of 9
Thread: I'm lost :(
 03022009, 10:45 PM #1Member
 Join Date
 Feb 2009
 Posts
 42
 Rep Power
 0
I'm lost :(
These are the instructions:
Write a program to allow the user to calculate the area and perimeter of a square, or the area and circumference of a circle, or the area of a triangle.
To do this, the user will enter one of the following characters: S, C, or T. The program should then ask the user for the appropriate information in order to make the calculation, and should display the results of the calculation.
Use a while loop to allow the user to get multiple calculations of results. Ask the user to enter a Q to indicate that he or she wishes to quit.
See the example program execution shown in class.
Only display your name once.
The program should use dialog boxes.
When expecting an S, C, T, or Q the program should reject other characters with an appropriate message.
Get extra points for allowing both the uppercase and lowercase versions of a valid character to work.
Here is what I have so far
import javax.swing.JOptionPane;
public class ShapesCalcLoop {
public static void main(String[] args) {
//Prompt the user to Enter S, C, T, or Q to quit
char shape = JOptionPane.showInputDialog("Enter S, C, T, or Q to quit: ").charAt(0);
switch(shape) {
while (shape != 'Q'){
case 's':;
case 'S': {
String lengthString = JOptionPane.showInputDialog("Enter length of side: ");
double length = Double.parseDouble(lengthString);
//Calculate perimeter and area of square
double perimeter= ( 4 * length);
double area=(length*length);
String output = "Perimeter is equal to: " + perimeter + "\n" + "Area is equal to: " + area;
JOptionPane.showMessageDialog(null,output);
}; break;
case 'c':;
case 'C': {
String radiusString = JOptionPane.showInputDialog("Enter radius: ");
double radius = Double.parseDouble(radiusString);
//Calculate circumference and area of circle
double circumference= (2*3.14*radius);
double area = (Math.PI * (radius * radius));
String output2 ="Circumference is equal to: " + circumference + "\n" + "Area is equal to: " + area;
JOptionPane.showMessageDialog(null,output2);
}; break;
case 't':;
case 'T': {
String baseString = JOptionPane.showInputDialog("Enter length of base: ");
double base = Double.parseDouble(baseString);
String heightString = JOptionPane.showInputDialog("Enter height of triange: ");
double height = Double.parseDouble(heightString);
//Calculate area of triangle
double area = (base * height) / 2;
String output3 = "Area is equal to: " + area;
JOptionPane.showMessageDialog(null,output3);
}; break;
default: {
JOptionPane.showMessageDialog(null,"Incorrect variable please enter S,C, or T only");
System.exit(1);
}
};
}
JOptionPane.showMessageDialog(null,"This program was written by Justeena Leonard");
}
}
 03022009, 11:49 PM #2Senior Member
 Join Date
 Sep 2008
 Posts
 564
 Rep Power
 7
and what exactly would you like help with?
 03022009, 11:54 PM #3Member
 Join Date
 Feb 2009
 Posts
 42
 Rep Power
 0
I don't really understand how to do the which loop
 03022009, 11:56 PM #4Member
 Join Date
 Feb 2009
 Posts
 42
 Rep Power
 0
I mean the while loop
 03022009, 11:58 PM #5Member
 Join Date
 Feb 2009
 Posts
 42
 Rep Power
 0
I don't even know where to start
 03032009, 12:02 AM #6Senior Member
 Join Date
 Sep 2008
 Posts
 564
 Rep Power
 7
in that case, you need to organize yourself and plan how you're gonna tackle the project if you don't know where to start.
 03032009, 12:31 AM #7Member
 Join Date
 Feb 2009
 Posts
 42
 Rep Power
 0
I made a flow chart I know where i want to put the while loop and what i want it to do but i really don't know how to write it up

have a look here: Sun Java Tutorial: The while and dowhile Statements
 03032009, 05:18 AM #9Member
 Join Date
 Feb 2009
 Posts
 42
 Rep Power
 0
Ok I looked at the tutorial and built this but when i run it the loop doesn't let me pick another shape it just keeps asking me to enter the numbers
import javax.swing.JOptionPane;
public class ShapesCalcLoop {
public static void main(String[] args) {
//Prompt the user to Enter S, C, T, or Q to quit
char shape = JOptionPane.showInputDialog("Enter S, C, T, or Q to quit: ").charAt(0);
while (shape != 'Q'){
switch (shape){
case 's':;
case 'S': {
String lengthString = JOptionPane.showInputDialog("Enter length of side: ");
double length = Double.parseDouble(lengthString);
//Calculate perimeter and area of square
double perimeter= ( 4 * length);
double area=(length*length);
String output = "Perimeter is equal to: " + perimeter + "\n" + "Area is equal to: " + area;
JOptionPane.showMessageDialog(null,output);
}; break;
case 'c':;
case 'C': {
String radiusString = JOptionPane.showInputDialog("Enter radius: ");
double radius = Double.parseDouble(radiusString);
//Calculate circumference and area of circle
double circumference= (2*3.14*radius);
double area = (Math.PI * (radius * radius));
String output2 ="Circumference is equal to: " + circumference + "\n" + "Area is equal to: " + area;
JOptionPane.showMessageDialog(null,output2);
}; break;
case 't':;
case 'T': {
String baseString = JOptionPane.showInputDialog("Enter length of base: ");
double base = Double.parseDouble(baseString);
String heightString = JOptionPane.showInputDialog("Enter height of triange: ");
double height = Double.parseDouble(heightString);
//Calculate area of triangle
double area = (base * height) / 2;
String output3 = "Area is equal to: " + area;
JOptionPane.showMessageDialog(null,output3);
}; break;
default: {
JOptionPane.showMessageDialog(null,"Incorrect variable please enter S,C, or T only");
System.exit(1);
};
JOptionPane.showMessageDialog(null,"This program was written by Justeena Leonard");
}
}
}
}
Similar Threads

So Lost
By kandt in forum New To JavaReplies: 5Last Post: 12132008, 10:55 PM 
need help with program im lost
By lifeturn in forum JCreatorReplies: 1Last Post: 10282008, 08:09 PM 
Lost my javadocs
By orchid in forum EclipseReplies: 3Last Post: 04302008, 10:45 PM 
Absolutely Lost
By Lehane_9 in forum New To JavaReplies: 2Last Post: 12032007, 07:25 PM 
Help Needed  I'm so lost
By adlb1300 in forum New To JavaReplies: 3Last Post: 11142007, 02:54 AM
Bookmarks