Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
8.9 kB
8
Indexable
Never
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Act3 extends JFrame {

    private JComboBox<String> shapeComboBox;
    private JTextField lengthTextField;
    private JTextField widthTextField;
    private JButton computeButton;
    private JLabel perimeterLabel;
    private JLabel areaLabel;
    private JTextField perimeterTextField;
    private JTextField areaTextField;

    private Shape shape;

    public Act3() {
        super("Shape Calculator");
        setLayout(new GridBagLayout());

        GridBagConstraints constraints = new GridBagConstraints();

        String[] shapes = {"Circle", "Rectangle", "Square", "Triangle"};
        shapeComboBox = new JComboBox<>(shapes);
        shapeComboBox.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String shapeName = (String) shapeComboBox.getSelectedItem();
                if (shapeName.equals("Circle")) {
                    shape = new Circle();
                    lengthTextField.setText("Radius:");
                    widthTextField.setText("");
                    widthTextField.setEnabled(false);
                } else if (shapeName.equals("Rectangle")) {
                    shape = new Rectangle();
                    lengthTextField.setText("Length:");
                    widthTextField.setText("Width:");
                    widthTextField.setEnabled(true);
                } 
                else if (shapeName.equals("Square")) {
                    shape = new Square();
                    lengthTextField.setText("Side:");
                    widthTextField.setText("");
                    widthTextField.setEnabled(false);
                } else if (shapeName.equals("Triangle")) {
                    shape = new Triangle();
                    lengthTextField.setText("Base:");
                    widthTextField.setText("Height:");
                    widthTextField.setEnabled(true);
                }
            }
        });
        constraints.gridx = 0;
        constraints.gridy = 0;
        constraints.gridwidth = 2;
        constraints.fill = GridBagConstraints.HORIZONTAL;
        add(shapeComboBox, constraints);

        lengthTextField = new JTextField(10);
        constraints.gridx = 0;
        constraints.gridy = 1;
        constraints.gridwidth = 1;
        constraints.fill = GridBagConstraints.HORIZONTAL;
        add(lengthTextField, constraints);

        widthTextField = new JTextField(10);
        constraints.gridx = 1;
        constraints.gridy = 1;
        constraints.gridwidth = 1;
        constraints.fill = GridBagConstraints.HORIZONTAL;
        add(widthTextField, constraints);

        computeButton = new JButton("Compute");
        constraints.gridx = 0;
        constraints.gridy = 2;
        constraints.gridwidth = 2;
        constraints.fill = GridBagConstraints.HORIZONTAL;
        constraints.insets = new Insets(10, 0, 0, 0);
        add(computeButton, constraints);

        computeButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    double length = Double.parseDouble(lengthTextField.getText());
                    double width = widthTextField.isEnabled()? Double.parseDouble(widthTextField.getText()) : 0;
                    shape.setMeasurement(length, width);
                    perimeterTextField.setText(String.format("%.2f", shape.getPerimeter()));
                    areaTextField.setText(String.format("%.2f", shape.getArea()));
                } catch (NumberFormatException ex) {
                    JOptionPane.showMessageDialog(Act3.this, "Please enter valid numerical values.");
                }
            }
        });

        perimeterLabel = new JLabel("Perimeter:");
        constraints.gridx = 0;
        constraints.gridy = 3;
        constraints.gridwidth = 1;
        constraints.fill = GridBagConstraints.HORIZONTAL;
        add(perimeterLabel, constraints);

        perimeterTextField = new JTextField(10);
        constraints.gridx = 1;
        constraints.gridy = 3;
        constraints.gridwidth = 1;
        constraints.fill = GridBagConstraints.HORIZONTAL;
        perimeterTextField.setEditable(false);
        add(perimeterTextField, constraints);

        areaLabel = new JLabel("Area:");
        constraints.gridx = 0;
        constraints.gridy = 4;
        constraints.gridwidth = 1;
        constraints.fill = GridBagConstraints.HORIZONTAL;
        add(areaLabel, constraints);

        areaTextField = new JTextField(10);
        constraints.gridx = 1;
        constraints.gridy = 4;
        constraints.gridwidth = 1;
        constraints.fill = GridBagConstraints.HORIZONTAL;
        areaTextField.setEditable(false);
        add(areaTextField, constraints);

        setSize(400, 500);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
        setLocationRelativeTo(null);
        setResizable(false);
    }

    public static void main(String[] args) {
        new Act3();
    }
}

interface Shape {
    void setMeasurement(double length, double width);
    double getPerimeter();
    double getArea();
}

class Circle implements Shape {
    private double radius;
    private double perimeter;
    private double area;

    @Override
    public void setMeasurement(double length, double width) {
        radius = length;
        perimeter = computePerimeter();
        area = computeArea();
    }

    @Override
    public double getPerimeter() {
        return perimeter;
    }

    @Override
    public double getArea() {
        return area;
    }

    private double computePerimeter() {
        return 2 * Math.PI * radius;
    }

    private double computeArea() {
        return Math.PI * radius * radius;
    }
}

class Rectangle implements Shape {
    private double length;
    private double width;
    private double perimeter;
    private double area;

    @Override
    public void setMeasurement(double length, double width) {
        this.length = length;
        this.width = width;
        perimeter = computePerimeter();
        area = computeArea();
    }

    @Override
    public double getPerimeter() {
        return perimeter;
    }

    @Override
    public double getArea() {
        return area;
    }

    private double computePerimeter() {
        return 2 * (length + width);
    }

    private double computeArea() {
        return length * width;
    }
}

class RightTriangle implements Shape {
    private double base;
    private double height;
    private double perimeter;
    private double area;

    @Override
    public void setMeasurement(double length, double width) {
        base = length;
        height = width;
        perimeter = computePerimeter();
        area = computeArea();
    }

    @Override
    public double getPerimeter() {
        return perimeter;
    }

    @Override
    public double getArea() {
        return area;
    }

    private double computePerimeter() {
        return base + height + Math.sqrt(base * base + height * height);
    }

    private double computeArea() {
        return 0.5 * base * height;
    }
}

class Square implements Shape {
    private double side;
    private double perimeter;
    private double area;

    @Override
    public void setMeasurement(double length, double width) {
        side = length;
        perimeter = computePerimeter();
        area= computeArea();
    }

    @Override
    public double getPerimeter() {
        return perimeter;
    }

    @Override
    public double getArea() {
        return area;
    }

    private double computePerimeter() {
        return 4 * side;
    }

    private double computeArea() {
        return side * side;
    }
}

class Triangle implements Shape {
    private double base;
    private double height;
    private double perimeter;
    private double area;

    @Override
    public void setMeasurement(double length, double width) {
        base = length;
        height = width;
        perimeter = computePerimeter();
        area = computeArea();
    }

    @Override
    public double getPerimeter() {
        return perimeter;
    }

    @Override
    public double getArea() {
        return area;
    }

    private double computePerimeter() {
        return base + height + Math.sqrt(base * base + height * height);
    }

    private double computeArea() {
        return 0.5 * base * height;
    }
}
Leave a Comment