作业3

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

作业三1.编写一个程序片断,定义一个学生类Student,包括成员变量“学号”、“班级”、“姓名”、“性别”、“年龄”,成员方法“获得学号”、“获得班号”、“获得姓名”、“获得性别”、“获得年龄”、“修改年龄”。publicclassStudent{privateStringstudentN;privateintclassID;privateStringname;privatechargener;privateintage;Student(Stringno,intid,Stringnm,charg,intag){studentN=no;classID=id;name=nm;gener=g;age=ag;}StringgetStringN(){returnstudentN;}intgetClassID(){returnclassID;}chargetGender(){returnGender;}StringgetName(){returnName;}intgetAge(){returnAge;}publicvoidsetStudentAge(intnewAge){Age=newAge;}}2.设计并测试一个表示图书的Book类,它包含图书的书名、作者、月销售量属性,另有两个构造方法(一个默认构造方法,另一个带参数构造方法)和两个成员方法setBook()、printBook()(用于输出书名、作者、月销售量数据)。classBook{Stringtitle;Stringauthor;intmonthsold;Book(){}Book(Stringstr1,Stringstr2,intnum){title=str1;author=str2;monthsold=num;}voidsetBook(Stringstr1,Stringstr2,intnum){title=str1;author=str2;monthsold=num;}voidprintBook(){System.out.println(书名:+title);System.out.println(作者:+author);System.out.println(月销售量:+monthsold);}}//下面是测试上述类的主类publicclassTestBook{publicstaticvoidmain(String[]args){Bookbook1=newBook();book1.setBook(Java实用教程,吴剑,600);book1.printBook();Bookbook2=newBook(C++教程,杨晓,500);book2.printBook();}}3.定义一个接口Area,其中包含一个计算面积的抽象方法calculateArea(),然后设计MyCircle和MyRectangle两个类都实现这个接口中的方法calculateArea(),计算圆和矩形的面积。interfaceArea{floatcalculateArea();}classMyCircleimplementsArea{privatefloatradius;MyCircle(floatradius){this.radius=radius;}publicfloatcalculateArea(){return((float)Math.PI*radius*radius);}}classMyRectangleimplementsArea{privatefloatwidth;privatefloatheight;MyRectangle(floatwidth,floatheight){this.width=width;this.height=height;}publicfloatcalculateArea(){return(width*height);}}publicclassInterfaceExample{publicstaticvoidmain(String[]args){MyCirclec=newMyCircle(8);System.out.println(Circle'sarea=+c.calculateArea());MyRectangler=newMyRectangle(4,6);System.out.println(Rectangle'sarea=+r.calculateArea());}}4.编写一个Application,键盘输入一个字母,加1后输出到屏幕。publicclassApp6{publicstaticvoidmain(String[]args){try{System.out.print(请输入一个字符:);charc=(char)(System.in.read()+1);System.out.println(刚输入的字符加1后为:+c);}catch(java.io.IOExceptione){}}}5.使用图形用户界面,编写一个完整的计算及打印整数(3个整数)乘积的程序。importjavax.swing.JOptionPane;publicclassProduct{publicstaticvoidmain(String[]args){intx,y,z,result;StringxVal,yVal,zVal;xVal=JOptionPane.showInputDialog(Enterfirstinteger:);yVal=JOptionPane.showInputDialog(Entersecondinteger:);zVal=JOptionPane.showInputDialog(Enterthirdinteger:);x=Integer.parseInt(xVal);y=Integer.parseInt(yVal);z=Integer.parseInt(zVal);result=x*y*z;JOptionPane.showMessageDialog(null,Theproductis+result);System.exit(0);}}

1 / 5
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功