计科 1501班 韩 猛 实验六
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" );
}
}
运行结果为:
java.lang.Exception: Exception thrown in method3
at shiyan6.PrintExceptionStack.method3(PrintExceptionStack.java:22)
at shiyan6.PrintExceptionStack.method2(PrintExceptionStack.java:18)
at shiyan6.PrintExceptionStack.metho1(PrintExceptionStack.java:14)
at shiyan6.PrintExceptionStack.main(PrintExceptionStack.java:6)
String getMessage():返回此throwable详细信息字符串
void printStackTrace():将此 throwable对象的堆栈跟踪输出至错误输出流。
当程序出现异常,try{}catch(){}块,捕获到所发生的异常,并进行相应的处理,
此程序中 method1捕获异常然后抛出继而给 method2, method2继续抛给 method3,所以问题输出顺序at shiyan6.PrintExceptionStack.method3(PrintExceptionStack.java:22)
at shiyan6.PrintExceptionStack.method2(PrintExceptionStack.java:18)
at shiyan6.PrintExceptionStack.method1(PrintExceptionStack.java:14)
at shiyan6.PrintExceptionStack.main(PrintExceptionStack.java:6)
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);
}
}
运行结果为:
原始元素之后:[One book, Two book, Three book]
Exception in thread "main" One book
java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.checkForComodification(Unknown Source)
at java.util.ArrayList$Itr.next(Unknown Source)
at shiyan6.Test.main(Test.java:15)
在迭代的过程删除进行处理时,集合的大小发生变化,因此产生程序异常。
如果在遍历时非要删除集合中的元素,则应修改程序,将
if (book.equals("One book"))
{
books.remove(book);
}
}
修改为:
if (book.equals("One book"))
{
it.remove();
}
}
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);
}
}
有程序看出虽然set.add中内容相同,但因为在实例化的过程中,开辟了两个不同的存储空间,存储地址不同,所以此情况下需重写HashCode()方法和equals()方法
所以:
public boolean equals(Object obj){
if(this==obj){
return true;
}
if(!(obj instanceof Student)){
return false;
}
Student s=(Student) obj;
if(this.name.equals(s.name)&&this.id.equals(s.id)){
return true;
}else{
return false;
}
}
public int hashCode(){
final int num=2;
int finall;
if(!id.equals(null)){
return finall=num+id.hashCode()+name.hashCode();
}
else
{return 0;
}
}
}
(二)实验总结
实验内容:
1.模拟KTV点歌系统
分别用LinkedList和ArrayList集合,实现 一个模拟KTV点歌系统的程序。实现以下功能 :
(1)显示歌曲列表
(2)添加歌曲到列表
(3)删除歌曲
(4)将歌曲置顶
(5)将歌曲前移一位
(6)退出
设计思路:非别利用LinkedList和ArrayList方法添加歌曲内容。然后利用switch case循环,在循环里非别写出菜单所需内容代码:
while (choice != 6) {
switch (choice) {
然后
case 4:
System.out.println("选择将歌曲置顶");
System.out.println("请输入要置顶的歌曲的位置");
int i = in.nextInt();
Collection<music> collection = new LinkedList<music>();
collection.add(list.get(i));
list.remove(list.get(i));
list.addAll(0, collection);
System.out.println(list);
System.out.println("请输入你的选择:");
choice = in.nextInt();
break;
等等。
2.模拟微博用户注册
用HashSet实现一个模拟微博用户注册的程序。用户输入用户名、密码、确认密码、生日(格式yyyy-mm-dd)、手机号码(11位,13、15、17、18开头)、邮箱信息进行微博的注册。要求对用户输入的信息进行验证,输入信息正确后,验证是否重复注册,如果不是则注册成功,否则注册失败。
提示:
(1)设计一个用户类存储用户注册信息
(2)设计一个校验信息类,定义校验方法完成对输入信息的校验。学习使用正则表达式完成对生日、手机号码和邮箱的验证。
(3)设计一个用户注册类模拟注册过程。用HashSet存储用户数据列表,定义一个initData()方法添加初始用户信息。在main方法中完成用户注册功能。
(三)代码托管(务必链接到你的项目)
git@git.oschina.net:qq2546914025/shiyanliu.git