「java几何包」Java几何

博主:adminadmin 2023-01-12 09:39:08 613

今天给各位分享java几何包的知识,其中也会对Java几何进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

JAVA里有Shape数据类型么?

JAVA里有Shape数据类型,是一个接口,具体解释如下:

public interface Shape

Shape 接口提供了表示一些几何形状的对象的定义。Shape 是由 PathIterator 对象描述的,它可以表示 Shape 的轮廓以及确定该轮廓如何将 2D 平面划分成内点和外点的规则。每个 Shape 对象都提供回调,以获取几何形状的边框,确定点或矩形是部分还是全部位于 Shape 内部,并检索一个描述 Shape 轮廓的轨迹路径的 PathIterator 对象。

java求几何图形面积

代码如下:

abstract class Geometry {

abstract double getArea();

}

// 三角形

class Triangle extends Geometry {

// 边

private double a;

// 边

private double b;

// 边

private double c;

public Triangle(double a, double b, double c) {

this.a = a;

this.b = b;

this.c = c;

}

@Override

double getArea() {

double p = (a + b + c) / 2;

return Math.sqrt(p * (p - a) * (p - b) * (p - c));

}

}

// 圆

class Circle extends Geometry {

// 半径

private double r;

public Circle(double r) {

this.r = r;

}

@Override

double getArea() {

return Math.PI * r * r;

}

}

// 梯形

class Ladder extends Geometry {

// 上底

private double a;

// 下底

private double b;

// 高

private double h;

public Ladder(double a, double b, double h) {

this.a = a;

this.b = b;

this.h = h;

}

@Override

double getArea() {

return (a + b) * h / 2;

}

}

class Test {

double computeGeometryArea(Geometry g) {

return g.getArea();

}

}

public class Demo {

public static void main(String[] args) {

Test test = new Test();

Triangle triangle = new Triangle(3, 4, 6);

System.out.println("三角形面积:" + test.computeGeometryArea(triangle));

Circle circle = new Circle(10);

System.out.println("圆面积:" + test.computeGeometryArea(circle));

Ladder ladder = new Ladder(10, 20, 15);

System.out.println("梯形面积:" + test.computeGeometryArea(ladder));

}

}

运行结果:

用JAVA编写求解几何图形的应用程序?

1. 抽象形状类

package com;

//抽象的形状类

public abstract class Shape{

}

2. 显示接口

package com;

//接口

public interface IDisplay{

void display(); //显示图形的基本信息

double getArea(); //计算面积

double getGirth(); //计算周长

}

3. 三角形类

package com.tri;

import com.*;

//三角形类

public class Triangle extends Shape implements IDisplay{

protected double a;

protected double b;

protected double c;

public Triangle(double a, double b, double c){

this.a = a;

this.b = b;

this.c = c;

}

@Override

public double getArea() {

double s = (a + b + c) / 2;

return Math.sqrt(s*(s-a)*(s-b)*(s-c));

}

@Override

public double getGirth() {

return this.a + this.b + this.c;

}

@Override

public void display() {

System.out.println("三角形");

System.out.println("边长:" + a + ", " + b + ", " + c);

}

}

4. 矩形类

package com.rec;

import com.*;

//矩形类

public class Rectangle extends Shape implements IDisplay {

protected double width;

protected double height;

public Rectangle(double width, double height){

this.width = width;

this.height = height;

}

@Override

public double getArea() {

return this.width * this.height;

}

@Override

public double getGirth() {

return 2 * ( this.width + this.height);

}

@Override

public void display() {

System.out.println("矩形");

System.out.println("宽:" + this.width + ", 高:" + this.height);

}

}

5. 圆类

package com.cir;

import com.*;

//圆类

public class Circle extends Shape implements IDisplay {

protected double radius;

public Circle(double radius){

this.radius = radius;

}

@Override

public double getArea() {

return Math.PI * this.radius * this.radius;

}

@Override

public double getGirth() {

return 2 * Math.PI * this.radius;

}

@Override

public void display() {

System.out.println("圆");

System.out.println("半径:" + this.radius);

}

}

6. 正多边形类

package com.mul;

import com.*;

//正多边形类

public class MulSide extends Shape implements IDisplay {

protected double side; //边长

protected int n; //边数

public MulSide(double side, int n){

this.side = side;

this.n = n;

}

@Override

public double getArea() {

return n * side * side * Math.tan((n-2) * Math.PI / (2 * n)) / 4;

}

@Override

public double getGirth() {

return this.side * this.n;

}

@Override

public void display() {

System.out.println("正多边形");

System.out.println("连长:" + this.side + ", 边数:" + this.n);

}

}

7. 主类(测试类)

package com;

import java.util.Scanner;

import com.cir.Circle;

import com.mul.MulSide;

import com.rec.Rectangle;

import com.tri.Triangle;

public class Test22 {

public static void main(String[] args) {

Scanner scan = new Scanner(System.in);

double a, b, c;

double width, height;

double radius;

double side;

int n;

IDisplay s;

System.out.println("请输入三角形的基本信息");

System.out.print("边长1:");

a = scan.nextDouble();

System.out.print("边长2:");

b = scan.nextDouble();

System.out.print("边长3:");

c = scan.nextDouble();

s = new Triangle(a, b, c);

s.display();

System.out.println("三角形的面积:" + s.getArea());

System.out.println("三角形的周长:" + s.getGirth());

System.out.println("请输入矩形的基本信息");

System.out.print("宽:");

width = scan.nextDouble();

System.out.print("高:");

height = scan.nextDouble();

s = new Rectangle(width, height);

s.display();

System.out.println("矩形的面积:" + s.getArea());

System.out.println("矩的周长:" + s.getGirth());

System.out.println("请输入圆的基本信息");

System.out.print("半径:");

radius = scan.nextDouble();

s = new Circle(radius);

s.display();

System.out.println("圆的面积:" + s.getArea());

System.out.println("圆的周长:" + s.getGirth());

System.out.println("请输入正多边形的基本信息");

System.out.print("边长:");

side = scan.nextDouble();

System.out.print("边数:");

n = scan.nextInt();

s = new MulSide(side, n);

s.display();

System.out.println("正多边形的面积:" + s.getArea());

System.out.println("正多边形的周长:" + s.getGirth());

}

}

运行测试:

请输入三角形的基本信息

边长1:3

边长2:4

边长3:5

三角形

边长:3.0, 4.0, 5.0

三角形的面积:6.0

三角形的周长:12.0

请输入矩形的基本信息

宽:3

高:4

矩形

宽:3.0, 高:4.0

矩形的面积:12.0

矩的周长:14.0

请输入圆的基本信息

半径:2

半径:2.0

圆的面积:12.566370614359172

圆的周长:12.566370614359172

请输入正多边形的基本信息

边长:2

边数:6

正多边形

连长:2.0, 边数:6

正多边形的面积:10.39230484541326

正多边形的周长:12.0

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才能设置参数值

import java.awt.Graphics是什么意思

导入java包;

java.awt.Graphics是一个用来绘制2D图像必须导入的java包,提供对图形图像的像素,颜色的绘制。

关于java几何包和Java几何的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。