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

printStackTrace方法的输出结果是

getMessage方法的输出结果是

析异常的传播过程:先是throw new Exception( "Exception thrown in method3" ),然后是method3()、method2()、method1()。

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

运行结果

产生错误的原因:集合本身的内容被破坏掉,所以迭代器出现错误,会停止输出。

运行结果

解释:运行时先取出集合中元素进行输出,最后取出的是Three book,所以前两个元素也能输出。
修改如下

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 ("One book".equals(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);                
    }

  }
  原因:没进行比较元素的地址是否相等。
  修改如下

import java.util.*;
class Student {
String id;  
String name;
	public Student(String id, String name) {
    	this.id = id;
    	this.name = name;
	}
	public boolean equals(Object obj){
		if(this==obj){
			return true;
		}
		if(!(obj instanceof Student)){
			return false;
		}
		Student p=(Student) obj;
		if(this.id.equals(p.id)&&this.name.equals(p.name)){
			return true;
		}
		else{
			return false;
		}
	}
	public int hashCode(){
		return this.id.hashCode()*this.name.hashCode();
	}
	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);                
	}
}

  

(二)实验总结

1.点歌系统

  • 程序设计思路:实例化一个对象。分别编写输出、添加、删除、歌曲置顶、歌曲前移、退出方法。在主方法中调用以上方法,用switch case 让用户选择想进行的操作。
    2.微博注册
  • 程序设计思路:编写一个用户类,储存用户注册的信息,包括用户名,密码,确认密码生日、手机号码、邮箱这几个属性。 设计一个校验信息类,判断用户注册侧的信息是否重复,包括用户名、手机号、邮箱。还要验证用户输入生日的格式是否正确。设计一个用户注册类,用HashSet存储用户信息,定义一个initData方法添加用户。

(三)代码托管

  • 码云commit历史截图

(四)学习进度条

代码行数(新增/累积) 学习时间(新增/累积) 本周学习内容
目标 5000行 300小时
第2-4周 100/100 20/20 学习了数组和方法
第5周 200/300 30/50 学习了String类和StringBuffer类
第6周 800/1100 40/90 学习了this、static关键字,Singleton模式
第八周 1200/1700 60/110 继承和多态,抽象方法
第九周 1500/2000 70/120 接口、工厂设计模式、包装类、匿名内部类、日期类、正则表达式
第十周 1900/2400 80/130 异常处理、泛型、集合
posted @ 2017-05-04 20:14  .M  阅读(165)  评论(0)    收藏  举报