Java二级操作题第43套

基本操作

在考生文件夹中存有文件名为Java_1.java的文件,该程序是不完整的,请在注释行“//Found”下一行语句的下划线地方填入正确内容,然后删除下划线,请勿删除注释行或改动其他已有语句内容。存盘时文件必须存放在考生文件夹下,不得改变原有文件的文件名。

下列程序的功能是在一个字符数组中查找指定的字符,然后将该字符在数组中出现过的所有位置以及出现的次数输出。程序的运行结果如下。请在横线处填入正确的代码,使程序完整并能正确运行。
Found A at index 0
Found A at index 2
The total amount of A is 2

public class Java_1 {
    public static void main(String[] args) {
       //**********found**********
        _________ arrayOfChars = { 'A', 'B', 'A','D', 'E','F','G' };
        char searchfor = 'A';
        
        int i=0, j=0;
       //**********found**********
        while ( i < arrayOfChars.__________) {
            if (arrayOfChars[i] == searchfor) {
                System.out.println("Found " + searchfor + " at index " + i);
                j++;
	    }
       //**********found**********
	    ________________ ;
        }
       //**********found**********
        if (_______) {
	    	System.out.println("The total amount of " + searchfor + " is " + j );
        } else {
	    	System.out.println(searchfor + " is not in the array");
        }
    }
}

本题着重考查考生对Java语言基本语法的掌握情况。
本题中的第一个空格:arrayOfChars是一个字符型数组,对其应该用char[]进行定义,此处填入char[];
本题中的第二个空格:遍历arrayOfChars数组,应该以arrayOfChars的长度作为最大遍历值,此处填入length;
本题中的第三个空格:i作为变量和arrayOfChars长度比较,因此i需要自增1,此处填入i++;
本题中的第四个空格:根据代码逻辑可知此处应该是字符数组里存在要查找的字符,可以根据j是否增加来判断,此处填入j>0;

本评析仅作参考。

public class Java_1 {
    public static void main(String[] args) {
       //**********found**********
        char[] arrayOfChars = { 'A', 'B', 'A','D', 'E','F','G' };
        char searchfor = 'A';
        
        int i=0, j=0;
       //**********found**********
        while ( i < arrayOfChars.length) {
            if (arrayOfChars[i] == searchfor) {
                System.out.println("Found " + searchfor + " at index " + i);
                j++;
	    }
       //**********found**********
	    i++ ;
        }
       //**********found**********
        if (j>0) {
	    	System.out.println("The total amount of " + searchfor + " is " + j );
        } else {
	    	System.out.println(searchfor + " is not in the array");
        }
    }
}

简单应用

在考生文件夹中存有文件名为Java_2.java的文件,该程序是不完整的,请在注释行“//Found”下一行语句的下划线地方填入正确内容,然后删除下划线,请勿删除注释行或改动其他已有语句内容。存盘时文件必须存放在考生文件夹下,不得改变原有文件的文件名。

下列程序包含了两个线程,分别打印字符串"Good Morning"和”Hello”。程序某次运行的结果如下。请在横线处填入正确的代码,使程序完整并能正确运行。
Good Morning0
Good Morning1
Main waiting for Hello!
Hello0
Hello1
Hello2
Hello3
Hello4
Good Morning2
Good Morning3
Good Morning4

//**********found**********
public class Java_2 extends ____________ {
    public static void main(String[ ] args) throws Exception{
	
	int i=0;
    //**********found**********
	Thread t = new ______________( );
    //**********found**********
	____________________;
	
	while( true){
		System.out.println("Good Morning"+ i++);
		if (i == 2 && t.isAlive()){
		   	System.out.println("Main waiting for Hello!");
                //**********found**********
		        ______________;  //等待线程t运行结束
	        } 
              //**********found**********
		if (i==5) ______________;
	}
	 
    }
    //**********found**********
    public void ______________{
   	int i =0;
	while( true){
		System.out.println("Hello"+ i++);
		if (i==5)  break ;
	}
    }
}

本题着重考查考生对Java语言线程的掌握情况。
本题中的第一个空格:使用线程时,关键字extends对应的是类,因此此处填入Thread;
本题中的第二个空格:根据题干,程序包含两个线程,因此此处应该再开一个线程,填入Java_2;
本题中的第三个空格:线程创建完成后需要启动才能开始工作,因此此处填入t.start();
本题中的第四个空格:根据注释,需要等待t线程运行完,因此此处填入t.join(),阻塞线程;
本题中的第五个空格:根据题干,只输出五组值,因此此处填入break;
本题中的第六个空格:线程的运行主体在run函数中,因此此处填入run();

本评析仅作参考。

//**********found**********
public class Java_2 extends Thread {
    public static void main(String[ ] args) throws Exception{
	
	int i=0;
    //**********found**********
	Thread t = new Java_2( );
    //**********found**********
	t.start();
	
	while( true){
		System.out.println("Good Morning"+ i++);
		if (i == 2 && t.isAlive()){
		   	System.out.println("Main waiting for Hello!");
                //**********found**********
		        t.join();  //等待线程t运行结束
	        } 
              //**********found**********
		if (i==5) break;
	}
	 
    }
    //**********found**********
    public void run(){
   	int i =0;
	while( true){
		System.out.println("Hello"+ i++);
		if (i==5)  break ;
	}
    }
}

综合应用

在考生文件夹中存有文件名为Java_3.java的文件,该程序是不完整的,请在注释行“//Found”下一行语句的下划线地方填入正确内容,然后删除下划线,请勿删除注释行或改动其他已有语句内容。 存盘时文件必须存放在考生文件夹下,不得改变原有文件的文件名

下列程序运行后显示下图所示界面。可在窗口的空白处输入字符,按“保存”按扭后,程序将空白处清空并把所输入的字符保存到当前目录下的out.txt文件中,再按“读取”按钮会将刚才输入的字符显示出来。请在横线处填入正确的代码,使程序完整并能正确运行。

import java.io.*;
import java.awt.*;
import java.awt.event.* ;
//**********found**********
import  ______________.*;

public class Java_3 implements ActionListener { 
   
    JTextArea ta;
    JFrame f ;
    JLabel label;
    JButton bs,br;
   
    public static void main(String[ ] args){
          Java_3 t = new Java_3();
	  t.go();
    }
	
    void go(){
	  f = new JFrame("Save data");
	  f.setSize( 20, 400);
      //**********found**********
	  f.______________(new FlowLayout());
	  label = new JLabel("请输入需要保存的文本 :");
	  ta = new JTextArea(3,20);
	  bs = new JButton("保存");
	  br = new JButton("读取");
	  f.add(label);
	  f.add(ta);
	  f.add(bs);
	  f.add(br);
      //**********found**********
	  bs.addActionListener(________________);
      //**********found**********
	  br.addActionListener(________________); 
	  
	  f.setSize(400,400);
	  f.pack( );
	  f.setVisible(true) ;
	  f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);	
    }
	
    public void actionPerformed(ActionEvent event){
          try{
              FileWriter  out = new FileWriter("out.txt");
              String str = ta.getText();
             //**********found**********
             out.____________(str);  
             out.close();
          } catch( Exception e){
          }
       	  ta.setText(" ");
     }  
     
    class ReadFile implements ActionListener{
       public void actionPerformed(ActionEvent event){
       	   String cc;
       	   StringBuffer str = new StringBuffer("");
           try{
               FileReader  in = new FileReader("out.txt");
               //**********found**********
               BufferedReader bin= new BufferedReader(_____________);      
               while ( (cc = bin.readLine())!= null)
                   str.append(cc);
               bin.close();
               ta.setText(str.toString());
           } catch( Exception e){ }
      } 
   } 
 }

本题着重考查考生对Java语言组件和文件流的掌握情况。
本题中的第一个空格:使用JFrame时,需要引入swing包,此处填入javax.swing;
本题中的第二个空格:括号里是新建的FlowLayout对象,要将该对象加入到frame中使用setLayout方法,此处填入setLayout;
本题中的第三个空格:保存是当前对象的保存,不需要new新的实例,可以使用this关键字代替,此处填入this;
本题中的第四个空格:读取的事件不在本类中,需要新建ReadFile类的实例,此处填入new ReadFile();
本题中的第五个空格:将str输出到out.txt中,采用write方法实现,此处填入write;
本题中的第六个空格:BufferedReader需要传入Reader对象,in为实例化的FileReader对象,传入即可,此处填入in;

本评析仅作参考。

import java.io.*;
import java.awt.*;
import java.awt.event.* ;
//**********found**********
import javax.swing.*;

public class Java_3 implements ActionListener { 
   
    JTextArea ta;
    JFrame f ;
    JLabel label;
    JButton bs,br;
   
    public static void main(String[ ] args){
          Java_3 t = new Java_3();
	  t.go();
    }
	
    void go(){
	  f = new JFrame("Save data");
	  f.setSize( 20, 400);
      //**********found**********
	  f.setLayout(new FlowLayout());
	  label = new JLabel("请输入需要保存的文本 :");
	  ta = new JTextArea(3,20);
	  bs = new JButton("保存");
	  br = new JButton("读取");
	  f.add(label);
	  f.add(ta);
	  f.add(bs);
	  f.add(br);
      //**********found**********
	  bs.addActionListener(this);
      //**********found**********
	  br.addActionListener(new ReadFile()); 
	  
	  f.setSize(400,400);
	  f.pack( );
	  f.setVisible(true) ;
	  f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);	
    }
	
    public void actionPerformed(ActionEvent event){
          try{
              FileWriter  out = new FileWriter("out.txt");
              String str = ta.getText();
             //**********found**********
             out.write(str);  
             out.close();
          } catch( Exception e){
          }
       	  ta.setText(" ");
     }  
     
    class ReadFile implements ActionListener{
       public void actionPerformed(ActionEvent event){
       	   String cc;
       	   StringBuffer str = new StringBuffer("");
           try{
               FileReader  in = new FileReader("out.txt");
               //**********found**********
               BufferedReader bin= new BufferedReader(in);      
               while ( (cc = bin.readLine())!= null)
                   str.append(cc);
               bin.close();
               ta.setText(str.toString());
           } catch( Exception e){ }
      } 
   } 
 }
posted @   槑孒  阅读(399)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
点击右上角即可分享
微信分享提示