Java 应用程序,该程序中有 3 个类:Triangle、T rapezium 和 Circle,分别用来 表示三角形、梯形和圆形。
具体要求如下。
Triangle 类具有类型为 double 的三个边、周长以及面积属性,Triangle 类具有构造方法初始化属性和返回周长、面积以及修改三个边的功能。另外,Triangle 类还具有一个 boolean 型的属性,该属性用来判断三个数能否构成一个三角形。
Trapezium 类具有类型为 double 的上底、下底、高以及面积属性,具有构造方法初始化属性和返回面积的功能。
Circle 类具有类型为double的半径、周长以及面积属性,具有构造方法初始化属性和返回周长、面积的功能。
生成实例测试该类。
import java.text.DecimalFormat;
import java.util.Scanner;
//三角形
class Triangle {
double x;
double y;
double z;
double c = 0;
double area = 0;
public Triangle() {
}
public void change(double x, double y, double z, double x1, double y1, double z1) {
x=y1;
y=y1;
z=z1;
}
public boolean judge(double x, double y, double z) {
if ((x + y > z) && (x + z > y) && (z + y > x)) {
return true;
} else {
return false;
}
}
public void area(double x, double y, double z) {
boolean g = true;
double d;
if (g == judge(x, y, z)) {
c = x + y + z;
d = c / 2;
area = Math.sqrt(d * (d - x) * (d - y) * (d - z));
System.out.println("面积为:" + area);
System.out.println("周长为:" + c);
} else {
System.out.println("输入不能构成三角形,请重新输入!");
}
}
}
//梯形的类
class Trapezium1 {
double a;
double b;
double c;
public Trapezium1(double m, double n, double h) {
a = m;
b = n;
c = h;
}
public double getTrArea() {
return (a + b) * c / 2;
}
}
//圆
class Circle2 {
double r;
public Circle2(double r1) {
r = r1;
}
public String getArea() {
DecimalFormat df = new DecimalFormat("0.0000");
return df.format(Math.PI * r * r);
}
public String getPerimeter() {
DecimalFormat df = new DecimalFormat("0.0000");
return df.format(2 * Math.PI * r);
}
}
public class 三角形的AP型 {
public static void main(String[] args) {
System.out.println("请输入 s或y再或者t,分别进行运算");
Scanner sc = new Scanner(System.in);
char sb = sc.next().charAt(0);
switch (sb) {
case 's': {
System.out.println("请输入三个数字,来构成一个三角形:");
double x = sc.nextDouble();
double y = sc.nextDouble();
double z = sc.nextDouble();
boolean s;
//创建对象
Triangle td = new Triangle();
td.judge(x, y, z);
td.area(x, y, z);
//分割线
System.out.println("-------------------分割线---------------------");
s = td.judge(x, y, z);
//修改三角形的三条边
System.out.println("修改三条边:");
double x1 = sc.nextDouble();
double y1 = sc.nextDouble();
double z1 = sc.nextDouble();
td.change(x, y, z, x1, y1, z1);
if (s == true) {
System.out.println("修改后能够成一个三角形");
td.area(x1, y1, z1);
} else {
System.out.println("修改后不能构成一个三角形");
}
/* if (s == false) {
//再次给值
Scanner sd = new Scanner(System.in);
double a = sd.nextDouble();
double b = sd.nextDouble();
double c = sd.nextDouble();
td.area(a, b, c);
System.out.println("-------------------分割线---------------------");
s = td.judge(x, y, z);
}*/
break;
}
case 'y': {
System.out.println("请输入圆半径:");
double r = sc.nextDouble();
Circle2 c = new Circle2(r);
System.out.println("圆的面积为:" + c.getArea());
System.out.println("圆的周长为" + c.getPerimeter());
break;
}
case 't':
{
System.out.println("请输入梯形的上底下底及其高:");
double d1=sc.nextDouble();
double d2=sc.nextDouble();
double h=sc.nextDouble();
Trapezium1 t=new Trapezium1(d1,d2,h);
System.out.println("梯形的面积为:" + t.getTrArea());
break;
}
}
}
}