Triangle类
package com.sugarpie.classtest20;
/**
* @author sugarpie
* @create 2021-06-03-14:40
*/
public abstract class GeometricObject {
private String color;
private boolean full;
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public boolean isFull() {
return full;
}
public void setFull(boolean full) {
this.full = full;
}
public abstract double getArea();
public abstract double getPerimeter();
}
package com.sugarpie.classtest20;
/**
* @author sugarpie
* @create 2021-06-03-14:41
*/
public class Triangle extends GeometricObject{
private double s1;
private double s2;
private double s3;
public Triangle(double s1, double s2, double s3) {
this.s1 = s1;
this.s2 = s2;
this.s3 = s3;
}
@Override
public double getArea() {
double p = (s1 + s2 + s3) / 2;
return Math.sqrt(p * (p - s1) * (p - s2) * (p - s3));
}
@Override
public double getPerimeter() {
return s1 + s2 + s3;
}
}
package com.sugarpie.classtest20;
import java.util.Scanner;
/**
* chapter 13 13.1 Triangle类
*
* @author sugarpie
* @create 2021-06-03-14:41
*/
public class TriangleTest {
public static void main(String[] args) {
System.out.println("请输入三角形的三条边、颜色、是否填充:");
Scanner input = new Scanner(System.in);
double s1 = input.nextDouble();
double s2 = input.nextDouble();
double s3 = input.nextDouble();
String color = input.next();
boolean full = input.nextBoolean();
Triangle t = new Triangle(s1, s2, s3);
t.setColor(color);
t.setFull(full);
System.out.println("面积:" + t.getArea() + ",周长:" + t.getPerimeter() + ",颜色:" + t.getColor() + ",是否被填充:" + t.isFull());
}
}