1、《面向对象程序设计》 1. 类和对象的概念和关系是什么? 2. 用UML表示交通工具Vehicle类及名为car1,car2及car3的三个Vehicle对象 3. 简述对象之间的消息传递机制是如何实现的? 4. import语句的用途是什么?Java程序是否总要包括import语句? 5. 什么是Java的源文件?什么是字节码文件? 6. Java虚拟机是什么?它有作用是什么? 7. 描述对象声明和对象生成之间的区别。使用内存状态图来说明这种区别 8. 编写Java应用程序,用一个对话框显示当前时间 9. 下面的代码段会有什么样的输出: class Q2main{
2、 public static void main(string[] args) { QuestionTwo q2; q2= new QuestionTwo(); q2.init(); q2.increment(); q2.increment(); system.out.println(q2.getCount()); } } class QuestionTwo{ private int count; public void int(){ count=1; }
3、public void increment(){ count=count+1; } public int getCount(){ return count; } } 10. 编写可以根据用户的年龄和身高给出推荐的体重的程序,利用下面的公式计算出推荐的体重: recommandedWeight=(height -100 + age/10)*0.9 定义名为Height(身高)的公共服务类,他应该有可以根据身高得出推荐体重的方法 作业练习二 11. 假如x的值为10,y的值为20,z的值为30,求出下列布尔表达式的值: a) x>y
4、 y>x
b) (x 5、 System.out.println(“You selected Red”);
else if (selection==0)
System.out.println(“You selected Blue”);
else if (selection==0)
System.out.println(“You selected Green”);
else
System.out.println(“Invalid selection”);
13. 画出下面两个switch语句的控制流程图
a) switch (choice){
case 1: a=0;
6、 break;
case 2: b=1;
break;
case 3: c=2;
break;
case 4: d=3:
break;
}
b) switch (choice){
case 1: a=0;
case 2: b=1;
case 3: c=2;
default: d=3;
}
14. 分别用for、do-while和while语句计算下面的累加和:
a) 1+1/2+1/3+1/4+…+1/15
b) 5+10+15+…+50
15. 编写一个计算闰年的程序, 7、要求用户输入一个年份,如果用户输入的年份不在0~3000年内则给予用户提示要求其重新输入,否则判断该年份是否为闰年并返回结果。
16. 下列哪一组重载方法是不合法的?
a) public void compute(int num){…}
public int compute(int num){…}
b) public void move(double length){…}
public void move(){…}
c) public int adjust(double amount){…}
public void adjust(double amount , double c 8、harge){…}
d) public void doWork() {…}
public void doWork(String name){…}
public int doWork(double num){…}
17. 完成下面这个类中的前四个构造方法。其中每一构造方法都是用关键字this调用第五个构造方法:
class Cat{
private static final String DEFAULT_NAME = "NO NAME";
private static final int DEFAULT_HGT=6;
private static final 9、double DEFAULT_WGT=10.0;
private String name;
private int height;
private double weight;
public cat(){
//分配缺省值给个成员变量
}
public cat(String name) {
//将参数值赋给name这个成员变量,height和weight赋缺省值
}
public cat(String name int height){
//将参数值分别赋给name和height两个成员变量, 10、weight赋缺省值
}
public cat(String name int weight){
//将参数值分别赋给name和weight两个成员变量,height赋缺省值
}
publie cat(String name,int height,double weight){
this.name=name;
this.height=height;
this.weight=weight;
}
....
}
18. 为Fraction定义一个类方法compare,compare接受两个Fraction对象f1和f2,并返回: 11、
1.如果f1小于f2,返回-1
2.如果f1等于f2,返回0
3.如果f1大于f2,返回1
作业练习三
19. 什么是异常?Java的异常处理机制是?
20. 分别输入-1,0和12XY,请写出这段代码的输出结果。
try{
number1 = Integer.parseInt(JOptionPane.showInputDialog(null,"input"));
if (number1 !=0){
throw new Exception("Not Zero");
}
}catch (NumberFormatException e){
System. 12、out.println("Cannot convert to int);
}catch (Exception e){
System.out.println("Error:"+e.getMessage());
}finnaly{
System.out.println(finally Clause Executed");
}
21. 分析下面程序代码的输出结果:
class MyException extends Exception {
public MyException() {}
public MyException(String msg) { super(m 13、sg); }
}
public class ExceptionTest{
public static void f() throws MyException {
System.out.println("The 1st line of f()");
throw new MyException("Originated in f()");
}
public static void main(String[] args) {
System.out.println("The 1st line of main()");
try {
14、 System.out.println("The 2nd line of main()");
f();
System.out.println("The 3rd line of main()");
} catch(MyException e) {
System.out.println(e.getMessage());
}
finally {
System.out.println("The 4th line of main()");
}
System.out.println( 15、"The 5th line of main()");
}
}
22. 分析下面程序代码的输出结果:
class Shape {
void draw(){
System.out.println("Shape.draw()");
}
}
class Circle extends Shape {
void draw() {
System.out.println("Circle.draw()");
}
}
class Square extends Shape {
void draw() {
System. 16、out.println("Square.draw()");
}
}
class ShapeGenerator {
public Shape getShape(int index) {
switch(index) {
default:
case 0: return new Circle();
case 1: return new Square();
}
}
public void shapeDraw(Shape s){
s.draw();
}
}
public clas 17、s Shapes {
private static ShapeGenerator gen =
new ShapeGenerator();
public static void main(String[] args) {
Shape[] s = new Shape[3];
for(int i = 0; i < s.length; i++)
s[i] = gen.getShape(i);
for(int i = 0; i < s.length; i++)
gen.shapeDraw(s[i]);
}
}
23. this和super关键字的意义和作用是?
24. 抽象方法和抽象类的特点和作用是?
25. 按以下要求给出相应的程序代码段:
a) 创建一个抽象类AbstractBase,该类包含一个抽象方法finBase();
b) 创建一个接口MyInterface,该类包含方法finInterface ();
c) 创建一个非抽象类MyClass,该类继承AbstractBase并实现MyInterface。
©2010-2025 宁波自信网络信息技术有限公司 版权所有
客服电话:4009-655-100 投诉/维权电话:18658249818