2012年5月29日 星期二
物件導向設計:99年期末考古
[學院]:資電學院
[系所]:資訊工程學系
[年級]:二
[學程]:SE軟體工程
[課名]:物件導向設計
[老師]:張貴忠老師
[學期]:99-1
[類型]:期末考
Totally 100 points
1. ( Inner Class – 5 pts) What are two advantages to use inner classes?
ANSWER:There are two big advantages to using inner classes. First, because they are defined within a class, they can be used to make the outer class self-contained. The second advantage is that the inner and outer classes’ methods have access to each other’s private methods and private instance variables.
2. (The final Modifier – 15 pts) What are the meanings of final class, final variable, and
final method?
ANS: Final class:不可被繼承的class
Final variable:一經給定就不可變動的常數
Final method:不可被overrided的method
3. (Inheritance - 10 pts) What are the differences between overloading and overriding?
ANS:overloading:在同一class中,相同名稱的method,使用不同的回傳型態或不同的傳入參數來作區別
overriding:繼承之後,如果覺得父類別的method不符使用,可在子類別中定義一模一樣的method來修改
不同處:繼承與非繼承 / 宣告部份一樣與宣告一模一樣
4. (Event-Driven Programming – 15 pts) Please explain what is event-driven
programming ? What are the differences between traditional programming and
event-driven programming?
ANS:event-driven programming:在程式執行的時候,是依靠某些事件(如:鍵盤滑鼠)來決定程式之後的執行,跟傳統的程式不同在於控制流程的改變
5. (Inheritance & Polymorphism - 20 pts) Find all compiler-time errors in the following codes and give brief explanations about these errors. When you correct all errors, please write down the output of this program.
public class A
{
String msg;
public A(String msg)
{
this.msg = msg;
}
public void sayHello()
{
System.out.println(“A:” + msg);
}
public void say()
{
sayHello();
}
}
public class B extends A
{
String value;
public B()
{
value = 0;
super("I'm class B");
}
public B(String msg)
{
super(msg);
value = 0;
}
public B(int val)
{
value = val;
}
private void sayHello()
{
System.out.println(“B:” + msg);
}
}
public class C
{
public static void main(String[] args)
{
A obj1 = new A(“I’m Class A”);
B obj2 = new B();
C mainObj = new C();
mainObj.showMessage(obj1);
mainObj.showMessage(obj2);
}
public void showMessage(A member)
{
member.say();
}
}
6. (Exception Handling - 10 pts) What’s the problem about the following code segment?
(類似概念的程式碼)
try
{
throw new Exception(StringArqument);
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
catch (ArithmeticException e)
{
System.out.println("In ArithmeticException"+e.getMessage());
}
7. (Array – 10 pts) What’s the problem about the following code segment?
(類似概念的程式碼)
public class objArray
{
private int i;
private String s;
public void method1()
{
i=0;
}
public void method2(String theString)
{
s=theString;
}
}
public static void main(String[] args)
{
objArray[] oa = new objArray[10];
oa[0].method1();
oa[10].method2("hahaha");
}
8. (Access Modifiers - 15 pts) Given the following codes, and some compile-time errors exist in this program. Please precisely indicate the line numbers of error statements in the program. (Remember to briefly explain why the indicated statement is error.)
1 package mypackage;
2 public class A {
3 public int v1;
4 protected int v2;
5 int v3;
6 private int v4;
7 }
8 package mypackage;
9 public class B {
10 public B() {
11 A obj1 = new A();
12 obj1.v1 = 10;
13 obj1.v2 = 10;
14 obj1.v3 = 10;
15 obj1.v4 = 10;
16 }
17 }
18 package mypackage;
19 public class C extends A {
20 public C() {
21 v1 = 10;
22 v2 = 10;
23 v3 = 10;
24 v4 = 10;
25 }
26 }
27 import mypackage.A;
28 public class D extends A {
29 public D() {
30 v1 = 10;
31 v2 = 10;
32 v3 = 10;
33 v4 = 10;
34 }
35 }
36 public class E {
37 public E() {
38 A obj1 = new A();
39 obj1.v1 = 10;
40 obj1.v2 = 10;
41 obj1.v3 = 10;
42 obj1.v4 = 10;
43 }
44 }
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言