您好,欢迎访问三七文档
当前位置:首页 > 中学教育 > 初中教育 > java编程期末复习
编程题:1.编写一个输出HelloWorld!的程序,用两种方式实现(Application、Applet)。application程序publicclassa{publicstaticvoidmain(Stringargs[]){System.out.println(HelloWorld!);}}2、applet程序publicclassbextendsjava.applet.Applet{publicpaint(java.awt.Graphicsg){g.outstring(HelloWorld!,10,10);}}2.求10个数中的最小值并输出。importjava.util.Scanner;publicclassTest{publicstaticvoidmain(Stringargs[]){intnum[]=newint[10];intmin;Scannerscan=newScanner(System.in);System.out.print(请输入10个数字:);for(inti=0;i10;i++){num[i]=scan.nextInt();}min=num[0];for(inti=1;i10;i++){if(minnum[i])min=num[i];}System.out.println(最小值是+min);}3.建立一个学生类,其中成员变量为学号,姓名,及三门课成绩。另外建立一个包含主方法的类,定义2个学生类的对象,求出这2个学生三门课总分的最高分,并将最高分这个学生的信息输出。classstudent{Stringname,no;floateng,math,chi;publicstudent(Stringname1,Stringno1,floateng1,floatmath1,floatchi1){name=name1;no=no1;eng=eng1;math=math1;chi=chi1;}floatsum(){return(eng+math+chi);}voidprint(){System.out.println(name:+name+\tno:+no+\ttotal:+sum());}}publicclassmax{publicstaticvoidmain(String[]args){students1=newstudent(lily,001,80,90,50);students2=newstudent(lucy,002,70,70,70);if(s1.sum()s2.sum())s1.print();elses2.print();}}4.请编写一个实现如下功能的Application:比较从键盘输入的两个整数是否相等,并根据比较结果显示“相等”或“不相等”。importjava.util.Scanner;publicclassCOMPare{publicstaticvoidmain(Stringargs[]){Scannertr=newScanner(System.in);intb=tr.nextInt();inta=tr.nextInt();if(a==b)System.out.println(相等);elseSystem.out.println(不相等);}}5.编写一个Application,利用数组求出”HELLO”,”JAVA””PROGRAM”三个字符串的平均长度。classAverage{publicstaticvoidmain(Stringargs[]){Stringarray[]=newString[3];array[0]=HELLO;array[1]=JAVA;array[2]=PROGRAM;inttotal=array[0].length();total+=array[1].length();total+=array[2].length();System.out.println(平均字符串长度为:+total/3);}}6、习题(6_13):importjava.util.*;classStudent{Stringnum;Stringname;Stringsex;booleancleader;floatmath;floatchn;floateng;publicStudent(StringnewNum,StringnewName,StringnewSex,booleannewCleader,floatnewMath,floatnewChn,floatnewEng){num=newNum;name=newName;sex=newSex;cleader=newCleader;math=newMath;chn=newChn;eng=newEng;}publicfloatsum(){returnmath+chn+eng;}publicfloatave(){returnsum()/3;}publicvoidoutput(){System.out.println(num+\t+name+\t+sex+\t+cleader+\t\t+math+\t+chn+\t+eng+\t+sum()+\t+ave());}}publicclassbook6_13_2{publicstaticvoidmain(String[]args){StringnewNum,newName,newSex;booleannewCleader;floatnewMath,newChn,newEng;Student[]s=newStudent[3];for(inti=1;i=s.length;i++){System.out.println(请输入第+i+个学生的信息);Scannerreader=newScanner(System.in);System.out.print(请输入该学生学号:);newNum=reader.nextLine();System.out.print(请输入该学生姓名:);newName=reader.nextLine();System.out.print(请输入该学生性别:);newSex=reader.nextLine();System.out.print(请输入该学生是否为班干部:);newCleader=reader.nextBoolean();System.out.print(请输入该学生数学成绩:);newMath=reader.nextFloat();System.out.print(请输入该学生语文成绩:);newChn=reader.nextFloat();System.out.print(请输入该学生英语成绩:);newEng=reader.nextFloat();s[i-1]=newStudent(newNum,newName,newSex,newCleader,newMath,newChn,newEng);}System.out.println(学号\t姓名\t性别\t班干部否\t数学\t语文\t英语\t总分\t平均分);for(inti=1;i=s.length;i++)s[i-1].output();}}7、//第5章第11题importjava.io.*;publicclassExercises5_11{publicstaticvoidmain(String[]args)throwsIOException{Stringstr;BufferedReaderbuf;buf=newBufferedReader(newInputStreamReader(System.in));System.out.print(请输入字符串,输入exit退出:);do{str=buf.readLine();;System.out.println(str);}while(!str.equals(exit));}}8、定义一个Person类,该类具有姓名、身高、体重、年龄属性,能够对上述相关信息进行输出display()。由Person类派生出China类,增加爱好属性。.classPerson{privateStringname;privateintage;privatefloathight;privatefloatweight;publicPerson(Stringn,inta,floath,floatw){name=n;age=a;hight=h;weight=w;}publicvoidshow(){System.out.println(Person[name=+name+,age=+age+,hight=+hight+,weight=+weight+]);}}classChinaextendsPerson{privateStringhobby;publicChina(Stringn,inta,floath,floatw,Stringhob){super(n,a,h,w);hobby=hob;}publicvoiddisplay(){System.out.println(China[hobby=+hobby+]);}}publicclassE8_1{publicstaticvoidmain(String[]args){Chinacc=newChina(wenwen,23,168,104,sing);cc.show();cc.display();}}9、创建一个图形类,包括计算图形面积的方法。创建两个子类长方形、圆形,分别继承图形类,重写子类中的计算图形面积的方法。写一个测试类,分别创建一个长方形和圆形类的对象,并且分别计算这两个图形的面积.//filename:app8_2.java抽象类的说明abstractclassShape{protectedStringname;publicShape(Stringxm){name=xm;System.out.print(名称:+name);}abstractpublicdoublegetArea();}classCircleextendsShape{privatefinaldoublePI=3.14;privatedoubleradius;publicCircle(StringshapeName,doubler){super(shapeName);radius=r;}publicdoublegetArea(){returnPI*radius*radius;}}classRectangleextendsShape{privatedoublewidth;privatedoubleheight;publicRectangle(StringshapeName,doublewidth,doubleheight){super(shapeName);this.width=width;this.height=height;}publicdoublegetArea(){returnwidth*height;}}publicclassE8_2{publicstaticvoidmain(String[]args){Shaperect=newRectangle(长方形,6.5,10.3);System.out.println(;面积=+rect.getArea());Shapecircle=newCircle(圆,10.2);System.out.println(;面积=+circle.getArea());}}10、编写一个“Student”类,该类拥有属性:校名、学号、性别、出生日期。方法包含构造方法和输出方法。再编写“Student”类的子类:Undergraduate(大学生)。Undergraduate类除拥有父类属性和方法外,还有其自己的属性和方法:附加属性包括系(department)、专业(major);方法包含构造方法和输出方法。classStudent{Stringname;intsNum;Stringsex;Stringb
本文标题:java编程期末复习
链接地址:https://www.777doc.com/doc-5562308 .html