2017《JAVA技术》第六次作业

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

(一)学习总结

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" );
}
}
PrintExceptionStack输出结果:

Exception thrown in method3

java.lang.Exception: Exception thrown in method3
at zuoye.PrintExceptionStack.method3(PrintExceptionStack.java:23)
at zuoye.PrintExceptionStack.method2(PrintExceptionStack.java:19)
at zuoye.PrintExceptionStack.method1(PrintExceptionStack.java:15)
at zuoye.PrintExceptionStack.main(PrintExceptionStack.java:7)

getMessage输出结果:

Exception thrown in method3
异常的传播过程:出现异常事件产生异常对象,然后抛出异常,最后捕获异常寻找处理异常的方法。
3.阅读下面程序,分析程序的运行结果,解释产生错误的原因,如果删除的是books集合的最后一个对象,运行的结果又是什么?你能对此作出解释吗?如果在遍历时非要删除集合中的元素,应如何实现?

import java.util.*;
public class Test
{
public static void main(String[] args)
{
Collection books = new ArrayList();
books.add("One book");
books.add("Two book");
books.add("Three book");
System.out.println("原始元素之后:"+books);
Iterator 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);
}
}
因为集合操作中也存在remove()方法,使用迭代器删除时要用迭代器的remove()方法,不能用对象的remove()方法。

books.remove(book);改成it.remove();
运行结果:

原始元素之后:[One book, Two book, Three book]
One book
Two book
Three book
移除元素之后:[Two book, Three book]
删除最后一个元素把equals比较的内容换掉即可
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 set = new HashSet();
set.add(new Student("1","Jack"));
set.add(new Student("2","Rose"));
set.add(new Student("2","Rose"));
System.out.println(set);
}
}
匿名对象存储的是对象引用,内容相同但存储的空间不同。
修改:删除重复就要判断是否重复,要覆写equals()方法判断对象是否相等,hashcode()方法求出不会重复的哈希吗。

package r;
import java.util.HashSet;
import java.util.Set;
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 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(thisobj){
return true;
}
if(!(obj instanceof Student)){
return false;
}
Student set=(Student)obj;
if(this.name.equals(set.name)&&this.id
set.id){
return true;

}
else{

return false;
}

}
}
package r;

import java.util.HashSet;

public class Test
{
public static void main(String[] args)
{
HashSet set = new HashSet();
set.add(new Student("1","Jack"));
set.add(new Student("2","Rose"));
set.add(new Student("2","Rose"));
System.out.println(set);
}
}

(二)实验总结

完成实验内容,代码上传到码云,并对完成实验内容过程中遇到的问题、解决方案以及程序的设计思路和思考等进行归纳总结。
格式如下:
第一题:
程序设计思路:设计一个song类设置歌曲名称和歌手的set()get()方法,测试类用switch来选择要进行哪项操作,并设置增加删除的方法。循环输出时可以用size()

for(int i=0;i<song.size();i++){
System.out.println(song.get(i));
}
置顶 到下标为0的位置

Song temp =song.get(i);
song.remove(i);
song.add(0,temp);
第二题:
程序设计思路:三个类,一个是用户的基本信息,一个是判断用户所输入的是不是符合规则,一个测试类

(三)代码托管(务必链接到你的项目)http://git.oschina.net/hebau_cs15/Java-cs02QSM

码云commit历史截图
上传实验项目代码到码云,在码云项目中选择“统计-commits”,设置搜索时间段,搜索本周提交历史,并截图。

posted on 2017-05-04 18:02  昨夜蔷薇  阅读(191)  评论(0)    收藏  举报