单选题下面程序段的输出结果是( )。 public class Test public static void main(String args[]) int x,y X=(int)Math.sqrt(5)/2+(int)Math.random()*5/2; y=(int)Math.sqrt(3)/2+(int)Math.random()*3/2; if(x>y) System.out.println("x>y"); elseif(x==y) System.out.println("x=y"); else System.out.println("x<y");
单选题数据字典(DD)所定义的对象包含于______。
A.数据流图(DFD)
B.程序流程图
C.软件结构图
D.方框图
单选题定义String s=null,会出现异常的选项是( )。 Ⅰ:if((s!=null)&(s.length()>0)) Ⅱ:if((s!=null)&&(s.length()>0)) Ⅲ:if(s==null)|(s.length()==0)) Ⅳ:if(s==null)||(s.length()==0)) A) Ⅱ、Ⅲ B) Ⅱ、Ⅲ、Ⅳ C) Ⅰ、Ⅲ D) Ⅲ、Ⅳ
单选题下列说法中,错误的一项是______。 A.使用Flowlayout进行布局管理时,构件的大小采用的是其最佳尺寸 B.使用FlowLayout在容器中放置构件的策略是:从上到下,从左到右 C.使用FolwLayout进行布局管理时,不能调节构件之间的距离 D.用FlowLayout管理的构件随容器大小变化的变化规律是:构件大小不变,构件之间的相对位置改变
单选题Applet不能继承以下哪个类的方法? ( )
单选题要向Applet传递参数,应该在下列drawing.html文件的下划线处填入的选项是
……
……
单选题执行完代码int[]x=new int[25];后,以下说明正确的是( )。
问答题本程序的功能是读取选中的文件。窗口中有“打开文件”和“关闭”两个按钮,单击“打开文件”按钮,则弹出“Openfile”对话框,选中其中一个文件,单击“打开”按钮后返回主窗口,同时选中的文件字符流就显示在文本域中。单击“关闭”按钮退出程序,如图所示。importjava.awt.*;importjava.awt.event.*;importjava.io.*;publicclassexam_98extendsFrameimplementsWindowAdapter{Stringdirectory;TextAreatextarea;publicexam_98(){this(null,null);}publicexam_98(Stringfilename){this(null,filename);}publicexam_98(Stringdirectory,Stringfilename){super();addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEvente){dispose();}});textarea=newTextArea("",24,80);textarea.setFont(newFont("MonoSpaced",Font.PLAIN,12));textarea.setEditable(false);this.add("Center",textarea);Panelp=newPanel();p.setLayout(newFlowLayout(FlowLayout.RIGHT,10,5));this.add(p,"South");Fontfont=newFont("SansSerif",Font.BOLD,14);Buttonopenfile=newButton("打开文件");Buttonclose=newButton("关闭");openfile.addActionListener(this);openfile.setCommand("open");openfile.setFont(font);close,addActionListener(this);close.setActionCommand("close");close.setFont(font);p.add(openfile);p.add(close);this.pack();if(directory==null){Filef;if((filename!=null)filename=f.getName();}elsedirectory=System.getProperty("user.dir");}this.directory=directory;setfile(directory,filename);}publicvoidsetFile(Stringdirectory,Stringfilename){if((filename==null)||(filename.length()==0))return;Filef;FileReaderin=null;try{f=newFile(directory,filename);in=newFileReader(f);char[]buffer=newchar[4096];intlen;textarea.setText("");while((len=in.read(buffer))!=-1){Strings=newString(buffer,0,len);textarea.append(s);}this.setTitle("exam_98:"+filename);textarea.setCaretPosition(0);}catch(IOExceptione){textarea.setText(e.getClass().getName()+":"+e.getMessage());this.setTitle("exam_98:"+filename+":I/OException");}finally{try{if(in!=null)in.close();}catch(IOExceptione){}}}publicvoidactionPerformed(ActionEvente){Stringcmd=e.getActionCommand();if(cmd.equals("open")){FileDialogf=newFileDialog(this,"OpenFile",FileDialog.LOAD);f.setDirectory(directory);f.show();directory=f.getDirectory();setFile(directory,f.getFile());f.dispose();}elseif(cmd.equals("close"))this.dispose();}staticpublicvoidmain(String[]args)throwsIOException{Framef=newexam_98((args.length==1)?args[0]:null);f.addWindowListener(newWindowAdapter(){publicvoidwindowClosed(WindowEvente){System.exit(0);}});f.show();}}
问答题在程序中,DataPool是一个数据池,能存放一个血型数据,线程a和线程b负责向其中存放数据,一次只能有一个线程向其中存放数据,数据放入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();
}
}
问答题本题程序的功能是:主窗口中有一个按钮,按钮的长和宽每200ms增加1,当达到100时又恢复原来大小重新进行增加。请将下述程序补充完整(注意:不得改动程序的结构,不得增行或删行)。
import java.awt.*;
import java.awt.event.*;
public class simple
{
public static void main (String args[])
{
Mywin win = new Mywin();
}
}
class Mywin extends Frame ______
{
Button b = new Button("按钮");int x = 5;
Thread bird = null;
Mywin ()
{
setBounds(100,100,400,400); setLayout(new FlowLayout());
setTitle ("simple");
setVisible (true);
add(b);
b.setBackground (Color.green);
addWindowListener (new WindowAdapter () {
public void windowClosing(WindowEvent e)
{
System.exit (0);
}
});
bird = new Thread(this);
bird.start();
}
public ______
{
while (true)
{
x = x+1;
if(x > 100)
x = 5;
b.setBounds(40,40,x,x);
try
{
bird.sleep (200);
}
catch(InterruptedException e) {}
}
}
}
问答题本题提示输入年份,然后判断该年份是否为闰年。
importjava.io.*;
public class javal{
public static void main(String[]args){
InputStreamReader ir;
BufferedReader in;
ir=new InputStreamReader(System.in);
in=new BufferedReader(ir);
int year=1900;
System.out.print("请输入年份:");
try{
String s=in.readLine();
;
} (Exception e){
}
if( )
System.OUt.println(year+"是闰年");
else
System.out.println(year+"不是闰年");
}
}
问答题本题中定义了长度为20的-维整型数组a,并将数组元素的下标值赋给数组元素,最后打印输出数组中下标为奇数的元素。
public class javal{
public static void main(String[]args){
int a[]=
Int i:
for
a[i]=i
for
i++)
=1:
i=0;i<20;i++){
System.out.print(”a[”+i+”]=”+a[i]+”,“);
}
问答题下列程序中,给出两个整数2和3,分别求2除以3和2乘以3的结果,要求调用类ex19_1的方法method()来输出相应的结果,请将程序补充完整。程序运行结果如下。 0.6666666666666666 6 源程序文件代码清单如下: public class exl9 1 public static void main(String args[]) int n1=2,n2=3; ex19_1 obj19_1=new ex19_1(); obj19_1. public void method(int x,int y) System.out.println(______); System.out.println(______);
问答题本题的功能是监听键盘键的敲击,并显示在窗口中。
问答题本程序定义了常量a为字符类型,变量b为布尔型,变量c为字节型。其中a为字符T,b为布尔值true,c为13。
public class exam_73{
public static void main(String[] args) {
______ a='T';
______ b=true;
______ c=13;
System.out.println("a="+a);
System.out.println("b="+b);
System.out.println("c="+c);
}
}
问答题本程序的功能是监听鼠标的操作。鼠标置于窗口中单击时(左键或右键),在所单击的地方会画一个小矩形,当鼠标置于小矩形上时,鼠标指针变成十字形,同时按下鼠标左键或右键后可以拖动该矩形,如果将鼠标置于小矩形上,双击鼠标左键(或右键)时,小矩形消失,如图所示。importjava.awt.*;importjava.awt.event.*;importjava.util.*;importjava.awt.geom.*;importjavax.swing.*;publicclassexam_20{publicstaticvoidmain(String[]args){MouseFrameframe=newMouseFrame();frame.setDefaultCloseOperation(JFrame.EXITONCLOSE);frame.show();}}classMouseFrameextendsJFrame{publicMouseFrame(){setTitle("exam_20");setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);MousePanelpanel=newMousePanel();ContainercontentPane=getContentPane();contentPane.add(panel);}publicstaticfinalintDEFAULT_WIDTH=300;publicstaticfinalintDEFAULT_HEIGHT=200;}classMousePanelextendsJPanel{publicMousePanel(){squares=newArrayList();current=null;addMouseListener(newMouseHandler());addMouseMotionListener(newMouseMotionHandler());}publicvoidpaintComponent(Graphicsg){super.paintComponent(g);Graphics2Dg2=(Graphics2D)g;for(inti=0;i<squares.size();i++)g2.draw((Rectangle2D)squares.get(i));}publicvoidfind(Point2Dp){for(inti=0;i<squares.size();i++){Rectangle2Dr=(Rectangle2D)squares.get(i);if(r.contains(p))returnr;}returnnull;}publicvoidadd(Point2Dp){doublex=p.getX();doubley=p.getY();current=newRectangle2D.Double(x-SIDELENGTH/2,y-SIDELENGTH/2,SIDELENGTH,SIDELENGTH);squares.add(current);repaint();}publicvoidremove(Rectangle2Ds){if(s==null)return;if(s==current)current=null;squares.remove(s);repaint();}privatestaticfinalintSIDELENGTH=I0;privateArrayListsquares;privateRectangle2Dcurrent;privateclassMouseHandlerextendsMouseActionListenerpublicvoidmousePressed(MouseEventevent){current=find(event.getPoint());if(current==null)add(event.getPoint());}publicvoidmouseClicked(MouseEventevent){current=find(event.getPoint());if(currentI=null}}privateclassMouseMotionHandlerimplementsMouseMotionListener{publicvoidmouseMoved(MouseEventevent){if(find(event.getPoint)==null)setCursor(Cursor.getDefaultCursor());elsesetCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));}publicvoidmouseDragged(MouseEventevent){if(current!=null{intx=event.getX);inty=event.getY);current.setFramex-SIDELENGTH/2,y-SIDELENGTH/2,SIDELENGTH,SIDELENGTH);repaint();}}}}
问答题本题程序的功能是计算圆和三角形的面积。通过菜单“选择”可以分别进行圆和三角形面积的计算。单击菜单项“圆面积计算”,窗口中就会显示两个文本框和一个“确定”按钮,在第一个文本框中输入圆的半径,单击“确定”按钮后就可以在第二个文本框中显示圆的面积。单击菜单项“三角形面积计算”,窗口中就会显示4个文本框和一个“确定”按钮,在前三个文本框中分别输入三角形三个边的长度,单击“确定”按钮后,如果三个边的长度不能组成三角形,结果文本框中会给出提示信息,否则显示三角形的面积;如果输入的值不是数值,则会给出提示信息。请将下述程序补充完整(注意:不得改动程序的结构,不得增行或删行)。
import java.awt.*;
import java.awt.event.*;
class circle extends Panel implements AetionListener
{
double r,area;
TextField radius = null,result = null;
Button b = null;
______;
{
radius = new TextField(10);
result = new TextField(10);
b = new Button("确定");
add (new Label ("输入半径"));
add (radius);
add(new Label("面积是"));
add(result);
add(b);
b.addActionListener (this);
result,setEnabled (false);
}
public void actionPerformed(ActionEvent e)
{
try
{
r = Double.parseDouble (radius.getText ());
area =Math.PI*r*r;
result,setText (""+area);
}
catch (Exception ee)
{
radius.setText ("请输入数字字符");
}
}
}
class triangle extends Panel implements ActionListener
{
double a = 0,b = 0,c = 0,area;
TextField border a = new TextField(6) ;
TextField border b = new TextField(6) ;
TextField border c = new TextField(6) ;
Result = new TextField(24);
Button button = new Button("确定");
triangle ()
{
add(new Label("输入三边的长度"));
add (border_a);
add (border_b);
add (border_c);
add(new Label("面积是:"));
add (result);
add (button);
button,addActionListener (this);
result.setEnabled(false);
}
public void actionPerformed(ActionEvent e)
{
try
{
a = Double.parseDouble{border_a.getText());
b = Double.parseDouble(border_b.getText());
c = Double.parseDouble(border_c.getText());
if(a+b>c&&a+c>b&&c+b>a)
{
double p = (a+b+c)/2;
area = Math.sqrt(p*(p-a)*(p-b)*(p-c));
result.setText(""+ area);
}
else
{
result.setText ("您输入的数字不能形成三角形");
}
}
catch(Exception ee)
{
result.setText ("请输入数字字符");
}
}
}
class Win ______ implements ActionListener
{
MenuBar bar = null;
Menu menu = null;
MenuItem item1,item2;
circle circle;
triangle trangle;
Win()
{
bar = new MenuBar(); menu = new Menu("选择");
setSize(300,200);
item1 = new MenuItem("圆面积计算");
item2 = new MenuItem("三角形面积计算");
menu.add(item1);
menu.add(item2);
bar.add(menu);
setMenuBar(bar);
circle = new circle();
trangle = new triangle();
item1.addActionListener(this);
item2.addActionListener(this);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == item1)
{
removeAll();
add(circle,"Center");
validate();
}
else
if(e.getSource() == item2)
{
removeAll ();
add (trangle,"Center");
validate ();
}
}
public class advance
{
public static void main (String args[])
{
Win win = new Win();
win.setTitle ("advance ");
win.setBounds (100,100,700,300);
win.setVisible (true);
win.addWindowListener (______)
{
public void windowClosing(WindowEvent e)
{
System.exit (0);
}
});
}
}
问答题本程序的功能是,根据用户输入的文件名,在相应的文件内容中查找匹配给定模式的字符串,并将这些字符串显示出来。模式串为“href="…"”。请填写横线处的内容。 注意:请勿改动main()主方法和其他已有语句内容,仅在横线处填入适当语句。 import java.io.*; import java.util.regex.*; import javax.swing.*; public class Example2_10 public static void main(String [] argv) final String patternString = "href//s*=//s*(/"[^/"]*/"|[^//s>])//s*; String fileName ; try System. out. print ( "请输入html 文件的文件名: "); InputStreamReader in = new InputStreamReader(System.in); BufferedReader imput = new BufferedReader(in); fileName = imput.readLine(); if(fileName.equals(" ")) return; StringBuffer buffer = new StringBuffer(); File file = new File(fileName); FileInputStream readfile = new FileInputStream(file); for(int c = 0; (c = readfile.read()) != -1; ) buffer.append((char)c); Pattern pattern = Pattern.compile( _____________ Pattern.CASE_INSENSITIVE); Matcher matcher =________; while (marcher. find ()) int start = matcher.start(); int end = matcher.end(); String match = buffer.substring(start, end); System.out.println (match); catch (Exception excption) System. out.println (excption. getMessage ()); System.exit(O);
问答题本题中,用表格表现某个月的月历,其中标题是从Sunday到Saturday,表格中的各项是可以修改的。 import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.table.*; public class java2 public static void main(String[]args) try UIManager.setLookAndFeeI(UIManager.getSystemLookAndFeelClassName()); catch(Exception e) JFrame frame=new CalendarTableFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.show(); class CalendarTableFrame extends JFrame private static final int WIDTH=500; private static final int HEIGHT=150; private______cells= null,null,null,new Integer(1),new Integer(2),new Integer(3),new Integer(4), new Integer(5),new Integer(6),new Integer(7),new Integer(8),new Integer(9),new Integer(10),new Integer(11)), new Integer(12),new Integer(13),new Integer(14),new Integer(15),new Integer(16),new Integer(17),newInteger(18)), new Integer(19),new Integer(20),new Integer(21),new Integer(22),new Integer(23),new Integer(24),newInteger(25)), new Integer(26),new Integer(27),new Integer(28),new Integer(29),new Integer(30),new Integer(31),null ; private String[]columnNames= "Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday" ; public CalendarTableFrame() setTitle("java2"); setSize(WIDTH,HEIGHT); JTable table=new______; getContentPane().add(new JScrollPane(table), BorderLayout.CENTER);
问答题本程序的功能是依次输出26个字母,即A~Z。
public class exam_21{
public static void main(String[] args){
int i;
char c='A';
______ (______; i++){
System.out.print(c+" ");
______;
}
}
}
