问答题System.out.println("i="+i);
问答题本程序中,有两个文本框、一个列表、一个按钮和一个标签,在文本框中分别输入两个操作数后,然后在列表中选中一个运算符,单击“=”按钮后就能在标签上显示两个操作数的运算结果(包含运算符的列表不支持多选),如图所示。importjava.awt.*;importjava.awt.event.*;publicclassexam_10extendsFrame{publicstaticvoidmain(Stringargs[]){exam_10f=newexam_10("exam_10");f.addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEvente){System.exit(0);}});f.init();}publicexam_10(Stringstr){super(str);}publicvoidinit(){addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEvente){System.exit(0);}});setLayout(newFlowLayout(FlowLayout.LEFT));setSize(400,100);finalTextFieldop1=newTextField(5);finalTextFieldop2=newTextField(5);Buttonequ=newButton("=");finalLabelres=newLabel("");______=newList(4,false);oper.add("+");oper.add("-");oper.add("*");oper.add("/");oper.select(0);equ.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){Stringo1=op1.getText();Stringo2=op2.getText();Stringo=______;charc=o.charAt(0);doublenum1=Double.parseDouble(o1);doublenum2=Double.parseDouble(o2);doubleresult=0;switch(c){case"+":result=num1+num2;break;case"-":result=num1-num2;break;case"*":result=num1*num2;break;case"/":result=num1/num2;break;}res.setText(Double.toString(result));}});add(op1);add(oper);add(op2);add(equ);add(res);setVisible(true);}}
问答题下面是一个Applet程序,设置其宽高为300和300像素,程序的功能是在小程序界面画一根水平线和一根垂直线,将小程序界面均分为4个区域,鼠标在不同的区域移动时显示不同的几何图形。请改正程序中的错误(有下划线的语句),使程序能输出正确的结果。注意:不改动程序的结构,不得增行或删行。程序运行结果如下:importjava.awt.*;{{U}}importjava.awt.Graphics.*;{{/U}}importjava.awt.event.*;/*<appletcode=AppletProgramingwidth=200height=200></applet>*/publicclassAppletProgramingextendsApplet{intm=-1,n=-1;publicvoidinit(){addMouseMotionListener(newMouseMotionAdapter(){publicvoidmouseMoved(Mousep){m=e.getX();n=e.getY();}{{U}}repaint(){{/U}}{}});}publicvoidpaint(Graphicsg){g.drawLine(100,0,100,200);g.drawLine(0,100,200,100);if(m<100g.fillOval(10,10,80,80);}elseif(m>100g.fillRect(110,10,80,80);}elseif(m<100g.fillArc(10,130,80,35,50,250);}elseif(m>100intyvals[]={120,170,170};g.setColor(Color.yellow);g.fillPolygon(xvals,yvals,3);}}}{{B}}ex35_3.html:{{/B}}<html><head><title>ASimpleProgram</title></head><body><appletcode="AppletPrograming.class"width=800height=400></applet></body></html>
问答题本程序的功能是展示四种不同的对话框。窗口中有四个按钮:“消息”、“输入”、“确认”和“选择”,单击任意一个按钮,就能弹出一个对应的对话框。其中,“消息”对话框只有一个提示信息和一个“确定”按钮,“输入”对话框中有一个供输入的文本框和“确定”和“撤销”两个按钮,“确认”对话框中有一个提示信息和“是”、“否”和“撤销”三个按钮,而“选择”对话框中有一个提示信息和“确定”和“取消”两个按钮,如图所示。importjavax.swing.*;importjava.awt.event.*;importjava.awt.*;publicclassexam_7extendsJFrameimplementsButtonSelectedActionListener{JButtonbtnMessage=newJButton("消息");JButtonbtnInput=newJButton("输入");JButtonbtnConfirm=newJButton("确认");JButtonbtnOption=newJButton("选择");publicexam_7(){super("exam_7");btnMessage.addActionListener(this);btnInput.addActionListener(this);btnConfirm.addActionListener(this);btnOption.addActionListener(this);getContentPane().setLayout(newFlowLayout());getContentPane().add(btnMessage);getContentPane().add(btnInput);getContentPane().add(btnConfirm);getContentPane().add(btnOption);addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEvente){System.exit(0);}});}publicstaticvoidmain(Stringargs[]){exam_7fr=newexam_7();fr.pack();fr.setVisible(true);}publicvoidactionperformed(ActionEvente){Object[]opt={"确认","取消"};JButtoninstance=(JButton)e.getObject();if(instance==btnMessage)JOptionPane.showMessageDialog(this,"消息对话框");elseif(instance==btnInput)JOptionPane.showInputDialog(this,"输入对话框");elseif(instance==btnConfirm)JOptionPane.showConfirmDialog(this,"确认对话框");elseJOptionPane.showOptionDialog(this,"选择对话框","选择",JOptionPane.YES_OPTION,JOptionPane.QUESTION_MESSAGE,null,opt,opt[1]);}}
问答题下面程序是判断某一个是否为闰年,请改正程序中的错误(有下划线的语句),使程序能输出正确的结果。(闰年的条件是符合下面两者之一:①能被4整除,但不能被100整除;②能被4整除,又能被100整除)。
注意:不改动程序的结构,不得增行或删行。
import java.io.*;
public class LeapYear
{
public static void main(String args[])
{
int year=1979;
if({{U}}(year %4= =0 || year % 100 !=0) || (year % 400= =0){{/U}})
System.out.println(year+"是闰年.");
else
System.out.println(year+"不是闰年。");
year=2000;
boolean leap;
if({{U}}year % 4 !=0{{/U}})
leap=false;
else if(year % 100 !=0)
leap=true;
else if(year % 400 !=0)
leap=false;
else
leap=true;
if(______)
System.out.println(year+"是闰年。");
else
System.out.println(year+"不是闰年。");
year=2010;
if(year % 4= =0)
{
if(year % 100= =0)
{
if(year % 400= =0)
leap=true;
else
______
}
else
leap=false;
}
if(1eap= =true);
System.out.println(year+"是闰年。");
else
System.out.println(year+"不是闰年。");
}
}
问答题本程序的功能是在文本域面板中添加一个带有行数的面板。窗口中有一个文本域,在文本域的左侧有一个带有数字的面板,该面板上的数字指示着文本域中的行数,如图所示。importjavax.swing.*;importjavax.swing.event.*;importjava.awt.*;publicclassexam_89extendsJFrame{publicstaticJTextPanetextPane;publicstaticJScrollPanescrollPane;JPanelpanel;publicexam_89(){super("exam_89()");panel=newJPanel();panel.setLayout(newBorderLayout());panel.setBorder(BorderFactory.createEmptyBorder(20,20,20,20));textPane=newJTextPane();textPane.setFont(newFont("monospaced",Font.PLAIN,12));scrollPane=newJScrollPane(textPane);panel.add(scrollPane);scrollPane.setPreferredsize(newDimension(300,250));setContentPane(panel);setCloseOperation(JFrame.EXIT_ON_CLOSE);LineNumberlineNumber=newLineNumber();scrollPane.setRowHeaderView(lineNumber);}publicstaticvoidmain(String[]args){exam_89ttp=newexam_89();ttp.pack();ttp.setVisible(true);}}
问答题下面是一个Applet程序,其功能是在图形用户界面中实现打印任意行数的杨辉三角形,编写一个含有整型形式参数的方法,此方法打印杨辉三角形,打印的行数由形式参数指定。编写Applet程序接受用户输入的整数,作为实际参数传递并调用上面的方法,要求图形用户界面上包含一个文本区作为输入参数的入口,一个带有纵向滚动条的文本面板作为输出打印结果的区域,一个按钮,点击则完成打印。请改正程序中的错误(有下划线的语句),使程序能输出正确的结果。注意:不改动程序的结构,不得增行或删行。程序运行结果如下:importjavax.swing.*;importjava.awt.*;importjava.awt.event.*;/*<appletcode="exll_3.class"width=800height=400></applet>*/publicclassexll_3extendsJAppletprivateJButtonjb=newJButton("OK");privateJTextPanejtp=newJTextPane();privateJTextFielditl=newJTextField(8);privateintn=15;publicvoidinit()jb.addActionListener(newActionListener()publicvoidactionPerformed(ActionEvente)n=jtf.getText();jtp.setText("");for(inti=n-1;i>=0;i--)Strings="";for(intj=0;j<i;j++)s+="";for(intj=0;j<n;j++)s+="*";jtp.setText(s+"/n"););Containercp=getContentPane();cp.add(BorderLayout.NORTH,jtf);cp.add(BorderLayout.CENTER,newJScrollPane(jtp));cp.add(BorderLayout.SOUTH,jb);publicstaticvoidmain(String[]args)ex11_3obj11_3=newex11_3();Stringstr=obj11_3.getClass().toString();if(str.indexOf("class")!=-1)str=str.substring(6);JFramefrm=newJFrame(str);frm.addWindowListener(newWindowAdapter()publicvoidwindowClosing(WindowEventwe)System.exit(0););frm.getContentPane().add(obj11_3);frm.setsize(300,500);frm.setVisible(true);ex11_3.html<HTML><HEAD><TITLE>ex11_3</TITLE></HEAD><BODY><appletcode="ex11_3.class"width=800height=400></applet></BODY></HTML>
问答题本程序中使用了选项卡。窗口中有“系统”、“声卡”、“显示卡”、“网卡”和“帮助”5个选项卡,单击各个选项卡的文字标签后,所选中的选项卡将为当前选项卡。“系统”选项卡上有3个复选框,选中某个按钮后,它所指示的选项卡就可用,否则不可用,如图所示。importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;importjavax.swing.event.*;publicclassexam_40extendsJFrame{JTabbedPaneconfig=newJTabbedPane();publicexam_40(){super("exam_40");setSize(500,300);setDefaultCloseOperation(EXIT_ON_CLOSE);JPanelconfigPane=newJPanel();configPane.setLayout(newBoxLayout(configPane,BoxLayout.Y_AXIS));JTextAreaquestion=newJTextArea("下面的哪个选项/n"+"你想设置?");question.setEditable(false);question.setMaximumSize(newDimension(300,50));question.setAlignmentX(0.0f);question.setBackground(configPane.getBackground());JCheckBoxaudioCB=newJCheckBox("声卡",true);JCheckBoxnicCB=newJCheckBox("网卡",true);JCheckBoxtvCB=newJCheckBox("显示卡",false);configPane.add(Box.createVerticalGlue());configPane.add(question);configPane.add(audioCB);configPane.add(nicCB);configPane.add(tvCB);configPane.add(Box.createVerticalGlue());JLabelaudioPane=newJLabel("声卡页面");JLabelnicPane=newJLabel("网卡页面");JLabeltvPane=newJLabel("显示卡页面");JLabelhelpPane=newJLabel("帮助信息");audioCB.addItemListener(newTabManager(audioPane));nicCB.addItemListener(newTabManager(nicPane));tvCB.addItemListener(newTabManager(tvPane));config.addTab("系统",null,configPane,"ChooseInstalledOptions");config.addTab("声卡",null,audioPane,"Audiosystemconfiguration");config.addTab("网卡",null,nicPane,"Networkingconfiguration");config.addTab("显示卡",null,tvPane,"Videosystemconfiguration");config.addTab("帮助",null,helpPane,"HowDoI...");getContentPane().add(config,BorderLayout.CENTER);}classTabManagerimplementsActionListener{Componenttab;publicTabManager(ComponenttabToManage){tab=tabToManage;}publicvoidItemStateChanged(ItemEventie){intindex=config.indexOfComponent(tab);if(index!=-1){config.setEnabledAt(index,ie.getStateChange()==ItemEvent.SELECTED);}this.repaint();}}publicstaticvoidmain(Stringargs[]){exam_40sc=newexam_40();sc.setVisible(true);}}
问答题基本操作题
下列程序中,给出两个整数2和3,分别求2除以3和2乘以3的结果,要求调用类ex1_1的方法method( )来输出相应的结果,请将程序补充完整。程序运行结果如下:
0.6666666666666666
6
public class ex1_1 {
public static void main(String[] args) {
int n1=2,n2=3;
ex1_1 obj1_1=new ex1_1();
obj1_1. ;
}
public void method(int x,int y){
System.out.println( );
System.out.println( );
}
}
问答题本程序的功能是用带有刻度的滑动条来控制字体的大小。窗口中有一个文字标签和一个名称为“Fontsize”的面板,面板上有一个刻度范围为8~36的滑动条,拖动滑动条上的滑块时,文字标签上的文本大小会随之改变,如图所示。importjavax,swing.*;importjavax.swing.event.*;importjava.awt.*;importjava.awt.event.*;publicclassexam_9extendsJFrame{privateJLabeltext=newJLabel("SampleText",JLabel.CENTER);privatestaticfinalintMINPOINTS=8;privatestaticfinalintMAXPOINTS=36;privateJSlidersizer=newJSlider();publicexam_9(StringtitleText){super(titleText);getContentPane().add(text,BorderLayout.CENTER);setDefaultCloseOperation(JFrame.EXITONCLOSE);sizer.setMinmum(MINPOINTS);sizer.setMaximum(MAXPOINTS);sizer.setValue(text.getFont().getSize());sizer.setMajorTickSpacing(4);sizer.setMinorTickSpacing(1);sizer.setPaintLabels(true);sizer.setPaintTicks(true);sizer.addChangeListener(newListener(){publicvoidstateChanged(ChangeEvente)Fontof=text.getFont();Fontnf=newFont(of.getName(),of.getStyle(),sizer.getValue());text.setFont(nf);text.repaint();}});JPanelsliderPanel=newJPanel(newBorderLayout());sliderPanel.setBorder(BorderFactory.createBorder(_"Font_size"_)__);sliderPanel.add(sizer,BorderLayout.SOUTH);getContentPane().add(sliderPanel,BorderLayout.SOUTH);setSize(250,175);setVisible(true);}publicstaticvoidmain(String[]args){newexam_9("exam_9");}}
问答题本题程序的功能是主窗口有一个按钮、一个文本域和一个复选框,初始时窗口的大小是不能调整的,勾选复选框后,窗口大小就可以进行调整,如果取消勾选复选框,则窗口的大小又不能调整,单击按钮可以关闭程序。请将下述程序补充完整(注意:不得改动程序的结构,不得增行或删行)。
import java.awt.*;
import java.awt.event.*;
class MyFrame extends Frame______
{
Checkbox box;
TextArea text;
Button button;
MyFrame(String s)
{
super (s);
box = new Checkbox("设置窗口是否可调整大小");
text = new TextArea(12,12);
button = new Button("关闭窗口");
button.addActionListener(this);
box.addItemListener(this);
setBounds(100,100,200,300);
setVisible(true);
add(text,BorderLayout.CENTER);
add(box,BorderLayout.SOUTH);
add(button,BorderLayout.NORTH);
______;
validate ();
}
public void itemStateChanged(ItemEvent e)
{
if (box.getState() == true)
{
setResizable(true);
}
else
{
setResizable(false);
}
}
public void actionPerformed(ActionEvent e)
{
dispose();
}
}
class simple
{
public static void main(String args[])
{
new MyFrame("simple");
}
}
问答题注意:下面出现的“考生文件夹”均为%USER%。
在考生文件夹中存有文件名为Java_3.java的文件,该程序是不完整的,请在注释行“//**********Found**********”下一行语句的下画线地方填入正确内容,然后删除下画线,请勿删除注释行或改动其他已有语句内容。存盘时文件必须存放在考生文件夹下,不得改变原有文件的文件名。
本题的要求是:
Java_3.java程序是一个二分法检索算法程序,完成程序,然后运行并所得结果为
对ArrayList分类:[白,粉红,紫,红,茶色,蓝,黄,黑]
检索关键字:黑
找到下标7
检索关键字:红
找到下标3
检索关键字:粉红
找到下标1
检索关键字:绿
没有找到<-5>
检索关键字:灰
没有找到<-1>
检索关键字:橙色
没有找到<-1>
给定源程序:
//Java_3.java:二分法检索算法
import java.util.*;
public class Java_3{
private String colors[]={"红","白","蓝",
"黑","黄",
"紫","茶色","粉红"};
private ArrayList aList;//ArrayList引用
public Java_3(){
aList=new ArrayList(Arrays.asList(colors));
Collections.sort(aList);//ArrayList分类
System.out.println("对ArrayList分类:"+aList);
public void printSearchResults(){
printSearchResultsHelper(colors[3]); //首项
printSearchResultsHelper(colors[0]); //中间项
printSearchResultsHelper(colors[7]); //最后项
printSearchResultsHelper("绿"); //最低
printSearchResultsHelper("灰"); //不存在
//*********Found********
printSearchResultsHelper("______"); //不存在
private void printSearchResultsHelper(String key){
int result=0;
System.out.println("/n检索关键字:"+key);
result=Collections.hinarySeareh(aList,key);
System.out.println((result>=0?"找到下标"
+result
:"没有找到("+result+")"));
}
public static void main(String args[]){
//*********Found********
new Java_3().______;
}
}
问答题本程序将数组中arr[]={5,6,3,7,9,1)的各个元素按下标的逆序输出。
public clas s exam_60{
public static void main(String[] args) {
int arr[]={5,6,3,7,9,1};
______;
n=______;
while(n>=0){
System.out.print(arr[n]+" ");
______;
}
}
}
问答题repaint();
问答题本程序中定义了一个带有参数的构造方法exam_40(),并定义一个该类的对象temp。构造方法exam_40()有两个参数:字符串name和整型age。定义对象temp时将字符串“Tom”和整数17传递给构造方法,构造方法将这两个参数打印输出(如Tom is 17 years old.)。
public clas s exam_40 {
String name;
int age;
public static void main(String[] args) {
String name="Tom";
int age=17;
exam_40 temp=______;
}
public exam_40(String name, ______){
______;
this.age=age;
System.out.println(name+"is"+age+"years old.");
}
}
问答题本题的功能是定义自已的组件类。窗口中排布着12个按钮,鼠标移动按钮时,按钮背景颜色改变,用鼠标单击按钮时,后台将显示该按钮对应的字符。
问答题下面的程序中,指定变量b为字节型,变量f为单精度实型,变量l为64位整型。
public class exam_99{
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="+l);
}
}
问答题本程序将一个整型变量op1的二进制的低3位全部变成1。
public class exam_53{
public static void main(String[] args) {
______op1=21;
int op2=3;
______res;
______;
System.out.println(res);
}
}
问答题本题程序是一个Applet应用程序,功能是计算前n个自然数的和,程序中用进度条来表示计算的进程。页面中有两个文本框、两个按钮和一个进度条,在第一个文本框中输入要计算的自然数的个数,单击“开始”按钮则开始进行计算,进度条同步显示计算完成的情况。程序中存在若干错误,请找出并改正(注意:不得改动程序的结构,不得增行或删行)。
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
public class advance extends Japplet
{
Container container = null;
JButton startButton,stopButton;
JTextField inputTextField,outputTextField;
JProgressBar pBar = null;
Timer timer = null;
static int sum = 0;
static int counter = 0;
public void init()
{
container = this.getContentPane();
container.setLayout(new GridLayout(3,1));
Box hboxl = Box.createHorizontalBox();
container.add(hboxl);
hboxl.add(Box.createHorizontalGlue());
JLabel labell = new JLabel("前",JLabeI.LEFT);
labell.setFont(new Font("Dialog",Font.PLAIN,15));
hboxl.add(labell);
inputTextField = new JTextField("100",4);
hboxl.add(inputTextField);
JLabel label2 = new JLabel("个自然数和是",JLabel.LEFT);
label2.setFont(new Font("Dialog",Font.PLAIN,15));
hboxl.add(label2);
outputTextField = new JTextField(10);
hboxl.add(outputTextField);
hboxl.add(Box.createHorizontalGlue());
Box hbox2 = Box.createHorizontalBox();
container.add(hbox2);
startButton : new JButton("开始");
startButton.addActionListener(new ButtonListener());
hbox2.add(Box.createHorizontalGlue());
hbox2.add(startButton);
hbox2.add(Box.createHorizontalGlue());
stopButton = new JButton("结束");
stopButton.addActionListener(new ButtonListener() ;
hbox2.add(Box.createHorizontalGlue());
hbox2.add(stopButton);
hbox2.add(Box.createHorizontalGlue());
pBar = new JProgressBar();
pBar.setStringPainted(true);
Border border = BorderFactory.createLineBorder(Color.red,2);
pBar.setBorder(border);
pBar.setBackground(Color.white);
pBar.setForeground(Color.blue);
pBar.setMinimum(0);
pBar.setMaximum(Integer.parseInt(inputTextField.getText()));
container.add(pBar);
timer = new Timer(0,TimerListener());
}
}
class TimerListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
if (Integer.parseInt (inputTextField.getText ()) > 0)
{
counter ++;
sum = sum+counter;
pBar.setValue (counter);
outputTextField,setText (Integer.toString (sum));
}
else
outputTextField,setText ("0");
}
if (counter >= Integer.parseInt(inputTextField.getText ()))
timer,stop ();
}
}
class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
JButton button = e.getSource();
if (button.getText () == "开始")
{
outputTextField,setText ("");
if (inputTextField.getText() != " ")
{
pBar.setMaximum (Integer.parseInt (inputTextField.getText ()) );
sum = 0 ;
counter = 0;
timer.begin ();
}
}
else
if (button.getText () =="结束")
{
timer,stop ();
outputTextField,setText ("");
sum = 0;
counter = 0;
pBar.setValue(0);
}
}
}
问答题本程序的功能是依次输出字符'4'、'2'和'7'。
public class exam_25{
public static void main(String[] args){
char c='1';
______;
System.out.println(c);
______;
System.out.println(c);
______;
System.out.println(c);
}
}
