「javapoint类型」java point类型

博主:adminadmin 2022-11-25 10:01:06 76

本篇文章给大家谈谈javapoint类型,以及java point类型对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

Java定义一个Point(点)类

public class Point

{

public static void main(String[] args)

{

Point p1=new Point();

Point p2=new Point(1,2);

p1.show();

p1.move(3,4);

p1.show();

p2.show();

p2.move(5,6);

p2.show();

}

Point()

{

this(0,0);

}

Point(float x,float y)

{

this.x=x;

this.y=y;

}

void move(float x,float y)

{

this.x=x;

this.y=y;

}

void show()

{

System.out.printf("(%f,%f)",x,y);

System.out.println();

}

private float x,y;

}

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,用来描述平面直角坐标系中点的坐标。

需要两个类,一个Point,一个Test.这两个类,是调用和被调用的关系,Point被Test调用.

关系说好了,就是类具体实现的问题.

Point.java

这个类近似于常说的工具类或者辅助类.这里面既然对坐标操作就应该定义全局的x,y变量.其他的就是

方法.

public void setXY(double x,double y){

this.x = x;

this.y = y;

}

set方法就是类似于这样,把传过来的值赋给定义的全局.而get方法里面很显然就是return.

而测试类就是调用Point的过程.

class Point{

double x,y;

Point(){

System.out.println("enter a x value");

x = Console.readDouble();

System.out.println("enter a y value");

y = Console.readDouble();

}

Point(double a,double b){

x = a;

y = b;

}

}

class PointTest{

public static void main(String [] args){

Point p = new Point();

System.out.println("here is the point :");

System.out.println(p.x +" " + p.y);

}

}

Point p = new Point();

用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;

}

}

关于javapoint类型和java point类型的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

The End

发布于:2022-11-25,除非注明,否则均为首码项目网原创文章,转载请注明出处。