JAVA题目,求解答,求代码!!!!
答案:3 悬赏:30
解决时间 2021-12-30 12:54
- 提问者网友:余味
- 2021-12-30 02:25
设计一个Point类,类中包含点的坐标x、y,同时,包含一个通过另一个点求两点之间距离的distance(Point p)方法。利用该类实现点(3,4)到点(5,6)的距离。
最佳答案
- 二级知识专家网友:心与口不同
- 2021-12-30 03:53
public class Point {
private double x, y;
public Point(double x, double y){
this.x = x;
this.y = y;
}
public double distance(Point p){
return Math.sqrt((p.x - x) * (p.x - x) + (p.y - y) * (p.y - y));
}
public String toString(){
return "(" + x +", "+ y +")";
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Point p1 = new Point(3, 4);
Point p2 = new Point(5, 6);
System.out.println("The distance between Point " + p1 + " and "+ p2 + " is " + p1.distance(p2) + ".");
}
}
private double x, y;
public Point(double x, double y){
this.x = x;
this.y = y;
}
public double distance(Point p){
return Math.sqrt((p.x - x) * (p.x - x) + (p.y - y) * (p.y - y));
}
public String toString(){
return "(" + x +", "+ y +")";
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Point p1 = new Point(3, 4);
Point p2 = new Point(5, 6);
System.out.println("The distance between Point " + p1 + " and "+ p2 + " is " + p1.distance(p2) + ".");
}
}
全部回答
- 1楼网友:努力只為明天
- 2021-12-30 06:05
public class Point
{
private double x,y;
Point(double x,double y){
this.x = x;
this.y = y;
}
public double distance(Point p){
return java.lang.StrictMath.sqrt(Math.pow(this.x-p.x,2) + Math.pow(this.y-p.y,2));
}
public static void main(String[] args)
{
System.out.println(new Point(3,4).distance(new Point(5,6)));
}
}
- 2楼网友:深街酒徒
- 2021-12-30 04:43
public static void main(string[] args) {
string[] suit={"黑桃","红桃","樱花","方块"};
string[] rank={"a","2","3","4","5","6","7","8","9","10","j","q","k"};
arraylist cards =new arraylist();
cards.add("大王");
cards.add("小王");
for (int i = 0; i < suit.length; i++)
for (int j = 0; j < rank.length; j++)
cards.add(suit[i]+rank[j]);
for (int i = 0; i < 54; i++) {
if(i==1 || (i-2 + 1)%13==0)
system.out.println(cards.get(i));
else
system.out.print(cards.get(i)+" ");
}
}
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯