首页 技术 正文
技术 2022年11月20日
0 收藏 680 点赞 2,783 浏览 2061 个字

学习内容:以点类 Point 及平面图形类 Plane 为基础设计三角形类 Triangle

代码示例:

import java.util.Scanner;

class Point{
private double x;
private double y;
public Point(double x,double y) {
this.x=x;
this.y=y;
System.out.println(“Point Constructor run”);
}
public void setX(double x) {//设置x坐标
this.x=x;
}
public final double getX(){//返回x坐标
return x;
}
public void setY(double y) {//设置y坐标
this.y=y;
}
public final double getY(){//返回y坐标
return y;
}
public void show() {//显示点的坐标
System.out.print(“Point(X=”+x+”,Y=”+y+”)”);
}
public double dis(Point p)
{
double _x = Math.abs(this.x – p.x);
double _y = Math.abs(this.y – p.y);
return Math.sqrt(_x*_x+_y*_y);
}
}
abstract class Plane{//抽象类Plane
double length() {
return 0;
}
double area() {
return 0;
}
}
public class Triangle extends Plane {

private Point a;
private Point b;
private Point c;
public Triangle(final Point a, final Point b, final Point c) {
this.a=a;
this.b=b;
this.c=c;
System.out.println(“Triangle Constructorrun”);
}
public void setA(final Point a) {
this.a=a;
}
public Point getA() {
return a;
}
public void setB(final Point b) {
this.b=b;
}
public Point getB() {
return b;
}
public void setC(final Point c) {
this.c=c;
}
public Point getC() {
return c;
}
public final void show() {
System.out.print(“Triangle(A=”);
a.show();
System.out.print(“,B=”);
b.show();
System.out.print(“,C=”);
c.show();
}
final double area() {
double l1, l2, l3,s;
l1 = a.dis(b);
l2 = a.dis(c);
l3 = b.dis(c);
s = (l1 + l2 + l3) / 2;
double area=Math.sqrt(s * (s – l1) * (s – l2) * (s – l3));
System.out.println(“Area=”+area);
return 0;
}
final double length() {
double l1=a.dis(b);
double l2=a.dis(c);
double l3=b.dis(c);
double length=l1+l2+l3;
System.out.println(“length=”+length);
return 0;
}
public static void main(String[] args) {
double x,y;
Point p1=new Point(0, 0);
Point p2=new Point(1, 1);
Point p3=new Point(2, 2);
Triangle t1=new Triangle(p1, p1, p1);
Triangle t2=t1;
t1.show();
System.out.println();//换行
t1.area();
t1.length();
Scanner sc=new Scanner(System.in);
System.out.println(“请输入x,y:”);
x=sc.nextDouble();
y=sc.nextDouble();
p1.setX(x);
p1.setY(y);
t2.setA(p1);
t2.setB(p2);
t2.setC(p3);
t2.show();
System.out.println();
t2.area();
t2.length();

}

}

运行截图:

遇到的问题:面积函数采用“return Math.sqrt(s * (s – l1) * (s – l2) * (s – l3));”的形式无法输出面积

解决方法:设置变量area,使area=Math.sqrt(s * (s – l1) * (s – l2) * (s – l3));输出area,返回值设为0

明天任务:以圆类 Circle 及立体图形类 Solid 为基础设计球类 Sphere

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,084
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,559
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,408
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,181
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,818
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,901