2670-3-1 Point類的建構函式-JAVA
阿新 • • 發佈:2018-12-17
3-1 Point類的建構函式
Time Limit: 1000 ms Memory Limit: 65536 KiB
Problem Description
通過本題目的練習可以掌握類的建構函式的定義;
設計一個點類Point,私有資料成員有x、y;公有成員函式有:無引數的建構函式Point(),帶引數的建構函式Point(int,int);ShowPoint()輸出點物件的資訊
在主函式main()中呼叫相應成員函式,從鍵盤接收時間物件的x和y的值,並向顯示器輸出相應的值。
Input
輸入2個整數,用一個空格間隔
Output
要求先輸出預設的點值,再輸出使用者構造的點的值
點的格式為:一對圓括號內 x,y的值,中間用“,”間隔;
Sample Input
10 11
Sample Output
(0,0)
(10,11)
Hint
Source
import java.util.*; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); while (scanner.hasNext()) { int x = scanner.nextInt(); int y = scanner.nextInt(); Point point1 = new Point(); point1.showPoint(); Point point2 = new Point(x, y); point2.showPoint(); } } } class Point { private int x = 0; private int y = 0; public Point() { super(); } public Point(int x, int y) { super(); this.x = x; this.y = y; } public void showPoint() { System.out.println("(" + x + "," + y + ")"); } }