java 扩展java点类以找到中点
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12036129/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Extending java point class to find midpoint
提问by BLL27
Im studying for an upcoming exam and am working at sample problems, in particular the following one:
我正在为即将到来的考试而学习,并且正在解决示例问题,尤其是以下问题:
Add to class Point below an instance method called midpoint which returns an object of type Point representing the midpoint of two points, where one of the points is supplied as a parameter and the other is the current point (i.e. the point provided by the local instance variables). Note that midpoint returns a new Point object. Making good use of class Point, write a program which reads two points and prints their midpoint. The input consists of two lines where each line contains the x- and y-coordinates of a point. An example of input/output follows, with input indicated by bolding:
添加到类 Point 下面一个称为 midpoint 的实例方法,该方法返回一个 Point 类型的对象,表示两点的中点,其中一个点作为参数提供,另一个是当前点(即本地实例提供的点)变量)。请注意,中点返回一个新的 Point 对象。充分利用 Point 类,编写一个程序,读取两点并打印它们的中点。输入由两行组成,每行包含一个点的 x 和 y 坐标。输入/输出示例如下,输入用粗体表示:
Enter two points
2.1 3.2
3.0 2.8
The midpoint is (2.55,3.0)
My code for the point class is as follows and seems to be ok (feel free to point out any errors or improvements):
我的点类代码如下,似乎没问题(请随时指出任何错误或改进):
class Point {
private double x, y; // coordinates
Point(double x0, double y0){ // all-args constructor
x = x0; y = y0;
}
Point(){}; // no-args constructor (defaults apply)
void get() {
x = Console.readDouble();
y = Console.readDouble();
}
public Point midPoint(Point p) {
double mx = (p.x + x)/2;
double my = (p.y + y)/2;
return new Point(mx,my);
}
public String toString()
{
return "(" + x + "," + y + ")";
}
}
And where I run into trouble is in actually using my midPoint method in the below code, any advice is appreciated.
我遇到麻烦的地方是在下面的代码中实际使用我的 midPoint 方法,任何建议表示赞赏。
import java.util.Scanner;
import java.io.*;
class Midpoint extends Point
{
public static void main (String[] args ) {
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter two points:");
double x1 = scanner.nextDouble();
double y1 = scanner.nextDouble();
double x2 = scanner.nextDouble();
double y2 = scanner.nextDouble();
Point p1 = new Point(x1, y1);
Point p2 = new Point(x2, y2);
p1.get();
return midPoint(p2);
}
}
采纳答案by Kazekage Gaara
The call to get()
method seems unnecessary.
对get()
方法的调用似乎没有必要。
Secondly, call your midPoint
using an object(as per the requirement in the question). Hence, it should be:
其次,调用您midPoint
使用的对象(根据问题中的要求)。因此,它应该是:
p1.midPoint(p2);
Finally, since that method returns a Point
type, ensure you catch what is returned.
最后,由于该方法返回一个Point
类型,请确保您捕获返回的内容。
Point p3 = p1.midPoint(p2);
回答by SpiXel
Well from what you've presented as your code , it's definitely wrong , midPoint is a class method so the only way to use it is to first instantiate the class , like you p1 , and then call the method for that specific instace:
好吧,从您提供的代码来看,这绝对是错误的,midPoint 是一个类方法,因此使用它的唯一方法是首先实例化类,就像您 p1 一样,然后调用该特定实例的方法:
Point p1 = new Point(whatever);
Point p2 = new Point(whatever);
Point p3 = p1.midPoint(p2);
回答by user902383
- your main method is void so it cant return point
- if you want to operate on points p1 and p2, midpoint between them is
p1.midPoint(p2)
if you do this way, you don't need extend point class - what is your
p1.get()
actually doing? by any chance is it same as scanner?
- 你的主要方法是无效的,所以它不能返回点
- 如果你想对点p1和p2进行操作,它们之间的中点是
p1.midPoint(p2)
如果你这样做,你就不需要扩展点类 - 你
p1.get()
实际上在做什么?万一它和扫描仪一样吗?
回答by tmszdmsk
Besides all that was written by others, your MidPoint
class shouldn't extend Point
class. I think you did that for purpose of using that midPoint method but it's wrong. You didn't add any new behaviour to Point class.
除了其他人编写的所有内容之外,您的MidPoint
类不应该扩展Point
类。我认为您这样做是为了使用该 midPoint 方法,但这是错误的。您没有向 Point 类添加任何新行为。
回答by user12812457
import java.util.*;
public class Hello {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int b=sc.nextInt();
int c=sc.nextInt();
int d=sc.nextInt();
System.out.println((a+c)/2,(b+d)/2);
}
}