import javax.swing.JOptionPane;
class homework{
public static void main(string[]args)
{
String ipt,n,h,r,r2,h2,b1,b2;
Double iptipt;

JOptionPane.showMessageDialog(null,"1… of a Circle\n" + "2.Area of a Trapezoid\n" + "3.Volume of a Sphere\n" + "4.Volume of a Cylinder");

ipt = JOptionPane.showInputDialog(null,"Input your desired choice(1,2,3,4)");

iptipt = Double.parseDouble(ipt);

if (iptipt == 1) ------ IF ELSE STATEMENTS WILL BE IN CASE
{
JOptionPane.showMessageDialog(null," Area of a Circle");}......
n = JOptionPane.showInputDialog(null,"Enter a Number"); } THIS SHOULD BE
int num = Integer.parseInt(n); } IN A METHOD
int form1 = (3.14 * (num*2)); } AND SO AS
JOptionPane.showMessageDialog(null,"… is "+form1); } THE OTHERS
}

else if (iptipt == 2)
{
JOptionPane.showMessageDialog(null,"… of a Trapezoid");
h = JOptionPane.showInputDialog(null,"Enter Height");
int heig = Integer.parseInt(h);
b1 = JOptionPane.showInputDialog(null,"Enter Base1");
int bas = Integer.parseInt(b1);
b2 = JOptionPane.showInputDialog(null,"Enter Base2");
int base = Integer.parseInt(b2);
int form2 = (heig * (bas+base) / 2);
JOptionPane.showMessageDialog(null,"… is "+form2);
}
else if (iptipt == 3)
{
JOptionPane.showMessageDialog(null,"… of a Sphere");
r = JOptionPane.showInputDialog(null,"Enter Radius");
int rad = Integer.parseInt(r);
int form3 = (.75 * 3.14 *(rad*2));
JOptionPane.showMessageDialog(null,"… is "+form3);
}
else
{
JOptionPane.showMessageDialog(null,"… of a Cylinder");
r2 = JOptionPane.showInputDialog(null,"Enter Radius");
int radiu = Integer.parseInt(r2);
h2 = JOptionPane.showInputDialog(null,"Enter Height");
int heigh = Integer.parseInt(h2);
int form4 = (3.14 * (radiu * 2) + heigh);
JOptionPane.showMessageDialog(null,"… is "+form4);
}
}
}

so in the case statement you will just put a code to call for the method where you have put the above codes.. try running this you would understand.. this is from jcreator(java language). instead of putting all of those codes in the if else, just put it in a method.. area of a circle = 1 method, area of a trapezoid = 1 method, volume of a cylinder = 1 method, volume of a sphere = 1 method.. then you will call it in the case statements. which i really don't know how.. i just need a syntax, and a little example too. please help thankyou.. sorry if some statements are redundant.. i just want to make it clear.

Recommended Answers

All 5 Replies

First thing, try parsing the input (ipt) as an int. Much friendlier.

If I can understand what you want to do, you want to make methods for finding the area of your various shapes and call them in your case statement?

Not difficult. Where are you starting from? Do you know how to write a simple method? For example, could you write a method to return the square of an integer?

JOptionPane.showMessageDialog(null,"… of a Cylinder");
r2 = JOptionPane.showInputDialog(null,"Enter Radius");
int radiu = Integer.parseInt(r2);
h2 = JOptionPane.showInputDialog(null,"Enter Height");
int heigh = Integer.parseInt(h2);
int form4 = (3.14 * (radiu * 2) + heigh);
JOptionPane.showMessageDialog(null,"… is "+form4);

- the above codes is what you will put in the method...and the name of this method is what you will put in the case statement to call it. and thats practically it..and no i do not know how to write a simple method.

Okay, this is a pretty remedial question, then. I don't have the time or inclination to write a tutorial on the basics of Java, when it's already been done. Go to the Google, my friend, and spend a few hours reading "Java basics tutorials" and "Java methods tutorials". These will be written by people who have spent more than a few minutes putting them together, and will be much more helpful to you than anything I throw together here.

i have done that. i wouldn't post here if i did understand tutorials and any examples on the net. but anyway thanks for your time and effort!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.