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" );
}
}
Exception thrown in method3
java.lang.Exception: Exception thrown in method3(抛出异常)
at Test.method3(Test.java:21)(捕获异常)
at Test.method2(Test.java:17)(捕获异常)
at Test.method1(Test.java:13)(捕获异常)
at Test.main(Test.java:5)(捕获异常)
e.getMessage(); 只会获得具体的异常名称. (抛出异常)
e.printStackTrace();会打出详细异常,异常名称,出错位置,便于调试用 .(捕获异常)
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);
}
}
books.remove(book),改成it.remove()
迭代器不能用对象的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);
}
}
因为添加的是匿名对象,而他们所引用的地址不同
解决办法:重写equals和hashcode方法
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);
}
}
(二)实验总结
实验内容:
模拟KTV点歌系统
程序设计思路:设计一个ArrayList类,增加,删除数据都在类中实现
问题:置顶和前移一位没有实现
模拟微博用户注册
程序设计思路:设计一个用户类,存储用户注册信息,判断用户输入的名字是否重复,一个电子邮件类,一个手机类 检验用户输入的生日,手机号和邮箱是否正确
用正则表达式判断手机号和邮箱
if(str .length() == 11){
if(str.startsWith("11")||str.startsWith("13")||str.startsWith("15")||str.startsWith("17")||str.startsWith("18")){
System.out.println("格式正确");
}
else{
System.out.println("格式错误");
}
}
else{
System.out.println("格式错误");
}
}
if(str .contains("@")&&str.contains(".")){
if(!str.startsWith("@")){
if((str.indexOf("@")<str.indexOf("."))){
if(str.endsWith("com")||str.endsWith("cn")||str.endsWith("net")||str.endsWith("gov")||str.endsWith("edu")||str.endsWith("org")){
System.out.println("格式正确");
}
else{
System.out.println("格式错误");
}
}
else{
System.out.println("格式错误");
}
}
else{
System.out.println("格式错误");
}
}
else{
System.out.println("格式错误");
}
代码托管
git@git.oschina.net:hebau_cs15/javacs02hxd.git
