vendredi 14 juillet 2017

How to create sub-class in my program (Shapes Area Calculator | JAVA)

I need help because I do have a problem on creating a sub-class in my program. I do only have a main-class as you can see below. I don't know how to create a sub-class for my formula to seperate it from my main class.

import javax.swing.JOptionPane;
import java.util.*;

public class shapes {

public static void main(String[] args) {


    String inputStr = JOptionPane.showInputDialog("Type 1 for the area of Circle \nType 2 for area of Triangle \nType 3 for Square \nType 4 for Rectangle \nType 5 for Octagon \nType 0 to exit.");

    int i = Integer.parseInt(inputStr);

    if (i == 1) {
        String inpu = JOptionPane.showInputDialog("Enter radius of a circle: ");
        double radius = Integer.parseInt(inpu);
        b.areaCircle(radius);
    }
    if (i == 2) {
        String input = JOptionPane.showInputDialog("Enter the base of a triangle: ");
        int n1 = Integer.parseInt(input);
        String inp = JOptionPane.showInputDialog("Enter the height of a triangle: ");
        int n2 = Integer.parseInt(inp);
        areaTriangle(n1, n2);
    }


    if (i == 3) {
        String sq = JOptionPane.showInputDialog("Enter the width of a square: ");
        int sqno1 = Integer.parseInt(sq);
        String sq2 = JOptionPane.showInputDialog("Enter the height of a square: ");
        int sqno2 = Integer.parseInt(sq2);
        areaSquare(sqno1, sqno2);
    }


    if (i == 4) {
        String inp = JOptionPane.showInputDialog("Enter the length of a rectangle: ");
        int m1 = Integer.parseInt(inp);
        String inp2 = JOptionPane.showInputDialog("Enter the width of a rectangle: ");
        int m2 = Integer.parseInt(inp2);
        areaRectangle(m1, m2);
    }

    if (i == 5) {
        String oct = JOptionPane.showInputDialog("Enter the side of octagon: ");
        double octno = Double.parseDouble(oct);
        areaOctagon(octno);
    }

    else {
        return;
    }
}



public static void areaCircle(double radius) {
    double areac = Math.PI * (radius * radius); // MATH PI is equivalent of pi = 3.14159
    JOptionPane.showMessageDialog(null, "The area of a circle is: " + areac);
}

public static void areaTriangle(int n1, int n2) {
    int areat = (n1 * n2) / 2;
    JOptionPane.showMessageDialog(null, "The area of a triangle is: " + areat);
}

public static void areaSquare(int sqno1, int sqno2) {
    int asquare = (sqno1 * sqno2);
    JOptionPane.showMessageDialog(null, "The area of square is " + asquare);
}

public static void areaRectangle(int m1, int m2) {
    int arear = (m1 * m2);
    JOptionPane.showMessageDialog(null, "The area of a rectangle is: " + arear);

}

public static void areaOctagon(double octno) {
    double aoct = (2 * 2.414 * octno);
    JOptionPane.showMessageDialog(null, "The area of a octagon is: " + aoct);

}


}

Can you help me to create a new sub-class for my program? Thanks

Aucun commentaire:

Enregistrer un commentaire