20分悬赏、这是java的题、急~~~~、一定要用那个公式啊、不要用别的糊弄
一、设计一个描述二维平面上点的类Position,(1)定义一个带参数的构造方法;(2)它有两个成员变量x,y;(3)分别为两个成员变量定义读取器。(4)定义一个成员方法...
一、设计一个描述二维平面上点的类Position,
(1)定义一个带参数的构造方法;
(2)它有两个成员变量x,y;
(3)分别为两个成员变量定义读取器。
(4)定义一个成员方法求两点间的距离。
定义一个测试类TestPosition,
建立平面上的两个点对象(Position对象)source和target,
输出此两点间坐标及它们之间的距离。
编程提示:
1、两点间距离公式:(x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)的开方,
其中,(x1,y1),(x2,y2)分别为两点的x,y坐标。
2、Math数学函数类中的方法sqrt(x)表示求x的开方:
格式为:Math.sqrt(x) 展开
(1)定义一个带参数的构造方法;
(2)它有两个成员变量x,y;
(3)分别为两个成员变量定义读取器。
(4)定义一个成员方法求两点间的距离。
定义一个测试类TestPosition,
建立平面上的两个点对象(Position对象)source和target,
输出此两点间坐标及它们之间的距离。
编程提示:
1、两点间距离公式:(x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)的开方,
其中,(x1,y1),(x2,y2)分别为两点的x,y坐标。
2、Math数学函数类中的方法sqrt(x)表示求x的开方:
格式为:Math.sqrt(x) 展开
6个回答
展开全部
两点间距离公式:(x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)的开方, 三角形求斜边。
public class Position{
//成员变量x,y;
private double x;
private double y;
public Position(double x,double y){ //平面坐标x,y 却一个点
this.x=x;
this.y=y;
}
public double getX(){
return x;
}
public double getY(){
return y;
}
//两点间的距离
public double distance(Position p){
return Math.sqrt( Math.pow( Math.abs( x-p.getX()),2)+ Math.pow( Math.abs( y-p.getY()),2));
}
}
public class Position{
//成员变量x,y;
private double x;
private double y;
public Position(double x,double y){ //平面坐标x,y 却一个点
this.x=x;
this.y=y;
}
public double getX(){
return x;
}
public double getY(){
return y;
}
//两点间的距离
public double distance(Position p){
return Math.sqrt( Math.pow( Math.abs( x-p.getX()),2)+ Math.pow( Math.abs( y-p.getY()),2));
}
}
更多追问追答
追问
E:\my java>javac TestPosition.java
TestPosition.java:1: 类 Position 是公共的,应在名为 Position.java 的文件中声明
public class Position{
^
1 错误
追答
类名和文件名相同
展开全部
public class Test1 {
public static void main(String[] args) {
Position p1 = new Position(0, 0);
Position p2 = new Position(3, 4);
double distance = Position.getDistance(p1, p2);
System.out.println(distance);
}
}
class Position{
private double x, y;
public Position(double x, double y){
this.x =x;
this.y = y;
}
public double getX() {
return x;
}
public double getY() {
return y;
}
public static double getDistance(Position p1, Position p2){
double x1 = p1.getX();
double y1 = p1.getY();
double x2 = p2.getX();
double y2 = p2.getY();
return Math.sqrt((x1 -x2)*(x1-x2) + (y1-y2)*(y1-y2));
}
}
public static void main(String[] args) {
Position p1 = new Position(0, 0);
Position p2 = new Position(3, 4);
double distance = Position.getDistance(p1, p2);
System.out.println(distance);
}
}
class Position{
private double x, y;
public Position(double x, double y){
this.x =x;
this.y = y;
}
public double getX() {
return x;
}
public double getY() {
return y;
}
public static double getDistance(Position p1, Position p2){
double x1 = p1.getX();
double y1 = p1.getY();
double x2 = p2.getX();
double y2 = p2.getY();
return Math.sqrt((x1 -x2)*(x1-x2) + (y1-y2)*(y1-y2));
}
}
更多追问追答
追问
老师说我要你求的是一个类和另一个类的距离、不是P1 和P2 的距离
追答
修改一下getDistance就可以了,文件名TestPosition.java
public class TestPosition {
public static void main(String[] args) {
Position p1 = new Position(0, 0);
Position p2 = new Position(3, 4);
double distance = p1.getDistance(p2);
System.out.println(distance);
}
}
class Position {
private double x, y;
public Position(double x, double y) {
this.x = x;
this.y = y;
}
public double getX() {
return x;
}
public double getY() {
return y;
}
public double getDistance(Position p1) {
double x1 = p1.getX();
double y1 = p1.getY();
return Math.sqrt((x1 - x) * (x1 - x) + (y1 - y) * (y1 - y));
}
}
----testing
5.0
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
class Position {
private int x;
private int y;
public Position(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public static double getDistance(Position p1,Position p2){
return Math.sqrt(Math.pow(p1.getX() - p2.getX(), 2) + Math.pow(p1.getY() - p2.getY(), 2));
}
}
public class TestPosition{
public static void main(String[] args) {
Position source = new Position(5, 4);
Position target = new Position(3, 2);
System.out.println(Position.getDistance(source,target));
}
}
private int x;
private int y;
public Position(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public static double getDistance(Position p1,Position p2){
return Math.sqrt(Math.pow(p1.getX() - p2.getX(), 2) + Math.pow(p1.getY() - p2.getY(), 2));
}
}
public class TestPosition{
public static void main(String[] args) {
Position source = new Position(5, 4);
Position target = new Position(3, 2);
System.out.println(Position.getDistance(source,target));
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
import javax.swing.JOptionPane;
public class Position {
String x, y;
public static void main(String[] args) {
JOptionPane jp = new JOptionPane();
String x = jp.showInputDialog(null, "输入X,Y坐标");
System.out.println(x);
String y = jp.showInputDialog(null, "输入X,Y坐标");
System.out.println(x);
distance(x, y);
}
public static void distance(String x, String y) {
int x1, y1, x2, y2;
String[] str1 = x.split(",");
x1 = Integer.parseInt(str1[0]);
y1 = Integer.parseInt(str1[1]);
String[] str2 = y.split(",");
x2 = Integer.parseInt(str2[0]);
y2 = Integer.parseInt(str2[1]);
double dis = Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
System.out.println("X,Y两点间的距离为:" + dis);
}
}
public class Position {
String x, y;
public static void main(String[] args) {
JOptionPane jp = new JOptionPane();
String x = jp.showInputDialog(null, "输入X,Y坐标");
System.out.println(x);
String y = jp.showInputDialog(null, "输入X,Y坐标");
System.out.println(x);
distance(x, y);
}
public static void distance(String x, String y) {
int x1, y1, x2, y2;
String[] str1 = x.split(",");
x1 = Integer.parseInt(str1[0]);
y1 = Integer.parseInt(str1[1]);
String[] str2 = y.split(",");
x2 = Integer.parseInt(str2[0]);
y2 = Integer.parseInt(str2[1]);
double dis = Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
System.out.println("X,Y两点间的距离为:" + dis);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
public class Position {
private double x;
private double y;
public double getX() {
return x;
}
public double getY() {
return y;
}
public Position(double x,double y){
this.x=x;
this.y=y;
}
public double getDistanceByPosition(Position p){
double rrr = (this.x-p.getX())*(this.x-p.getX())+(this.y-p.getY())*(this.y-p.getY());
double res = Math.sqrt(rrr);
return res;
}
}
public class PositionRun {
/**
* @param args
*/
public static void main(String[] args) {
Position p1 = new Position(2,2);
Position p2 = new Position(5,9);
double distance = p1.getDistanceByPosition(p2);
System.out.println("p1的坐标是:("+p1.getX()+","+p1.getY()+")");
System.out.println("p2的坐标是:("+p2.getX()+","+p2.getY()+")");
System.out.println("p1,p2之间的距离:"+distance);
}
}
private double x;
private double y;
public double getX() {
return x;
}
public double getY() {
return y;
}
public Position(double x,double y){
this.x=x;
this.y=y;
}
public double getDistanceByPosition(Position p){
double rrr = (this.x-p.getX())*(this.x-p.getX())+(this.y-p.getY())*(this.y-p.getY());
double res = Math.sqrt(rrr);
return res;
}
}
public class PositionRun {
/**
* @param args
*/
public static void main(String[] args) {
Position p1 = new Position(2,2);
Position p2 = new Position(5,9);
double distance = p1.getDistanceByPosition(p2);
System.out.println("p1的坐标是:("+p1.getX()+","+p1.getY()+")");
System.out.println("p2的坐标是:("+p2.getX()+","+p2.getY()+")");
System.out.println("p1,p2之间的距离:"+distance);
}
}
更多追问追答
追问
TestPosition.java:1: 类 Position 是公共的,应在名为 Position.java 的文件中声明
public class Position {
^
TestPosition.java:26: 类 PositionRun 是公共的,应在名为 PositionRun.java 的文件
中声明
public class PositionRun {
^
2 错误
追答
如果你要两个类放在一个文件里面的话
就把
public class Position {
前的public 去掉
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询