读书人

下机模拟试题(五)

发布时间: 2012-12-22 12:05:05 作者: rapoo

上机模拟试题(五)

上机模拟试题(五)

一、??????????? 基本操作题 (本大题共 1 小题,共 30 分)

下列程序中,给出两个整数2和3,分别求2除以3和2乘以3的结果,要求调用类ex1_1的方法method()来输出相应的结果,请将程序补充完整。程序运行结果如下:

0.??? 6666666666666666

6

? public class ex1_1{

public static void main(String[] args) {

?? int n1=2,n2=3;

????? ex1_1 obj1_1=new ex1_1();

?? obj1_1.________________;

}

public void method(int x,int y) {

?? System.out.println(__________________);

?? System.out.println(__________________);

? }

}

二、简单应用题 (本大题共 1 小题,共 30 分)

请完成下列java程序:创建一个具有2行3列的GridLayout管理器,包括Choice,Label,Button构件,布局为第一行包括一个Choice构件(包括2个选项item1和item2),一个Label构件(当选择Choice构件中的选项时,Label构件显示相应的名称,即,如果点击item1则Label中显示item1),和一个exit按钮(点击则退出应用程序),第2行包括3个Button构件。

注意:请勿改动main()主方法和其他已有语句内容,仅在下划线处填入适当的语句。

?? import java.awt.* ;

?? import java.awt.event.*;

?? public class ex1_2 extends Frame implements ActionListener,ItemListener{

private Label 1;

private String str=”label”;

private Choice choice1_2;

public static void main(String[] arg) {

? new ex1_2();

}

ex1_2(){

?setLayout(____________);

?choice1_2=new Choice();

?choice1_2.addItem(“item1”);

?choice1_2.addItem(“item2”);

choice1_2._____________;

add(choice1_2);

1=new Label(str);

add(1);

Button exit1_2=new Button(“exit”);

exit1_2.addActionListener(this);

add(exit1_2);

for(int i=0;i<3;i++)

?? add(new Button(“button”+ I));

?? setSize(300,300);

?? pack();

?? show();

}

public void actionperformed(ActionEvent event){

? if(event.getActionCommand().equals(“exit”)){

??? System.exit(0);

? }

}

public void itemStateChanged(ItemEvent event){

?? str=choice1_2getSelectedItem();

1.setText(str);

}

????????? }

三、综合应用题 (本大题共 1 小题,共 40 分)

下面是一个Applet程序,其功能是在绘图区域中通过鼠标的移动来绘制直线,并且有清除绘图区域按钮,用来清除已经绘制的图像。请改正程序中的错误(有下划线的语句),使程序能输出正确的结果。

注意:不能改动程序的结构,不得增行或删行。

程序:

import java.awt.*;

import java.applet.*;

/*

<applet code=ex1_3.class width=800 height=400>

</applet>

*/

public class ex1_3 extends Applet{

? private Button btn;

? private boolean bDraw,bClear;

? private int upX,upY,downX,downY

? public void init(){

??? setLayout(null);

??? bClear=false;

??? bDraw=false;

? btn=new Button(“clear”);

? btn..reshape(250,150,70,30);

? add(btn);

}

public void paint(Graphics g){

? if(bClear){

???? g.clearRect(0,0,getSize().width,getSize().height);

???? bClear=false;

}

?if(bDraw){

? ?g.drawLine(upY, upX,down Y,downX);

?? bDraw=false;

}

}

public void update(Graphics g){

? paint(g);

?}

public Boolean mouseDown(Event event,int x,int y){

? downX=x;

? downy=y;

? return true;

}

public Boolean action(Event event,Object object){

if(event.target !=clear){

? bClear=true;

? repaint();

? }

? return true;

}

}

读书人网 >编程

热点推荐