问题 问答题

下面是一个Applet程序,其功能是从3~100之间(包括3和100)每隔0.5秒显示一个新的数字,如果数字为素数,则显示为灰色,其他为绿色。请更正题中带下划线的部分。
注意:不改变程序的结构,不得增行或删行。
import java.awt.*;
import java.applet.Applet;
/*
<applet code="exl5_3.class"width=800 height=400>
</applet>
*/
public class ex15_3 extends Applet

public Color color15_3=Color.black;
private int n15_3=3;
public myPrime thPrimel5_3;
public void init()

thPrimel5_3=new myPrime(this);
thPrimel5_3.start();

public void paint(Graphics g)

g.setColor(Colorl5_3);
g.drawString(n15_3,50,50);

public int getInt()

return n15_3;


class myPrime extends Thread

ex15_3 objl5_3;
myPrime(ex15_3 o)

this.objl5_3=o;

public boolean isPrime(int n)

boolean bPrime=true;
int i=2;
if(n<3||n>100)
return false;
while(i<n-1 && bPrime)

if((n %i)= =0)
bPrime=false;
i++;

return bPrime;

public void run()

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

if(isPrime(i))
objl5_3.color15_3=Color.gray;
else
objl5_3.color15_3=Color.green;
obj15_3.n15_3;
objl5_3.repaint();
try
sleep(500);
catch(InterruptedException ie)



exl5_3.html
<HTML>
<HEAD>
<TITLE>exl5_3</TITLE>
</HEAD>
<BODY>
<applet code="exl5_3.class" width=800 height=400>
</applet>
</BODY>
</HTML>

答案

参考答案:String.valueOf(n15_3)
i<101或i<=100
obj15_3.setInt(i)

解析:[讲解] 本题主要考查线程的概念和使用,Applet的执行过程和窗口,for循环语句,字符串和血型的数据转换,以及面向对象编程的基本思想。解题关键是熟练地将Applet的执行和线程的基本思想结合,完成一定的综合性的应用;熟练掌握线程的建立、运行以及线程类与封装类之间的信息传递方式,即通过对象调用封装的方法来进行,如语句。obj15_3.repaint()。本题中,第1处,不可以直接填入n15_3,会导致参数类型不符合的错误,应该用String类的 valueOf()方法对血型数据进行转换得到String类型数据;第2处,注意题目要求,需要包括 3和100,因此循环变量的上界应该是i<101或者i≤100;第3处,由于n15_3是类exl5_3的私有成员变量,因此不可以直接用对象。obj15_3来调用这个成员变量,需要通过类exl5_3的方法setInt()来实现对私有成员变量的修改。

问答题
多项选择题