# I'm lost :(

• 03-02-2009, 10:45 PM
leonardjr
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.

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': {
//Calculate circumference and area of circle
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");
}
}
• 03-02-2009, 11:49 PM
emceenugget
and what exactly would you like help with?
• 03-02-2009, 11:54 PM
leonardjr
I don't really understand how to do the which loop
• 03-02-2009, 11:56 PM
leonardjr
I mean the while loop
• 03-02-2009, 11:58 PM
leonardjr
I don't even know where to start
• 03-03-2009, 12:02 AM
emceenugget
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.
• 03-03-2009, 12:31 AM
leonardjr
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
• 03-03-2009, 02:30 AM
Fubarable
• 03-03-2009, 05:18 AM
leonardjr
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': {
//Calculate circumference and area of circle
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");
}
}

}
}