北京大学国家Java自学考试09年6月1日下午上机实践题

由于是自己做,因此答案只做参考
 
一、素数和程序 30分
计算数值n的素数和,其要求是将小于n(包括n本身)的所有数值的素数相加求和,但必须要通过命令行参数、或者输入流、或者是对话框形式来展现,并保存为PSum.java名
 
二、鼠标事件测试 40分
建立一个文本区,该文本用于测试鼠标的操作,当鼠标进入文本区时,文本区显示 鼠标进入 ;当鼠标离开时,显示 鼠标离开 ;当鼠标按下时,显示 鼠标按下 ;当鼠标是方法时,显示 鼠标释放
保存为MEvt.java
 
三、字符串集合类 30分
以自己的角度建立一个MSet类,该类是用于存储字符串的集合类,并实现相应的操作,保存为MSet.java
 
PSum.java
 
import java.util.Vector;
import java.util.Iterator;
import java.util.Scanner;
import java.util.InputMismatchException;
import java.util.NoSuchElementException;
import java.lang.IllegalStateException;

/**
* 该类是一个计算素数和的类
* @author sunspot
*/
public class PSum {
   /**
  * 用来判断n是否为素数
  * @param n 数值
  * @return 如果为素数,返回true,否则返回false
  */
   public static boolean isPrimeNumber( int n) {
     for( int i = 2;i< n/2+1;i++)    
       if(n % i == 0)    
         return false;

     return true;
  }

   /**
  * 获得n数值以内所有数的素数集合
  * @param n 数值
  * @return 存储了数值以内所有的素数
  */
   public static Vector getPrimeNumbers( int n) {
    Vector temp = new Vector();
     for( int i = 1;i<=n;i++)    
       if(isPrimeNumber(i))    
        temp.add( new Integer(i));
    
     return temp;
  }

   /**
  * 打印素数集合和的方法
  * @param pn 素数集合
  *
  */
   public static void printPrimeNumberSum(Vector pn) {
    Iterator it = pn.iterator();
     int count = 1;
     int result = 0;
     while(it.hasNext()) {
       int temp = ((Integer)it.next()).intValue();
      result += temp;
       if(count != pn.size())
        System.out.print(temp+ "+");
       else    
        System.out.print(temp+ "=");
      count++;
    }
    System.out.println(result);
  }

   public static void main(String[] args) {
    System.out.println( "欢迎进入素数求和程序--sunspot");
     int temp = 0;
    String quit = "";
     boolean isGoon = true;
     while( true) {
      Scanner in = new Scanner(System.in);
      System.out.print( "请输入要分析的数值:");
       try{
        temp = in.nextInt();
        isGoon = true;
      } catch (InputMismatchException i) {
        System.out.println( "***请输入正确的数值!***");
        isGoon = false;
      } catch (NoSuchElementException n) {
        System.out.println( "***请输入正确的数值!***");
        isGoon = false;
      } catch (IllegalStateException ii) {
        System.out.println( "***输入流已经关闭!***");
        isGoon = false;
      }    
        
       if (isGoon) {
        System.out.println( "数值计算结果如下:");
        printPrimeNumberSum(getPrimeNumbers(temp));
        
        System.out.print( "是否要退出程序?输入quit表示退出,其他表示继续:");
        quit = in.next();
         if(quit.equalsIgnoreCase( "quit")) {
           break;
        }
      }    
    }
  }
}
 
运行效果:
 
MEvt.java
 
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

/**
* 该类是一个鼠标测试类
* @author sunspot
*/
public class MEvt extends JFrame {
   private JLabel title, test;
   private Container con;
   public MEvt(String title) {
     super(title);
     this.initialization();
  }

   public void initialization() {
    title = new JLabel( "这是测试鼠标事件的程序,下面红边框的文本区是测试区");
    test    = new JLabel( " 测试区域 ");
    test.setBorder(BorderFactory.createLineBorder(Color.RED));
    test.addMouseListener( new MouseAction());
    test.setCursor( new Cursor(Cursor.HAND_CURSOR));

    JPanel top = new JPanel( new FlowLayout(FlowLayout.LEFT));
    top.setBackground(Color.WHITE);
    top.add(title);
    top.setBorder(BorderFactory.createMatteBorder(0,0,1,0,Color.BLACK));

    JPanel center = new JPanel( new FlowLayout(FlowLayout.CENTER));
    center.setBackground(Color.WHITE);
    center.add(test);

    con = this.getContentPane();
    con.add(top,BorderLayout.NORTH);
    con.add(center,BorderLayout.CENTER);

     this.pack();
     this.setResizable( false);
     this.setLocation(getCenterPoint( this.getSize()));
  }

   private Point getCenterPoint(Dimension size) {
    Toolkit tk = this.getToolkit();
    Dimension screen = tk.getScreenSize();
     int x = ( int)((screen.getWidth() - size.getWidth())/2);
     int y = ( int)((screen.getHeight()-size.getHeight())/2);
     return new Point(x,y);
  }

   public class MouseAction extends MouseAdapter {
     public void mouseEntered(MouseEvent e) {
       if(e.getSource() == test) {
        test.setText( " 鼠标进入 ");
      }
    }

     public void mouseExited(MouseEvent e) {
       if(e.getSource() == test) {
        test.setText( " 鼠标离开 ");
      }
    }

     public void mousePressed(MouseEvent e) {
       if(e.getSource() == test) {
        test.setText( " 鼠标按下 ");
      }
    }

     public void mouseReleased(MouseEvent e) {
       if(e.getSource() == test) {
        test.setText( " 鼠标释放 ");
      }
    }
  }

   public static void main(String[] args) {
    MEvt m = new MEvt( "鼠标测试程序--宋九璋制作--c158");
    m.setVisible( true);
    m.setDefaultCloseOperation(MEvt.EXIT_ON_CLOSE);
  }
}
 
运行效果:
 
MSet.java
 
/**
*    
* 该类是一个字符串集合类
* @author sunspot
*/
public class MSet {
   /**
  *
  *存储数据的数组
  */
   private String[] strs;

   /**
  *当前已存储的数据个数
  */
   private int length = 0;

   /**
  *容器的总容量
  */
   private int size;

   /**
  * 构造方法,默认容器大小为50
  */
   public MSet() {
    size = 50;
    strs = new String[size];
  }

   /**
  *
  * 构造方法,设定容器大小
  */
   public MSet( int size) {
     this.size = size;
    strs = new String[ this.size];
  }

   /**
  *
  * 获得当前容器中的数据个数
  * @return 数据个数
  */
   public int length() {
     return length;
  }

   /**
    * 向容器尾部添加数据
    * @param str 数据
    * @return 如果成功,返回true
    * @throws IndexOutOfBoundsException    
    */
   public boolean add(String str) throws IndexOutOfBoundsException {
     if(length == size) {
       throw new IndexOutOfBoundsException( "容器已满!");
    }
    strs[length] = str;
    length++;
     return true;
  }
    
   /**
    * 删除i位置的数据
    * @param i 数据位置
    * @return 如果成功,返回true
    * @throws IndexOutOfBoundsException
    */
   public boolean remove( int i) throws IndexOutOfBoundsException {
     if(i < 0 || i > length-1 ) {
       throw new IndexOutOfBoundsException( "下标越界!");
    }
     for( int j = i; j < length-1;j++) {
      strs[j] = strs[j+1];
    }
    length--;
     return true;
  }
    
   /**
    * 删除容器中第一次出现的str
    * @param str 删除的数据
    * @return 如果含有此数据,返回true,否则返回false
    * @throws IndexOutOfBoundsException
    */
   public boolean remove(String str) throws IndexOutOfBoundsException {
     for( int i = 0;i<length;i++) {
       if(strs[i].equals(str)) {
        remove(i);
         return true;
      }
    }
     return false;
  }

   /**
    * 删除容器所有的str数据
    * @param str 删除的数据
    * @return 如果成功,返回true
    * @throws IndexOutOfBoundsException
    */
   public boolean removeAll(String str) throws IndexOutOfBoundsException {
     for( int i = 0; i<length;i++) {
       if(strs[i].equals(str)) {
        remove(i);
      }
    }
     return true;
  }
    
   /**
    * 向容器的i位置插入数据str
    * @param i 位置
    * @param str 数据
    * @return 如果成功,返回true
    * @throws IndexOutOfBoundsException
    */
   public boolean insert( int i, String str) throws IndexOutOfBoundsException {
     if(length == size) {
       throw new IndexOutOfBoundsException( "容器已满!");
    }
     if(i < 0 || i > length) {
       throw new IndexOutOfBoundsException( "下标越界!");
    }
     for( int j = length; j > i; j--) {
      strs[j] = strs[j-1];
    }
    strs[i] = str;
    length++;
     return true;
  }
    
   /**
    * 更新i位置的数据为str
    * @param i 位置
    * @param str 新数据
    * @return 如果成功,返回true
    * @throws IndexOutOfBoundsException
    */
   public boolean update( int i , String str) throws IndexOutOfBoundsException {
     if(i < 0 || i > length-1) {
       throw new IndexOutOfBoundsException( "下标越界!");
    }
    strs[i] = str;
     return true;
  }

   /**
    * 更新第一次出现的str1数据为str2
    * @param str1 原始数据
    * @param str2 新数据
    * @return 如果含有str1数据,返回true,否则返回false
    * @throws IndexOutOfBoundsException
    */
   public boolean update(String str1, String str2) throws IndexOutOfBoundsException {
     for( int i = 0 ;i <length; i++) {
       if(strs[i].equals(str1)) {
        update(i,str2);
         return true;
      }
    }
     return false;
  }

   /**
    * 更新所有的str1数据为str2
    * @param str1 原始数据
    * @param str2 新数据
    * @return 如果成功,返回true
    * @throws IndexOutOfBoundsException
    */
   public boolean updateAll(String str1 , String str2)     throws IndexOutOfBoundsException {    
     for( int i = 0 ; i < length;i++) {
       if(strs[i].equals(str1)) {
        update(i,str2);
      }
    }
     return true;
  }

   /**
    * 获得str数据的位置
    * @param str 数据
    * @return 如果该数据存在,返回位置,否则返回-1
    */
   public int getNodePosition(String str) {
     for( int i = 0; i < length; i++) {
       if(strs[i].equals(str)) {
         return i;
      }
    }
     return -1;
  }

   /**
    * 获得str在容器中出现的所有位置
    * @param str 数据
    * @return str在容器的所有位置数组
    */
   public int[] getNodePositions(String str) {
     int count = 0;
     for( int i = 0; i < length ; i++) {
       if(strs[i].equals(str)) {
        count++;
      }
    }
     int[] temp = new int[count];
    count = 0;
     for( int i = 0;i <length ;i++) {
       if(strs[i].equals(str)) {
        temp[count] = i;
        count++;
      }
    }
     return temp;
  }

   /**
    * 获得i位置的结点
    * @param i 位置
    * @return i位置的数据
    * @throws IndexOutOfBoundsException
    */
   public String getNode( int i) throws IndexOutOfBoundsException {
     if(i < 0 || i > length-1) {
       throw new IndexOutOfBoundsException( "下标越界!");
    }
     return strs[i];
  }

   /**
    * 判断是否包含str数据
    * @param str 数据
    * @return 如果包含,返回true,否则返回false
    */
   public boolean contains(String str) {
     for( int i = 0 ;i < length ;i++) {
       if(strs[i].equals(str)) {
         return true;
      }
    }
     return false;
  }

   /**
    * 判断容器中是否没有数据
    * @return 如果没有数据,返回true,否则返回false
    */
   public boolean isEmpty() {
     if(length > 0) {
       return false;
    }    
     return true;
  }

   /**
    * 清空容器
    *
    */
   public void clear() {
     for( int i = 0 ;i < size;i++) {
      strs[i] = null;
    }
    length = 0;
  }

   /**
    * 重写了Object中的toString方法
    * @return 返回的字符串的格式为[结点位置,结点]
    */
   public String toString() {
    StringBuffer sb = new StringBuffer();
     for( int i = 0 ;i < length; i++) {
      sb.append( "["+i+ ","+strs[i]+ "] ");
    }
     return sb.toString();
  }

   public static void main(String[] args) {
    System.out.println( "*** 测试开始--sunspot ***");
    System.out.println( "一、容器初始化,容器大小为6");
    MSet m = new MSet(6);

    System.out.println();

    System.out.println( "二、测试add(String str)方法,向容器中添加两条数据");
    System.out.println( "添加北京大学是否成功?"+m.add( "北京大学"));
    System.out.println( "添加中国人民大学是否成功?"+m.add( "中国人民大学"));
    System.out.println( "打印数据如下:");
    System.out.println(m);

    System.out.println();

    System.out.println( "三、测试insert(int i , String str)方法,向容器中的第1号位置插入清华大学");
    System.out.println( "插入数据是否成功?"+m.insert(1, "清华大学"));
    System.out.println( "打印数据如下:");
    System.out.println(m);

    System.out.println();

    System.out.println( "四、测试remove(int i)方法,将容器中的第2号位置的数据删除");
    System.out.println( "删除数据是否成功?"+m.remove(2));
    System.out.println( "打印数据如下:");
    System.out.println(m);

    System.out.println();

    System.out.println( "五、添加若干数据,打印数据如下:");
    m.add( "中国人民大学");
    m.add( "北京理工大学");
    m.add( "中国人民大学");
    m.add( "北京理工大学");
    System.out.println(m);

    System.out.println();

    System.out.println( "六、测试remove(String str)方法,将容器中第一次出现的中国人民大学删除");
    System.out.println( "删除数据是否成功?"+m.remove( "中国人民大学"));
    System.out.println( "打印数据如下:");
    System.out.println(m);

    System.out.println();

    System.out.println( "七、测试removeAll(String str)方法,将容器中所有的北京理工大学的数据删除");
    System.out.println( "删除数据是否成功?"+m.removeAll( "北京理工大学"));
    System.out.println( "打印数据如下:");
    System.out.println(m);

    System.out.println();

    System.out.println( "八、测试update(int i, String str)方法,将1号位置的数据更新为中国人民大学");
    System.out.println( "更新是否成功?"+m.update(1, "中国人民大学"));
    System.out.println( "打印数据如下:");
    System.out.println(m);

    System.out.println();

    System.out.println( "九、测试update(String str1, String str2)方法,将容器中第一次的出现的中国人民大学更新为清华大学");
    System.out.println( "更新是否成功?"+m.update( "中国人民大学", "清华大学"));
    System.out.println( "打印数据如下:");
    System.out.println(m);

    System.out.println();

    System.out.println( "十、添加若干数据,打印数据如下:");
    m.add( "北京理工大学");
    m.add( "中国人民大学");
    m.add( "北京理工大学");
    System.out.println(m);

    System.out.println();

    System.out.println( "十一、测试updateAll(String str1, String str2)方法,将容器中所有的中国人民大学更新为北京邮电大学");
    System.out.println( "更新是否成功?"+m.updateAll( "中国人民大学", "北京邮电大学"));
    System.out.println( "打印数据如下:");
    System.out.println(m);
    
    System.out.println();
    System.out.println( "十二、测试getNodePositions(String str)方法,获得容器中北京理工大学的所有出现位置,如下:");
     int[] temp = m.getNodePositions( "北京理工大学");
     for( int i = 0; i <temp.length;i++) {
      System.out.print(temp[i]+ " ");
    }

    System.out.println();
    System.out.println();
    System.out.println( "十三、测试contains(String str)方法,判断是否存在北京理工大学和中国人民大学");
    System.out.println( "是否存在北京理工大学?"+m.contains( "北京理工大学"));
    System.out.println( "是否存在中国人民大学?"+m.contains( "中国人民大学"));
    
    System.out.println();
    System.out.println( "十四、测试clear()方法,将容器清空,并用isEmpty()方法判断是否为空");
    m.clear();
    System.out.println( "容器是否清空?"+m.isEmpty());
    System.out.println( "打印数据如下:");
    System.out.println(m);

    System.out.println();
    System.out.println( "*** 测试完毕 ***");
  }
}
 
运行效果:
 
*** 测试开始--sunspot ***
一、容器初始化,容器大小为6
二、测试add(String str)方法,向容器中添加两条数据
添加北京大学是否成功?true
添加中国人民大学是否成功?true
打印数据如下:
[0,北京大学] [1,中国人民大学]
三、测试insert(int i , String str)方法,向容器中的第1号位置插入清华大学
插入数据是否成功?true
打印数据如下:
[0,北京大学] [1,清华大学] [2,中国人民大学]
四、测试remove(int i)方法,将容器中的第2号位置的数据删除
删除数据是否成功?true
打印数据如下:
[0,北京大学] [1,清华大学]
五、添加若干数据,打印数据如下:
[0,北京大学] [1,清华大学] [2,中国人民大学] [3,北京理工大学] [4,中国人民大学] [5,
北京理工大学]
六、测试remove(String str)方法,将容器中第一次出现的中国人民大学删除
删除数据是否成功?true
打印数据如下:
[0,北京大学] [1,清华大学] [2,北京理工大学] [3,中国人民大学] [4,北京理工大学]
七、测试removeAll(String str)方法,将容器中所有的北京理工大学的数据删除
删除数据是否成功?true
打印数据如下:
[0,北京大学] [1,清华大学] [2,中国人民大学]
八、测试update(int i, String str)方法,将1号位置的数据更新为中国人民大学
更新是否成功?true
打印数据如下:
[0,北京大学] [1,中国人民大学] [2,中国人民大学]
九、测试update(String str1, String str2)方法,将容器中第一次的出现的中国人民大学
更新为清华大学
更新是否成功?true
打印数据如下:
[0,北京大学] [1,清华大学] [2,中国人民大学]
十、添加若干数据,打印数据如下:
[0,北京大学] [1,清华大学] [2,中国人民大学] [3,北京理工大学] [4,中国人民大学] [5,
北京理工大学]
十一、测试updateAll(String str1, String str2)方法,将容器中所有的中国人民大学更
新为北京邮电大学
更新是否成功?true
打印数据如下:
[0,北京大学] [1,清华大学] [2,北京邮电大学] [3,北京理工大学] [4,北京邮电大学] [5,
北京理工大学]
十二、测试getNodePositions(String str)方法,获得容器中北京理工大学的所有出现位置
,如下:
3 5
十三、测试contains(String str)方法,判断是否存在北京理工大学和中国人民大学
是否存在北京理工大学?true
是否存在中国人民大学?false
十四、测试clear()方法,将容器清空,并用isEmpty()方法判断是否为空
容器是否清空?true
打印数据如下:

*** 测试完毕 ***
 
 
提供代码下载,此代码只供参考,如果错误,请指出,谢谢

你可能感兴趣的:(java,休闲,自考,北京大学,上机实践)