Java作业06 计科1501 闫国雨

(一)学习总结

1.用思维导图对本周的学习内容进行总结。

参考资料: XMind

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" );
       }
    }

程序的运行结果为:

答:如上图所示,System.err.println( e.getMessage() + "\n" );进行输出的时候,输出的是throw抛出来的异常结果,而e.printStackTrace();输出的是每一个异常的位置。
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);
        }
    }

答:在p518页中有说明,正常情况下,一个集合要把内容交给Iterator输出,但是几何操作中也有一个remove()方法,如果在使用Ierator输出时候由几何对象调用了自身的删除方法,则会出现运行时的错误。所以答案应该改成:

it.remove(book);

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);                
        }
    }

答:类中需要添加比较器,才可以去除重复项,例如:

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 ;
    }
	@Override
	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;
	}
	@Override
	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;
	}

    
}
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);                
    }
}

5.其他需要总结的内容。

答:在写程序的时候,要仔细分析,确定需要实现的功能后,选择最恰当的接口来写类集。

(二)实验总结

实验内容:

1.模拟KTV点歌系统

分别用LinkedList和ArrayList集合,实现一个模拟KTV点歌系统的程序。实现以下功能:
(1)显示歌曲列表
在显示歌曲列表的时候,直接可以用System.out.println来进行输出就可以了。
(2)添加歌曲到列表
在添加的时候,每个类集都有add的添加功能,直接添加就可以了。
(3)删除歌曲
在删除的时候,同理都拥有remove方法进行删除。
(4)将歌曲置顶
将歌曲置顶的时候,我就是单纯的把置顶的歌曲和第一个的歌曲换了一个位置。
(5)将歌曲前移一位
这个就是把上移的歌曲和上移上面的歌曲换了一个位置。
(6)退出
直接break就可以进行退出了。
题目扩展:歌曲包括曲名、演唱者。增加排序显示歌曲列表功能。

2.模拟微博用户注册

用HashSet实现一个模拟微博用户注册的程序。用户输入用户名、密码、确认密码、生日(格式yyyy-mm-dd)、手机号码(11位,13、15、17、18开头)、邮箱信息进行微博的注册。要求对用户输入的信息进行验证,输入信息正确后,验证是否重复注册,如果不是则注册成功,否则注册失败。
提示:
(1)设计一个用户类存储用户注册信息
设计一个类,这个类里面有这么多个private封装的基本信息。
(2)设计一个校验信息类,定义校验方法完成对输入信息的校验。学习使用正则表达式完成对生日、手机号码和邮箱的验证。
在这个里面用正则表达式来进行验证就可以了。
(3)设计一个用户注册类模拟注册过程。用HashSet存储用户数据列表,定义一个initData()方法添加初始用户信息。在main方法中完成用户注册功能。
在initData方法中,实现HashSet的添加就可以完成了。

(三)代码托管(务必链接到你的项目)

码云commit历史截图
上传实验项目代码到码云,在码云项目中选择“统计-commits”,设置搜索时间段,搜索本周提交历史,并截图。
https://git.oschina.net/hebau_cs15/Java-CS01ygy.git

posted @ 2017-05-04 12:16  闫国雨  阅读(124)  评论(0)    收藏  举报