问答题本程序求整型数组a(a={45,22,91,87,45,96,78))中的最大元素,并打印输出计算结果。
public class exam_80{
public static void main(String[] args) {
int []a={45,22,91,87,45,96,78);
______;
int i=1;
while(______){
if(a[i]>max)
______;
i++;
}
System.out.println("Max="+max);
}
}
问答题基本操作题
下面的程序是求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
问答题注意:下面出现的“考生文件夹”均为%USER%。在考生文件夹中存有文件名为Java_3.java和Java_3.html文件,本题的功能是用递归定义:n!=n*(n-1)!计算0至10的阶乘。请完善Java_3.java文件,并进行调试(在命令行中使用appletviewerJava_3.html,或者使用集成开发环境),使小程序结果如下:由于Java_3.java文件不完整,请在注释行“//*********Found*****l****”下一行语句的下画线地方填入正确内容,然后删除下画线,请勿删除注释行或其他已有语句内容。存盘时,文件必须存放在考生文件夹下,不得改变原有文件的文件名。给定源程序:importjava.awt.*;importjavax.swing.*;//*********Found*********publicclassJava_3extends______{JTextAreaoutputArea;publicvoidinit(){outputArea=newJTextArea();//*********Found*********Containerc=______();//*********Found**********cadd(______);//计算0至10的阶乘for(longi=0;i<=10;i++)outputArea.append(i+"!="+factorial(i)+"/n");}//阶乘的递归定义publiclongfactorial(longnumber){if(number<=1)return1;elsereturnnumber*factorial(number-1);}}
问答题下面是一个类的定义程序,请将程序补充完整。 注意:不改动程序结构,不得增行或删行。 public class______ int x,y; My Class (int i,______) x=i; y=j;
问答题本题中,鼠标在窗口中单击一下,就在单击的位置生成一个小矩形,如果在小矩形上双击鼠标左键,则删除小矩形。 import java.awt.*; import java.awt.event.*; import javax.swing.*; class MousePanel extends JPanel extends MouseMotionListener public MousePanel() addMouseListener(new MouseAdapter() public void mousePressed(MouseEvent evt) int x=evt.getX(); int y=evt.getY(); current=find(x,y); if(current<0) add(x,y); public void mouseClicked(MouseEvent evt) int x=evt.getX(); int y=evt.getY(); if(evt.getClickCount()>=2) remove(current); ); addMouseMorionListener(this); public void paintComponent(Graphics g) (super.paintComponent(); for(int i=0;i<nsquares;i++) draw(g,i); public int find(int x,int y) for(int i=0;i<nsquares;i++) if(squares[i].x-SQUARELENGTH/2<=x return-1; public void draw(Graphics g,int i) g.drawRect(squares[i].x-SQUARELENGTH/2, squares[i].y-SQUARELENGTH/2, SQUARELENGTH, SQUARELENGTH); public void add(int x,int y) if(nsquares<MAXNSQUARES) squares[nsquares]=new Point(x,y); current=nsquares; nsquares++; repaint(); public void remove(int n) if(n<0 || n>=nsquares)return; nsquares--; squares[n]=squares[nsquares]; if(current==n)current=-1; repaint(); public void mouseMoved(MouseEvent evt) public void mouseDragged(MouseEvent evt) private static final int SQUARELENGTH=10; private static final int MAXNSQUARES=100; private Point[] squares=new Point[MAXNSQUARES]; private int nsquares=0; private int current=-1; class MouseFrame extends JFrame public MouseFrame() setTitle("java3"); setSize(300,200); addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e) System.exit(0); ); Container contentPane=getContentPane(); contentPane.add(MousePanel()); public class java3 public static void main(String[]args) JFrame frame=new MouseFrame(); frame.show();
问答题请完成下列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();
}
}
}
问答题本程序的功能是对树的操作,包括添加兄弟节点和子节点,以及删除节点。窗口中有一个树结构和三个按钮:“增加兄弟节点”、“增加子节点”和“删除”。选中某个节点后,如果单击“增加兄弟节点”按钮,则在树中添加一个该节点的名为“New”的兄弟节点,如果单击“增加子节点”按钮,则在该节点下添加一个名为“New”的子节点,如果单击“删除”按钮,则删除该节点以及该节点下所有的子节点,如图所示。importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;importjavax.swing.tree.*;publicclassexam_26{publicstaticvoidmain(String[]args){JFrameframe=newTreeEditFrame();frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.show();}}classTreeEditFrameextendsJFrame{publicTreeEditFrame(){setTitle("exam_26");setSize(WIDTH,HEIGHT);TreeNoderoot=makeSampleTree();model=newDefaultTreeModel(root);tree=newJTree(model);tree.setEditable(true);JScrollpanescrollPane=newJScrollpane(tree);getContentPane().add(scrollPane,BorderLayout.CENTER);makeButtons();}publicvoidmakeSampleTree(){DefaultMutableTreeNoderoot=newDefaultMutableTreeNode("世界国家概览");DefaultMutableTreeNodecountry=newDefaultMutableTreeNode("美国");root.add(country);DefaultMutableTreeNodestate=newDefaultMutableTreeNode("加利福尼亚");country.add(state);DefaultMutableTreeNodecity=newDefaultMutableTreeNode("SanJose");state.add(city);city=newDefaultMutableTreeNode("Cupertino");state.add(city);state=newDefaultMutableTreeNode("密歇根");country.add(state);city=newDefaultMutableTreeNode("AnnArbor");state.add(city);country=newDefaultMutableTreeNode("德国");root.add(country);state=newDefaultMutableTreeNode("Schleswig-Holstein");country.add(state);city=newDefaultMutableTreeNode("Kiel");state.add(city);returnroot;}publicvoidmakeButtons(){JPanelpanel=newJPanel();JButtonaddSiblingButton=newJButton("增加兄弟节点");addSiblingButton.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEventevent){DefaultMutableTreeNodeselectedNode=(DefaultMutableTreeNode)tree.getLastSelectedPathComponent();if(selectedNode==null)return;DefaultMutableTreeNodeparent=(DefaultMutableTreeNode)selectedNode.getParent();if(parent==null)return;DefaultMutableTreeNodenewNode=newDefaultMutableTreeNode("New");intselectedIndex=parent.getIndex(selectedNode);model.insertNodeInto(newNode,parent,selectedIndex+1);TreeNode[]nodes=model.getPathToRoot(newNode);TreePathpath=newTreePath(nodes);tree.scrollPathToVisible();}});panel.add(addSiblingButton);JButtonaddChildButton=newJButton("增加子节点");addChildButton.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEventevent){DefaultMutableTreeNodeselectedNode=(DefaultMutableTreeNode)tree.getLastSelectedPathComponent();if(selectedNode==null)return;DefaultMutableTreeNodenewNode=newDefaultMutableTreeNode("New");model.insertNodeInto(newNode,selectedNode,selectedNode.getChildCount());TreeNode[]nodes=model.getPathToRoot(newNode);TreePathpath=newTreePath(nodes);tree.scrollPathToVisible(path);}});panel.add(addChildButton);JButtondeleteButton=newJButton("删除");deleteButton.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEventevent){DefaultMutableTreeNodeselectedNode=(DefaultMutableTreeNode)tree.getLastSelectedPathComponent();if(selectedNode!=null}});panel.add(deleteButton);getContentPane().add(panel,BorderLayout.SOUTH);}privateDefaultTreeModelmodel;privateJTreetree;privatestaticfinalintWIDTH=400;privatestaticfinalintHEIGHT=200;}
问答题
问答题System.out.println(______);
问答题本程序的功能是监听键盘敲击事件,并将敲击的字符显示在标签上。起始,文字标签提示“Pleasepressyourkeyboard!”,当你按下键盘上的字符键时,文字标签就变为“"X"ispressed!”(X代表用户按下的键所代表的字符),如图所示。importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;publicclassexam_27extendsFrame______{publicstaticvoidmain(Stringargs[]){exam_27f=newexam_27("exam_27");Panelpan=newPanel();f.init();}publicexam_27(Stringstr){super(str);}publicvoidinit(){addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEvente){System.exit(0);}});setSize(200,200);setLayout(newFlowLayout());lab=newLabel("Pleasepressyourkeyboard!");add(lab);addKeyListener(this);setVisible(true);}publicvoidkeyTyped(KeyEvente){lab.setText("""+______+""ispressed!");repaint();}publicvoidkeyPressed(KeyEvente){}publicvoidkeyReleased(KeyEvente){}privateLabellab;}
问答题本程序的功能是读入运行程序时所传入的参数(一个或多个),并将参数依次显示出来,例如运行程序:java exam_5 par1 par2,则打印输出为:par1 par2。
public class exam_5{
public static void main(String[] args){
int i=0;
while(______){
System.out.print(______+" ");
______;
}
System.out.println();
}
}
问答题基本操作题
下面的程序是求菲波那契(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(){
System.out.println("Fibonacci is"+" "+ );
}
static long fib(int n){
if( )
return 1;
else
return
}
}
问答题本题定义了一个求两个数的最大值的方法max,并调用该方法计算67和23的最大值。 public class java1 public static void main(String[]args) java1 temp=new java1(); int res=max(67,23); System.out.println("res="+res); static int max(______) int maxNum; if(a>b) ______; else maxNum=b; ______;
问答题System.out.print(Integer.toString(a[i]));
问答题本程序的功能是让用户在文本框中随意输入一个字符和一个字符串,单击“搜索”按钮后,在字符串中查找输入的字符,并显示找到该字符的个数,如图所示。importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;publicclassexam_4{publicstaticvoidmain(String[]args){IFrameframe=newIFrame();frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame,show();}}classIFrameextendsJFrame{publicstaticfinalintDEFAULT_WIDTH=250;publicstaticfinalintDEFAULT_HEIGHT=250;publicIFrame(){setTitle("Welcome");setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);IPanelpanel=newIPanel();ContainercontentPane=getContentPane();______;}}classIPanelextendsJPanel{publicIPanel(){setLayout(newFlowLayout(FlowLayout.LEFT));JLabella1=newOLabel("请输入字符c:");JLabella2=newJLabel("输入字符串s:");finalJTextFieldnuml=newJTextField(2);finalJTextFieldnum2=newJTextField(20);JButtonbut=newJButton("查找");finalJLabelres=newJLabel("");add(la1);add(num1);add(la2);add(num2);add(but);add(res);but.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEventevent){Strings1=num1.getText();Strings2=num2.getText();charc=s1.charAt(0);intsum=0;chart;for(inti=0;i<s2.length();i++){t=s2.charAt(i);if(______)sum++;}res.setText("结果:查找到"+sum+"个"+c);}});}}
问答题本程序监听用户在“输入密码”文本框中的输入。程序窗口中有两个文本框:“输入密码”和“偷窥密码”,在“输入密码”文本框中输入时显示*,而“偷窥密码”文本框中可实时显示在密码框中输入的真实字符(“偷窥密码”文本框是只读的),如图所示。importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;importjavax.swing.event.*;publicclassexam_41{publicstaticvoidmain(String[]args){PasswordFieldFrameframe=newPasswordFieldFrame();frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.show();}}classPasswordFieldFrameextendsJFrame{publicPasswordFieldFrame(){setTitle("exam_41");setSize(WIDTH,HEIGHT);ContainercontentPane=getContentPane();DocumentListenershowListener=newShowListener();JPaneltextPanel=newJPanel();myPassword=newJPasswordField(20);myPassword.getDocument().addDocumentListener(showListener);textPanel.add(newJLabel("输入密码:"));textPanel.add(myPassword);checkPassword=newJTextField(20);textPanel.add(newJLabel("偷窥密码:"));textPanel.add(checkPassword);contentPane.add(textPanel);}publicvoidshowPassword(){char[]s;Stringgets;s=______;gets=newString(s);checkPassword.setText(gets);}privateclassShowListenerimplementsDocumentListener{publicvoidinsertUpdate(DocumentEvente){showPassword();}publicvoidremoveUpdate(DocumentEvente){showPassword();}publicvoidchangedUpdate(DocumentEvente){}}publicstaticfinalintWIDTH=350;publicstaticfinalintHEIGHT=150;privateJPasswordFieldmyPassword;privateJTextFieldcheckPassword;}
问答题请在画线处填写适当语句,使程序能正确执行。
import javax.swing.*;
public class JOptionPaneExample{
public static void main(String args[]){
int firstNum,secondNum;
String input,s=" ";
input=JOptionPane.showInputDialog(null,"请输入第一个数");
s+="第一个数是:"+input+"/n";
firstNum=Integer.parseInt(input);
input=JOptionPane.showInputDialog(null,"请输入第二个数");
s+="第二个数是:"+input+"/n";
secondNum=Integer.parseInt(input);
if(firstNum>secondNum)s+="第一个数大!";
else if(secondNum>firstNum)s+="第二个数大!";
else s+="两个数一样大!";
JOptionPane.______; //显示比较结果
}
}
问答题利用JApplet扩展DisplayQuote子类,并用paint(Graphics g)编写宽400、长200的窗口,在该窗口中显示字符串"are you ready"的Applet程序和HTML文件。
问答题10位同学参加某次团队测试,要求每位同学都必须及格、同时团队平均分不少于80分,整个团队才能够通过。每位同学的成绩可以通过随机数产生(0~100)。请在程序的每条横线处填写一条语句,是程序的功能完整。
注意:请勿改动main()主方法和其他已有的语句内容,仅在横线处填入适当的语句。
源程序文件代码清单如下:
______;
public class While
{
public static void main(String args[])
{
int fenshu=60;
int sumfenshu=0;
int i=1;
while((fenshu>=60)
System. out. print in (fenshu+" ");
sumfenshu+=fenshu;
i++;
}
System.out.println();
if(_________)
System. out. println ( "团队通过")
else
System. out.println ("团队测试不通过");
}
}
问答题下面是一个Applet程序,其功能是根据给出的小时,分钟和秒数计算相等的秒数,即将1分钟化为60秒,依此类推。要求建立一个时间类,时间参数均作为类的成员变量,并且给出换算时间的方法,也作为这个类的成员函数,可以供外部对象进行调用。同时还需要在输出窗口中显示换算结果,并且将结果写到out3_3.txt文件中,本题给出确定的时间为4小时23分47秒,要求换算成以秒做单位的时间。请改正程序中的错误(有下划线的语句),使程序能输出正确的结果。注意:不改动程序的结构,不得增行或删行。程序运行结果如下:importjava.io.*;importjava.awt.*;importjava.applet.*;/*<appletcode=ex3_3.classwidth=800height=400></applet>*/publicclassex3_3extendsApplet{publicvoidpaint(Graphicsg){intnSum;classmyTime3_3{publicinth;publicintm;publicints;publicintout;publicintcaculateSecond(){{{U}}out=h*60+m*60+s{{/U}};returnout;}}myTime3_3objTime3_3=newmyTime3_3);objTime3_3.h=4;objTime3_3.m=23;objTime3_3.s=47;nSum=objTime3_3.caculateSecond();g.drawString(时:"+objTime3_3.h,2030);g.drawString("分:"+objTime3_3.m,2050);g.drawString("秒:"+objTime3_3.s,20,70);{{U}}g.drawString("合计:"+objTime3_3.out+"秒",20,90);{{/U}}try{FileOutputStreamlos3_3=newFileOutputStream("out3_3.txt,,);BufferedOutputStreambos3_3=newBufferedOutputStream(fos3_3,1024);PrintStreamps3_3=newPrintStream(bos3_3,false);System.setOut(ps3_3);{{U}}System.out.println("合计:"+objTime3_3.out+"秒");{{/U}}ps3_3.close();}catch(IOExceptionioe){System.out.println(ioe);}}}{{B}}ex3_3.html{{/B}}<HTML><HEAD><TITLE>ex3_3</TITLE></HEAD><BODY><appletcode="ex3_3.class"width=800height=400></applet></BODY></HTML>
