[英]Solve Calculate the Distance of point x and y given methods and class
Hi please could anyone offer to help.您好,请问有没有人愿意提供帮助。 I was told to randomly generate a few Line objects and display the Line info.
有人告诉我随机生成一些 Line 对象并显示 Line 信息。 For example, a method generates and returns a random integer;
例如,一个方法生成并返回一个随机的 integer; a method generates and returns the two random points.
一种方法生成并返回两个随机点。
class Point
{
int x;
int y;
public Point(){}
public Point(int x, int y){
this.x=x;
this.y=y;
}
public Point(Point p){
//copy constructor?
}
public double distance(Point p){
//what codes should be in here
}
public double getDistance(Point p){
//what codes should be in here
}
public int getX(){
//what codes should be in here
}
public int getY(){
//what codes should be in here
}
public void set(int x, int y)
//what codes should be in here
}
public String toString(){
//what codes should be in here
}
}
was also given a class line to do some coding in it but wasn't sure exactly what to key in还给了一个 class 行来在其中进行一些编码,但不确定要输入什么
class Line{
public Line(){
}
public Line(Point p1,Point p2){
}
public Line(Line aline){
}
public double getDistance(){
}
public Point getP1(){
}
public Point getP2(){
}
public void set(Point p1,Point p2){
}
public String toString(){
}
and under main class,在主要 class 下,
class Main{
public static int getInt(){
//what codes should be in here
}
public static void getTwoPoints(Point p1,Point p2){
//what codes should be in here
}
public static void main(String[]args){
//what codes should be in here
}
}
The output should be like this,
Set 1
//Given Point(66,36)
//Given Point(78,-83)
//Line (Point(66,36),Point(78,-83),distance=119.6035)
//--------------------
//Set 2
//Given Point(-13,90)
//Given Point(39,16)
//Line (Point(-13,90),Point(39,16),distance=90.4434)
Don't if I understand what you have to do...does this make sense to you?如果我明白你必须做什么……这对你有意义吗?
class Point
{
int x;
int y;
public Point(int x, int y){
this.x=x;
this.y=y;
}
public double distance(Point p){
return Math.sqrt(Math.pow(Math.abs(this.x - p.x), 2) + Math.pow(Math.abs(this.y - p.y), 2));
}
public int getX(){
return this.x;
}
public int getY(){
return this.y;
}
public void set(int x, int y) {}
this.x = x;
this.y = y;
}
public String toString(){
return "(" + this.x ", " + this.y + ")";
}
}
class Line{
Point p1;
Point p2;
public Line(Point p1,Point p2){
this.p1 = p1;
this.p2 = p2;
}
public double getDistance(){
return this.p1.distance(this.p2);
}
public Point getP1(){
return this.p1;
}
public Point getP2(){
return this.p2;
}
public void set(Point p1,Point p2){
this.p1 = p1;
this.p2 = p2;
}
public String toString(){
return "[" this.p1.toString() + ", " + this.p2.toString() + "]"
}
}
class Main{
protected static final Random random = new Random();
public static int getInt(){
return random.nexInt();
}
public static Line getLine(){
return new Line(new Point(getInt(), getInt()), new Point(getInt(), getInt()));
}
public static void main(String[]args){
System.out.println(getLine().toString());
System.out.println(getLine().toString());
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.