第四届全国ITAT教育工程就业技能大赛复赛试题
Java程序设计(B卷)
1、 水仙花数是指其个位、十位、百位三个数的立方和等于这个数本身。编写一个Java应
用程序,求出所有水仙花数。(20分) public class aaa {
public static void main(String[] args) { int b = 1, s, g; int c = 0; for (b = 1; b <= 9; b++) {
for (s = 0; s <= 9; s++) {
for (g = 0; g <= 9; g++) { int self = 100 * b + 10 * s + g;
int sum = b * b * b + s * s * s + g * g * g; if (self == sum) { System.out.println(self);
} } } } } }
2、 编写一个Java应用程序,利用RandomAccessFile类往某个文本文件中写入20个整数
(0~19),然后从该文件的第12个字节开始,将后面所有的数据(对应写入的整数)读出。(25分) package ITAT4;
import java.io.IOException;
import java.io.RandomAccessFile;
public class test2 { /**
? wanglong
*/
public static void main(String args[]) throws IOException {
RandomAccessFile f = new RandomAccessFile(““iata4””, ““rw””); int data[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
16, 17, 18, 19 };
for (int i = 0; i < data.length; i++) { f.writeInt(data[i]);
}
f.seek(12);
for (long i = 1; i <= 17; i++) { if (f.getFilePointer() != 0) { System.out.print(““,”” + f.readInt()); } else { return;
}
}
f.close();
} }
3、 编写一个Java GUI应用程序,窗口标题为“GridLayout”,窗口布局如下图A所示,在
图A窗口中单击任意一个Button,网格的划分方式会变化为图B;在图B窗口中单击任意一个Button,网格的划分方式会变化为图A。(25分)
图A 图B package ITAT4;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JFrame;
class WinGrid extends Frame implements ActionListener {
/**
? wanglong */
private static final long serialVersionUID = 1L;
GridLayout grid;
Button one = new Button(““one””); Button two = new Button(““two””); Button three = new Button(““three””); Button four = new Button(““four””); Button five = new Button(““five””); Button six = new Button(““six””); int i = 0;
WinGrid(int a, int b) {
this.setLayout(new GridLayout(a, b)); add(one); add(two); add(three); add(four); add(five); add(six);
this.setSize(400, 300); this.setResizable(false);
this.setLocationRelativeTo(null); this.setVisible(true);
this.addWindowListener(new WindowAdapter() // 为了关闭窗口 {
public void windowClosing(WindowEvent e) {
System.exit(0);
} });
one.addActionListener(this); two.addActionListener(this); three.addActionListener(this); four.addActionListener(this); five.addActionListener(this);
six.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
if (i == 0) { i = 1;
this.setLayout(new GridLayout(2, 3)); this.setVisible(true); } else { i = 0;
this.setLayout(new GridLayout(3, 2)); this.setVisible(true);
} } }
public class Example7_15 {
public static void main(String args[]) { new WinGrid(3, 2); } }
4、 采用Java多线程技术,设计实现一个符合生产者和消费者问题的程序。对一个对象(枪
膛)进行操作,其最大容量是12颗子弹。生产者线程是一个压入线程,它不断向枪膛中压入子弹;消费者线程是一个射出线程,它不断从枪膛中射出子弹。(30分) 要求:
(1)给出分析过程说明。(10分)
(2)程序输出,要模拟体现对枪膛的压入和射出操作;(10) (2)设计程序时应考虑到两个线程的同步问题。(10)
两个线程对同一个对象的同一个成员变量进行操作,要考虑到该成员变量的属性范围,同
时考虑线程的同步问题。 package ITAT4; public class test4 {
public static void main(String[] args) {
xiancheng x = new xiancheng(); System.out.println(““线程开始””); x.yaru.start(); x.shechu.start(); } }
class qiangtang {
int zidan = 0; void add() { zidan++;
}
void move() {
zidan--;
} int get() {
return zidan;
} }
class xiancheng implements Runnable {
Thread yaru; Thread shechu;
xiancheng() {
yaru = new Thread(this); yaru.setName(““压入””);
shechu = new Thread(this); shechu.setName(““射出””); }
qiangtang q = new qiangtang(); public void run() {