欢迎来到天天文库
浏览记录
ID:37915431
大小:38.00 KB
页数:8页
时间:2019-06-02
《answer-程序复习》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、程序复习1、class例题:编写程序,模拟银行账户功能。要求如下:属性:账号、姓名、地址、存款余额、最小余额。方法:存款、取款、查询。根据用户操作显示储户相关信息。如存款操作后,显示储户原有余额、今日存款数额及最终存款余额。取款时,若取款成功,显示信息提示;若最后余额小于最小余额,拒绝取款,并显示信息提示。具体显示信息可据方便性原则自拟。classAccount{Stringaccount;Stringname;Stringaddress;doublebalance;staticdoubleminBalance=10;publi
2、cAccount(Stringaccount,Stringname,Stringaddress,doublebalance){this.account=account;this.address=address;this.name=name;this.balance=balance;}voiddeposit(doublecash){balance+=cash;System.out.println(balance);}voidwithdraw(doublecash){doubletemp=balance-cash;if(temp3、inBalance)System.out.println("至少保留余额:"+minBalance);else{balance=temp;System.out.println(balance);}}voidquery(){System.out.println("现有余额:"+balance);}voidshowInformation(){System.out.print("账号:"+account+"");System.out.print("姓名:"+name+"");System.out.print("地址:"+address4、+"");System.out.println("现有余额:"+balance+"");}publicstaticvoidmain(String[]args){Accountaccount1=newAccount("650101","李晓","天河学院",200.00);account1.deposit(300.5);account1.withdraw(200);account1.withdraw(300);account1.query();account1.showInformation();System.out.printl5、n(account1.name);}}2、Inheritance(继承)例题:设计一个表示二维平面上圆的类Circle,它继承自类Point,还包括表示圆半径的成员变量r、设置r值的方法、计算圆面积的方法。设计一个表示圆柱体的类Cylinder,它继承自类Circle,还包括表示圆柱体高的成员h、设置h的方法、计算圆柱体体积的方法。建立若干个Cylinder对象,输出其半径、高、底面积及其体积的值。//CircleclassCircle{doubler;finaldoublePI=3.14159;publicCircle(dou6、bler){this.r=r;}publicvoidgetArea(){System.out.println(PI*r*r);}}//CylinderclassCylinderextendsCircle{doubleh;publicCylinder(doubler,doubleh){super(r);this.h=h;}publicdoublegetVolume(){returngetArea()*h;}publicstaticvoidmain(String[]args){Cylinderc1=newCylinder(5.0);7、System.out.println("半径:"+c1.r());System.out.println("底面积:"+c1.getArea());System.out.println("体积:"+c1.getVolume());}}3、I/O例题1:键盘输入从键盘输入一个整数,求n!。importjava.io.*;classEx1{publicstaticvoidmain(Stringargs[])throwsIOException{inti,t=1,n;InputStreamReaderin1=newInputStreamR8、eader(System.in);BufferedReaderin2=newBufferedReader(in1);System.out.println("请输入一个正整数:");n=Integer.parseInt(in2.readLine());for(i=
3、inBalance)System.out.println("至少保留余额:"+minBalance);else{balance=temp;System.out.println(balance);}}voidquery(){System.out.println("现有余额:"+balance);}voidshowInformation(){System.out.print("账号:"+account+"");System.out.print("姓名:"+name+"");System.out.print("地址:"+address
4、+"");System.out.println("现有余额:"+balance+"");}publicstaticvoidmain(String[]args){Accountaccount1=newAccount("650101","李晓","天河学院",200.00);account1.deposit(300.5);account1.withdraw(200);account1.withdraw(300);account1.query();account1.showInformation();System.out.printl
5、n(account1.name);}}2、Inheritance(继承)例题:设计一个表示二维平面上圆的类Circle,它继承自类Point,还包括表示圆半径的成员变量r、设置r值的方法、计算圆面积的方法。设计一个表示圆柱体的类Cylinder,它继承自类Circle,还包括表示圆柱体高的成员h、设置h的方法、计算圆柱体体积的方法。建立若干个Cylinder对象,输出其半径、高、底面积及其体积的值。//CircleclassCircle{doubler;finaldoublePI=3.14159;publicCircle(dou
6、bler){this.r=r;}publicvoidgetArea(){System.out.println(PI*r*r);}}//CylinderclassCylinderextendsCircle{doubleh;publicCylinder(doubler,doubleh){super(r);this.h=h;}publicdoublegetVolume(){returngetArea()*h;}publicstaticvoidmain(String[]args){Cylinderc1=newCylinder(5.0);
7、System.out.println("半径:"+c1.r());System.out.println("底面积:"+c1.getArea());System.out.println("体积:"+c1.getVolume());}}3、I/O例题1:键盘输入从键盘输入一个整数,求n!。importjava.io.*;classEx1{publicstaticvoidmain(Stringargs[])throwsIOException{inti,t=1,n;InputStreamReaderin1=newInputStreamR
8、eader(System.in);BufferedReaderin2=newBufferedReader(in1);System.out.println("请输入一个正整数:");n=Integer.parseInt(in2.readLine());for(i=
此文档下载收益归作者所有