java基础上机题分类整理版_第1页
java基础上机题分类整理版_第2页
java基础上机题分类整理版_第3页
java基础上机题分类整理版_第4页
java基础上机题分类整理版_第5页
已阅读5页,还剩35页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、一、 类和对象基础题(共13题)1编写一个java应用程序,该程序中有3个类:lader、circle和主类a。具体要求如下:lader类具有类型为double的上底、下底、高、面积属性,具有返回面积的功能,包括一个构造方法对上底、下底、高进行初始化。circle类具有类型为double的半径、周长和面积属性,具有返回周长、面积的功能,包括一个构造方法对半径进行初始化。主类a用来测试类lader和类circle的功能。解答:解题思路:该题需要构建lader、circle和主类a,lader类主要是做什么什么,a类主要做什么,起什么作用。解题过程(关键代码):解题总结:该题用到了什么什么知识点c

2、lass laderdouble s,x,g,area;double getarea()return (s+x)*g*0.5);lader(double s,double x,double g)this.s=s;this.x=x;this.g=g; class circle double r,m,l; circle(double rr) r=rr;double getm()return 3.14*r*r;double getl()return 2*3.14*r; public class a public static void main(string args) lader text=new

3、 lader(3,4,5); system.out.println("上底下底高分别为3,4,5的梯形面积为:"+text.getarea(); circle text1=new circle(5); system.out.println("半径为5的圆面积是:"+text1.getm()+",周长是"+text1.getl(); 结题总结:本题用到了 构造方法,new实例化对象,方法的声明和调用以及对象的引用等等。2按要求编写java应用程序:(1)编写西游记人物类(xiyoujirenwu)其中属性有:身高(height),名字(

4、name),武器(weapon)方法有:显示名字(printname),显示武器(printweapon)(2)在主类的main方法中创建二个对象:zhubajie,sunwukong。并分别为他们的两个属性(name,weapon)赋值,最后分别调用printname, printweapon方法显示二个对象的属性值。package java2;class xiyoujirenwudouble height;string name,weapon;void printname(string name)system.out.println("name:"+name); voi

5、d printweapon(string weapon)system.out.println("weapon:"+weapon);public class people public static void main(string args) xiyoujirenwu zhubajie=new xiyoujirenwu();xiyoujirenwu sunwukong=new xiyoujirenwu();zhubaj="猪八戒"sunwuk="孙悟空"zhubajie.weapon="九齿钉耙

6、"sunwukong.weapon="如意金箍棒"zhubajie.printname(zhubaj);zhubajie.printweapon(zhubajie.weapon);sunwukong.printname(sunwuk);sunwukong.printweapon(sunwukong.weapon);3编写java应用程序。首先定义一个描述银行账户的account类,包括成员变量“账号”和“存款余额”,成员方法有“存款”、“取款”和“余额查询”。其次,编写一个主类,在主类中测试account类的功能。class acco

7、untprotected string accountnumber;protected float balance;float desposit(float amout)balance+=amout;return balance;float withdraw(float amout)balance-=amout;return balance;account()this("",0);account(string number,float balance)accountnumber=number;this.balance=balance;void show()system.ou

8、t.printf("账号:%s的余额是:%f",accountnumber,balance);public class text public static void main(string args) account a1=new account("1000001",2000f); a1.desposit(1000f); a1.withdraw(500f); a1.show(); 4编写java应用程序。首先,定义一个时钟类clock,它包括三个int型成员变量分别表示时、分、秒,一个构造方法用于对三个成员变量(时、分、秒)进行初始化,还有一个成员方法

9、show()用于显示时钟对象的时间。其次,再定义一个主类testclass,在主类的main方法中创建多个时钟类的对象,使用这些对象调用方法show()来显示时钟的时间。class clockint hour,min,sec;clock(int h,int m,int s)hour=h;min=m;sec=s;clock()void show()system.out.printf("时间 %d:%d:%d",hour,min,sec);system.out.println();public class textclass public static void main(st

10、ring args)clock c1=new clock(4,20,9);clock c2=new clock();clock c3=new clock(14,2,9);c2.hour=2;c2.min=3;c2.sec=4;c1.show();c2.show();c3.show(); 5编写java应用程序。首先,定义描述学生的类student,包括学号(int)、姓名(string)、年龄(int)等属性;二个方法:student(int stuno,string name,int age)用于对对象的初始化,output()用于输出学生信息。其次,再定义一个主类testclass,在主类

11、的main方法中创建多个student类的对象,使用这些对象来测试student类的功能。class studentint number;string name;int age;student(int stuno,string name,int age)number=stuno;name=name;this.age=age;void output()system.out.printf("学号 :%d 姓名:%s 年龄:%d",number,name,age);system.out.println();public class textclass public static

12、void main(string args) student s1=new student(12001,"张三",21); student s2=new student(12002,"张飞",20); s1.output(); s2.output(); 6编写一个java应用程序,该应用程序包括2个类:print类和主类e。print类里有一个方法output()功能是输出100 999之间的所有水仙花数(各位数字的立方和等于这个三位数本身,如: 371 = 33 + 73 + 13。)在主类e的main方法中来测试类print。class printv

13、oid output()int a,b,c;for(int i=100;i<1000;i+)a=i%10;b=i/10%10;c=i/100;if(a*a*a+b*b*b+c*c*c)=i)system.out.print(i+"t");public class e public static void main(string args) print text=new print(); text.output(); 7编写java应用程序。首先,定义一个print类,它有一个方法void output(intx),如果x的值是1,在控制台打印出大写的英文字母表;如果x

14、的值是2,在控制台打印出小写的英文字母表。其次,再定义一个主类testclass,在主类的main方法中创建print类的对象,使用这个对象调用方法output ()来打印出大小写英文字母表。class print void output(int x)if(x=1)for(int j=65;j<=90;j+)system.out.printf("%c ",j);system.out.println();else if(x=2)for(int j=97;j<=122;j+)system.out.printf("%c ",j);system.ou

15、t.println();public class testclass public static void main(string args) print p=new print(); p.output(1); p.output(2); public static void main(string args) cat cat=new cat("妮妮8按要求编写java应用程序。(1)建立一个名叫cat的类:属性:姓名、毛色、年龄 行为:显示姓名、喊叫(2)编写主类:创建一个对象猫,姓名为“妮妮”,毛色为“灰色”,年龄为2岁,在屏幕上输出该对象的毛色和年龄,让该对象调用显示姓名和喊叫两

16、个方法。class cat string name,color; int age; void showname() system.out.println(name); void shout(string s) system.out.println(s); cat(string nam,string colo,int ag) name=nam; color=colo; age=ag;public class testclass","灰色",2); cat.showname(); system.out.println("颜色是"+cat.color

17、+" 年龄是"+cat.age+"岁"); cat.shout("喵喵"); 9创建一个三角形类,成员变量三边,方法求周长,创建类主类a来测试它。class triangleint a,b,c;void showperimeter()system.out.println("周长是"+(a+b+c);triangle(int a,int b,int c)this.a=a;this.b=b;this.c=c;public class a public static void main(string args) tria

18、ngle t=new triangle(3,4,5); t.showperimeter(); 10按要求编写java应用程序。(1)创建一个叫做people的类:属性:姓名、年龄、性别、身高行为:说话、计算加法、改名编写能为所有属性赋值的构造方法;(2)创建主类:创建一个对象:名叫“张三”,性别“男”,年龄18岁,身高1.80;让该对象调用成员方法:说出“你好!”计算23+45的值将名字改为“李四”class peoplestring name,sex;int age;float height;void speak(string s)system.out.println(s);void add

19、(int a,int b)system.out.printf("%d+%d=%dn",a,b,a+b);void rename(string name1)name=name1;system.out.println(name);people(string name_,int age_,string sex_,float height_)name=name_;age=age_;sex=sex_;height=height_;public class a public static void main(string args) people t=new people("

20、张三",18,"男",1.80f); t.speak("你好!"); t.add(23,45); t.rename("李四"); 11按要求编写java应用程序。(1)创建一个叫做机动车的类:属性:车牌号(string),车速(int),载重量(double)功能:加速(车速自增)、减速(车速自减)、修改车牌号,查询车的载重量。编写两个构造方法:一个没有形参,在方法中将车牌号设置“xx1234”,速度设置为100,载重量设置为100;另一个能为对象的所有属性赋值;(2)创建主类:在主类中创建两个机动车对象。创建第一个时调用无

21、参数的构造方法,调用成员方法使其车牌为“辽a9752”,并让其加速。创建第二个时调用有参数的构造方法,使其车牌为“辽b5086”,车速为150,载重为200,并让其减速。输出两辆车的所有信息class carstring number;int speed;double deadweight;car(string number,int speed,double deadweight)this.number=number;this.speed=speed;this.deadweight=deadweight;car()number="xx1234"speed=100;deadw

22、eight=100;void accelerate(int speed1)speed+=speed1;void reduction(int speed2)speed-=speed2;void changenumber(string number)number=number;void showdeadweight()system.out.println(deadweight); public class a public static void main(string args) car c1=new car(); c1.changenumber("辽a9752"); c1.

23、accelerate(30); car c2=new car("辽b5086",150,200); c2.reduction(50); system.out.println(c1.number+" 车速:"+c1.speed+" 载重量:"+c1.deadweight); system.out.println(c2.number+" 车速:"+c2.speed+" 载重量:"+c2.deadweight); 12创建一个point类,有成员变量x,y,方法getx(),setx(),还有一个构造

24、方法初始化x和y。创建类主类a来测试它。class pointprivate int x;public string y;public int getx()return this.x;public void setx(int x)this.x=x;point(int x,string y)this.x=x;this.y=y; public class a public static void main(string args) point p=new point(3,"text1"); point p1=new point(55,"text2");sys

25、tem.out.println(p.getx()+" "+p.y);system.out.println(p1.getx()+" "+p1.y); 13首先,编写一个类chongzai,该类中有3个重载的方法void print();其次,再编写一个主类来测试chongzai类的功能。class chongzaipublic double area(double radius)return (math.pi*radius*radius);public double area(double length,double width)return (lengt

26、h*width);public double area(double length,double width,double height)return ( length*width*height); public class a public static void main(string args) chongzai t=new chongzai(); system.out.println("圆的半径为5,面积是:"+t.area(5); system.out.println("矩形长为6,宽为21,面积是:"+t.area(6,21); system

27、.out.println("立方体长宽高分别为4,5,6,面积是:"+t.area(4,5,6); 二、 数组相关(共4题)14已知2个一维数组:a=3,4,5,6,7,b=1,2,3,4,5,6,7;把数组a与数组b对应的元素乘积再赋值给数组b,如:b2=a2*b2;最后输出数组b的元素。public class a public static void main(string args) int a=3,4,5,6,7;int b=1,2,3,4,5,6,7;for(int i=0;i<5;i+)bi*=ai;for(int j:b)system.out.prin

28、t(j+","); 15找出如下数组中最大的元素和最小的元素,a=3,2,6,6,8,2,10,5,12,3,23public class a public static void main(string args) int a=3,2,6,6,8,2,10,5,12,3,23;int min=a00,max=a00;for(int i=0;i<4;i+)for(int j=0;j<ai.length;j+)if(aij>max)max=aij;if(aij<min)min=aij;system.out.println("最大的元素是&qu

29、ot;+max+"n最小的元素是"+min); 16按要求编写java应用程序。编写一个名为test的主类,类中只有一个主方法;在主方法中定义一个大小为50的一维整型数组,数组名为x,数组中存放着1,3,5,99输出这个数组中的所有元素,每输出十个换一行;在主方法中定义一个大小为10*10的二维字符型数组,数组名为y,正反对角线上存的是*,其余位置存的是#;输出这个数组中的所有元素。public class text public static void main(string args) int x; x=new int50; int j=0,k=0; for(int i

30、=0;i<50;i+) xi=2*i+1; system.out.print(xi+"t"); j+; if(j%10=0) system.out.println(); char y=new char1010; for(int r=0;r<10;r+) for(int w=0;w<10;w+) if(r=w)|(9-r=w) yrw='*' else yrw='#' system.out.print(yrw+"t"); k+; if(k%10=0) system.out.println(); 17从键盘上

31、输入一个正整数n,请按照以下五行杨辉三角形的显示方式,输出杨辉三角形的前n行。请采用循环控制语句来实现。(三角形腰上的数为1,其他位置的数为其上一行相邻两个数之和。)11 11 2 11 3 3 11 4 6 4 1 1 5 10 10 5 1import java.util.scanner; public class text public static void main(string args) scanner sc=new scanner(system.in); system.out.println("请输入杨辉三角形n的值:(025)"); int n=sc.ne

32、xtint(); int a=new int2525; for(int i=0;i<n;i+)ai0=1; for(int i=1;i<n;i+)for(int j=1;j<=i;j+)if(i=j)aij=1;else aij=ai-1j+ai-1j-1; for(int i=0;i<n;i+) for(int j=0;j<=i;j+) system.out.print(aij+"t"); system.out.println(); 三、 继承相关(共13题)18实现如下类之间的继承关系,并编写music类来测试这些类。class instr

33、umentpublic void play()system.out.println("弹奏乐器"); class wind extends instrumentoverridepublic void play()system.out.println("弹奏wind");public void play2()system.out.println("调用wind的paly2"); class brass extends instrument override public void play() system.out.println(&q

34、uot;弹奏brass"); public void play2()system.out.println("调用brass的paly2"); public class musicpublic static void tune(instrument i)i.play(); public static void main(string args)wind a1=new wind();brass a2=new brass();tune(a1);tune(a2); 19创建如下三个类:(people类中的三个方法分别输出一些信息,chinapeople和americanp

35、eople类重写父类的三个方法)。package java1;public class people protected double height;protected double weight;public void speakhello()system.out.println("welcome!");public void averageheight()system.out.println("height");public void averageweight()system.out.println("weight");pack

36、age java1;public class americanpeople extends people public void speakhello()system.out.println("hello!");public void averageheight()system.out.println("180cm");public void averageweight()system.out.println("144kg");public void americarboxing()system.out.println("直

37、拳,勾拳");package java1;public class chinapeople extends people public void speakhello()system.out.println("你好!");public void averageheight()system.out.println("185c m");public void averageweight()system.out.println("140kg");public void chinagongfu()system.out.println

38、("坐如钟,站如松,睡如弓");20编写一个java应用程序,该程序包括3个类: a类、b类和主类e。其中类b是类a的子类,在子类b中新增了成员变量和成员方法,并且隐藏了父类a的成员变量和重写了父类a的成员方法。在主类e的main方法中,创建类b的对象并赋给父类a的对象a,使用上转型对象a来测试上转型对象的一些特性。class a int a1=5; void ma() system.out.println("a.ma"); class b extends a int b1=6; int a1=7; void mb() system.out.printl

39、n("b.mb"); void ma() system.out.println("b.ma"); public class e public static void main(string args) b b=new b(); a a=b; system.out.println("b.a1="+b.a1); system.out.println("b.b1="+b.b1); b.ma(); b.mb(); system.out.println("a.a1="+a.a1); a.ma(); 21编

40、写一个java应用程序,该程序包括3个类:monkey类、people类和主类e。要求:(1) monkey类中有个构造方法:monkey (string s),并且有个public void speak()方法,在speak方法中输出“咿咿呀呀.”的信息。(2)people类是monkey类的子类,在people类中重写方法speak(),在speak方法中输出“小样的,不错嘛!会说话了!”的信息。(3)在people类中新增方法void think(),在think方法中输出“别说话!认真思考!”的信息。(4)在主类e的main方法中创建monkey与people类的对象类测试这2个类的功

41、能。class monkeymonkey (string s) system.out.println(s);public void speak()system.out.println("咿咿呀呀."); class people extends monkey people(string s) super(s);public void speak()system.out.println("小样的,不错嘛!会说话了!");public void think()system.out.println("别说话!认真思考!");public c

42、lass epublic static void main(string args) monkey monkey = new monkey("我是猴子");monkey.speak();people people = new people("刘德华");people.speak();people.think();22编写一个类a,该类创建的对象可以调用方法f输出小写的英文字母表。然后再编写一个a类的子类b,要求子类b必须继承类a的方法f(不允许重写),子类b创建的对象不仅可以调用方法f输出小写的英文字母表,而且可以调用子类新增的方法g输出大写的英文字母表

43、。最后编写主类c,在主类的main方法中测试类a与类b。class a public void f() system.out.println("英文字母小写:"); for(char d='a'd<='z'd+) system.out.print(d+"t"); system.out.println(); class b extends a public void g() system.out.println("英文字母大写:"); for(char d='a'd<='

44、;z'd+) system.out.print(d+"t"); system.out.println(); public class cpublic static void main(string args) b b = new b(); b.f(); b.g();23实现一个名为person的类和它的子类employee,employee有两个子类faculty和staff。具体要求如下:(1)person类中的属性有:姓名name(string类型),地址address(string类型),电话号码telphone(string类型)和电子邮件地址email(s

45、tring类型);(2)employee类中的属性有:办公室office(string类型),工资wage(double类型),受雇日期hiredate(string类型);(3)faculty类中的属性有:学位degree(string类型),级别level(string类型);(4)staff类中的属性有:职务称号duty(string类型)。class personprotected string name;protected string address;protected string telephone;protected string email;person(string na

46、me, string address,string telephone, string email)=name;this.telephone=telephone;this.address=address;this.email=email;public string p() return +"t"+this.telephone+"t"+this.address+"t"+this.email;class employee extends personprotected string office;pro

47、tected double salary;protected string hiredate;employee(string name, string address, string telephone, string email,string office,double salary,string hiredate) super(name, address, telephone, email);this.office=office;this.salary=salary;this.hiredate=hiredate;public string e() return +&quo

48、t;t"+this.telephone+"t"+this.address+"t"+this.email+"t"+this.office+"t"+this.salary+"t"+this.hiredate;class faculty extends employeepublic string degree,level;faculty(string name, string address, string telephone, string email,string office, dou

49、ble salary, string hiredate,string degree,string level) super(name, address, telephone, email, office, salary, hiredate);this.degree=degree;this.level=level;public string f() return +"t"+this.telephone+"t"+this.address+"t"+this.email+"t"+this.office+&

50、quot;t"+this.salary+"t"+this.hiredate+"t"+this.degree+"t"+this.level;class staff extends employeepublic string duty;staff(string name, string address, string telephone, string email,string office, double salary, string hiredate,string duty) super(name, address, tel

51、ephone, email, office, salary, hiredate);this.duty=duty;public string s() return +"t"+this.telephone+"t"+this.address+"t"+this.email+"t"+this.office+"t"+this.salary+"t"+this.hiredate+"t"+this.duty;public class test public

52、 static void main(string args) person person = new person("张三","中国","122","1122");employee employee = new employee("张气","美国","1211","22122","304",3000,"20081211");faculty faculty = new faculty("李

53、哈","中国","1222","00221","303",5000,"20090311","本科","五级");staff staff = new staff("赵氏","日本","12111","99922","509",7000,"20080511","领事");system.out.println( p

54、erson.p();system.out.println(employee.e();system.out.println( faculty.f();system.out.println(staff.s();24编写一个car类,具有final类型的属性品牌,具有功能drive;定义其子类aodi和benchi,具有属性:价格、型号;具有功能:变速;定义主类e,在其main方法中分别创建aodi和benchi的对象并测试对象的特性。class car final string carbrand="品牌"void drive()system.out.println(carbrand+"car开车");class aodi extends car int price;

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论