首页>自考>历年真题>2012年自考真题>正文
2012年1月自考试题:Java语言程序设计(一)

www.zige365.com 2012-3-19 11:47:19 点击:发送给好友 和学友门交流一下 收藏到我的会员中心

30.以下是一个JPanel的子类的paintComponent方法,该方法利用Graphics2D绘制一个正方形,正方形的位置、边长和颜色分别存于该类的成员变量lTopedgec中。

int edge;    Point 1Top;  Color c

public void paintComponent(Graphics g){

Graphics2D g2=(Graphics2D)g

g2.setColor(c)

Rectangle2D rec=new Rectangle2D.____________(1Top.xlTop.y,edgeedge)

_____________;

}

31.以下小应用程序的界面有一个文本区,文本区同时作为发生键盘事件的事件源,并对键盘事件实施监视。程序运行时,先点击文本区,让它激活,以便能响应键盘事件。以后输入英文字母时,在文本区中顺序显示输入的字母。

import java.applet.*import javax.swing.*

import java.awt.*import java.awt.event.*

public class Test31 extends Applet implements____________{

JTextArea text=new JTextArea(510)

public void init(){setSize(200200)

__________________________;

text.setBackground(Color.gray);  add(text)

}

public void keyPressed(KeyEvent e){}

public void keyReleased(KeyEvent e){}

public void keyTyped(KeyEvent e){

int t=e.getKeyCode()

if(t>=KeyEvent.VK_A&&t<=KeyEvent.VK_Z){

text.append(""+(char)t)

}

}

}

五、程序分析题(本大题共5小题,每小题4分,共20分)

32.阅读下列程序,请写出该程序的功能。

public  class  Test32

{   public  static  void   main(String   args[])

{   double   sum=0.0

For   (int i=1;  i<=500i++)

sum+=1.0/(double)i

System.out.println( "sum="+sum)

}

}

33.阅读下列程序,请回答以下问题:

(1)界面中有哪些组件?

(2)点击每一个按钮分别会显示什么内容?

import java.applet.*;  import java.awt.*

import java.awt,event.*import javax.swing.*;

public class Test33 extends Applet implements ActionListener{

String msg=""

String buttonCom[]={"Yes""No""Undecided"}

JButton bList[]=new JButton[buttonCom.length]

JTeXtField t

public void init(){

setLayout(new GridLayout(41))

for(int i=0i<buttonCom.1engthi++){

bList[i]=new JButton("按钮"+(i+1))

add(bList[i])

bList[i].addActionListener(this)

}

t=new JTeXtField()

add(t)

}

public void actionPerformed(ActionEvent e){

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

if(e.getSource()bList[i]){

t.setTeXt("You pressed"+buttonCom[i])

break

}

}

}

}

34.阅读下列程序,请写出该程序的功能。

import java.io.*;  import java.awt.*;  import javax.swing.*

import java.awt.event.*

class MyWindow extends JFrame implements ActionListener{

JTextArea textBufieredReader inJTextField fileName

FileReader file

MyWindow(){

Contaiher con=this.getContentPane()//获得内容面板

con.setLayout(new BorderLayout())

fileName=new JTextField("输入文件名")

fileName.addActionListener(this)

text=new JTextArea(2030)

JScrollPane jspnew JScrollPane(text)

con.add(jspBorderLayout.CENTER)

con.add(fileName"South")setVisible(true)

}

public void actionPerformed(ActionEvent e)

{    String s

try{File f=new File(fileName.getText())

file=new FileReader(f)

in=new BufferedReader(file)

} catch(FileNotFoundException el){}

try{ while((s=in.readLine())!=null)

text.append(s+'n')

}catch(IOException exp){}

}

}

public class Test34{

public static void main(String args[ ])   {new MyWindow()}

}

35.阅读下列程序,请回答以下问题:

(1)该程序中的类MyPanel的成员变量twoClick的作用是什么,该成员变量的值是如何改变的?

(2)程序运行时,用户在界面的不同位置,用鼠标点击两次,界面中会出现什么?

import java.awt.*import javax.swing.*import java.awt.event.*

class MyPanel extends JPanel implements MouseListener{

int leftxlefly,rightxrighty

boolean twoClick

MyPanel() {

twoClick=falseaddMouseListener(this)

}

public void mousePressed(MouseEvent e){

if(!twoClick){

leftx=e.getX()lefty=e.getY()

}else{

rightx=e.getX()righty=e.getY()repaint()

}

twoClick=!twoClick

}

public void mouseClicked(MouseEvent e){}

public void mouseEntered(MouseEvent e){}

public void mouseExited(MouseEvent e){}

public void mouseReleased(MouseEvent e){}

public void paintComponent(Graphics g){

g.clearRect(00this.getWidth()this.getHeight())

g.fillRect(1efixlefty,Math.abs(rightx-leftx)

Math.abs(righty-lefty))

}

}

class MyWin extends JFrame{

MyPanel p=new MyPanel()

MyWin(){

getContentPane().add(p)

setSize(400400)setLocation(100100)setVisible(true)

}

}

public class Test35 extends JFrame{

public static void main(String[]args){new MyWin()}

}

36.阅读下列程序,请回答以下问题:

(1)程序执行时创建的线程个数。

(2)各线程的名称。

(3)举例给出程序可能的输出结果。

class SeltManaged extends Thread{

int countDown

public SelfManaged(String nameint c){

countDown=c

setName(name)start()

}

public void run(){

while(true){

System.out.println(getName()+" ("+countDown+")")

try{

sleep(50)

}catch(InterruptedException e){}

if(--countDown== 0)

return

}

}

}

public class Test36{

public static void main(String[]args){

for(int i=0i<2i++)

new SelfManaged("线程"+String.valueOf(i)2)

}

}

六、程序设计题(本大题共2小题,每小题6分,共12分)

37.编写方法int[] delete(int []aint d),方法将创建一个新数组b,新数组b中只包含原数组中所有值不等于d的元素,并返回该新数组b

38.以下程序的界面有一个按钮ave、一个文本区text和一个标签label。程序运行时,在文本区中输入数字序列,单击ave按钮,则在标签label中显示这个数字序列中正实数的平均值。

注:这里是给定程序的部分代码,你要编写的是actionPerformed(ActionEvent e)方法。

import javax.swing.*;………

class Ave extends JFrame implements ActionListener{

JLabel label 1label; JTextArea text;  JButton ave

Ave() {

Container con=getContentPane()

con.setLayout(new GridLayout(22))

labell=new JLabel("输入整数序列")

text=new JTextArea(510)

ave=new JButton("求正实数平均值")

label=new JLabel()

con.add(1abel 1)con.add(text)con.add(ave)

ave.addActionListener(this)

con.add(1abel)

………

}

public void actionPerformed(ActionEvent e){

∥请在以下位置编写代码

}

}

class Test38{

public static void main(String args[]){new Ave();  }

}

本新闻共2页,当前在第2页  1  2  

我要投稿 新闻来源: 编辑: 作者:
相关新闻
2012年1月自考试题:浙江省会展策划与组织试题
2012年1月自考试题:国际贸易实务(一)
2012年1月自考试题:外国新闻事业史试题
2012年1月自考试题:高等数学(工专)
2012年1月自考试题:全国统一命题考试 英语(二)