本文最后更新于706 天前,其中的信息可能已经过时,如有错误请发送邮件到echobydq@gmail.com
运用继承,接口,包等技术,编写求解几何图形(包括三角形,矩形,圆)的周长,面积
//package oop123; //包
import java.io.*; //引入键盘输入所需要的类所在的包
interface getProperty{ //接口定义,方法声明
double Pi = 3.1415926;
double getArea(); //面积
double getCircum(); //周长
String getName(); //名称
}
class mpoint{ //定义点
static int i = 0;
double x,y;
mpoint(double a,double b){
this.x = a;
this.y = b;
}
double getX(){
return x;
}
double getY(){
return y;
}
}
class disp{ //定义屏幕输出所需要的类
double area; //面积
double circum; //周长
String drawingName; //名称
disp(double a,double b,String ss){
this.area = a;
this.circum = b;
this.drawingName = ss;
}
public void display(){
System.out.println("图像 是 "+ drawingName);
System.out.println("面积 = "+area+'\n'+"周长 = "+circum);
}
}
class triangle implements getProperty{ //定义三角形
mpoint p1,p2,p3; //三点
double s1,s2,s3; //三边
String drawingName = "triangle";
triangle(mpoint p1,mpoint p2, mpoint p3){ //构造方法
this.p1 = p1;
this.p2 = p2;
this.p3 = p3;
this.s1 = Math.sqrt((p1.x - p2.x)*(p1.x - p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
this.s2 = Math.sqrt((p3.x - p2.x)*(p3.x - p2.x)+(p3.y-p2.y)*(p3.y-p2.y));
this.s3 = Math.sqrt((p1.x - p3.x)*(p1.x - p3.x)+(p1.y-p3.y)*(p1.y-p3.y));
}
//方法实现
public double getArea(){
double ss,ssa;
ss = (s1+s2+s3)*Pi/2.0/Pi;
ssa = Math.sqrt(ss*(ss-s1)*(ss-s2)*(ss-s3));
return ssa;
}
public double getCircum(){
return s1+s2+s3;
}
public String getName(){
return drawingName;
}
public boolean tline(){
return true;
}
}
class circle implements getProperty{ //定义圆形
mpoint p1; //圆心
double radius; //半径
String drawingName = "Circle";
circle(mpoint p1,double radius){ //构造方法
this.p1 = p1;
this.radius = radius;
}
public double getArea(){
double ssa;
ssa = Math.PI*radius*radius;
return ssa;
}
public double getCircum(){
return Math.PI*2.0*radius;
}
public String getName(){
return drawingName;
}
public boolean tcircle(){
return true;
}
}
class rectangle implements getProperty{ //定义长方形
mpoint p1,p2;
double s1,s2;
String drawingName = "Rectangle";
rectangle(mpoint p1,mpoint p2){
this.p1 = p1;
this.p2 = p2;
this.s1 = Math.sqrt((p1.x-p2.x)*(p1.x-p2.x));
this.s2 = Math.sqrt((p1.y-p2.y)*(p1.y-p2.y));
}
public double getArea(){
return s1*s2;
}
public double getCircum(){
return s1+s2+s1+s2;
}
public String getName(){
return drawingName;
}
public boolean rline(){
return true;
}
}
public class drawing extends disp{ //定义 main() 所在的类
drawing (double a, double b, String ss){
super(a,b,ss);
//由于子类不能继承父类的构造方法,因此,如果要调用父类的构造方法,
//可以使用 super 关键字。super 可以用来访问父类的构造方法、普通方法和属性。
}
public static void main(String[] args) throws IOException {
BufferedReader keyin = new BufferedReader(new InputStreamReader(System.in));
//new BufferedReader 将字符流放到字符流缓冲区之中
//new InputStreamReader 将字节流变为字符流
//System.in 字节输入流
String strxx;
for(;true;){
System.out.println("输入字符串如 Triangle,Rectangle,Circle:");
strxx = keyin.readLine(); //读取输入
if(strxx.length() == 0) continue;
char charxx;
charxx = strxx.toUpperCase().charAt(0); //大小写转换
switch(charxx)
{
case'T': //三角形
System.out.println("请输入(三角形) 第一个点 x(enter) y(enter)");
mpoint p1 = new mpoint(aVar(keyin),aVar(keyin));
System.out.println("请输入(三角形) 第二个点 x(enter) y(enter)");
mpoint p2 = new mpoint(aVar(keyin),aVar(keyin));
System.out.println("请输入(三角形) 第三个点 x(enter) y(enter)");
mpoint p3 = new mpoint(aVar(keyin),aVar(keyin));
triangle t1 = new triangle(p1,p2,p3); //构造三角形
disp tdisp = new drawing(t1.getArea(),t1.getCircum(),t1.getName());
tdisp.display();
break;
case'C': //圆形
System.out.println("请输入(圆形) 圆心 x(enter) y(enter)");
mpoint p4 = new mpoint(aVar(keyin),aVar(keyin));
System.out.println("请输入半径 x(enter)");
double radius = aVar(keyin);
circle t2 = new circle(p4,radius); //构造圆形
disp cdisp = new drawing(t2.getArea(),t2.getCircum(),t2.getName());
cdisp.display();
break;
case'R': //长方形
System.out.println("请输入(长方形)第一个点 x(enter) y(enter)");
mpoint p6 = new mpoint(aVar(keyin),aVar(keyin));
System.out.println("请输入(长方形)第一个点 x(enter) y(enter)");
mpoint p7 = new mpoint(aVar(keyin),aVar(keyin));
rectangle t3 = new rectangle(p6,p7); //构造长方形
disp rdisp = new drawing(t3.getArea(),t3.getCircum(),t3.getName());
rdisp.display();
break;
default:System.out.println("错误,请输入 t(T),c(C),r(R)");
}//switch
}//for循环
}//main 方法
static double aVar(BufferedReader keyin) throws IOException
{//得到一个双变量
String xx;
xx = keyin.readLine();
return Double.parseDouble(xx);
}
}