import javax.swing.JOptionPane;
public class CylinderTest{
static double aRadius;
static double aHeight;
public static void main(String[] args) {
String outputString = "";
Cylinder[] cylAr = new Cylinder[3];
for (int i = 0; i < cylAr.length; i++) {
while (true) {
try {
aRadius = Double.parseDouble(JOptionPane //this is where I lose it.
.showInputDialog("Please, enter cylinder "
+ (i + 1) + "'s radius: "));
} catch (NumberFormatException z) {
JOptionPane.showMessageDialog(null,
"Sorry, invalid input.", "Invalid",
JOptionPane.ERROR_MESSAGE);
continue;
}
if(aRadius <= 0){
JOptionPane.showMessageDialog(null, "Radius cannot be zero or less.", "Invalid", JOptionPane.ERROR_MESSAGE);
continue;
}
cylAr[i].setRadius(aRadius);
break;
}
while (true) {
try {
aHeight = Double.parseDouble(JOptionPane
.showInputDialog("Please, enter cylinder "
+ (i + 1) + "'s height: "));
} catch (NumberFormatException z) {
JOptionPane.showMessageDialog(null,
"Sorry, invalid input.", "Invalid",
JOptionPane.ERROR_MESSAGE);
continue;
}
if(aHeight <= 0){
JOptionPane.showMessageDialog(null, "Height cannot be zero or less.", "Invalid", JOptionPane.ERROR_MESSAGE);
continue;
}
cylAr[i].setHeight(aHeight);
break;
}
cylAr[i] = new Cylinder(cylAr[i].getRadius(), cylAr[i].getHeight());
}
for(int j = 0; j < cylAr.length; j++){
outputString += "Cylinder " + (j + 1) + ": \n" + "Radius: "
+ cylAr[j].getRadius() + " \n" + "Height: " + cylAr[j].getHeight() + " \n"
+ "Volume: " + cylAr[j].getVolume() + "\n-------------------\n";
}
JOptionPane.showMessageDialog(null, outputString, "Complete!",
JOptionPane.INFORMATION_MESSAGE);
}
}
class Cylinder {
private double radius;
private double height;
public Cylinder(double radius, double height) {
this.radius = radius;
this.height = height;
}
public double getRadius() {
return radius;
}
public void setRadius(double aRadius) {
aRadius = radius;
}
public double getHeight() {
return height;
}
public void setHeight(double aHeight){
aHeight = height;
}
public double getVolume() {
return (radius * radius * height * Math.PI);
}
}
So what's up? Will my program work like I think it will? I need to add DecimalFormat yet but I want it to work first. Thanks either way.

New Topic/Question
Reply




MultiQuote




|