第四届全国ITAT教育工程就业技能大赛复赛试题Java程序设计
B卷
第四届全国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;