30.用getImage()方法和drawImage()方法能加載和顯示圖像,。以下程序加載的是seattle.jpg文件,,并假定該文件與包含這個小應(yīng)用程序的HTML文件在同一個目錄下,。 import java.awt.*; import java.applet.*; public class SimpleImageLoad extends Applet{ Image img; public void init() { img = getImage(______,"seattle.jpg"); } public void paint(Graphics g) { g.drawImage(img, 0, 0, ______); } } 31.以下程序段定義的類ShareDataManager用于管理多個線程共享數(shù)據(jù)data,。為了對data的取,、存和修改操作保持完整,,多線程在data上的操作有互斥要求,;另外,,限制線程對data的修改不能讓data為負數(shù),,所以多線程在data上的操作還有同步要求。為此,,程序為管理data上的操作定義成類,,以下是管理共享數(shù)據(jù)data的類ShareDataManager的代碼。 class ShareDataManager { int data; ShareDataManager(int init){data = init; } public int getData(){ return data;} private void putData(int newValue){ data = newValue;} synchronized void modiData(int delta){ if (delta >= 0) {putData(getData()+ delta); } else { while (______<0) { try {wait(); } catch (InterruptedException e) { } } putData(getData()+ delta); } ______; } } 五,、程序分析題(本大題共5小題,,每小題4分,共20分) 32.閱讀下列程序,,請寫出該程序的輸出結(jié)果,。 class Tree { private String name;public boolean flower; public int birthYear; Tree(String n, boolean f, int y){ name = n; flower = f; birthYear = y; } public void setName(String n){name = n;} public String getName(){return name; } public void printTree(String str){ System.out.println(str); System.out.println("Name:"+name); System.out.println("Birth Year:"+birthYear); System.out.println("Flower:"+flower); } } class PineTree extends Tree { public boolean coniferous = true; PineTree(String n, boolean f, int y, boolean c){super(n, f, y);coniferous = c; } public void printTree(String str){ super, printTree(str); System.out.println("Coniferous:" + coniferous); } } class Test32 { public static void main(String []args){ Tree fOb = new Tree("May Flower", true, 1980); PineTree sOb = new PineTree("Pine", false, 2000, true); fOb.printTree(" fOb:"); sOb.printTree("sOb:"); } } 33.閱讀下列程序,請寫出該程序的輸出結(jié)果,。 class Test33 { static void s (int b[ ], int k) { int i, j, t; for(i = 1;i< k; i++) { for(t = b[i], j = i-1; j >= 0 && t < b[j]; j--) b[j+l] = b[j]; b[j+ 1 ] = t; } } public static void main(String []args){ int i, a[] = {6, 12, 7, 11, 5 }; s(a, 5); for(i = 0;i< a.length; i++) System.out.print(a[i]+" "); System. out. Println(); } } 34.閱讀下列程序,,請寫出該程序的功能。 import java.applet.*; import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Test34 extends Applet implements ActionListener { JTextField text;int r; JPanel panel; public void paint(Graphics g){ Graphics gc = panel.getGraphics(); gc.clearRect(0,0, panel.getWidth(), panel.getHeight()); gc.setColor(Color.red); gc.fillOval(10, 10, r, r); } public void init() { text = new JTextField(10); panel = new JPanel(); setSize(200, 100); setLayout(new GridLayout(2,1)); add(text); add(panel); text.addActionListener(this); } public void actionPerformed(ActionEvent e) { if (e.getSource() == text) { r = Integer.parseInt(text.getText()); repaint(); } } } 35.閱讀下列程序,,請回答以下問題: (1)該程序的菜單條中共有幾個菜單項? (2)單擊哪個菜單項,,可以打開一個對話框? (3)該對話框是強制型還是非強制型? (4)該對話框中顯示的是什么內(nèi)容? Import javax.swing.*;import java.awt.*; import java.awt.event.*; import java.util.*; class MyDialog extends JDialog { MyDialog(JFrame F, String s) { super(F, s, false); Date date = new Date(); String t = date.toString(); JLabel text = new JLabel(t); add(text); setSize(200,100); setVisible(true); } } public class MenuDemo extends JFrame implements ActionListener { JMenuBar menubar; JMenu action; JMenuItem show, set, quit; public MenuDemo() { menubar = new JMenuBar(); setJMenuBar(menubar); action = new JMenu(''操作''); menubar.add(action); show= new JMenuItem(''顯示''), action.add(show); set = new JMenuItem("設(shè)置"); action.add(set); action.addSeparator(); quit = new JMenuItem("退出"); action.add(quit); show.addActionListener(this); set.addActionListener(this); quit.addActionListener(this); setSize(300,300); setVisible(true); } public void actionPerformed(ActionEvent e) { if(e.getSource() == show) {MyDialog diag = new MyDialog(this, "信息");} else if(e.getSource()== set) { this.setTitle("菜單演示");} else if(e.getSource() == quit){ System.exit(0); } } public static void main(String[] args) {new MenuDemo();} } 36.閱讀以下程序,,請寫出該程序的輸出結(jié)果,。 class CurrentThreadDemo { public static void main(String args[]) { Thread t = new Thread(); System.out.println("Current thread"); t.setName("My Thread"); System.out.println("After name change"); try {for(int n = 3; n > 0; n--) { System.out.println(n); Thread.sleep(1000); } } catch (InterruptedException e) { System.out.println("Main thread interrupted"); } } } 六、程序設(shè)計題(本大題共2小題,,每小題6分,,共12分) 37.編寫方法int searchMaxNumIndex(int[]a),尋找已知數(shù)組中最大數(shù)的下標并返回,。 38.以下程序的界面有一個按鈕button,、一個文本區(qū)textA和一個文本框textF。程序運行時,,在文本區(qū)中輸入數(shù)字序列,,單擊按鈕,則在文本框中顯示數(shù)字序列的和,。以下是要求你編寫的actionPerformed(ActionEvent e)方法的部分代碼,。 public void actionPerformed(ActionEvent e) { if (e.getSource() == button) { String s = textA.getText(); double sum =0; StringTokenizer fenxi = new StringTokenizer(s, " ,\n"); //請在以下位置編寫代碼 |
|