请完成下列Java程序。程序的功能是显示用户在命令行方式下指定的任意驱动器文件夹的内容。
提示:public string()list();//将文件夹中所有文件名保存在字符数组中返回。
注意:请勿改动main()主方法和其他已有的语句内容,仅在下划线处填入适当的语句。
源程序文件代码清单如下:
import java.io.*;
public class FindDirectories
{
public static void main(String args[])
{
if(args.length==0)
args=new String(){".."};
try
{
______;
String[] fileName=pathName.list();
for(int i=0;i<fileName.length;i++)
{
File f=new File(pathName.getPath(),fileName[i]);
if(______)
{
System.out.println(f.getCanonicalPath());
main(new String[]
{
f.getPath()
});
}
}
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
第2题
一个线程向其中存放数据,数据放入DataPool以后,该线程随机休眠一段时间,让另外一个线程运行,请将程序补充完整。
注意:请勿修改main()主方法和其他已有语句内容,仅在横线处填入适当语句。
class PutData extends Thread
{
DataPool s;
int c;
String name;
public PutData(DataPool s,String name)
{
this.s=s;
this.name=name;
}
public void run()
{
for(int i=0;i<10000000;i++)
{
c=(int)(Math.random()*10);
s.setData(c);
System.out.println(name+":push"+c);
try
{
______((int) (Math.random()*1000));//休眠
}
catch(InterruptedException e)
{}
}
}
}
class DataPool
{
private int data=0;
public ______void setData(int d)
{
data=d;
}
}
public class simple
{
public static void main(String[] args)
{
DataPool s=new DataPool();
PutData a=new PutData(s,"Thread a");
PutData b=new PutData(s,"Thread b");
a.start();
b.start();
}
}
第3题
个单行文本框,在这个文本框中输入的内容将会显示在“Center”位置。运行结果如下图所示。
注意:请勿改动main()主方法和其他已有语句内容,仅在横线处填入适当语句。
import java.awt.*;
import java.awt.event.*;
public class Example2_6 extends Frame
{
public static void main(String [] argv)
{
Example2_6 frame. = new Example2_6("Example2_6");
frame, init ();
frame.setSize(300, 300);
frame, show ();
}
public Example2_6(String name)
{
super (name);
addWindowListener (new WindowAdapter ()
{ public void windowClosing(WindowEvent e)
{ __________;
}
} );
}
public void init()
{
setLayout (new ___________);
Label labelTitle = new Label("在文本框中输入字符串,可以在Frame. 中间显示");
Label showTextLabel = new Label();
TextField textField = new TextField("请在这里输入字符串");
textField.addActionListener (new AddStringListener(showTextLabel, textField) );
add("North", labelTitte);
add("Center", showTextLabel);
add("South", textField);
}
}
class AddStringListener implements ActionListener
{
Label label;
TextField textField;
public AddStringListener(Label label, TextField textField)
{
this. label = label;
this.textField = textField;
}
public void actionPerformed(ActionEvent e)
{
label, setText (textField. getText ());
}
}
第4题
下列程序是整数除法计算的程序,要求在出现异常时,能抛出异常信息。
考虑两种异常:
(1)输入非数字除数
(2)输入除法分母为零
该程序运行的三种结果状态如下:
(1)输入两个合法整数
(2)输入非数字除数
(3)输入除数为零
请将程序填写完整。
注意:不改动程序结构,不得增行或删行。
import java.awt.event.*;
public class ex3 extends ______implements ActionListener
{
private JTextField input1,input2, output;
private int number1,number2;
private double result;
public ex3()
{
______("示范异常");
Container c=getContentPane();
c.setLayout(new GridLayout(3,2));
c.add(new JLabe1("输入分子",SwingConstants.RIGHT));
input1=new JTextField(8);
c.add(input1);
c.add(new JLabe1("输入分母和回车",SwingConstants.RIGHT));
input2=new JTextField(8);
c.add(input2);
input2.addActionListener(this);
c.add(new JLabe1(”计算结果”,SwingConstants.RIGHT));
output=new JTextField();
c.add(output);
setSize(400,100);
show();
}
public void actionPerformed(ActionEvent e)
{
DecimalFormat precision3=new DecimalFormat("0.000");
output.setText("");//空的JTextField输出
try{
number1=Integer.parseInt(input1.getText());
number2=Integer.parseInt(input2.getText());
result=quotient(number1,number2);
______;
}
catch (NumberFormatException nfe)
{
______(this,"你必须输入两个整数","非法数字格式",
JOptionPane.ERROR_MESSAGE);
}
catch (Exception dbz)
{
______(this,"除法异常","除数为零",
JOptionPane.ERROR_MESSAGE);
}
}
//定义求商的方法,如遇除数为零时,能抛出异常。
public double quotient(int numerator,int denominator)
throws Exception
{
if(denominator= =0)
throw new Exception();
return(double) numerator/denominator;
}
public static void main(String args[])
{
Java3 app=new Java3();
app.addWindowListener(
new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
e.getWindow().dispose();
System.exit(0);
}
}
);
}
}
第5题
文本框,在这个文本框中输入的内容将会显示在Center位置。运行结果如下图所示。
注意:请勿修改main()主方法和其他已有语句内容,仅在横线处填入适当语句。
import java.awt.*;
import java.awt.event.*;
public class simple extends Frame
{
public static void main(String[] args)
{
simple frame=new simple("simple");
frame.init();
frame.setSize(300,300);
frame.show();
}
public simple(String name)
{
super(name);
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{______;
}
} );
}
public void init()
{
setLayout(new______);
Label labelTitle=new Label("在本文框中输入字符串, 可以早Frame中间显示");
Label showTextLabel=new Label();
TextField textField=new TextField("请在这里输入字符串");
textField.addActionListener(new AddStringListener(showTextLabel,
textField));
add("North", labelTitle);
add("Center", showTextLabel);
add("South", textField);
}
}
class AddStringListener implements ActionListener
{
Label label;
TextField textField;
public AddStringListener(Label label, TextField textField)
{
this.label=label;
this.textField=textField;
}
public void actionperformed(ActionEvent e)
{
label.setText(textField.getText());
}
}
第7题
。
for(int i=0;i<a.length;i++)
if(a[i]%3==0)System.out.println(a[i]+" ");
第10题
直线,将小程序界面均分为4个区域,鼠标在不同的区域移动时显示不同的几何图形。请改正程序中的错误(有下划线的语句),使程序能输出正确的结果。
注意:不改动程序的结构,不得增行或删行。
程序运行结果如下:
import java.awt.*;
import java.awt.Graphics.*;
import java.awt.event.*;
/*
<applet code=AppletPrograming width=200 height=200>
</applet>
*/
public class AppletPrograming extends Applet{
int m=-1,n=-1;
public void init()
{
addMouseMotionListener(new MouseMotionAdapter()
{
public void mouseMoved(Mouse p)
{
m=e.getX();
n=e.getY ( );
}
repaint ( )
{ }
});
}
public void paint(Graphics g)
{
g.drawLine(100,0,100,200);
g.drawLine(0,100,200,100);
if(m<100&&n<100)
{
g.setColor(Color.black);
g.fillOval(10,10,80,80);
}
else if(m>100&&n<100)
{
g.setColor(Color.green);
g.fillRect(110,10,80,80);
}
else if(m<100&&n>100)
{
g.setColor(Color.blue);
g.fillArc(10,130,80,35,50,250);
}
else if(m>100&&n>100)
{
int xvals[ ]={150,180,120};
int yvals[ ]={120,170,170};
g.setColor(Color.yellow);
g.fillPolygon(xvals,yvals,3);
}
}
}
ex35_3.html:
<html>
<head>
<title>A Simple Program</title>
</head>
<body>
<applet code="AppletPrograming.class"width=800 height=400>
</applet>
</body>
</html>
为了保护您的账号安全,请在“上学吧”公众号进行验证,点击“官网服务”-“账号验证”后输入验证码“”完成验证,验证成功后方可继续查看答案!