Java期末50题.docVIP

  • 81
  • 0
  • 约4.06万字
  • 约 50页
  • 2016-12-19 发布于贵州
  • 举报
1. 编写一个Java应用程序,计算1~1000之间能被3和7同时整除的整数之和。 public class ThreeSeven { public static void main(String[] args) { for(int i=1;i=1000;i++) { if(i%3==0i%7==0) { System.out.println(i); } } System.out.println(1和1000之间能被3和7同时整除的整数有:); } } 2.编写Java应用程序。首先,定义一个时钟类——Clock,它包括三个int型成员变量分别表示时、分、秒,一个构造方法用于对三个成员变量(时、分、秒)进行初始化,还有一个成员方法show()用于显示时钟对象的时间。其次,再定义一个主类——TestClass,在主类的main方法中创建多个时钟类的对象,使用这些对象调用方法show()来显示时钟的时间。 public class Clock { int h,m,s; Clock(int h1,int m1,int s1) { h=h1; m=m1; s=s1; } void show() { System.out.println(h+时+m+分+s+秒); } } public class TestClass { public static void main(String[] args) { Clock c1=new Clock(10,10,10); c1.show(); } } 3.编写Java应用程序。首先,定义一个Print类,它有一个方法void output(int x){},如果x的值是1,在控制台打印出所有大写的英文字母;如果x的值是2,在控制台打印出所有小写的英文字母。其次,再定义一个主类——TestClass,在主类的main方法中创建Print类的对象,使用这个对象调用方法output ()来打印出大小写英文字母。 public class Print { void output(int x) { if(x==1) { for(char c=A;c=Z;c++) { System.out.println(c+); } } if(x==2) { for(char c=a;c=z;c++) { System.out.println(c+); } } } } public class TestClass { public static void main(String[] args) { Print p=new Print(); p.output(1); p.output(2); } } 4.编写一个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类的对象类测试这两个类的功能。 public class Monkey { Monkey(String s) { } public void speak() { System.out.println(咿咿呀呀……); } } public class People extends Monkey { People() { super(); } public void speak() { System.out.println(小样,不错嘛……); } void think() { System.out.println(别说话,认真思考~!); } } public class E { public static void main(String[] args) { Monkey m=new Monkey(); People p=new People(); m.speak(); p.speak(); p.think(); } } 5.编写一个类A,该类创建的对象可

文档评论(0)

1亿VIP精品文档

相关文档