Java第六次做业--异常处理和Java类集

Java第六次做业--异常处理和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" );
       }
    }

getMessage方法的运行结果是异常信息:
java

printStackTrace方法的输出结果:git


缘由:printStackTrace:打印方法调用堆栈,String getMessage():返回此throwable详细信息字符串。
异常传播过程:
一个Throwable类的对象都有一个getMessage方法,method3()产生异常并抛出,method2() 调用method3()产生异常并抛出它返回一个字串,这个字串是在Exception构造函数中传入的,一般让这一字串包含特定异常的相关信息。正则表达式

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

正确代码以下:函数

import java.util.*;
public class Test13 {
    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("Three 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);                
    }
}
  • 程序运行结果:
    学习

  • 结果有相同数据缘由:
    每个实例化的对象,对应一个哈希曼编码,有三个,因此会有相同的值出现。测试

  • 修改方法:
    须要重写equals()方法、hashCode()方法。以下:this

public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((id == null) ? 0 : id.hashCode());
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        return result;
    }

    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Student other = (Student) obj;
        if (id == null) {
            if (other.id != null)
                return false;
        } else if (!id.equals(other.id))
            return false;
        if (name == null) {
            if (other.name != null)
                return false;
        } else if (!name.equals(other.name))
            return false;
        return true;
    }

}

5.其余须要总结的内容。

1.正则表达式能够方便的对数据进行匹配,能够执行更加复杂的字符串验证、拆分、替换功能。
2.若是要想在程序中应用正则表达式则必须依靠Pattern类与Matcher类,这两个类都在java.util.regex包中定义。Pattern类的主要做用是进行正则规范的编写,而Matcher类主要是执行规范,验证一个字符串是否符合其规范。
3. 几个经常使用规范格式:编码

日期(yyyy-MM-dd):“\\d{4}-\\d{2}-\\d{2}”  Email: “\\w+@\\w+.(com|com.cn|cn|net|gov|edu|org)”

(二)实验总结

1.模拟KTV点歌系统

分别用LinkedList和ArrayList集合,实现一个模拟KTV点歌系统的程序。实现如下功能:
(1)显示歌曲列表
(2)添加歌曲到列表
(3)删除歌曲
(4)将歌曲置顶
(5)将歌曲前移一位
(6)退出
题目扩展:歌曲包括曲名、演唱者。增长排序显示歌曲列表功能。

(1)程序设计思路:

  • 想建立一个存放歌曲的类,存放歌曲。在经过编写每个功能的方法函数,用LinkedList和ArrayList集合,在用集合类的方法实现功能。
    (2)问题1:程序组应用 swicth case 循环语句时。没法要主程序无线循环运行,只能运行一次就break结束。
    解决方案:思考半天,自己swicth语句,是没有无线执行功能的,只能在外层嵌套一个for循环,要程序屡次运行。
    问题2:在实现功能4时,应用list接口方法,但必定要注意把原来的数据删除吊。*.remove()

2.模拟微博用户注册

  • 内容:用HashSet实现一个模拟微博用户注册的程序。用户输入用户名、密码、确认密码、生日(格式yyyy-mm-dd)、手机号码(11位,1三、1五、1七、18开头)、邮箱信息进行微博的注册。要求对用户输入的信息进行验证,输入信息正确后,验证是否重复注册,若是不是则注册成功,不然注册失败。

- 程序设计思路:

(1)设计一个用户类存储用户注册信息

(2)设计一个校验信息类,定义校验方法完成对输入信息的校验。学习使用正则表达式完成对生日、手机号码和邮箱的验证。

(3)设计一个用户注册类模拟注册过程。用HashSet存储用户数据列表,定义一个initData()方法添加初始用户信息。在main方法中完成用户注册功能。
(4)设计一个users类,存储用户的用户名、密码、生日、手机号、邮箱等属性; 使用正则表达式方法写 check 检验函数,看用户输入的信息是否相符。最后设计一个测试注册类,进行判断。

  • 问题1:

注册成功,用户信息存入HashSet类集,要定义一个intData()方法,以下:

  • 问题2:

在写检查输入信息手机号,生日,邮箱,格式是否有误时,应用正则表达式判断,翻之前课件,最后才写好,关键代码:

String birthCheck="\\d{4}-\\d{2}-\\d{2}";
String phoneCheck="^(18|15|17|13)\\d{9}$";
String emailCheck="\\w+@\\w+.(com|cn|edu|net|gov|org)";
		Pattern p2 = Pattern.compile(emailCheck);
		Matcher m2 =p2.matcher(email);

(三)代码托管

  • 码云commit历史截图

(四)学习进度条

代码行数(新增/累积) 学习时间(新增/累积) 本周学习内容
目标 5000行 300小时
第2-4周 200/200 20/20 java基本语法
第5周 150/350 20/40 学习了构造函数,各类基本调用方法。
第6周 150/350 30/70 运行
第7周 500/850 15/85 面向对象编程的知识点(封装、继承和多态)
相关文章
相关标签/搜索