Java query

CSDN Q & A 2022-02-13 09:01:16 阅读数:138

java query

1、 Programming a class of points on a two-dimensional plane Point, And write test classes Point Class , draw Point Class UML Class diagram .
Point Class contains the following member variables and methods :
(1) Two private double A member variable of type x、y, Denote points respectively x and y coordinate ;
(2) A construction method without parameters ;
(3) One for x、y A parametric construction method for setting the initial value of coordinates ;
(4) One movePoint(int dx, int dy) Method to move the position of the point ,dx,dy Respectively represent the points in the coordinates x Direction and y Displacement in direction ;
(5) One public String toString() Method returns the coordinate position information of the point .

Test requirements :
(1) Use the parameterless construction method to create a Point object p1;
(2) Use the parametric construction method to create a Point object p2, Enter the point from the keyboard x、y coordinate ;
(3) call movePoint() Method makes p2 stay x Direction and y Move in all directions 2 A unit of ;
(4) Refer to the example , Output the coordinate positions of two points respectively .


Take the answer :

If help , Please accept . Click on the top right corner of my answer 【 Adopt 】 Button .
design sketch :

import java.util.Scanner;public class Point { private double x; private double y; public Point(){ x=0.0; y=0.0; } public Point(double x, double y) { this.x = x; this.y = y; } public void movePoint(int dx,int dy){ this.x=this.x+dx; this.y=this.y+dy; } @Override public String toString(){ return "("+x+","+y+")"; } public static void main(String[] args) { Scanner in=new Scanner(; Point p1=new Point(); Point p2=new Point(in.nextDouble(),in.nextDouble()); System.out.println("p1 The coordinates of the for "+p1); System.out.println("p2 The coordinates of the for "+p2); p2.movePoint(2,2); System.out.println("p2 The coordinates after moving are "+p2); }}

copyright:author[CSDN Q & A],Please bring the original link to reprint, thank you.