java由三点求三角形的周长
来源:学生作业帮 编辑:神马作文网作业帮 分类:综合作业 时间:2024/11/19 02:09:47
java由三点求三角形的周长
public class Point {
int x;
int y;
Point(){
}
Point(int a,int b){
x=a;
y=b;
}
}
public class Triangle {
Point n1,n2,n3;
Triangle(Point n1,Point n2,Point n3){
n1=new Point();
n2=new Point();
n3=new Point();
}
double x1=Math.pow(n1.x-n2.x,2)+Math.pow(n1.y-n2.y,2);
double x2=Math.pow(n1.x-n3.x,2)+Math.pow(n1.y-n3.y,2);
double x3=Math.pow(n2.x-n3.x,2)+Math.pow(n2.y-n3.y,2);
double getPerimeter(){
return Math.sqrt(x1)+Math.sqrt(x2)+Math.sqrt(x3);
}
}
public class Test {
public static void main(String[] args) {
Point n1=new Point(0,0);
Point n2 =new Point(0,3);
Point n3=new Point(4,0);
Triangle sanjiao=new Triangle(n1,n2,n3);
System.out.println(sanjiao.getPerimeter());
}
}
public class Point {
int x;
int y;
Point(){
}
Point(int a,int b){
x=a;
y=b;
}
}
public class Triangle {
Point n1,n2,n3;
Triangle(Point n1,Point n2,Point n3){
n1=new Point();
n2=new Point();
n3=new Point();
}
double x1=Math.pow(n1.x-n2.x,2)+Math.pow(n1.y-n2.y,2);
double x2=Math.pow(n1.x-n3.x,2)+Math.pow(n1.y-n3.y,2);
double x3=Math.pow(n2.x-n3.x,2)+Math.pow(n2.y-n3.y,2);
double getPerimeter(){
return Math.sqrt(x1)+Math.sqrt(x2)+Math.sqrt(x3);
}
}
public class Test {
public static void main(String[] args) {
Point n1=new Point(0,0);
Point n2 =new Point(0,3);
Point n3=new Point(4,0);
Triangle sanjiao=new Triangle(n1,n2,n3);
System.out.println(sanjiao.getPerimeter());
}
}
你的Triangle构造方法里面传入三个参数都没用到.而且你现在的代码很不符合JAVA面向对象,点之间距离应该是Point里面的方法才合适吧.在你的基础上修改如下:
class Point {
int x;
int y;
Point() {
}
Point(int a,int b) {
x = a;
y = b;
}
public static double getInstance(Point p1,Point p2) {
return Math.sqrt(Math.pow(p1.x - p2.x,2) + Math.pow(p1.y - p2.y,2));
}
}
class Triangle {
Point n1,n2,n3;
Triangle(Point n1,Point n2,Point n3) {
this.n1 = n1;
this.n2 = n2;
this.n3 = n3;
}
public double getPerimeter() {
return Point.getInstance(n1,n2) + Point.getInstance(n1,n3) + Point.getInstance(n2,n3);
}
}
public class Test {
public static void main(String[] args) {
Point n1 = new Point(0,0);
Point n2 = new Point(0,3);
Point n3 = new Point(4,0);
Triangle sanjiao = new Triangle(n1,n2,n3);
System.out.println(sanjiao.getPerimeter());
}
}
--------------
12.0
class Point {
int x;
int y;
Point() {
}
Point(int a,int b) {
x = a;
y = b;
}
public static double getInstance(Point p1,Point p2) {
return Math.sqrt(Math.pow(p1.x - p2.x,2) + Math.pow(p1.y - p2.y,2));
}
}
class Triangle {
Point n1,n2,n3;
Triangle(Point n1,Point n2,Point n3) {
this.n1 = n1;
this.n2 = n2;
this.n3 = n3;
}
public double getPerimeter() {
return Point.getInstance(n1,n2) + Point.getInstance(n1,n3) + Point.getInstance(n2,n3);
}
}
public class Test {
public static void main(String[] args) {
Point n1 = new Point(0,0);
Point n2 = new Point(0,3);
Point n3 = new Point(4,0);
Triangle sanjiao = new Triangle(n1,n2,n3);
System.out.println(sanjiao.getPerimeter());
}
}
--------------
12.0
java由三点求三角形的周长
java中声明三角形类,继承图像类,计算三角形的周长和面积,如何编程
怎样打印出由“*”组成的java 三角形 图形是左边一个三角形右边一个三角形共有一个“*”为顶点
用Java语言编写,要求申明三角形类,继承图形抽象类,计算三角形的周长和面积!
java编写程序输入三角形的三条边,如果输入有效,计算它的周长;否则显示输入无效
java定义一个三角形类(Triangle),成员变量是三条边的长度,定义方法perimeter()求周长
java:求三角形、正方形、长方形和圆的面积、周长,按从小到大分别排序后输出(要用继承)
能否由三角形的周长求出其面积?
java里判断三角形的问题
用JAVA编写程序:输入整数n,输出由n行n列星号字符组成的三角形图案
Java编译:定义三角形的三条边长a=4,b=8.54,c=4.44;求三角形的周长d
java:如果告诉你a,b,c三条边,判断是否可以构成三角形,如果能计算出这个三角形的面积和周长?