问答题下面是一个Applet程序,其功能是接收用户输入的两个整数,比较它们的大小,并在用户按下“比较大小”按钮后,将Applet中显示的“请先输入两个待比较的整数”,改为“两个整数中最大值是:x”,x是两个数中的最大值。请改正程序中的错误(有下划线的语句),使程序能输出正确的结果。注意:不改动程序的结构,不得增行或删行。程序运行结果如下:importjava.applet.*;importjava.awt.*;importjava.awt.event.*;/*<appletcode=LookForMaxwidth=800height=400></applet>*/publicclassLookForMaxextendsAppletimplementsActionListener{Labelresult;TextFieldinl,in2;Buttonbtn;inta=0,b=0,max=0;publicvoidinit(){result=newLabel("请先输入两个待比较得整数");in1=newTextField(5);in2=newTextField(5);btn=newButton("比较大小");add(in1);add(in2);add(btn);add(result);btn.addActionListener(supper);}publicvoidactionPerformed(ActionEvente){a=Integer.parseInt(in1);b=Integer.parseInt(in2);if(a>b)max=a;elsemax=b;result,setText("两个数中最大值是:"+max);}}{{B}}LookFormax.html:{{/B}}<html><head><title>ASimpleProgram</title></head><body><appletcode="LookForMax.class"width=800height=400></applet></body></html>
问答题下面是一个Applet程序,程序的功能是用鼠标画图。本题是通过单击鼠标后画出一个Java字。请改正程序中的错误(有下划线的语句),使程序能输出正确的结果。
注意;不改动程序的结构,不得增行或减行。
源程序文件代码清单如下;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code=SimpleHitMouse.java width=800 height=400>
</applet>
*/
public class {{U}}SimpleHitMouse implements{{/U}} Applet
{
private java.util.Vector points=new java.util.Vector();
public void init()
{
addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
points.add(new Point(e.getX(),e.getY())
{{U}}paint(){{/U}};
}
});
}
public void update(Graphics g)
}
{{U}}repaint(g);{{/U}}
}
public void paint(Graphics g)
{
for(int i=0;i<points.size();i++)
{
Point dot=(Point)points.elementAt(i);
g.drawString("x",dot.x,dot.y);
}
}
public static void main(String args[ ])
{
Frame fram=new Frame("CelayTree");
SimpleHitMouse dot=new SimpleHitMouse();
dot.init ();
dot.start ();
fram.add ( "Center", dot );
fram.setSize( 400,300 );
fram.addWindowListener( new WindowAdapter(){
public void windowClosing( WindowEvent e )
{
System.exit(0);
}
});
fram.show ();
}
}
{{B}}ex12_3.html:{{/B}}
<html>
<head>
<title>A Simple Program</title>
</head>
<body>
<applet code=" SimpleHitMouse.class" width=800 height=400>
</applet>
</body>
</html>
问答题本题的功能是对下拉菜单项的操作,包括添加和删除。页面包括一个下拉菜单、一个文本框和两个按钮“删除”和“添加”,选中下拉菜单的一项后,可以通过“删除”按钮从下拉菜单中删除该项,在文本框中填入字符串后,单击“添加”按钮就可以将该项添加到下拉菜单中,所有信息都将显示在右侧的文本域中。
import java.awt.*;
import java.awt.event.*;
public class java2 extends java.applet.Applet imple-
ments hemListener,ActionListener
{Choice choice;
TextField text;
TextArea area;
Button add,del;
public void init() .
{choice:new Choice();
text=new TextField(8);
area:new TextArea(6,15);
choice.add("音乐天地");
choice.add("武术天地");
choice.add("象棋乐园");
choice.add("交友聊天");
add=new Button("添加");
del=new Button("删除");
add.addActionListener(this);
del.addActionListener(this);
choice.addItemListener(this);
add(choice);
add(del);add(text);add(add);add(area);
}
public void itemStateChanged(hemEvent e)
{String name= ;
int index=choice.getSelectedIndex();
area.setText("\n"+index+":"+name);
}
public void actionPerformed(ActionEvent e)
{if(e.getSource()= =add||e.getSource()= =
text)
{String name=text.getText();
if(name.length()>0)
{choice.add(name);
choice.select(name);
area.append("\n添加"+name);
}
}
else if(e.getSource()= =del)
{choice.remove( );
area.append("\n删除"+choice.getSelectedItem
());
}
}
}
问答题下面是验证“1+1=2”的程序。将6~100之内的偶数表示为两个素数之和。(注:哥德巴赫猜想是要证明对任何大于6的自然数n,n之内的所有偶数可以表示为两个素数之和)。请在下画线处编写适当语句,完成此程序使它能正确执行。
public class Exercise41{
public static void main(String args[]){
for(int i=6;i<=100;i+=2){
for(int j=2;j<100;j++){
______
System.out.println(i+"="+j+"+"+(i-j));
break;
}
}//loop j
}//loop i
}
public static boolean isPrime(int n){
for(int i=2;i<n;i++)
if(n%i==0)return false;
return true;
}
}
问答题注意:下面出现的“考生文件夹”均为%USER%。在考生文件夹下存有文件名为Java_3.java和Java_3.html文件,本题的功能是利用switch语句完成画直线、矩形和椭圆的图形。请完善Java_3.java文件,并进行调试(存命令行中使用appletviewerJava_3.html,或者使用集成开发环境),程序先显示小程序和输入窗口如下:当输入1、2、3中任意一个整数后,在小程序窗口中呈现相应的多个直线或多个矩形或多个椭圆的图形:由于Java_3.java文件不完整,请在注释行“//*********Found*********”下一行语句的下画线地方填入正确内容,然后删除下画线,请勿删除注释行或其他已有语句内容。存盘时,文件必须存放在考生文件夹下,不得改变原有文件的文件名。给定源程序:importJava.awt.Graphics;importJavax.swing.*;//*********Found**********publicclassJava_3extends______intchoice;publicvoidinit()Stringinput;input=JOptionPane.showInputDialog("输入1去画直线/n"+"输入2去画矩形/n"+"输入3去画椭圆/n');//*********Found**********choice=Integer.______(input);//*********Found**********publicvoidpaint(______)for(inti=0;i<10;i++)switch(choice)case1:g.drawLine(10,10,250,10+i*10);break;case2:g.drawRect(10+i*10,10+i*10,50+i*10,50+i*10);break;case3:g.drawOval(10+i*10,10+i*10,50+i*10,50+i*10);break;default;JOptionPane.showMessageDialog(null,"输入非法值");
问答题本程序的功能是随机产生'0'~'9'字符,直到产生的字符是'5'时结束。
public class exam_63{
public static void main(String[] args) {
while(true){
int i=(int)(Math.random()*10);
char c;
______;
System.out.print(c+",");
if(______)
______;
}
}
}
问答题下列程序使用CardLayout管理了2张卡片,每张都是一个Panel,每个Panel有一个Button,单击按钮,显示下一张卡片中的内容。请将程序补充完整。 注意:不改动程序结构,不得增行或删行。 import java.awt.*; import java.awt.event.*; public class ex3 implements______ private Panel p1,p2; private Button btn1,btn2; private Frame frm; private CardLayout cl; public static void main(String[] args) ex3 tt=new ex3(); tt.method(); public void method() Frm=new Frame("CardLayout"); Cl=new CardLayout(); btn1=new Button("Card1"); btn2=new Button("Card2"); pl=new Panel(); p2=new Panel(); p1.add(btn1); btn1.addActionListener(this); p2.add(btn2); ______ frm.SetLayout(cl); frm.add(pl,"Layer1"); frm.add(pl,"Layer1"); frm.SetSize200,200); frm.SetVisible(true); public void actionPerformed(ActionEvent ae) ______
问答题System.out.println("str中的第"+m+"个字符是:
问答题本程序是一个Applet,页面上有一个“请点击”按钮,单击该按钮后弹出一个对话框,对话框上有三个按钮:“橙色”、“蓝色”和“红色”,单击其中任意一个按钮,则可以将对话框的背景色设置为按钮名称所对应的颜色,如图所示。importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;publicclassexam_47extendsJApplet{privateJFrameframe;______(){frame=newJFrame();frame.setTitle("exam_47");frame.setSize(300,200);frame.getContentPane().add(newButtonPanel());JButtonPopButton=newJButton("请点击");getContentPane().add(PopButton);PopButton.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEventevt){if(frame.isVisible())frame.setVisible(false);else______;}});}}classButtonPanelextendsJPanel{privateclassColorActionimplementsActionListener{privateColorbackgroundColor;publicvoidactionPerformed(ActionEventactionevent){setBackground(backgroundColor);repaint();}publicColorAction(Colorcolor){backgroundColor=color;}}publicButtonPanel(){JButtonjbutton=newJButton("橙色");JButtonjbutton1=newJButton("蓝色");JButtonjbutton2=newJButton("红色");add(jbutton);add(jbutton1);add(jbutton2);ColorActioncoloraction=newColorAction(Color.orange);ColorActioncoloractionl=newColorAction(Color.blue);ColorActioncoloraction2=newColorAction(Color.red);jbutton.addActionListener(coloraction);jbuttonl.addActionListener(coloractionl);jbutton2.addActionListener(coloraction2);}exam_47.htmh:<html><head><title>exam_47</title></head><body><appletcode="exam_47.java"width="100"height="50"</applet></body></html>
问答题本程序比较两个字符串"Welcome to China!"和"Welcome you back home!"的大小(即对应字符相比较,出现第一个不相等的字符时,大的所在的字符串大,小的所在的字符串小,字符的比较相当于字符对应的ASCII的比较)。
public class exam_43{
public static void main(String[] args){
String str1="Welcome to China!";
String str2="Welcome you back home!";
int l=str1.length()<str2.length()?______;
char c1='', c2='';
for(int i=0; i<1; i++){
c1=str1.charAt(i);
c2=str2.charAt(i);
if(______)
______;
}
if(c1>c2)
System.out.println("""+str1+"""+">"+"""+str2+""");
else
System.out.println("""+str1+"""+"<"+"""+str2+""");
}
}
问答题下列Aplication程序实现:从1到100的整数分别对35求余数,并根据不同余数(0,1,2,3时分别输出“余0”,“余1,”“余2”,“余3”,其他输出“其他”)输出相应信息。请将程序补充完整。 public class test18_2 Public static Void main(String[]args) for(int i=1;i ______(i______35) Case 0: System.out.print(“余0”); break; Case 1: System.out.print(“余1”); break; Case 2: System.out.print(“余2”); break; ______: System.out.print(“其他”); break; System.out.println();
问答题本题中,主窗口有一个按钮“显示Dialog”,单击该按钮后显示一个对话框,对话框的标题是“Dialog”,其上有一个文字标签“欢迎学习Java.”,此时仍允许对原来窗口进行操作,当关闭新生成的对话框时退出程序。 import java.awt.*; import java.awt.event.*; import javax.swing.*; public class java2 extends Frame implements ActionListener public static void main(String args[]) java2 f=new java2("java2"); Panel pan=new Panel(); f.init(); public java2(String str) super(str); public void init() addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e) System.exit(0); ); setSize(200,200); setLayout(new FlowLayout()); but=new Button("显示Dialog"); add(but); but.addActionListener(this); dlg=new Dialog(this,"Dialog",______); dlg.setSize(100,50); dlg.addWindowListener(new WindowAdapter() public void windowClosing(WindowEvent e) ______; ); dlg.add("Center",new Label("欢迎学习Java.")); setVisible(true); public void actionPerformed(ActionEvent e) dlg.setVisible(true); private Dialog dlg; private Button but;
问答题下面是一个Applet程序,其功能是有2个按钮,分别为First和Second,以及一个Labe1构件。要求点击First时则能在Labe1中显示出Command:First,而点击Second时则能显示出Command:Second,要求只能使用重载一次actionPerformed()方法。请改正程序中的错误(有下划线的语句),使程序能输出正确的结果。注意:不改动程序的结构,不得增行或删行。程序运行结果如下:importjava.awt.*;importjava.awt.event.*;importjava.applet.*;/*<appletcode=ex2_3.classwidth=800height=400></applet>*/publicclassex2_3extendsAppletimplementsActionListenerprivateStringstr="ok";privateLabe11;privateButtonbtn;publicvoidinit()setLayout(null);1=newLabel(str);1.reshape(10,10,150,30);add(1);btn=newButton("First");btn.reshape(10,50,60,20);1.addActionListener(this);add(btn);btn=newButton("Second");btn.reshape(10,100,60,20);btn.setActionCommand("First");btn.addActionListener(this);add(btn);publicvoidactionPerformed(ActionEventae)str="Command:"+ae.getActionCommand();btn.setText(str);ex2_3.htm1<HTML><HEAD><TITLE>ex2_3</TITLE></HEAD><BODY><appletcode="ex2_3.class"width=800height=400></applet></BODY></HTML>
问答题本程序提取字符串"China is a great country."中的前5个字符生成一个新的字符串,并将剩余字符组成另外一个新的字符串,最后将两个新的字符串连接输出。
public class exam_33{
public static void main(String[] args) {
String str="China is a great country.";
______;
headstr=str.substring(______);
trailstr=str.substring(______);
System.out.println(headstr+trailstr);
}
}
问答题注意:下面出现的“考生文件夹”均为%USER%。在考生文件夹中存有文件名为Java_1.java的文件,该程序是不完整的,请在注释行“//**********Found**********”下一行语句的下画线地方填入正确内容,然后删除下画线,请勿删除注释行或改动其他已有语句内容。存盘时文件必须存放在考生文件夹下,不得改变原有文件的文件名。本题的要求是:阅读Java_1.java程序,该程序是分析球队能否升级的程序,每个球队由10名队员组成。程序要求输入考核队员及格否的标记数,如考核及格输入1,否则输入2。完成程序,运行后完成十个整数(1或2)的输入:运行结果可能是:或给定源程序://分析球队升级程序importJavax.swing.JOptionPane;publicclassJava_1publicstaticvoidmain(Stringargs[])//初始化变量intpasses=0;//考试及格的队员数failures=0;//考试不及格的队员数student=1;//学生计数器result;//分析结果Stringinput;//输入值output;//输出字符串//10个队员,用计数器控制循环while(student<=10)input=JOptionPane.showInputDialog("输入队员考核结果(1=及格,2=不及格)");//*********Found********result=Integer.parseInt(______);if(result==1)passes=passes+1;elsefailures=failures+1;student=student+1;//最终分析output="及格:"+passes+"/n不及格:"+failures;if(passes>8)output=output+"/n球队升级";JOptionPane.showMessageDialog(null,output,"球队升级分析结果",//*********Found********JOptionPane.______);System.exit(0);
问答题本程序定义了一个长度为10的boolean型数组,并给数组元素赋值,要求如果数组元素下标为奇数的话,则数组元素值为false,否则为true。
public class exam_75{
public static void main(String[] args) {
boolean b[]=______;
for(int i=0; i<10; i++){
if(______)
b[i]=false;
else
______;
}
for(int i=0; i<10; i++)
System.out.print("b["+i+"]="+b[i]+", ");
}
}
问答题本程序计算半径为2.4的圆的面积。
public class exam_50{
public static void main(String[] args) {
double r=2.4;
double area=area(r);
System.out.println("area="+area);
}
public ______ area(______){
double area;
area=StrictMath.PI*r*r/2;
______;
}
}
问答题本程序的功能是随机产生一个1~10间的随机数,根据提示用户可以猜这个随机数。例如,程序随机产生的随机数是7,如果输入8,则提示“Try smaller...”并继续提示输入,如果输出6,则提示“Try bigger...”并继续提示输入,如果输入7,则输出“Great!”并退出程序。
import java.io.*;
public class exam_29{
public static void main(String[] args){
InputStreamReader ir;
BufferedReader in;
ir=new InputStreamReader(System.in);
in=new BufferedReader(ir);
int ranNum=______;
int guessNum=-1;
String str;
while(______){
System.out.print("Please Input:");
try{
______;
guessNum=Integer.parseInt(str);
}catch(Exception e){
}
if(guessNum>ranNum)
System.out.println("Try smaller...");
else
System.out.println("Try bigger...");
}
System.out.println("Great!");
}
}
问答题本程序统计各个成绩段的人数,分别为<60、≥60以及<80、≥80。
public class exam_55{
public static void main(String[] args) {
int Score[]={67,90,45,84,76,73,92,37};
______;
int i=0;
while(______){
if(Score[i]<60)
sum1++;
______
sum2++;
else
sum3++;
i++;
}
System.out.println("<60:"+sum1+">=60
}
}
问答题本题是一个Applet,功能是监听用对于文本域中文本的选择。页面中有一个文本域、一个“复制”按钮和一个文本框,选中文本域中部分文字后,单击按钮“复制”,所选文字将显示在文本框中。
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class java3 extends Applet implements ActionL-istener
{
TextArea ta=new TextArea(5,30);
TextField tf=new TextField(30);
Button button=new Button("复制");
String text="AWT提供基本的GUl组件,\n"+"
具有可以扩展的超类,\n"+"它们的属性是继承的。\n":
public void init()
{
setLayout(new FlowLayout(FlowLayout.left));
ta.setText(text);
ta.setEditable(true);
add(ta);
add(button);
add(tf);
ta.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
String S;
s=ta.getSelectText();
if(e.getSource()= =button)
tf.setText(s);
}
}