[課業] 視窗程式考古題 2010期末文字檔
1. 下列程式執行時會發生例外而中止執行,請在b=0及a/b不變的情況下,修改本程式使
不會因例外發生而中斷執行。(10%)
public class app
{
public static void main(String args[])
{
int a=4,b=0;
System.out.println(a+"/"+b+"="+a/b);
}
}
-
2. 簡單說明 public, private 及 protected 對類別成員及建構元存取的作用。(10%)
-
3. 下列程式執行的結果會不正確,請說明原因及如何修正。(10%)
class CBank
{
private static int sum=0;
public static void add(int n)
{
int tmp=sum;
tmp=tmp+n;
try
{
Thread.sleep((int)(1000*Math.random()));
}
catch(InterruptedException e){}
sum=tmp;
System.out.println("sum= "+sum);
}
}
class CCustomer extends Thread
{
public void run()
{
for(int i=1;i<=3;i++)
CBank.add(100);
}
}
public class app
{
public static void main(String args[])
{
CCustomer c1=new CCustomer();
CCustomer c2=new CCustomer();
c1.start();
c2.start();
}
}
-
4. 下面的程式希望產生亂數並將其寫到檔案 randow.txt,但實際執行時 random.txt並
沒有存下任何內容,請說明如何修改程式使執行結果正確。(10%)
import java.io.*;
public class app
{
public static void main(String args[])
{
try
{
FileWriter fw=new FileWriter("c:\\random.txt");
BufferedWriter bfw=new BufferedWriter(fw);
for(int i=1;i<=5;i++)
{
bfw.write(Double.toString(Math.randow()));
bfw.newLine();
}
fw.close;
}
catch(IOException e){}
}
}
-
5. 請簡要說明 Java 的"委派事件模式"的運作流程。(10%)
-
6. 若一個視窗物件其中包含一個按鈕 (button) 物件及一個功能表的 MenuItem 物件,
這兩個物件均會觸發 ActionEvent 事件,假設有一個ActionEvent 事件被觸發,請說
明如何設計程式使能區分是由何物件所觸發。(10%)
-
7. 以 Java 繪圖時,若需要重畫時常會呼叫 update(),但是若圖形會移動且呼叫很頻繁
時會感覺會面閃爍,請說明其原因及解決方式。(10%)
-
8. 下列程式希望按按鈕時會將視窗的背景顏色改為黃色,但程式尚有欠缺,請予以完成
。(10%)
import java.awt.*;
import java.awt.event.*;
public class app extends Frame implements ActionListener
{
static app frm=new app();
static Button btn=new Button("Click Me");
public static void main(String args[])
{
btn.addActionListener(frm);
frm.setLayout(new FlowLayout());
frm.setTitle("Action Event");
frm.setSize(200,150);
frm.setBackground(Color.blue);
frm.add(btn);
frm.setVisible(true);
}
}
-
9. 簡單註解下列程式加底線粗體部分其意義。(10%) (這裡用紅字代替底線粗體)
import java.net.*;
import java.io.*;
public class app
{
public static void main(String args[])
{
try
{
ServerSocket svs=new ServerSocket(2525); //
Socket s=svs.accept(); //
OutputStream out=s.getOutputStream();
String str="Honor shows the man.";
out.write(str.getBytes());
out.close();
s.close(); //
}
catch(Exception e){}
}
}
-
10. 請簡單畫出下面 applet 及 html 程式合併執行的結果。(10%)
import java.awt.*;
import java.applet.Applet;
public class App extends Applet
{
public void paint(Graphics g)
{
g.drawString(getParameter("str1"),30,40);
g.drawString(getParameter("str2"),30,60);
}
}
<APPLET
CODE = "App.class"
WIDTH = "250"
HEIGHT = "120" >
<PARAM NAME = "str1" VALUE = "Repentance is a bitter physic." >
<PARAM NAME = "str2" VALUE = "Laughter is the best medicine." >
</APPLET>
--
不知道有沒有打錯或其他什麼的問題
之後有空再排版吧
系學會考古題快更新吧 嘿嘿
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 120.127.47.94
推
12/17 08:22, , 1F
12/17 08:22, 1F
→
12/19 03:22, , 2F
12/19 03:22, 2F
推
12/20 03:39, , 3F
12/20 03:39, 3F
推
12/21 16:11, , 4F
12/21 16:11, 4F