}
四、程序填空题
1.以下程序是一个算术测试小软件,用来训练小学生的算术能力。程序有3个类组成,其中Teacher对象充当监视器,负责给出算术题目,并判断回答者的答案是否正确。ComputerFrame对象负责为算术题目提供视图,比如用户可以通过ComputerFrame对象提供的GUI界面看到题目,并通过该GUI界面给出题目的答案;MailClass是软件的主类。
程序运行效果如下图所示。
仔细阅读程序代码,请按模板要求,在【代码】处填写合适的Java程序代码。
MainClass.java
public class MainClass {
public static void main(String args[]) { ComputerFrame frame;
frame=new ComputerFrame(); frame.setTitle(\算术测试\
frame.setBounds(100,100,650,180); } }
ComputerFrame.java
import java.awt.*;
import java.awt.event.*; import javax.swing.*;
public class ComputerFrame extends JFrame { JMenuBar menubar;
JMenu choiceGrade; //选择级别的菜单 JMenuItem grade1,grade2;
JTextField textOne,textTwo,textResult; JButton getProblem,giveAnwser; JLabel operatorLabel,message; Teacher teacherZhang; ComputerFrame() {
teacherZhang=new Teacher(); teacherZhang.setMaxInteger(20);
setLayout(【代码1】_new FlowLayout()________________________________); //设置ComputerFrame的布局为FlowLayout布局
menubar = new JMenuBar();
choiceGrade = new JMenu(\选择级别\ grade1 = new JMenuItem(\幼儿级别\ grade2 = new JMenuItem(\儿童级别\
grade1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { teacherZhang.setMaxInteger(10); } });
grade2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { teacherZhang.setMaxInteger(50); } }); choiceGrade.add(grade1); choiceGrade.add(grade2); menubar.add(choiceGrade); setJMenuBar(menubar);
【代码1】__ textOne=new JTextField(5);_________________________________ //创建textOne,其可见字符长是5 textTwo=new JTextField(5); textResult=new JTextField(5); operatorLabel=new JLabel(\
operatorLabel.setFont(new Font(\ message=new JLabel(\你还没有回答呢\ getProblem=new JButton(\获取题目\ giveAnwser=new JButton(\确认答案\ add(getProblem); add(textOne);
add(operatorLabel); add(textTwo);
add(new JLabel(\ add(textResult); add(giveAnwser); add(message);
textResult.requestFocus(); textOne.setEditable(false); textTwo.setEditable(false);
getProblem.setActionCommand(\ textResult.setActionCommand(\ giveAnwser.setActionCommand(\
teacherZhang.setJTextField(textOne,textTwo,textResult); teacherZhang.setJLabel(operatorLabel,message);
【代码2】_______ getProblem.addActionListener(teacherZhang); ____________ //将teacherZhang注册为getProblem的ActionEvent事件监视器
【代码3】_____giveAnwser.addActionListener(teacherZhang); ___//将teacherZhang注册为giveAnwser的ActionEvent事件监视器
【代码4】__________textResult.addActionListener(teacherZhang); __//将teacherZhang注册为textResult的ActionEvent事件监视器 setVisible(true); validate();
setDefaultCloseOperation(DISPOSE_ON_CLOSE); } }
Teacher.java
import java.util.Random; import java.awt.event.*; import javax.swing.*;
public class Teacher implements ActionListener { int numberOne,numberTwo; String operator=\ boolean isRight;
Random random; //用于给出随机数 int maxInteger; //题目中最大的整数 JTextField textOne,textTwo,textResult; JLabel operatorLabel,message; Teacher() {
random = new Random(); }
public void setMaxInteger(int n) { maxInteger=n; }
public void actionPerformed(ActionEvent e) { String str = e.getActionCommand(); if(str.equals(\
numberOne = random.nextInt(maxInteger)+1;//1至maxInteger之间的随机数; numberTwo=random.nextInt(maxInteger)+1;
double d=Math.random(); // 获取(0,1)之间的随机数 if(d>=0.5)
operator=\ else
operator=\
textOne.setText(\ textTwo.setText(\ operatorLabel.setText(operator); message.setText(\请回答\ textResult.setText(null); }
else if(str.equals(\
String answer=textResult.getText();
try{ int result=Integer.parseInt(answer); if(operator.equals(\
if(result= =numberOne+numberTwo) message.setText(\你回答正确\ else
message.setText(\你回答错误\ }
else if(operator.equals(\
if(result= =numberOne-numberTwo) message.setText(\你回答正确\ else
message.setText(\你回答错误\ } }
catch(NumberFormatException ex) {
message.setText(\请输入数字字符\ } } }
public void setJTextField(JTextField ... t) { textOne=t[0]; textTwo=t[1]; textResult=t[2]; }
public void setJLabel(JLabel ...label) { operatorLabel=label[0]; message=label[1]; } }
2. 有如下 java源程序,程序的功能是在数组中查找键盘键入的整数。如果找到则输出该整数在数组中的序号,否则输出“not found”。例如,运行程序,输入99,程序输出:99在数组中的序号是5;输入 59,程序输出:not found。
仔细阅读程序代码,请按模板要求,在【代码】处填写合适的Java程序代码。(注意: 不改动程序的结构,不得增行或删行)。 import java.io.*;
import java.util.Scanner; public class J_Test {
public static void main(String[] args) throws IOException {
int 【代码1】________a[]_____________ = {83,75,92,67,55,99,78,61}; Scanner in = new Scanner(【代码2】___System.in_________________); int x = Integer.parseInt(【代码3】__________in.readLine()_________); int index = -1;
for(int i = 0; 【代码4】___i<=a..Length_____________________; ++i) {
if(a[i] == x) { index = i; break; } }
if(【代码5】_____index==-1____________________________) System.out.println(\ else
System.out.println(x + \在数组中的序号是\ } }
3. 下面程序的运行结果如下图。单击“Close”按钮关闭窗口。
请阅读程序,在(1)和(2)处补充相应的内容,使整个程序能够正常运行。 import java.awt.*;
import java.awt.event.*; import javax.swing.*; public class J_Test { private JFrame f;
private JButton button1, button2, button3; public void go() {
f = new JFrame(\
f.setLayout(new FlowLayout()); button1 = new JButton(\ button2 = new JButton(\ button3 = new JButton(\ f.add(button1); f.add(button2); f.add(button3);
____ button3.addActionListener(new Monitor()); ________ f.setSize(160, 106); f.setVisible(true); }
public static void main(String[] args) { J_Test t = new J_Test(); t.go(); } }
class Monitor implements ActionListener {