您的位置:

用java编程计算图形面积,编程计算图形的面积

本文目录一览:

Java编程——求解几何图形的周长、面积的程序。

/**

* The Class PerimeterArea. This Class is to compute perimeter and area

*/

public class PerimeterArea {

/**

* The main method.

*

* @param args

* the arguments

*/

public static void main(String[] args) {

Shape TriangleObj = new Triangle(3, 4, 5);// 定义三边为3,4,5的三角形

Shape RectangleObj = new Rectangle(5, 6);// 定义长为5,宽为6的矩形

Shape CircleObj = new Circle(5);// 定义半径为5的对象

// 打印各个形状的周长和面积

System.out.println(TriangleObj.toString());

System.out.println(RectangleObj.toString());

System.out.println(CircleObj.toString());

}

}

// 周长接口

interface perimeter {

public abstract double cutePerimeter();

}

// 面积接口

interface area {

public abstract double cuteArea();

}

// 抽象形状类

abstract class Shape implements perimeter, area {

public abstract double cutePerimeter();

public abstract double cuteArea();

}

// 三角形

class Triangle extends Shape {

private int aSide;

private int bSide;

private int cSide;

public Triangle(){}

public Triangle(int aSide, int bSide, int cSide) {

this.aSide = aSide;

this.bSide = bSide;

this.cSide = cSide;

}

public double cutePerimeter() {

return aSide + bSide + cSide;

}

public double cuteArea() {

//面积公式

/*设三边长分别为A、B、C,面积为S;周长的一半P为(A+B+C)/2.

S=√[P(P-A)*(P-B)*(P-C)].

√为开二次方. */

int x = (aSide + bSide + cSide) / 2;

return Math.sqrt(x*(x-aSide)*(x-bSide)*(x-cSide));

}

public int getASide() {

return aSide;

}

public void setASide(int side) {

aSide = side;

}

public int getBSide() {

return bSide;

}

public void setBSide(int side) {

bSide = side;

}

public int getCSide() {

return cSide;

}

public void setCSide(int side) {

cSide = side;

}

public String toString() {

return "三角形的周长为:" + cutePerimeter() + "\n三角形的面积为:" + cuteArea() + "\n";

}

}

// 矩形

class Rectangle extends Shape {

private int longLength;

private int widthLength;

public Rectangle(){}

public Rectangle(int longLength, int widthLength) {

this.longLength = longLength;

this.widthLength = widthLength;

}

public double cutePerimeter() {

return 2 * (longLength + widthLength);

}

public double cuteArea() {

return longLength * widthLength;

}

public int getLongLength() {

return longLength;

}

public void setLongLength(int longLength) {

this.longLength = longLength;

}

public int getWidthLength() {

return widthLength;

}

public void setWidthLength(int widthLength) {

this.widthLength = widthLength;

}

public String toString() {

return "矩形的周长为:" + cutePerimeter() + "\n矩形的面积为:" + cuteArea() + "\n";

}

}

// 圆形

class Circle extends Shape {

public final double pi = 3.14;

private double radius;

public Circle(){}

public Circle(double radius) {

this.radius = radius;

}

public double cutePerimeter() {

return 2 * pi * radius;

}

public double cuteArea() {

return pi * radius * radius;

}

public double getRadius() {

return radius;

}

public void setRadius(double radius) {

this.radius = radius;

}

public String toString() {

return "圆形的周长为:" + cutePerimeter() + "\n圆形的面积为:" + cuteArea() + "\n";

}

}

三角形面积已经修正~谢谢楼上的兄弟~set和get方法只是为了拓展性考虑~不局限于new才能设置参数值

java编程。类。 常见平面图形(如三角形、圆、矩形和正方形等)的面积。利用抽象类,编写程序实现该

abstract class Shape{

abstract double area();

}

class Circle extends Shape {

private double r;

private final double PI=3.14;

public Circle(double r) {

this.r = r;

}

@Override

double area() {

return PI*r*r;

}

}

 

class Rectangle extends Shape {

private double w;

private double h;

public Rectangle(double w, double h) {

this.w = w;

this.h = h;

}

@Override

double area() {

return w * h;

}

}

class Triangle extends Shape {

private double h;

private double l;

public Triangle(double h, double l) {

this.h = h;

this.l = l;

}

@Override

double area() {

return 0.5 * h * l;

}

}

class Square extends Rectangle {

public Square(double l) {

super(l, l);

}

}

public class Test {

public static void main(String[] args) {

System.out.println("圆 "+new Circle(1).area());

System.out.println("矩形 "+new Rectangle(4,2).area());

System.out.println("正方形 "+new Square(3).area());

System.out.println("三角形 "+new Triangle(3,4).area());

}

}

用java面向对象(刚学到类和对象,传参这一块)的方法求圆的周长和面积

public class Test{

public static void main(String[] args){

Test t = new Test();

String d = "5";

double girth = t.roundGirth(d);

System.out.println ("周长是: " + girth);

double area = t.roundArea(d);

System.out.println ("面积是: " + area);

}

private double roundGirth(String str){

double d = 0;//半径

double roundGirth = 0;

d = Double.parseDouble(str);

roundGirth = 2 * d * 3.14;

return roundGirth;

}

private double roundArea(String str){

double d = 0;//半径

double roundArea = 0;

d = Double.parseDouble(str);

roundArea = d * d * 3.14;

return roundArea;

}

}

java编程实现编写一个图形用户界面实现多边形面积计算要求建立坐标系输入多边形顶点数输出结果

import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.Container;

import java.awt.Dimension;

import java.awt.FlowLayout;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

import javax.swing.JScrollPane;

import javax.swing.JTextField;

public class CalculateArea extends JFrame {

private static final long serialVersionUID = 8109685421829572012L;

private JTextField cntField;

private JPanel panel;

private JScrollPane sPanel;

private JTextField[][] coordinateFields;

private JTextField resultField ;

public CalculateArea(String title){

super(title);

init();

}

public void init(){

Container c = this.getContentPane();

JPanel panel1 = new JPanel();

panel1.setPreferredSize(new Dimension(260, 30));

panel1.setLayout(new FlowLayout());

JLabel cntLab = new JLabel("多边形边数:");

cntLab.setPreferredSize(new Dimension( 70, 20));

panel1.add(cntLab);

cntField = new JTextField();

cntField.setPreferredSize(new Dimension(110, 20));

panel1.add(cntField);

JButton okBut = new JButton("确定");

okBut.setPreferredSize(new Dimension( 60, 20));

okBut.addActionListener(new CreatePanelActionListener(this));

panel1.add(okBut);

c.add(panel1,BorderLayout.NORTH);

//给确定按钮添加自定义事件

panel = new JPanel();

sPanel = new JScrollPane();

sPanel.setPreferredSize(new Dimension(240, 300));

sPanel.setViewportView(panel);

sPanel.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);

c.add(sPanel, BorderLayout.CENTER);

JPanel panel2 = new JPanel();

panel2.setPreferredSize(new Dimension(260, 30));

panel2.setLayout(new FlowLayout());

JButton calculateBut = new JButton("计算");

calculateBut.setPreferredSize(new Dimension( 60, 20));

panel2.add(calculateBut);

JLabel resultLab = new JLabel("计算结果为:");

resultLab.setPreferredSize(new Dimension( 70, 20));

panel2.add(resultLab);

resultField = new JTextField();

resultField.setPreferredSize(new Dimension(110, 20));

panel2.add(resultField);

calculateBut.addActionListener(new CalculateActionListener(this));

c.add(panel2,BorderLayout.SOUTH);

this.addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) {

super.windowClosing(e);

System.exit(0);

}

});

// this.setResizable(false); 

this.setSize(300, 500);

setLocationRelativeTo(null);

this.setVisible(true);

}

public static void main(String[] args) {

new CalculateArea("多边形面积计算");

}

public JTextField getCntField() {

return cntField;

}

public JPanel getPanel() {

return panel;

}

public JTextField[][] getCoordinateFields() {

return coordinateFields;

}

public void setCoordinateFields(JTextField[][] coordinateFields) {

this.coordinateFields = coordinateFields;

}

public JScrollPane getsPanel() {

return sPanel;

}

public JTextField getResultField() {

return resultField;

}

}

//计算面积的事件

class CalculateActionListener implements ActionListener{

private JFrame frame;

public CalculateActionListener(JFrame frame){

this.frame = frame;

}

public void actionPerformed(ActionEvent e) {

JPanel panel = ((CalculateArea)frame).getPanel();

JTextField[][] coordinateFields = ((CalculateArea)frame).getCoordinateFields();

JTextField resultField = ((CalculateArea)frame).getResultField();

int[][] coord = new int[2][coordinateFields[0].length];

if(coordinateFields==null){

JLabel label = new JLabel("请先输入多边形,然后填写坐标信息!");

label.setBounds(40, 200, 295, 20);

panel.add(label);

frame.repaint();

}else{

for (int i = 0; i  coordinateFields[0].length; i++) {

try{

String xStr = coordinateFields[0][i].getText();

String yStr =coordinateFields[1][i].getText();

int x = Integer.parseInt(xStr);

int y = Integer.parseInt(yStr);

coord[0][i] = x;

coord[1][i] = y;

}catch (Exception ex) {

resultField.setText("输入的坐标值有误,请修改后重新输入!");

frame.repaint();

return;

}

}

calulateArea(coord, resultField);

}

}

//根据坐标计算面积

//计算多边形面积公式为:0.5*abs(x1*y2-y1*x2+x2*y3-y2*x3+...+xn*y1-yn*x1) 

public void calulateArea(int[][] coord,JTextField resultField){

double area = 0;

int sum = 0;

for (int i = 0; i  coord[0].length-1; i++) {

sum = sum + coord[0][i]*coord[1][i+1]-coord[1][i]*coord[0][i+1];

}

sum = sum + coord[0][coord[0].length-1]*coord[1][0]-coord[1][coord[0].length-1]*coord[0][0];

area = 0.5*Math.abs(sum);

resultField.setText(area+"");

}

}

//生成填写坐标的面板的事件

class CreatePanelActionListener implements ActionListener{

private JFrame frame;

public CreatePanelActionListener(JFrame frame){

this.frame = frame;

}

public void actionPerformed(ActionEvent eve) {

String cntStr = ((CalculateArea)frame).getCntField().getText();

JPanel panel = ((CalculateArea)frame).getPanel();

panel.removeAll();

int cnt=0;

//获取填写的多边形坐标数

try{

cnt = Integer.parseInt(cntStr);

}catch (Exception e) {

JLabel label = new JLabel("请输入大于等于3的整数!");

label.setBounds(70, 200, 295, 20);

panel.add(label);

((CalculateArea)frame).repaint();

}

if(cnt=2){

JLabel label = new JLabel("请输入大于等于3的整数!");

label.setBounds(70, 200, 295, 20);

panel.add(label);

((CalculateArea)frame).repaint();

}else{

panel.setLayout(new FlowLayout());

panel.setPreferredSize(new Dimension(260,25*cnt+25));

panel.setSize(new Dimension(260,25*cnt+25));

//清除填写坐标面板上的所有控件

JLabel label = new JLabel("请输入每个点的坐标:");

label.setPreferredSize(new Dimension(260, 20));

panel.add(label);

label = new JLabel("(注:必须按顺时针或逆时针依次填入坐标!)");

label.setForeground(Color.red);

label.setPreferredSize(new Dimension(260, 20));

panel.add(label);

//填写坐标的标签和输入框

JLabel[] coordinateLabels = new JLabel[cnt];

JTextField[][] coordinateFields = new JTextField[2][cnt];

for (int i = 0; i  cnt; i++) {

coordinateLabels[i] = new JLabel("第"+(i+1)+"点:");

coordinateLabels[i].setPreferredSize(new Dimension( 45, 20));

panel.add(coordinateLabels[i]);

JLabel labelx = new JLabel("X:");

labelx.setPreferredSize(new Dimension(20, 20));

panel.add(labelx);

coordinateFields[0][i] = new JTextField();

coordinateFields[0][i].setPreferredSize(new Dimension(70, 20));

panel.add(coordinateFields[0][i]);

JLabel labely = new JLabel("Y:");

labely.setPreferredSize(new Dimension(20, 20));

panel.add(labely);

coordinateFields[1][i] = new JTextField();

coordinateFields[1][i].setPreferredSize(new Dimension(70, 20));

panel.add(coordinateFields[1][i]);

}

((CalculateArea)frame).setCoordinateFields(coordinateFields);

((CalculateArea)frame).repaint();

}

}

}

用java语言编程,求多种形状的面积之和

public Interface Shape

{ private double area;

public abstract double getArea();

}

然后对于各种形状分别定义,比如:

public Circle implements Shape

{ private double radius:

public Circle(double radius)

{ this.radius=radius;

this.area=radius*radius*Maths.PI;

}

public double getArea()

{ return area;

}

}

public Rectangle implements Shape

{ private double height;

private double width;

public Rectangle(double height,double width)

{ this.height=height;

this.width=width;

this.area=height*width;

}

public double getArea()

{ return area;

}

}

等等。

用的时候分别定义各个形状的具体参数。

求面积的和可以用

Object[] shapes;

double sum=0.0;

for(int i=0;ishapes.length;i++)

sum+=(Shape)shapes[i].getArea();

用java编写,求总面积

Shape.java

abstract public class Shape {

abstract public double area();

}

Circle.java

public class Circle extends Shape {

private double radius;

public Circle() {

}

public Circle(double radius) {

this.radius = radius;

}

@Override

public double area() {

return Math.PI * radius * radius;

}

}

Rectangle.java

public class Rectangle extends Shape {

private double length;

private double width;

public Rectangle(double length, double width) {

this.length = length;

this.width = width;

}

@Override

public double area() {

return length * width;

}

public Rectangle() {

}

}

Test.java

public class Test {

public static void main(String[] args) {

Shape[] shape = {new Circle(1), new Circle(2), new Rectangle(3, 2), new Rectangle(4, 3)};

double area_total = 0;

for(int i = 0; i shape.length; i++) {

area_total += shape[i].area();

}

System.out.println(area_total);

}

}