⑴ 求高手用java幫我設計一個完整的Point類,可以計算兩點之間距離和中間點坐標的,萬分感謝!
你好,程序如下:
public class Point {
double x1,x2,y1,y2;
double d,x3,y3;
Point(double x1,double y1,double x2,double y2){//構造方法
回this.x1 = x1;
this.x2 = x2;
this.y1 = y1;
this.y2 = y2;
}
void TestPoint(){//求距離和答中點坐標
x3 = (x1 + x2)/2;
y3 = (y1 + y2)/2;
d = Math.sqrt((x1 - x2)*(x1 - x2) + (y1 - y2)*(y1 - y2));
System.out.println("(" + x3 + ","+ y3 + ")");
System.out.println(d);
}
public static void main(String[] args) {
Point p = new Point(1,1,2,2);
p.TestPoint();
}
}
如果還有其他要求的話,我再改進一下。
⑵ java定義一個點類Point, 具備坐標系中的橫坐標x, 和縱坐標y, 並實現如下功能:
Point類
publicclassPoint{
privatefloatx;
privatefloaty;
publicPoint(floatx,floaty){
this.x=x;
this.y=y;
}
publicfloatgetX(){
returnx;
}
publicvoidsetX(floatx){
this.x=x;
}
publicfloatgetY(){
returny;
}
publicvoidsetY(floaty){
this.y=y;
}
publicfloatdistanceToOrigin(){
return(float)Math.sqrt(Math.pow(this.x,2)+Math.pow(this.y,2));
}
publicfloatdistanceToOther(intx,inty){
return(float)Math.sqrt(Math.pow(this.x-x,2)+Math.pow(this.y-y,2));
}
publicfloatdistanceToOther(Pointpoint){
return(float)Math.sqrt(Math.pow(this.x-point.x,2)+Math.pow(this.y-point.y,2));
}
}
測試類
publicclassTestPoint{
publicstaticvoidmain(String[]args){
Pointp1=newPoint(3,5);
Pointp2=newPoint(7,8);
System.out.println(p1.distanceToOrigin());
System.out.println(p2.distanceToOrigin());
System.out.println(p1.distanceToOther(20,30));
System.out.println(p2.distanceToOther(20,30));
System.out.println(p1.distanceToOther(p2));
}
}
⑶ 用JAVA定義一個描述點的point類.它有兩個成員變數:橫坐標x,縱坐標y
實現思路就是有一個類Point,之後這個類有兩個私有成員變數,之後可以通過set和get方法進行賦值和數值讀取:
public
class
Point
{
private
String
x;
private
String
y;
/**
*
默認構造函數
*/
public
Point(){
}
/**
*有x和y的構造方法。
*/
public
Point(String
x,String
y){
this.x=x;
this.y=y;
}
public
String
getX()
{
return
x;
}
public
void
setX(String
x)
{
this.x
=
x;
}
public
String
getY()
{
return
y;
}
public
void
setY(String
y)
{
this.y
=
y;
}
}
⑷ java編寫一個表示坐標點的類(Point),其中包含x和y兩個坐標點的值,並且包含一個列印出該點坐標的方法
publicclassPoint{
privateintx;
privateinty;
publicgetX(){returnx;}
publicgetY(){returny;}
publicsetX(intx){this.x=x;}
publicsetY(inty){this.y=y;}
publicPoint(intx,inty){
this.x=x;
this.y=y;
}
publicdisplayPoint(){
//輸出坐標
System.out.println("x:"+x+",y:"+y);
}
publicstaticdistancePoint(Pointp1,Pointp2){
//計算坐標距離
inta=p1.getX()-p2.getX();
intb=p1.getY()-p2.getY();
returnMath.sqrt(a*a+b*b);
}
}
⑸ JAVA編程問題 定義一個泛型類Point,它包含橫坐標x和縱坐標y兩個變數,類型均為T
public class Point<T> {
private T x;
private T y;
public Point(T x, T y) {
this.x = x;
this.y = y;
}
public T getX() {
return x;
}
public void setX(T x) {
this.x = x;
}
public T getY() {
return y;
}
public void setY(T y) {
this.y = y;
}
public void outPut(Point<T> p){
System.out.println("點的x坐標為:"+p.getX());
System.out.println("點的y坐標為:"+p.getY());
}
public static void main(String[] args) {
Point<Integer> p=new Point<Integer>(1,2);
p.outPut(p);
Point<Double> d=new Point<Double>(1.0,2.0);
d.outPut(d);
Point<Float> f=new Point<Float>(1.0f,2.0f);
f.outPut(f);
}
}