java第六次作业 计科1501班 张鹏

java

java 第六次作业

计科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" );

}

}

printStackTrace方法的输出结果:

java.lang.Exception: Exception thrown in method3

getMessage方法的输出结果是:

    at test.Test.method3(Test.java:22)

at test.Test.method2(Test.java:18)

at test.Test.method1(Test.java:14)

at test.Test.main(Test.java:6)

异常的传播:

在发生异常的地方直接处理,使用try{}catch(){}块,捕获到所发生的异常,并进行相应的处理。


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

}

}

程序运行中会产生错误,因为在迭代时,如果调用集合对象的remove()方法删除对象,会出现运行错误,如果想要删除,必须调用迭代器本身的remove

()方法。

删除的是books集合的最后一个对象,运行的结果是

原始元素之后:[One book, Two book, Three book]

One book

Exception in thread "main" Two book

Three book

java.util.ConcurrentModificationException

at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:819)

at java.util.ArrayList$Itr.next(ArrayList.java:791)

at test.main(test.java:15)

假如删除的是最后一个对象的话,遍历输出时迭代器的大小其实没有发生改变,所以列表中的内容能够正常输出;当对最后一个对象进行删除时,迭代器的储存能力的大小发生变化,产生异常。

如果在遍历时非要删除集合中的元素,那么应该调用迭代器的remove()方法

程序如下:

Iterator<String> it = books.iterator();

while(it.hasNext())

{

String book = (String)it.next();

System.out.println(book);

if (book.equals("One book"))

{

it.remove(book);

}

}

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接口不允许存放重复元素。HashSet依靠Object类的hashCode()方法和equals()方法完成重复元素的判断。

hashCode():先判断对象的哈希码是否相同,依靠哈希码取出对象的内容。

equals():将对象的属性依次比较

所以,加入HashSet中的元素所属类必须重写hashCode()方法和equals()方法以确保对象的唯一性。

程序修改后如下(即重写hashCode()方法和equals()方法):

import java.util.*;

class Student {

String id;

String name;

public Student(String id, String name) {

this.id = id;

this.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 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.模拟KTV点歌系统

分别用LinkedList和ArrayList集合,实现一个模拟KTV点歌系统的程序。实现以下功能:

(1)显示歌曲列表

(2)添加歌曲到列表

(3)删除歌曲

(4)将歌曲置顶

(5)将歌曲前移一位

(6)退出

程序设计思路:分别建立LinkedList和ArrayList集合,然后分别调用两者所包含的方法,实现模拟KTV点歌系统的程序即可。


2.模拟微博用户注册

用HashSet实现一个模拟微博用户注册的程序。用户输入用户名、密码、确认密码、生日(格式yyyy-mm-dd)、手机号码(11位,13、15、17、18开头)、邮箱信息进行微博的注册。要求对用户输入的信息进行验证,输入信息正确后,验证是否重复注册,如果不是则注册成功,否则注册失败。

提示:

(1)设计一个用户类存储用户注册信息

(2)设计一个校验信息类,定义校验方法完成对输入信息的校验。学习使用正则表达式完成对生日、手机号码和邮箱的验证。

(3)设计一个用户注册类模拟注册过程。用HashSet存储用户数据列表,定义一个initData()方法添加初始用户信息。在main方法

程序设计思路:按照题目要求分别建立三个类,里边存上相应的方法供测试类进行调用。在Jiaoyan类里边定义jiaoyan()方法,依此判断输入的电话号码,生日类型以及邮箱是否与正规的匹配;在Userzhuce类里边建立zhuce()方法,以此来判断是否重复注册。

(三)代码托管

以上是 java第六次作业 计科1501班 张鹏 的全部内容, 来源链接: utcz.com/z/392398.html

回到顶部