问答题下面程序执行结果为: 1×1=1 2×1=2 2×2=4 3×1=3 3×2=6 3×3=9 …… 9×1=9 9×2=18 9×3=27 9×4=36 9×5=45 9×6=54 9×7=63 9×8=72 9×9=81 请在每条横线处填写一个语句,使程序的功能完整。 注意:请勿改动main()主方法和其他已有的语句内容,仅在横线处填入适当的语句。 public class ForLoopStatement public static void main(String args[]) int m,n; for(m=1; m<10; ______) ______ System.out.print(m+"*"+n+"="+m*n+" "); ______
问答题下面是一个Apple(程序,程序的功能是在Applet显示区内画一个动态的、多维的绿色椭圆环。本题主要是通过数学方法sin(),cos()画出一个圆环。请改正程序中的错误(有下划线的语句),使程序能输出正确的结果。注意:不改动程序的结构,不得增行或删行。程序的执行结果:importjava.awt.*;importjava.awt.event.*;importjava.applet.*;publicclassYuanHuanextendsAppletpublicvoidpaint(Graphicsg)doublew=getSize().width/2;doubleh=getSize().height/2;g.getColor(Color.green);for(doubleth=0;th<10;th+=0.00003)doubler=Math.cos(16*th)+th;doublex=r*Math.cos(th)+w;doubley=r*Math.sin(th)+h;g.drawOval((int)x-1,(int)y-1,3,3);publicstaticvoidmain(Stringargs[])Framef=newFrame("Draw");YuanHuanp=newYuanHuan();p.inti();p.start();f.add(p);f.setSize(400,300);f.addWindowListener(newWindowAdapter()publicvoidwindowClosing(WindowEvente)System.out.exit(0););f.show();ex33_3.htm1:<html><head><title>ASimpleProgram</title></head><body><appletcode="YuanHuan.class"width=800height=400></applet></body></html>
问答题本题程序的功能是用复选框来控制鼠标右键的弹出菜单是否弹出。窗口中有一个复选框“弹出菜单”,勾选该复选框后,鼠标置于窗口上,右击会弹出一个菜单,单击菜单中的命令后,后台会输出所单击的菜单项。如果取消勾选该复选框,右击就不能弹出菜单。请将下述程序补充完整(注意:不得改动程序的结构,不得增行或删行)。
import java.awt.*;
import java.awt.event.*;
class CanvasWithPopup extends Canvas
{
______;
CanvasWithPopup(PopupMenu popup)
{
enableEvents(AWTEvent.MOUSE EVENT MASK);
this.popup = popup;
}
void addPopup()
{
add(popup);
}
void removePopup()
{
remove(popup);
}
protected void processMouseEvent(MouseEvent evt)
{
if (popup.getParent() ! = null && evt.isPopupTrigger())
{
popup.show(evt.getComponent(),evt.getX(),evt.getY());
}
super.processMouseEvent(evt);
}
}
public class advance extends Frame implements ItemListener,ActionListener
{
Checkbox cb = new Checkbox("弹出菜单",false);
CanvasWithPopup canvas;
advance()
{
super("advance");
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
add(cb,BorderLayout.NORTH);
cb.addItemListener(this);
PopupMenu popup = new PopupMenu("Button Control");
popup.add("item1");
popup.add("item2");
popup.addActionListener(this);
canvas = new CanvasWithPopup(popup);
add(canvas,BorderLayout.CENTER);
setSize(100,200);
show();
}
public void itemStateChanged(ItemEvent evt)
{
______
{
case ItemEvent.SELECTED:
canvas.addPopup(); break;
case ItemEvent.DESELECTED:
canvas.removePopup(); break;
}
}
public void actionPerformed(ActionEvent evt)
{
______;
}
static public void main(String[] args)
{
new advance ();
}
}
问答题本程序打印1~9的乘法表。运行结果如图所示。publicclassexam_56{publicstaticvoidmain(String[]args){______;for(i=1;______;i++){for(j=1;______;j++)System.out.print(i+"x"+j+"="+i*j+"");System.out.println();}}}
问答题注意:下面出现的“考生文件夹”均为%USER%。
在考生文件夹中存有文件名为Java_1.java文件,请完善Java_1.java文件,并进行调试,使程序结果为
a b c d e f g h i
由于Java_1.java文件不完整,请在注释行“//*********Found*********”下一行语句的下画线地方填入正确内容,然后删除下画线,请勿删除注释行或其他已有语句内容。存盘时,文件必须存放在考生文件夹下,不得改变原有文件的文件名。
给定源程序:
import java.io.*;
public class Java_1{
public static void main(String[] args){
char[] charArray={'a','b','c','d','e','f','g','h','i'};
char c;
try{
//*********Found**********
DataOutputStream out=new______(
new FileOutputStream("test.dat"));
for(int i=0;i<charArray.length;i++){
out.writeChar(charArray[i]);
}
out.close();
DataInputStream in=new DataInputStream(
//*********Found**********
new FileInputStream("______"));
while(in.available()!=0){
c=in.readChar();
System.out.print(c+"");
}
System.out.println();
//*********Found**********
in.______();
}catch(10Exception e){)
}
}
问答题本题的功能是在文本域面板中添加一个带有行数的面板。窗口中有一个文本域,在文本域的左侧有一个带有数字的面板,该面板上的数字指示着文本域中的行数。 import javax.swing.*; import javax.swing.event.*; import java.awt.*; public class java3 extends JFrame public static JTextPane textPane; public static JScrollPane scrollPane; JPanel panel; public java3() super("java3()"); panel=new JPanel(); panel.setLayout(new BorderLayout()); panel.setBorder(BorderFactory.createEmptyBorder(20,20,20,20)); textPane=new JTextPane(); textPane.setFont(new Font("monospaced",Font.PLAIN,12)); scrollPane=new JScrollPane(textPane); panel.add(scrollPane); scrollPane.setPreferredsize(new Dimension(300,250)); setContentPane(panel); setCloseOperation(JFrame.EXIT_ON_CLOSE); LineNumber lineNumber=new LineNumber(); scrollPane.setRowHeaderView(lineNumber); public static void main(String[]args) java3 ttp=new java3(); ttp.pack(); ttp.setVisible(true); class LineNumber extends JTextPane private final static Color DEFAULT_BACKGROUND=Color.gray; private final static Color DEFAULT_FOREGROUND=Color.black; private final static Font DEFAULT_FONT=new Font("monospaced",Font.PLAIN,12); private final static int HEIGHT=Integer.MAX_VALUE-1000000; private final static int MARGIN=5; private FontMetrics fontMetrics; private int lineHeight; private int currentRowWidth; private JComponent component; private int componentFontHeight; private int componentFontAscent; public LineNumber(JComponent component) if(component==null) setBackground(DEFAULT_BACKGROUND); setForeground(DEFAULT_FOREGROUND); setFont(DEFAULT_FONT); this.component=this; else setBackground(DEFAULT_BACKGROUND); setForeground(component.getForeground()); setFont(component.getFont()); this.component=component; componentFontHeight=component.getFontMetrics(component.getFont()).getHeight(); componentFontAscent=component.getFontMetrics(component.getFont()).getAscent(); setPreferredWidth(9999); public void setPreferredWidth(int row) int width=fontMetrics.stringWidth(String.valueOf(row)); if(currentRowWidth<width) currentRowWidth=width; setPreferredSize(new Dimension(2*MARGIN+width,HEIGHT)); public void setFont(Font font) super.setFont(font); fontMetrics=getFontMetrics(getFont()); public int getLineHeight() if(lineHeight==0) return componentFontHeight; else return lineHeight; public void setLineHeight(int lineHeight) if(lineHeight>0) this.lineHeight=lineHeight; public int getStartOffset() return component.getInsets().top+componentFontAscent; public void paintComponent(Graphics g) int lineHeight=getLinerteight(); int startOffset=getStartOffset(); Rectangle drawHere=g.getClipBounds(); g.setColor(getBackground()); g.fillRect(drawHere.x,drawHere.y,drawHere.width.drawHere.height); g.setColor(getForeground()); int startLineNumber=(drawHere.y/lineHeight)+1; int endLineNumber=startLineNumber+(drawHere.height/lineHeight); int start=(drawHere.y/lineHeight)*lineHeight+startOffset; for(int i=startLineNumber;i<=endLineNumber;i++) String lineNumber=String.valueOf(i); int width=fontMetrics.stringWidth(lineNumber); g.drawstring(lineNumber,MARGIN+currentRowWidth-width,start); start+=lineHeight; setPreferredWidth(endLineNumber);
问答题本程序定义了一个方法add(),用于求两个整型数的和。方法中有两个整型参数a和b,方法体中计算a和b的和sum,并将结果返回。程序中调用add()方法求整数24和34的和,并将结果打印输出。
public class exam_28{
public static void main(String[] args){
int a=24, b=34;
System.out.println(add(a,b));
}
public static int add(______){
______;
sum=a+b;
______;
}
}
问答题请完成下列Java程序:运行3个线程有自己的标志,用a,b,c表示,每个线程显示一个“Start”信息和一个“End”信息并且间隔地显示2个“Loop”信息(间隔变化为0.5~2秒之间的随机延迟)。
程序运行结果如下(注:由于事件间隔为随机数,所以,运行结果的顺序不唯一):
a Start
b Start
c Start
b Loop
a Loop
b Loop
b End
c Loop
a Loop
a End
c Loop
c End
注意:请勿改动main()主方法和其他已有语句内容,仅在下划线处填入适当的语句。
public class ex5_2 implements Runnable
{
static char flag5_2='a';
public static void main(String[] args)
{
ex5_2 obj5_2=new ex5_2();
Thread thread5_2=new Thread(obj5_2);
Thread5_2.start();
thread5_2=new Thread(obj2_2);
thread5_2.start();
threa'd5_2=new Thread(obj2_2);
thread5_2.start();
}
public void run()
{
char myflag5_2;
synchronized(this)
{
______;
}
System.out.println(myflag5_2+"Start");
for(int i=0;i<2;i++)
{
try
{
Thread.sleep(rand(500,2000));
System.out.println(myflag5_2+"Loop");
}catch(InterruptedException ie)
{
System.out.println(ie);
}
}
System.out.println(myflag5_2+"End");
}
final private iht rand(int low,int high)
{
return(______);
}
}
问答题下面的程序是求9999以内的完全数。所谓完全数是指这样的自然数:它的各个约数(不包括该数自身)之和等于该数自身。如28=1+2+4+7+14就是一个完全数。请在程序的每条横线处填写一个语句,使程序的功能完整。 注意:请勿改动main()主方法和其他已有的语句内容,仅在横线处填入适当的语句。 源程序文件代码清单如下: public class QuanShu public static void main(String args[]) for (int n=1;n<9999; n++) if(______) System.out.println (n); public static int divsum(int n) //该方法功能是求一个数的所有约数 int s=0; for (int i=1; i<n; i++) if(______) ______ return s;
问答题本题的功能是监听鼠标的拖曳操作。窗口中有一个列表框,列表框中列出了当前目录的所有文件,鼠标选中一个或多个文件后拖曳出窗口,此操作的功能是将拖曳的文件复制一份在拖曳的目的目录下。 import java.awt.*; import java.awt.datatransfer.*; import java.awt.dnd.*; import java.awt.event.*; import java.io.*; import java.util.*; import javax.swing.*; public class java3 public static void main(String[]args) JFrame frame=new DragSourceFrame(); frame.setDefauhCloseOperation(JFrame.EXIT_ON_CLOSE); frame.show(); class DragSourceFrame extends JFrame public DragSourceFrame() setTitle("java3"); setSize(WIDTH,HEIGHT); Container contentPane=getContentPane(); File f=new File(".").getabsoluteFile(); File[]files=f.listFiles(); model=new DefauhListModel(); for(int i=0;i<files.length();i++) try model.addElement(files[i].getCanonicalFile()); catch(IOException exception) JOptionPane.showMessageDialog(this,exception); fileList=new JList(model); contentPane.add(new JScrollPane(fileList), BorderLayout.CENTER); contentPane.add(new JLabel("从列表中拖曳出文件"), BorderLayout.NORTH); DragSource dragSource=DragSource.getDefaultDragSource(); dragSource.createDefaultDragGestureRecognizer(ileList, DnDConstants.ACTION_COPY_OR_MOVE,new DragGestureListener() public void dragGestureRecognized( DragGestureEvent event) draggedValues=fileList.getSelectedValues(); Transferable transferable =new FileListTransferable(draggedValues); event,startDrag(null,transferable, new FileListDragSourceListener()); ); private class FileListDragSourceListener implements DragSourceAdapter public void dragDropEnd(DragSourceDropEvent event) if(event.getDropSuccess()) int action=event.getDropAction(); if(action==DnDConstants.ACTION_MOVE) for(int i=0;i<draggedValues.length i++) model.removeElement(draggedValues[i]); private JList fileList; private DefaultListModel model; private Object[]draggedValues; private static final int WIDTH=300; private static final int HEIGHT=200; class FileListTransferable implements Transferable public FileListTransferable(Object[]files) fileList=new ArrayList(Arrays.asList(files)); public DataFlavor[]getTransferDataFlavors() return flavors; public boolean isDataFlavorSupported(DataFlavor flavor) return Arrays.asList(flavors).contains(flavor); public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException if(flavor.equals(DataFlavor.javaFileListFlavor)) return fileList; else if(flavor.equals(DataFlavor.stringFlavor)) return fileList.toString(); else throw new UnsupportedFlavorException(flavor); private static DataFlavor[]flavors= DataFlavor.javaFileListFlavor, DataFlavor.stringFlavor ; private java.util.List fileList;
问答题本程序中,在窗口左侧添加了一个菜单,右侧为一个文本域,菜单有“File”和“Help”,“File”菜单中有“New”、“Open”、“Save”、“SaveAs”和“Exit”菜单项,其中“Open”的快捷键为“Ctrl+O”,“Save”的快捷键为“Ctrl+S”,而“Help”菜单以及其中的“Index”和“About”菜单项设定了第一个字母为其快捷字母,通过鼠标单击任一个菜单项或通过快捷键以及快捷字母,都能在后台输入所选择的菜单项,如图所示。importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;importjavax.swing.event.*;publicclassexam_57extendsJFrame______{privateJMenuItemsaveItem;privateJMenuItemsaveAsItem;privateJPopupMenupopup;privateJTextAreatextArea;publicexam_57(){setTitle("exam_57");setSize(400,300);addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEvente){System.exit(0);}});textArea=newJTextArea(0,0);ContainercontentPane=getContentPane();contentPane.add(newJScrollPane(textArea),"Center");JMenuBarmenuBar=newJMenuBar();menuBar.setLayout(newBoxLayout(menuBar,BoxLayout.Y_AXIS));getContentPane().add(menuBar,BorderLayout.WEST);HorizontalMenufileMenu=newHorizontalMenu("File");fileMenu.addMenuListener(this);JMenuItemopenItem=newJMenuItem("Open");openItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,InputEvent.CTRL_MASK));saveItem=newJMenuItem("Save");saveItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.CTRL_MASK));saveAsItem=newJMenuItem("SaveAs");menuBar.add(makeMenu(fileMenu,newObject[]{"New",openItem,null,saveItem,saveAsItem,null,"Exit"},this));HorizontalMenuhelpMenu=newHorizontalMenu("Help");helpMenu.______("H");menuBar.add(Box.createVerticalGlue());menuBar.add(makeMenu(helpMenu,newObject[]{newJMenuItem("Index","I"),newJMenuItem("About","A")},this));}publicvoidactionPerformed(ActionEventevt){Stringarg=evt.getActionCommand();System.out.println(arg);if(arg.equals("Exit"))System.exit(0);}publicvoidmenuSelected(MenuEventevt){}publicvoidmenuDeselected(MenuEventevt){}publicvoidmenuCanceled(MenuEventevt){}publicHorizontalMenumakeMenu(Objectparent,Object[]items,Objecttarget){HorizontalMenum=null;if(parentinstanceofHorizontalMenu)m=(HorizontalMenu)parent;elseif(parentinstanceofString)m=newHorizontalMenu((String)parent);elsereturnnull;m.setMinimumSize(m.getPreferredSize());for(inti=0;i<items.length;i++){if(items[i]==null)m.addSeparator();elsem.add(makeMenuItem(items[i],target));}returnm;}publicstaticJMenuItemmakeMenuItem(Objectitem,Objecttarget){JMenuItemr=null;if(iteminstanceofString)r=newJMenuItem((String)item);elseif(iteminstanceofJMenuItem)r=(JMenuItem)item;elsereturnnull;if(targetinstanceofActionListener)r.addActionListener((ActionListener)target);returnr;}classHorizontalMenuextendsJMenu{HorizontalMenu(Stringlabel){super(label);JPopupMenupm=getPopupMenu();pm.setLayout(newBoxLayout(pm,BoxLayout.XAXIS));setMinimumSize(getPreferredSize());}}publicstaticvoidmain(String[]args){Framef=newexam_57();f.show();}}
问答题请完成下列Java程序。程序的功能是显示用户在命令行方式下指定的任意驱动器目录的内容。(提示:publicString[]list();//将目录中所有文件名保存在字符数组中返回)注意:请勿改动main()主方法和其他已有的语句内容,仅在下划线处填入适当的语句。importjava.io.*;publicclassFindDirectories{publicstaticvoidmain(Stringargs[]{if(args.length==0)args=newString[]{".."};try{_______________String[]fileName=pathName.list();for(inti=0;i<fileName.length;i++{Filef=newFile(pathName.getPath()fileName[i]);if(___________________){System.out.println(f.getCanonicalPath());main(newString[]{f.getPath()});}}}catch(IOExceptione){e.printStackTrace();}}}
问答题本程序是一个Applet,其功能是监听对复选框的选择。页面中有四个复选框,每个复选框代表一个方向的箭头标签,选中其中任意一个复选框后,被选中的复选框对应的箭头标签将添加到文本域中,然后将该复选框置于未选中状态,如图所示。importjava.awt.*;importjava.awt.event.*;importjava.applet.*;publicclassexam_76extendsApplet______{Checkboxbox1,box2,box3,box4;TextAreatext;publicvoidinit(){box1=newCheckbox("→");box2=newCheckbox("↑");box3=newCheckbox("←");box4=newCheckbox("↓");box1.addItemListener(this);box2.addItemListener(this);box3.addItemListener(this);box4.addItemListener(this);text=newTextArea(16,18);add(box1);add(box2);add(box3);add(box4);add(text);}publicvoiditemStateChanged(ItemEvente){Checkboxbox=(Checkbox)e.getItemSelectable();if(box.getState()){intn=text.getCaretPosition();text.insert(______,n);box.setState(false);}}}exam_76.html:<html><head><title>exam_76</title></head><body><appletcode="exam_76.class"width="400"height="500"></applet></body></html>
问答题程序如下:∥Java_3.javaimportjava.awt.*;importjavax,swing.*;∥**********Found**********publicclassJava_3{{U}}(5){{/U}}JApplet{∥**********Found**********publicvoid{{U}}(6){{/U}}(){ContainercontentPane=getContentPane();JLabellabel=newJLabel("Java的诞生是对传统计算模式的挑战!",SwingConstants.CENTER);∥**********Found**********contentPane,add({{U}}(7){{/U}});}}∥Java_3.html<html><appletcode="Java_3.class"width=300height=50></applet></html>
问答题本程序的功能是统计成绩不及格的人数,分数有56,90,89,23,45,61,60,59。
public class exam_30{
public static void main(String[] args){
int []score={56,90,89,23,45,61,60,59};
int num=0;
______;
int i=0;
while(______){
if(______)
sum++;
i++;
}
System.out.println("<60:"+sum);
}
}
问答题本程序中定义了一个长度为20的整型数组并赋值,且数组中的数值是递增的,程序将数组中元素做头尾置换,即数组中第1个元素和倒数第1个元素交换,数组中第2个元素和倒数第2个元素交换,以此类推,最后从第1个元素开始打印输出。
public clasS exam 9{
public static void main(String[] args){
int[] arr=new int[20];
int i=0;
for(i=0; i<20; i++)
arr[i]=(i+1)*5;
int temp;
i=0;
int j=19;
while(______){
temp=arr[i];
arr[i]=arr[j];
______;
i++;
______;
}
for(i=0; i<19; i++)
System.out.print(arr[i]+" ");
}
}
问答题本题的功能是计算1~10之间除了5以外的各个自然数的和。 public class java1 public static void main(String[]args) int i=1; int sum=0; while(i<=10) if(i==5) ______; ______; ______; i++; System.out.println("sum="+sum);
问答题本题中,在下画线上填写代码,指定变量b为字节型,变量f为单精度实型,变量1为64位整型。 public class java1 public static void main(String[]args) ______b=49; ______f=8.9f; ______l=0xfedl; System.out.println("b="+b); System.out.println("f="+f); System.out.println("l="+1);
问答题本程序的功能是使用按钮来添加新的进程。窗口中有“增加”和“关闭”两个按钮,单击“增加”按钮即在窗口上增加一个文本框,文本框开始从0~49计数,每增加1间隔50毫秒,继续单击“增加”按钮,窗口中继续添加计数文本框,直到文本框有16个后停止增加,单击“关闭”按钮则退出程序,如图所示。importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;classCounterSubTaskimplementsRunnable{privateTextFieldtextField;publicCounterSubTask(TextFieldargTextField){textField=argTextField;}publicvoidrun(){for(inti=0;i<50;i++){try{textField.setText(i);Thread.sleep(50);}catch(InterruptedExceptione){}}}}publicclassexam_34{publicstaticvoidmain(String[]args){JFrameframe=newCounterFrame();frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.show();}}classCounterFrameextendsJFrame{publicstaticfinalintWIDTH=400;publicstaticfinalintHEIGHT=300;JPaneltextPanel=newJPanel();intnumber=0;publicCounterFrame(){setSize(WIDTH,HEIGHT);setTitle("exam_34");ContainercontentPane=getContentPane();JPanelbuttonPanel=newJPanel();addButton(buttonPanel,"增加",newActionListener(){publicvoidactionPerformed(ActionEventevt){addSubTask();}});addButton(buttonPanel,"关闭",newActionListener(){publicvoidactionPerformed(ActionEventevt){System.exit(0);}});textPanel.setLayout(newFlowLayout(FlowLayout.LEFT));contentPane.add(textPanel,BorderLayout.NORTH);contentPane.add(buttonPanel,BorderLayout.SOUTH);}publicvoidaddButton(Containerc,Stringtitle,ActionListenerlistener){JButtonbutton=newJButton();c.add(button);button.addActionListener(listener);}publicvoidaddSubTask(){if(number>=16){return;}number++;TextFieldtextField=newTextField(20);textPanel.add(textField);show();CounterSubTaskcst=newCounterSubTask(textField);cst.start();}}
问答题请在每条横线处填写一个语句,使程序的功能完整,且输出结果为9 1 1。
注意:请勿改动main()主方法和其他已有的语句内容,仅在横线处填入适当的语句。
源程序文件代码清单如下:
public class Outer
{
public static void main(String args[])
{
Outer i=new Outer();
i.taskInner();
}
public class Inner
{
private int size;
public void doSomething(int size)
{
______//访问局部变量
this.size++;//访问内部类的成员变量
______//访问外部类的成员变量
System.out.println(size+" "+this.size+" "+Outer.this.size);
}
}
public void taskInner()
{
______
k.doSomething(8);
}
private static int size;
}
