실험 1 : Java 개발 환경 및 객체 지향 프로그래밍 사용

  1. 다음 요구 사항을 충족하기 위해 2 차원 공간과 3 차원 공간에서 점을 나타내는 두 개의 클래스 Point2D 및 Point3D를 각각 작성합니다.
  1. Point2D에는 두 개의 정수 멤버 변수 x, y (각각 2 차원 공간의 X 및 Y 방향 좌표)가 있습니다 .Point2D의 구성 방법은 멤버 변수 x, y를 초기화해야합니다.

  2. Point2D에는 Point2D의 변환을 실현할 수있는 void 형 멤버 메서드 offset (int a, int b)이 있습니다.

  3. Point3D는 Point2D의 직접적인 하위 클래스로, 3 개의 정수 멤버 변수 x, y, z (각각 3 차원 공간의 X, Y, Z 방향 좌표)를 가지며, Point3D에는 두 가지 구성 방법이 있습니다. Point3D (int x, int y , int z) 및 Point3D (Point2D
    p, int z)는 모두 Point3D의 멤버 변수 x,
    y, z의 초기화 를 실현할 수 있습니다 .

  4. Point3D에는 Point3D의 변환을 실현할 수있는 void 멤버 메서드 offset (int a, int b, int c)이 있습니다.

  5. Point3D의 주 함수 main ()에서 두 개의 Point2D 객체 p2d1, p2d2를 인스턴스화하고 그 사이의 거리를 인쇄 한 다음 두 개의 Point2D 객체 p3d1, p3d2를 인스턴스화하고 그 사이의 거리를 인쇄합니다.

package test;
public class Point2D {
 int x;
 int y;
 Point2D(int _x,int _y){
  x=_x;
  y=_y;
 }
 public void offset(int a,int b) {
  x+=a;
  y+=b;
 }
}
package test;
public class Point3D extends Point2D{
 int z;
 Point3D(int _x, int _y,int _z) {
  super(_x, _y);
  z=_z;
 }
 Point3D(Point2D p,int _z){
  super(p.x,p.y);
  z=_z;
 }
 public void offset(int a,int b,int c) {
  x+=a;
  y+=b;
  z+=c;
 }
 public static void main(String[] args) {
  Point2D p2d1=new Point2D(1,4);
  Point2D p2d2=new Point2D(2,5);
  System.out.println(Math.sqrt(Math.pow((p2d1.x-p2d2.x),2)+Math.pow((p2d1.y-p2d2.y),2)));
  Point3D p3d1=new Point3D(1,4,8);
  Point3D p3d2=new Point3D(2,5,9);
  System.out.println(Math.sqrt(Math.pow((p3d1.x-p3d2.x),2)+Math.pow((p3d1.y-p3d2.y),2)+Math.pow((p3d1.z-p3d2.z),2)));
 }
}

추천

출처blog.csdn.net/Warmmm/article/details/106984677