下面的程序是求菲波那契(Fibonacci)数列的前10项。已知该数列的前两项都为1,即F(1)=1,F(2)=1;而后面各项满足: 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 _________________
}
}
第2题
正程序中的错误(有下划线的语句),使程序能输出正确的结果。
注意:不改动程序的结构,不得增行或删行。
源程序文件代码清单如下:
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>
第3题
单标签(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);
}
}
第4题
序的功能完整,且运行程序后的输出结果为:
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();
}
}
第5题
时则能在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>
第6题
下列程序建立一个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();
}
}
}
第7题
出数据。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
第8题
下列程序的输出结果是_______。
class Test{
public static void main(String args []){
int m=6;
do{m--:}while(m>0);
System.out.println("m="+m);
}
}
为了保护您的账号安全,请在“上学吧”公众号进行验证,点击“官网服务”-“账号验证”后输入验证码“”完成验证,验证成功后方可继续查看答案!