Java二级操作题第37套
基本操作
在考生文件夹中存有文件名为Java_1.java的文件,该程序是不完整的,请在注释行“//Found”下一行语句的下划线地方填入正确内容,然后删除下划线,请勿删除注释行或改动其他已有语句内容。存盘时文件必须存放在考生文件夹下,不得改变原有文件的文件名。
本题的要求是:
完善该程序并进行调试,使程序输出结果如下: 

import javax.swing.*;
public class Java_1{
   public static void main( String args[] ){
      //*********Found**********
      StringBuffer buf = new __________________( "Hello, how are you?" );
      String output = "buf = " + buf.toString() +
                      "\nlength = " + buf.length() +
                      "\ncapacity = " + buf.capacity();
      buf.ensureCapacity( 75 );
      output += "\n\nNew capacity = " + buf.capacity();
      buf.setLength( 10 );
      //*********Found**********
      __________ += "\n\nNew length = " + buf.length() +
                "\nbuf = " + buf.toString();
      JOptionPane.showMessageDialog( null, output,
         "字符串缓存长度和容量的实例",
      //*********Found**********
         _______________.INFORMATION_MESSAGE );
      //*********Found**********
      System.___________________( 0 );
   }
}
本题着重考察考生对类的使用和JOptionPane的理解情况。
本题中的第一个空格:buf为StringBuffer类型,因此在新建对象时应该new出StringBuffer类型,因此此处空格填入StringBuffer;
本题中的第二个空格:output作为输出的字符串变量,因此要将所有输出的内容存入output,因此此处空格填入output;
本题中的第三个空格:JOptionPane.showMessageDialog最后一个参数为图标类型,类型一般为JOptionPane,因此此处空格填入JOptionPane;
本题中的第四个空格:退出虚拟机程序一般使用"System.exit(0)",因此此处空格填入exit。
本评析仅作参考。
import javax.swing.*;
public class Java_1{
   public static void main( String args[] ){
      //*********Found**********
      StringBuffer buf = new StringBuffer( "Hello, how are you?" );
      String output = "buf = " + buf.toString() +
                      "\nlength = " + buf.length() +
                      "\ncapacity = " + buf.capacity();
      buf.ensureCapacity( 75 );
      output += "\n\nNew capacity = " + buf.capacity();
      buf.setLength( 10 );
      //*********Found**********
      output += "\n\nNew length = " + buf.length() +
                "\nbuf = " + buf.toString();
      JOptionPane.showMessageDialog( null, output,
         "字符串缓存长度和容量的实例",
      //*********Found**********
         JOptionPane.INFORMATION_MESSAGE );
      //*********Found**********
      System.exit( 0 );
   }
}
简单应用
在考生文件夹中存有文件名为Java_2.java的文件,该程序是不完整的,请在注释行“//Found”下一行语句的下划线地方填入正确内容,然后删除下划线,请勿删除注释行或改动其他已有语句内容。存盘时文件必须存放在考生文件夹下,不得改变原有文件的文件名。
本题的要求是:
程序中定义了二维点的类,类中定义了包括点的平移在内的部分方法。完善程序,使其运行 时的输出结果如下:
点的当前坐标:(5,5)
平移到:(8,9)
class Point{
   public int x,y;
   public Point() { 
   }
   public Point(int x,int y){
      this.x = x;
      this.y = y;
   }
   //*********Found**********
   public Point(____________ p){
      x = p.x;
      y = p.y;
   }
   public int getX(){
      return x;
   }
   public int getY(){
      return y;
   }
   public void moveTo(int x,int y){
      this.x = x;
      this.y = y;
   }
   public void moveTo(Point p){
      x = p.x;
      y = p.y;
   }
   public String toString(){
      return "("+ x + ","+ y + ")";
   }
   public void translate(int dx,int dy){ //平移
      this.x += dx;
      //*********Found**********
      ____________;
   }
}
public class Java_2 {
   public static void main(String args[]){
      //*********Found**********
      Point p = new ____________(5,5);
      System.out.println("点的当前坐标:("+p.x + "," + p.y+")");
      p.translate(3,4);
      //*********Found**********
      System.out.println("平移到:"+____________());
   }
}
本题着重考察考生对类的使用的理解情况。
本题中的第一个空格:p有两个参数:x和y,程序中有这两个参数的类只有Point,因此此处空格填入Point;
本题中的第二个空格:由于是平移,因此对x和y坐标的改变应该是一致的,因此此处空格填入this.y += dy;
本题中的第三个空格:p为坐标为(5,5)的新点,需要新建对象,因此此处空格填入Point;
本题中的第四个空格:输出结果为p平移之后的坐标值,Point提供了toString()方法可以输出当前的坐标值,因此此处空格填入p.toString。
本评析仅作参考。
class Point{
   public int x,y;
   public Point() { 
   }
   public Point(int x,int y){
      this.x = x;
      this.y = y;
   }
   //*********Found**********
   public Point(Point p){
      x = p.x;
      y = p.y;
   }
   public int getX(){
      return x;
   }
   public int getY(){
      return y;
   }
   public void moveTo(int x,int y){
      this.x = x;
      this.y = y;
   }
   public void moveTo(Point p){
      x = p.x;
      y = p.y;
   }
   public String toString(){
      return "("+ x + ","+ y + ")";
   }
   public void translate(int dx,int dy){ //平移
      this.x += dx;
      //*********Found**********
      this.y += dy;
   }
}
public class Java_2 {
   public static void main(String args[]){
      //*********Found**********
      Point p = new Point(5,5);
      System.out.println("点的当前坐标:("+p.x + "," + p.y+")");
      p.translate(3,4);
      //*********Found**********
      System.out.println("平移到:"+p.toString());
   }
}
综合应用
在考生文件夹中存有文件名为Java_3.java的文件,该程序是不完整的,请在注释行“//Found”下一行语句的下划线地方填入正确内容,然后删除下划线,请勿删除注释行或改动其他已有语句内容。存盘时文件必须存放在考生文件夹下,不得改变原有文件的文件名。
本题的要求是:
在标题为“Button Test”的窗口中显示一个按钮,当点击该按钮时将弹出另一个标题为“An Other”窗口(如图所示)。运行结果为: 

import java.awt.*;
import java.awt.event.* ;
     //*********Found********
import ___________________;   
     //*********Found********
public class Java_3 ________________ ActionListener{
    public static void main(String args[ ]){
        Java_3 tb = new Java_3();
     //*********Found********
        JFrame f = new JFrame("______________");  
        f.setSize(200,100);
        f.setLayout(new FlowLayout(FlowLayout.CENTER));
        JButton b = new JButton("Press the Button!");  /////JButton
     //*********Found********
        b._________________(tb); 
     //*********Found********
        ________.add(b);
        f.addWindowListener(new WindowAdapter(){
              public void windowClosing(WindowEvent e){
                 System.exit(0);
              }
        });
        f.setVisible(true) ;
    }
     //*********Found********
    public void ________________(ActionEvent e){
        JFrame fr = new JFrame("An Other");   
        fr.setBackground(Color.green);
        fr.add(new JLabel("This frame shows when "+"pressing the button in Button Test"));
        fr.addWindowListener(new WindowAdapter(){
           public void windowClosing(WindowEvent e){
              System.exit(0);
           }
        });
        fr.pack();
        fr.setVisible(true) ;
    } 
}
本题着重考察考生对程序监听的理解情况。
本题中的第一个空格:JFrame为swing组件下的类型,因此使用之前要导入swing包,因此此处空格填入javax.swing.*;
本题中的第二个空格:ActionListener为接口,要继承接口使用implements关键字,因此此处空格填入implements;
本题中的第三个空格:图中窗口的标题栏为Button Test,在创建JFrame时直接确定标题,因此此处空格填入Button Test;
本题中的第四个空格:按钮创建完成后需要添加监听,因此此处空格填入addActionListener;
本题中的第五个空格:按钮创建完成后要添加到容器中,因此此处空格填入f;
本题中的第六个空格:按钮点击后要弹出另外一个窗口,因此要对按钮的点击监听填写完整的事件,程序中事件已经完成编写,因此此处空格填入actionPerformed。
本评析仅作参考。
import java.awt.*;
import java.awt.event.* ;
     //*********Found********
import javax.swing.*;   
     //*********Found********
public class Java_3 implements ActionListener{
    public static void main(String args[ ]){
        Java_3 tb = new Java_3();
     //*********Found********
        JFrame f = new JFrame("Button Test");  
        f.setSize(200,100);
        f.setLayout(new FlowLayout(FlowLayout.CENTER));
        JButton b = new JButton("Press the Button!");  /////JButton
     //*********Found********
        b.addActionListener(tb); 
     //*********Found********
        f.add(b);
        f.addWindowListener(new WindowAdapter(){
              public void windowClosing(WindowEvent e){
                 System.exit(0);
              }
        });
        f.setVisible(true) ;
    }
     //*********Found********
    public void actionPerformed(ActionEvent e){
        JFrame fr = new JFrame("An Other");   
        fr.setBackground(Color.green);
        fr.add(new JLabel("This frame shows when "+"pressing the button in Button Test"));
        fr.addWindowListener(new WindowAdapter(){
           public void windowClosing(WindowEvent e){
              System.exit(0);
           }
        });
        fr.pack();
        fr.setVisible(true) ;
    } 
}

 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号