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

getMessage方法输出:

Exception thrown in method3

printStackTrace方法输出:

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

异常的传播过程:

首先执行try中的method1(),然后跳转到method1方法,method1()方法把异常抛出给method2(),执行method2()方法,又把异常抛出给method3(),执行method3()方法,抛出新异常"Exception thrown in method3",然后回到主函数执行catch中的语句,输出Error的信息,输出具体错误信息和位置。

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

程序运行结果:

错误的意思是并发篡改,Iterator 被创建之后会建立一个指向原来对象的单链索引表,当前对象"One book"正在处理,不能删除,如果删除原来对象数量改变,但索引表的内容不会同步改变,会造成类似于指针丢失的错误。

如果一定要删除,可以使用 Iterator 本身的方法 remove() 来删除对象, Iterator.remove() 方法会在删除当前迭代对象的同时维护索引的一致性。

只要将if中的"book.remove()"语句改为"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);                
    }
}

HashSet是通过判断栈中存储地址,即栈所指向的堆是否是同一处来判断元素是否重复。程序中两个学生信息虽然内容相同,但是都开辟了新的堆空间,所以存入了相同元素。

要想实现Hashset的不可重复元素的方法,就必须在类中重写hashcode和equals方法。

(二)实验总结

实验内容:
1.模拟KTV点歌系统

程序设计思路:①定义一个歌曲类,包括歌名和歌手

②添加测试类,主函数里定义操作菜单,根据需要定义其他方法

问题1:刚开始只指定了一个泛型类型,后来写拓展时需要改为两个,而LinkedList类里只能包含一个范例对象,所以写不对。

原因:直接写成LinkedList l=new LinkedList();

解决方案:将String改写为Song,然后将Song写成一个类,其中包括它的内部成员。

2.模拟微博用户注册

程序设计思路:①设置一个用户类,定义用户所需属性

②设计一个校验信息类,将用户输入的信息进行判断

③定义测试类,完成用户的输入和判断结果的输出

问题1:在判断字符串是否为空时出现问题

原因:我的写法是“input.equals(null)”,这样写会出异常,提示我为空指针(NullPointerException)

解决方案:查了相关资料,判断字符串是否为空有四种方法:

①str==null;

②"".equals(str);

③str.length<=0;

④str.isEmpty();

问题2:对于用户输入信息的判断,我用的方法是在用户输入的set和get方法里添加限制条件,但是以前的程序为了方便,使用构造函数将所有输入不经过get和set方法限制就一起输入过来,所以我的错误输入总是不能返回我输入的警告信息

原因:构造函数中的直接赋值跳过了get、set检查

解决方案:将所有get、set方法添加到构造函数中,删除以前的直接调用

(三)代码托管

  • 码云commit历史截图
posted @ 2017-05-04 17:57  许琳13号  阅读(184)  评论(0编辑  收藏  举报