一、基本操作 (本大题共 4 小题,共 30 分)
1.下列Application程序中,先创建一个Frame对象,然后设置Frame的大小(长、宽为200),背景为蓝色,Frame为可见,请将程序补充完整。
import java.awt.*;
public class test_1_3_1{
public static void main(String args[]){
Frame f=new Frame(“Hello World!”);
f.setSize__________; //设置Frame的大小
f.setBackground(______.blue); //设置Frame的背景
f.setVisible(_________); //设置Frame为可见
}
}
2. 下列Application程序中,指定ch为字符类型,指定bo为布尔类型,指定f为单精度类型,定义一些变量,执行相应输出,请将程序补充完整。
public class test_1_3_2{
public static void main(String args[]){
_________f=1.876f;
_________bo=true;
_________ch;
System.out.println(“ch=”+ch);
System.out.println(“f=”+f);
System.out.println(“bo=”+bo);
}
}
3. 下列Application程序,要求在空格处输入运算符和变量,求出b的相反数,j的值输出0,k的值输出1,请将程序补充完整。
public class test_1_3_3{
public static void main(String args[]){
int a=9;
int b=__________;
int i=0;
int j=___________; //i=1,j=0
int k=__________; //j=1,k=1
System.out.println(“a=”+a);
System.out.println(“b=”+b);
System.out.println(“i=”+i);
System.out.println(“j=”+j);
System.out.println(“k=”+k);
}
}
4. 下列Application程序输入一个正整数,将各位数字反转后输出,请根据注释,将程序补充完整。
public class test_1_3_4{
public static void main(String args[])throws IOException{
int x;
//下面7行语句的作用是从键盘输入x的值
InputStreamReader ir;
BufferedReader in;
ir=_________ InputStreamReader(System.in);
in=new BufferedReader(ir);
System.out.println(“Input x is:”);
String .s=in.readLine();
x=Integer.parseInt(s);
//下面进行反转输出
_____________
{
System.out.print(x________10);//除以10取余数输出
x/=10;
}while(x!=0);
System.out.print(“\n”);
}
}
二、简单应用 (本大题共 2 小题,共 30 分)
1.下面java程序实现对后序表达式的求值操作。请勿改动原有代码,在下划线处填入适当语句,将程序补充完整。
import java.util.*;
import java.io.*;
public class test_3_2_1{
public static void main(String args[])throws Exception{
StackArray Operand=new StackArray();
String Expression=new String();
Int Position=0;
Int Operand1=0;
Int Operand2=0;
System.out.println(“Please imput the postorder expression:”);
InputStreamReader ir;
BufferedReader in;
ir=new InputStreamReader(System.in);
in=new BufferedReader(ir);
Expression=in.readLine();
While(true) {
If(Operand.IsOperator(Expression.charAt(Position))) {
Operand1= Operand.pop();
Operand2= Operand.pop();
Operand.Push(Operand.TwoResult(Expression.charAt(Position), Operand1, Operand2));
}
else
Operand.Push (Expression.charAt(Position)-48);
Position++;
If(Position>=Expression.length())
Break;
}
Evaluate=Operand.Pop();
System.out.print(“The expression[“+Expression+”]”);
System.out.print(“result is”+Evaluate);
}
}
class StackArray{
int MaxSize=20;
int []Astack=new int[MaxSize];
int Top=-1;
public void Push(int Value) {
int i;
if(Top>=MaxSize)
System.out.print(“The stack is full”);
else {
Top++;
Astack[Top]=value;
}
}
public int Pop() {
int Temp;
int i;
if(Top<0) {
System.out.println(“The stack is Empty!”);
Return –1;
}
Temp=Astack[Top];
Return Temp;
}
public Boolean IsOperator(int Operator) {
if(Operator= =43||Operator= =45||Operator= =42||Operator= =47)
return true; //+ ﹣ * /
else
reture false;
}
public int Priority(int Operator) {
if(Operator= =43||Operator= =45)
return 1; //+ ﹣
else if(Operator= =42|| Operator= =47)
return 2; //* /
else
return 0;
}
public int TwoResult(________) {
switch(operator) {
case 43:
return(operand1+ operand2);
case 45:
return(operand1﹣operand2);
case 42:
return(operand1*operand2);
case 47:
return(operand1/operand2);
}
return 0;
}
}
2.下面是一个字符界面的JAVA Application程序,它接受用户输入的一个浮点数,那它的整数部分和小数部分分别输出。请勿改动原有代码,在下划线处填入适当语句,将程序补充完整。
import java.io.*;
public class test_3_2_2{
public static void main(String args[]) {
String s;
Double d;
Int i;
Boolean b=false;
do {
try {
System.out.println(“请输入一个浮点数:”);
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
s=br.readLine();
i=s.indexOf(‘.’);
d=_________.parseDouble(s);
System.out.println(d+”整数部分为:”+_________);
If(I= =-1)
System.out.println(d+”小数部分为:0.0”);
else
System.out.println(d+”小数部分为:”+Double.parseDouble(
((s.charAt(0)= =’﹣’)?” ﹣”:” ”)
+”0.”+s.substring(i+1,s.length())));
b=false;
}
catch(NemberFormatException nfe) {
System.out.println(“输入浮点数格式有误。\n”);
B=true;
}
catch(IOException ioe) {
b=false;
}
}while(b);
}
}
三、综合应用(本大题共 1 小题,共 40 分)
下面程序演示项目选择事件的处理。请改正其中的错误(有下划线的语句),使程序能输出正确的结果。
import java.awt.event.*;
import javax.swing.*;
import java.awt. *;
public class test_3_3 extends JFrame implements ItemListener {
BorderLayout bord=new BorderLayout();
JTextField result=new JtextField27.;
JComboBox pick=new JComboBox();
Public test_3_3() {
Super(“Select Item”);
pick.addItemListener(it);
pick.addItem(“Navigator”);
pick.addItem(“Internet Explorer”);
pick.addItem(“Opera”);
pick.setEditable(false);
result.setHorizontalAlignment(SwingConstants.CENTER);
result.setEditable(false);
Jpanel pane=new Jpanel();
Pane.add(result,”South”);
Pane.add(pick,”Center”);
SetContentPane(pane1);
}
public static void main(String[] arguments) {
Jframe frame=new SelectItem();
ExitWindow exit=new ExitWindow();
Frame.addWindowListener(exit);
Frame.pack();
Frame.setVisible(true);
}
public void itemStatechanged(ItemEvent evt) {
Object source=evt.getSource();
if(source= =pick) {
Object newPick=evt.getItem();
Result.setText(newPick.toString()+”is the selection.”);
}
repaint;
}
}