注意:不改动程序的结构,不得增行或删行。
程序的执行结果为:
import java.awt.*;
import java.applet.*;
/*
<applet code=Di9Ti width=200 height=100>
</applet>
*/
pubic class Di9Ti extends Applet
{
public void paint(Graphics g)
{
int x,y,r1=5, r2=4;
int count=0;
for(x=1; x<y;x++)
{
for(y=1;y<r1;y)
{
if(x*x+y*y<=r2*r2||x*x+y*y>=r1*r1)
break;
g.drawString("x="+x+"y="+y,20+count*80,20);
count++;
}
}
g.drawstring("count="+count,20,40);
}
}
ex38_3.html:
<html>
<head>
<title>A Simple Program</title>
</head>
<body>
<applet code="Di9Ti.class" width=800 height=400>
</applet>
</body>
</html>
第1题
下列Application程序输入一个整数,求出它的所有因子,请根据注释,将程序补充完整。
public class test19_2 {
public static void main(String args[])throws IOException {
int n;
//下面7行语句的作用是从键盘输入n的值
InputStreamReader ir;
BufferedReader in;
ir=new lnputStreamReader(System.in);
in=new BufferedReader(ir);
System.out.println("Input n is:");
String s=in.______;//读取1行字符
n=Integer.parseInt(s);
//下面求n的所有因子
______(int i=1;i<=n;i++) {
if(n%i!=0)
______;//跳过打印语句回到循环起始
System.out.print(i+””);
}
System.out.print(“\n”);
}
第2题
三个变量打印输出。请将程序补充完整。
注意:不改动程序结构,不得增行或删行。
import java.io.*;
public class Assign
{
public static void main(String args [])
{
______b=20;
______f=0.34F;
______l=0xeefL;
System.out.println("b="+b);
System.out.println("f="+f);
System.out.println("l="+1);
}
}
第3题
面各项满足: F(n)=F(n-1)+F(n-2)。请在程序的每条横线处填写一条语句,使程序的功能完整。
注意:请勿改动main()主方法和其他已有的语句内容,仅在横线处填入适当的语句。
public class Fibonacci{
public static void main(String args[]){
System.out.printtn("Fibonacci is"+" "+"_______________________);
}
static long fib(int n){
if(______________)
return 1;
else
return _________________
}
}
第5题
正程序中的错误(有下划线的语句),使程序能输出正确的结果。
注意:不改动程序的结构,不得增行或删行。
源程序文件代码清单如下:
import java.awt.*;
import java.applet.*;
import java.awt.image.*;
import java.util.*;
import java.net.*;
/*
<applet code="ex01_3.class"width=800 height=400>
</applet>
*/
public class ex01_3 extends Applet
{
private Image IMG nClick=over(this) title=放大;
public void init()
{
URL url=getCodeBase();
IMG nClick=over(this) title=放大=getImage(url,"ex01_3.gif");
MediaTracker mt=new MediaTracker();
mt.addImage(IMG nClick=over(this) title=放大,1);
try
{
mt.waitForAll();
} catch(Exception e)
{
e.printStackTrace();
}
setBackground(Color.black);
}
public void paint(Graphics g)
{
int thick=3,x=10,y=10;
g.drawImage(IMG nClick=over(this) title=放大,x,y);
g.setColor(Color.lightGray);
int w = IMG nClick=over(this) title=放大.getWidth(this);
int h = IMG nClick=over(this) title=放大.getHeight(this);
for(int i=0;i<thick;i++)
g.draw3DRect(x,y,w+(a*i),true);
}
}
ex01 3.html
<HTML>
<HEAD>
<TITLE>ex01_3</TITLE>
</HEAD>
<BODY>
<applet code="ex01_3.class"width=800 height=400>
</applet>
</BODY>
</HTML>
第6题
单标签(Labe1)显示欢迎信息,一个使用按钮和图标。
注意:请勿改动main()主方法和其他已有语句内容,仅在下划线处填入适当的语句。
程序运经结果如下:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ex8_2 extends JPane1 {
public ex8_2() {
JTabbedPane jtp=new JTabbedPane(SwingConstants.BOTTOM);
Icon icon=new ImageIcon("icon.gif");
JButton btn=new JButton(icon);
JLabel l=new JLabel("Welcome to the NCR Examination!");
______________;
______________;
setLayout(new BorderLayout());
add(jtp,"Center");
}
public static void main(String args[]){
ex8_2 obi8_2=new ex8_2();
String str= obj8_2.getClass().toString();
if(str.indexOf("class") != -1)
str = str.substring(6);
JFrame. frm = new JFrame(str);
frm.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(O);
}
});
frm.getContentPane () .add(obj8_2);
frm.setSize(300, 200);
frm.setVisible(true);
}
}
第7题
序的功能完整,且运行程序后的输出结果为:
I am parentclass!
I am childclass!
I am childclass!
注意: 请勿改动main()主方法和其他已有的语句内容,仅在下划线处填入适当的语句。
class Parent {
void printMe() {
System.out.println("I am parentclass!");
}
}
class Child extends Parent {
void printMe() {
System.out.println("I am childclass!");
}
void printAll() {
______________.printMe ( ); // 调用父类的方法
______________. printMe ( ); //调用本类的方法
printMe ( );
}
}
public class TestJieCheng {
public static void main(String args[]) {
______________
myC.printAll();
}
}
第8题
时则能在Label中显示出Command: First,而单击Second时则能显示出Command: Second,要求只能使用重载一次actionPerformed()方法。请改正程序中的错误(有下划线的语句),使程序能输出正确的结果。
注意:不改动程序的结构,不得增行或删行。
程序运行结果如下:
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code=ex3_3.class width=800 height=400>
</applet>
*/
public class ex3_3 extends Applet implements ActionListener
{
private String str="ok";
private Label 1;
private Button btn;
public void init( )
{
setLayout(null);
1=new Label(str);
1.reshape(10,10,150,30);
add(1);
btn=new Button("First");
btn.reshape(10,50,60,20);
1.addActionListener(this);
add(btn);
btn=new Button("Second");
btn.reshape(10,100,60,20);
btn.setActionCommand("First");
btn.addActionListener(this);
add(btn);
}
public void actionPerformed(ActionEvent ae)
{
str="Command:"+ae.getActionCommand( );
btn.setText(str);
}
}
ex3_3.html
<HTML>
<HEAD>
<TITLE>ex3_3</TITLE>
</HEAD>
<BODY>
<applet code="ex3_3.class"width=800 height=400>
</applet>
</BODY>
</HTML>
第9题
下列程序建立一个file.dat的文件,并且允许按照不同的数据类型向里面输入数据,请将程序补充完整。
注意:不改动程序结构,不得增行或删行。
package ch3;
import java.io.*;
public class Test
{
public static void main(String[] args)
{
try
{
______fos=new______("ch3/file.dat");
______
}
catch(IOException ioe)
{
ioe.printStackTrace();
}
}
}
第10题
出数据。DataPool类一次只能存放一个数据。请更正题中带下划线的部分。
注意:不改变程序的结构,不得增行或删行。
class DataPool
{
private int data;
private boolean isFull;
public DataPool()
{
isFull=false;
}
public synchronized void putData(int d)
{
if(isFull= =true)
{
try
{
this.notify();
}
catch(InterruptedException e)
{}
}
data=d;
isFull=true;
System.out.println("生产了一个数据:"+data);
this.notify();
}
public synchronized int getData()
{
if(isFull= =false)
{
try
{
this.wait();
}
catch(InterruptedException e)
{}
}
isFull=false;
System.out.println("消费了一个数据"+data);
this.wait();
return this.data;
}
boolean getIsFull()
{
return isFull;
}
}
class Producer extends Thread
{
DataPool pool;
public Producer(DataPool pool)
{
this.pool=pool;
}
public void run()
{
for(int i=0; i<10; i++)
{
int data=(int) (Math.random()*1000);
try
{//用于生产数据
sleep(data);
}
catch(InterruptedException e)
{}
pool.putData(data);
}
}
}
class Consumer implements Runnable
{
DataPool pool;
public Consumer(DataPool pool)
{
this.pool=pool;
}
public void run()
{
for(int i=0; i<10; i++)
{
int data=pool.getData();
try
{//用于处理数据
sleep((int) (Math.random()*1000));
}
catch(InterruptedException e)
{}
}
}
}
public class advance
}
public static void main(String[] args)
{
Data Pool pool=new Data Pool();
Producer pro=new Producer(pool);
Runnable con=new Consumer(pool);
Thread conTh=new Thread(con);
&n
为了保护您的账号安全,请在“上学吧”公众号进行验证,点击“官网服务”-“账号验证”后输入验证码“”完成验证,验证成功后方可继续查看答案!