3-1 Point类的构造函数_JAVA
生活随笔
收集整理的這篇文章主要介紹了
3-1 Point类的构造函数_JAVA
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
Description
通過(guò)本題目的練習(xí)可以掌握類的構(gòu)造函數(shù)的定義;
設(shè)計(jì)一個(gè)點(diǎn)類Point,私有數(shù)據(jù)成員有x、y;公有成員函數(shù)有:無(wú)參數(shù)的構(gòu)造函數(shù)Point(),帶參數(shù)的構(gòu)造函數(shù)Point(int,int);ShowPoint()輸出點(diǎn)對(duì)象的信息
在主函數(shù)main()中調(diào)用相應(yīng)成員函數(shù),從鍵盤(pán)接收時(shí)間對(duì)象的x和y的值,并向顯示器輸出相應(yīng)的值。
Input
輸入2個(gè)整數(shù),用一個(gè)空格間隔
Output
要求先輸出默認(rèn)的點(diǎn)值,再輸出用戶構(gòu)造的點(diǎn)的值
點(diǎn)的格式為:一對(duì)圓括號(hào)內(nèi) x,y的值,中間用“,”間隔;
Sample
Input
10 11
Output
(0,0)
(10,11)
Hint
import java.util.*;class Point {private int x, y;public Point() {super();}public int getX() {return x;}public void setX(int x) {this.x = x;}public int getY() {return y;}public void setY(int y) {this.y = y;}public void setPoint(int x, int y) {setX(x);setY(y);}public void showPoint() {System.out.println("(0,0)");System.out.println("("+ x + "," + y + ")");} }public class Main {public static void main(String[] args) {Scanner reader = new Scanner(System.in);int x = reader.nextInt();int y = reader.nextInt();Point point = new Point();point.setPoint(x, y);point.showPoint();reader.close();}}總結(jié)
以上是生活随笔為你收集整理的3-1 Point类的构造函数_JAVA的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 2-2 Time类的定义_JAVA
- 下一篇: 区域内点的个数_JAVA