Hello All,
I am hoping one of you Java Jedi's can help me with my code. Currently my code works and gives the end result, but I need to use a method perimeter() in a square class and then the other classes circle, rectangle, triangle should inherit it. I understand how to extend the classes, but I am unsure how to create the method perimeter() and have the other classes use it. also, I need to create a loop to continue to prompt until you exit. Can you help? i am desperate!! thx for any help!
import javax.swing.*;
import javax.swing.UIManager;
public class square
{
public static void main(String[] args)
{
double sq;
double cir;
double tri;
double rec;
String shape2 = JOptionPane.showInputDialog("Enter shape you want to calculate the circumference for - Enter 1-square, 2-rectangle, 3-circle, 4-triangle, and 5-Exit. ");
double shape = Double.parseDouble(shape2);
if (shape ==1){
String square2 = JOptionPane.showInputDialog("Enter numeric value in centimeters ");
double square = Double.parseDouble(square2);
sq= 4 * square;
JOptionPane.showMessageDialog(null,
"You selected Square \n Length of each side= " + square + " cm \n Area=" + sq +"cm");
}
if (shape ==2){
String H2 = JOptionPane.showInputDialog("Enter numeric Length in centimeters ");
double H = Double.parseDouble(H2);
String L2 = JOptionPane.showInputDialog("Enter numeric Height in centimeters ");
double L = Double.parseDouble(L2);
rec= H + L * 2;
JOptionPane.showMessageDialog(null,
"You selected Rectangle \n Height= " + H + " cm \n Length= " + L + "cm \n Area=" + rec +"cm");
}
if (shape ==3){
String circle = JOptionPane.showInputDialog("Enter method to calculate circumference of a circle, Enter 1-Diameter or 2-Radius ");
double circle2 = Double.parseDouble(circle);
String value = JOptionPane.showInputDialog("Enter numeric value in centimeters ");
double value2 = Double.parseDouble(value);
if (circle2 ==1){
cir= 3.14 * value2;
JOptionPane.showMessageDialog(null,
"You selected Circle \n circumference = " + cir +"cm");
}
else {
cir= 3.14 * (value2*2);
JOptionPane.showMessageDialog(null,
"You selected Circle \n circumference = " + cir +"cm");
}}
if (shape ==4){
String triangle2 = JOptionPane.showInputDialog("Enter numeric value in centimeters");
double triangle = Double.parseDouble(triangle2);
tri= 3 * triangle;
JOptionPane.showMessageDialog(null,
"You selected Triangle \n Length of each side= " + triangle + " cm \n Area=" + tri +"cm");
}
if (shape ==5){
System.exit(0);
}
System.exit(0);
}}