java实验一二三.docx
- 文档编号:10075524
- 上传时间:2023-02-08
- 格式:DOCX
- 页数:26
- 大小:777.24KB
java实验一二三.docx
《java实验一二三.docx》由会员分享,可在线阅读,更多相关《java实验一二三.docx(26页珍藏版)》请在冰豆网上搜索。
java实验一二三
实验一
6.编写一程序,运行后从键盘输入三个整数,输出其中的最大数。
(if语句)
7.编写一程序,将从键盘输入的一个百分制考试成绩转化为五分制输出。
百分制与五分制的对应如下:
(switch语句)
100~90—A,89~80—B,79~70—C,69~60—D,<60—E
实验二流程控制程序设计
一、实验目的和要求
掌握Java流程控制语句、Math类、数组的编程方法。
要求至少完成实验内容题的70%。
二、实验内容
1.打印一个三角形的1~9的乘法表。
2.编写一程序,将从键盘输入的每个月份数(整数)显示出其对应的英文,直至输入0结束,注意对非法数据的处理。
(while,switch语句)
packagecn.itcast.test;
importjava.util.Scanner;
publicclassmonth{
publicstaticvoidmain(String[]args){
Scannerc=newScanner(System.in);
System.out.println("请输入月份;");
inta=c.nextInt();
switch(a){
case1:
System.out.println("January");
break;
case2:
System.out.println("February");
break;
case3:
System.out.println("March");
break;
case4:
System.out.println("April");
break;
case5:
System.out.println("May");
break;
case6:
System.out.println("June");
break;
case7:
System.out.println("July");
break;
case8:
System.out.println("August");
break;
case9:
System.out.println("September");
break;
case10:
System.out.println("October");
break;
case11:
System.out.println("November");
break;
case12:
System.out.println("December");
break;
}
while(a==0)
{
System.out.println("结束");
break;
}
}
}
3.打印图案:
一个由n行星花组成的三角形。
如n=5时的图案为:
4.打印出所有的“水仙花数”。
所谓“水仙花数”是指一个三位数,其各位数字的立方和等于该数本身。
例如153是一个“水仙花数”,因为153=13+53+33。
5.编写一个程序,从键盘读一个年份的数字,然后判断该年是否是闰年,如果是就输出“闰年”,如果不是就输出“非闰年”。
闰年的条件是满足以下两个条件之一:
1)能被4整除,但不能被100整除;
2)能被4整除,又能被400整除。
6.统计个位数是6,并且能被3整除的五位数共有多少个.
7.编写一个程序,在其中建立一个有10个整数的数组,运行后从键盘输入10个数,然后输出其中的最小数。
8.编写一个程序,在其中建立一个有10个整数的数组,运行后从键盘输入10个数,然后排序(升序)后输出。
9.编写一个程序,找出5行5列整数矩阵中的最小元素,矩阵元素由键盘输入。
10.编写一个程序,计算出5行5列整数矩阵的每行之和。
11.编写一个程序,在其中定义一个66的二维整型数组,利用随机函数产生36个10~20之间的随机整数放入,然后将数组输出到屏幕上(6行6列格式)。
最后计算出数组中对角线元素的平方根和。
实验三面向对象程序设计
一、实验目的和要求
掌握类、对象、方法、继承、接口的编程技术;
要求至少完成实验内容题的70%。
二、实验内容
1.编一程序,求两个正整数m、n的最大公约数。
要求程序中有两个方法,分别使用循环和递归求最大公约数,最后在主方法中分别调用这两个方法求解56与91的最大公约数。
提示:
用辗转相除法。
方法:
(1)求m除n的余数r;
(2)如果r为0,则n为最大公约数,结束。
否则转(3);
(3)用n置m,用r置n,回到
(1)。
packageduixiang;
importjava.util.*;
publicclassGongyueshu{
publicstaticvoidmain(Stringargs[]){
Scannerin=newScanner(System.in);
System.out.print("请输入第一个整数:
");
intnum1=in.nextInt();//取出控制台输入的信息
System.out.print("请输入第二个整数:
");
intnum2=in.nextInt();//取出控制台输入的信息
in.close();
System.out.println(maxCommonDivisor(num1,num2));}
//调用maxCommonDivisor()方法
//递归法求最大公约数
publicstaticintmaxCommonDivisor(intm,intn){
if(m inttemp=m; m=n; n=temp; } if(m%n==0){//若余数为0,返回最大公约数 returnn; }else{//否则,进行递归,把n赋给m,把余数赋给n returnmaxCommonDivisor(n,m%n); } } //循环法求最大公约数 publicstaticintmaxCommonDivisor2(intm,intn){ if(m inttemp=m; m=n; n=temp; } while(m%n! =0){//在余数不能为0时,进行循环 inttemp=m%n; m=n; n=temp; } returnn;//返回最大公约数 } } 2.编写一个完整的JavaApplication程序,其中设计一个复数类Complex,利用它验证两个复数1+2i和3+4i相加产生一个新的复数4+6i。 复数类Complex的设计必须满足如下要求: 1)Complex的属性有: realPart: int型,代表复数的实数部分; maginPart: int型,代表复数的虚数部分。 2)Complex的方法有: Complex(): 构造方法,将复数的实部和虚部都置0; Complex(intr,inti): 构造方法,形参r为实部的初值,i为虚部的初值。 3)ComplexcomplexAdd(Complexa): 将当前复数对象与形参复数对象相加,最后的结果仍是一个复数对象,返回给此方法的调用者。 4)StringtoString(): 把当前复数对象的实部、虚部组合成a+bi的字符串形式,其中a和b分别为实部和虚部的数据。 3.编写一个包含圆类的程序,并为圆类设计几个构造方法和一般方法,在主方法中创建一个圆类对象并输出它的周长和面积。 要求: 属性有3个: x,y,r,分别放置圆心坐标和半径; 构造方法有2个。 一个是无参的,用于设置3个属性的值都为0;另一个有参的,用于设置3个属性的值,以确定一个具体的圆。 计算周长的方法: doublezc(); 计算面积的方法: doublemj()。 4.编写一个程序,它含有一个圆类、圆柱类和主类。 要求: 1)圆类参考上一题中的圆类; 2)圆柱类: 继承圆类,并加入一个属性h(高); 构造方法(给4个属性赋值); 计算面积的方法(doublemj()); 计算体积的方法(doubletj())。 注意,要充分利用父类的方法。 3)主类: 在主方法中创建圆和圆柱类的对象,然后计算并输出它们的面积及圆柱的体积。 packagetest; importtest.Circle; importtest.Cylinder; publicclassMainmath{ publicstaticvoidmain(Stringargs[]){ Circlecircle=newCircle(5,6,7); Cylindercylinder=newCylinder(6,7,8,9); System.out.println("圆的周长为: "+circle.zc()); System.out.println("圆的面积为: "+circle.mj()); System.out.println("圆柱的周长为: "+cylinder.mj()); System.out.println("圆柱的面积为: "+cylinder.mj()); } } classCircle{ protecteddoublex,y,r; publicCircle(intx1,inty1,intr1){ this.x=x1; this.y=y1; this.r=r1; } publicdoublezc(){returnMath.PI*2*r;} publicdoublemj(){returnMath.PI*r*r;} } classCylinderextendsCircle{ privatedoublehight; publicCylinder(intx,inty,intr,inthight){ super(x,y,r); this.hight=hight; } publicdoublemj(){ return2*super.mj()+hight*super.zc(); } publicdoubletj(){ returnhight*super.mj(); } } 5.编写一个含有5个类的程序: 类Person: 属性: 编号、姓名、性别; 构造方法: 确定编号和姓名; 一般方法: 修改编号、姓名,获取编号、姓名。 类Teacher: 继承类Person并增加: 属性: 系别; 构造方法: 调用父类的构造方法; 一般方法: 修改、获取系别。 类Student: 继承类Person并增加: 属性: 班级; 构造方法: 调用父类的构造方法; 一般方法: 修改、获取班级属性值。 类Classes: 属性: 班级名称,学生名单(Student类对象的数组); 构造方法: 确定班级名称; 一般方法: 建立学生名单,输出学生名单。 类Main: 主类。 主方法中创建一个班级,然后建立该班级的学生名单,最后 输出学生名单。 packageduixiang; publicclassMainclass{ publicstaticvoidmain(String[]args){ Classesclasses=newClasses("信安1班"); Studentstudent1=newStudent("1","小明"); Studentstudent2=newStudent("2","小张"); Studentstudent3=newStudent("3","小红"); Studentstudent4=newStudent("4","小刘"); Studentstudent5=newStudent("5","小刚"); classes.studentList[0]=student1.getNumberName(); classes.studentList[1]=student2.getNumberName(); classes.studentList[2]=student3.getNumberName(); classes.studentList[3]=student4.getNumberName(); classes.studentList[4]=student5.getNumberName(); classes.outStudentList(classes.studentList); } } classPerson{ privateStringnumber; privateStringname; Stringsex; publicPerson(Stringnumber,Stringname){ this.name=name; this.number=number; } publicvoidsetPerson(Stringnumber,Stringname){ this.name=name; this.number=number; } publicStringgetNumberName(){ returnthis.number+""+this.name; } } classTeacherextendsPerson{ Stringdepartment; publicTeacher(Stringnumber,Stringname){ super(number,name); } publicvoidsetDepartment(Stringdepartment){ this.department=department; } publicStringgetDepartment(){ returnthis.department; } } classStudentextendsPerson{ intclassNumber; publicStudent(Stringnumber,Stringname){ super(number,name); } publicvoidsetClassNumber(intclassNumber){ this.classNumber=classNumber; } publicintgetClasses(){ returnthis.classNumber; } } classClasses{ privateStringclassName; String[]studentList=newString[5]; publicClasses(StringclassName){ this.className=className; } publicvoidsetStudentList(String[]studentList){ this.studentList=studentList; } publicvoidoutStudentList(String[]studentList){ for(inti=0;i System.out.println(studentList[i]); } } }
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- java 实验 一二