java第六次做業

(一)學習總結

1.用思惟導圖對本週的學習內容進行總結。

2.當程序中出現異常時,JVM會依據方法調用順序依次查找有關的錯誤處理程序。可以使用printStackTrace 和getMessage方法瞭解異常發生的狀況。閱讀下面的程序,說明printStackTrace方法和getMessage方法的輸出結果分別是什麼?並分析異常的傳播過程。

public class PrintExceptionStack {
    public static void main( String args[] )
    {
         try {
             method1();
          } catch ( Exception e ) {
             System.err.println( e.getMessage() + "\n" );
             e.printStackTrace();
          }
    }
   public static void method1() throws Exception
   {
      method2();
   }
   public static void method2() throws Exception
   {
      method3();
   }
   public static void method3() throws Exception
   {
      throw new Exception( "Exception thrown in method3" );
   }
}

printStackTrace()的輸出結果:java

Exception thrown in method3

java.lang.Exception: Exception thrown in method3
at PrintExceptionStack.method3(PrintExceptionStack.java:21)
at PrintExceptionStack.method2(PrintExceptionStack.java:17)
at PrintExceptionStack.method1(PrintExceptionStack.java:13)
at PrintExceptionStack.main(PrintExceptionStack.java:5)

getMessage()的輸出結果:git

Exception thrown in method3

異常傳播過程:程序檢測到一個錯誤時會拋出一個異常對象,異常處理代碼會捕獲並處理這個錯誤。catch語句塊中的代碼是用來處理錯誤,當異常發生時,程序控制流程由try語句塊跳轉到catch語句塊,進行處理。正則表達式

3.閱讀下面程序,分析程序的運行結果,解釋產生錯誤的緣由,若是刪除的是books集合的最後一個對象,運行的結果又是什麼?你能對此做出解釋嗎?若是在遍歷時非要刪除集合中的元素,應如何實現?

import java.util.*;
public class Test
{
    public static void main(String[] args) 
    {
        Collection<String> books = new ArrayList<String>();
        books.add("One book");
        books.add("Two book");
        books.add("Three book");
        System.out.println("原始元素以後:"+books);
        Iterator<String> it = books.iterator();
        while(it.hasNext())
        {
            String book = (String)it.next();
            System.out.println(book);
            if (book.equals("One book"))
            {
                books.remove(book);
            }
        }
        System.out.println("移除元素以後:"+books);
    }
}

運行結果:jvm

原始元素以後:[One book, Two book, Three book]
One book
Exception in thread "main" java.util.ConcurrentModificationException
        at java.util.ArrayList$Itr.checkForComodification(Unknown Source)
    at java.util.ArrayList$Itr.next(Unknown Source)
        at Test.main(Test.java:14)

刪除的是books集合的最後一個對象,運行的結果:學習

Exception in thread "main" java.util.ConcurrentModificationException
        at java.util.ArrayList$Itr.checkForComodification(Unknown Source)
    at java.util.ArrayList$Itr.next(Unknown Source)
        at test.main(test.java:14)
原始元素以後:[One book, Two book, Three book]
One book
Two book
Three book

緣由:
在刪除時調用了remove()方法,刪除元素後集合的大小發生了變化,因此輸出時有問題。測試

在遍歷時非要刪除集合中的元素:this

import java.util.*;
public class Test
{
    public static void main(String[] args) 
    {
        Collection<String> books = new ArrayList<String>();
        books.add("One book");
        books.add("Two book");
        books.add("Three book");
        System.out.println("原始元素以後:"+books);
        Iterator<String> it = books.iterator();
        while(it.hasNext())
        {
            String book = (String)it.next();
            System.out.println(book);
            if (book.equals("One book"))
            {
                it.remove();
            }
        }
        System.out.println("移除元素以後:"+books);
    }
}

4.HashSet存儲的元素是不可重複的。運行下面的程序,分析爲何存入了相同的學生信息?若是要去掉重複元素,應該如何修改程序。

import java.util.*;
class Student {
    String id;  
    String name;
    public Student(String id, String name) {
        this.id = id;
        this.name = name;
    }
    public String toString() {
        return "Student id=" + id + ", name=" + name ;
    }
}
public class Test
{
    public static void main(String[] args) 
    {
        HashSet<Student> set = new HashSet<Student>();
        set.add(new Student("1","Jack"));
        set.add(new Student("2","Rose"));
        set.add(new Student("2","Rose"));
        System.out.println(set);                
    }
}

運行結果爲:.net

[Student id=2, name=Rose, Student id=2, name=Rose, Student id=1, name=Jack]

(二)實驗總結

實驗內容:

1.模擬KTV點歌系統

分別用LinkedList和ArrayList集合,實現一個模擬KTV點歌系統的程序。實現如下功能:
(1)顯示歌曲列表
(2)添加歌曲到列表
(3)刪除歌曲
(4)將歌曲置頂
(5)將歌曲前移一位
(6)退出
題目擴展:歌曲包括曲名、演唱者。增長排序顯示歌曲列表功能。
程序設計思路:分別用linkedList和ArrayList集合來完成添加刪除置頂移動的測試類設計

2.模擬微博用戶註冊

用HashSet實現一個模擬微博用戶註冊的程序。用戶輸入用戶名、密碼、確認密碼、生日(格式yyyy-mm-dd)、手機號碼(11位,1三、1五、1七、18開頭)、郵箱信息進行微博的註冊。要求對用戶輸入的信息進行驗證,輸入信息正確後,驗證是否重複註冊,若是不是則註冊成功,不然註冊失敗。
提示:
(1)設計一個用戶類存儲用戶註冊信息
(2)設計一個校驗信息類,定義校驗方法完成對輸入信息的校驗。學習使用正則表達式完成對生日、手機號碼和郵箱的驗證。
(3)設計一個用戶註冊類模擬註冊過程。用HashSet存儲用戶數據列表,定義一個initData()方法添加初始用戶信息。在main方法中完成用戶註冊功能。3d

(三)代碼託管


連接:
http://git.oschina.net/hebau_cs15/Java-CS02lc/tree/master

相關文章
相關標籤/搜索