本文目錄一覽:
- 1、Java編程——求解幾何圖形的周長、面積的程序。
- 2、java編程。類。 常見平面圖形(如三角形、圓、矩形和正方形等)的面積。利用抽象類,編寫程序實現該
- 3、用java面向對象(剛學到類和對象,傳參這一塊)的方法求圓的周長和面積
- 4、java編程實現編寫一個圖形用戶界面實現多邊形面積計算要求建立坐標系輸入多邊形頂點數輸出結果
- 5、用java語言編程,求多種形狀的面積之和
- 6、用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);
}
}
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/280579.html