共计 2598 个字符,预计需要花费 7 分钟才能阅读完成。
* 做 Triangle 类,Trapezium 类,Circle 类,实现相应功能。
1.Triangle 类有返回周长和面积属性,并且有 Boolean 属性可以根据用户输入判断该三边是否可以构成三角形,如不能则重新输入。
2.Trapezium 类可以返回面积。
3.Circle 类可以返回周长及面积 *
Triangle 类
public class Triangle {
private double length1;
private double length2;
private double length3;
private double perimeter;
private double area;
public Triangle() {}
public Triangle(double length1,double length2,double length3) {
this.length1=length1;
this.length2=length2;
this.length3=length3;
}
public double getperimeter() {
boolean flag=true;
if(flag==islength()) {
perimeter=length1+length2+length3;
System.out.println("三角形周长是"+perimeter);
}
else {System.out.println("该三边不能构成三角形, 无法输出周长");
}
return perimeter;
}
public double getarea() {
boolean flag=true;
if(flag==islength()) {
area=0.5*length1+length2+length3;
System.out.println("三角形面积是"+area);
}
else {System.out.println("该三边不能构成三角形,无法输出面积");
}
return area;
}
public void setlength(double length1,double length2,double length3) {
this.length1=length1;
this.length2=length2;
this.length3=length3;
}
public boolean islength() {if(((length1+length2)>length3)&&((length1+length3)>length2)&&((length2+length3)>length1)) {return true;}
else {return false;}
}
}
Trapezium 类
public class Trapezium {
private double uplength;
private double downlength;
private double high;
private double area;
public Trapezium() {}
public Trapezium(double uplength,double downlength,double high){
this.uplength=uplength;
this.downlength=downlength;
this.high=high;
}
public double getarea() {area=(uplength+downlength)*high/2;
System.out.println("梯形的面积是"+area);
return area;
}
}
Circle 类
public class Circle {
private double radius;
private double perimeter;
private double area;
public Circle() {}
public Circle(double radius) {this.radius=radius;}
public double getperimeter() {
perimeter=2.0*3.14*radius;
System.out.println("圆的周长是"+perimeter);
return perimeter;
}
public double getarea() {
area=3.14*radius*radius;
System.out.println("圆的面积是"+area);
return area;
}
}
测试类
import java.util.Scanner;
public class Test {public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("请输入三角形三个边长");
Scanner sc=new Scanner(System.in);
boolean flag1=true;
boolean flag3;
do{double length1=sc.nextDouble();
double length2=sc.nextDouble();
double length3=sc.nextDouble();
Triangle t1=new Triangle(length1,length2,length3);
flag3=t1.islength();
if(flag1==flag3){t1.getperimeter();
t1.getarea();}
else{System.out.println("该三边不能构成三角形, 请重新输入三边");
}
}while(flag3==false);
System.out.println("请输入梯形上底,下底,高");
double uplength=sc.nextDouble();
double downlength=sc.nextDouble();
double high=sc.nextDouble();
Trapezium tr1=new Trapezium(uplength,downlength,high);
tr1.getarea();
System.out.println("请输入半径");
double radius=sc.nextDouble();
Circle c1=new Circle(radius);
c1.getperimeter();
c1.getarea();
sc.close();}
}
新手学习,请多指教。
正文完
发表至: java
2019-11-01