《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方法的输出结果是:
-
at PrintExceptionStack.method3(PrintExceptionStack.java:21)
at PrintExceptionStack.method2(PrintExceptionStack.java:17)
at PrintExceptionStack.method1(PrintExceptionStack.java:13)
at PrintExceptionStack.main(PrintExceptionStack.java:5) -
getMessage方法的输出结果是:
Exception thrown in method3
将try语句中写可能出现的异常代码catch来捕获异常,若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);
}
}
运行结果为:
原始元素之后:[One book, Two book, Three book]
One book
Exception in thread "main" java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.checkForComodification(Unknown Source)
at java.util.ArrayList$Itr.next(Unknown Source)
at Test.main(Test.java:14)
原因:不能在ArrayList遍历的时候删掉其中的元素,使大小发生改变,iterator发生异常
删除最后一个对象的运行结果为:
原始元素之后:[One book, Two book, Three book]
One book
Two book
Three book
Exception in thread "main" java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.checkForComodification(Unknown Source)
at java.util.ArrayList$Itr.next(Unknown Source)
at Test.main(Test.java:14)
原因:在遍历输出时,大小没有发生变化,而在删除的时候大小发生了变化,产生异常。
修改后的代码为:
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("Three 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);
}
原因:在实例化中,他们都是新建的内存空间,地址不同,所以判断为 不相同的,需要重写equals()方法和hashCode()方法
修改后的代码为:
import java.util.*;
class Student {
String id;
String name;
public Student(String id, String name) {
this.id = id;
this.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 (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点歌系统
-
程序设计思路:设计一个Music类,定义属性歌名及歌手,分别定义方法显示歌曲列表,添加歌曲。删除歌曲,指定歌曲,将歌曲前移一位。分别建一个test类用LinkedList和ArrayList集合实现点歌系统,在test类中用while语句来循环,用switch()case来使用户选择要进行的操作
-
实验问题分析:
问题1:
public static List
top(List list,int i){
for(int j=0;j<list.size();j++){
if(j==i){
String song=list.get(i).getName();
String songer=list.get(i).getSonger();
list.remove(i);
list.add(0,new Music(song,songer));
}
}
return list;
}
原因:没有定义get及set方法解决方案:在得到歌名时,不知应如何查找,问同学同学说不用setget方法,最后自己试了下定义那两个方法,然后可以出来。
问题2:
case 2:
System.out.println("请输入要添加的歌曲");
String name=in.next();
System.out.println("请输入要添加的歌曲的歌手");
String songer=in.next();
Music.add(list,name,songer);
break;
原因:不知道应该怎样调用方法解决方案:通过问同学得知应该用类名来进行调用,然后试了一下调用成功
2.模拟微博用户注册
- 程序设计思路:设计一个用户类,定义属性用户名,密码,确认密码密码,生日,手机号码,邮箱,再设计一个校验信息类,验证信息(用户名、手机号、邮箱)是否重复及确认密码和密码相等并用正则表达式来完成对手机号码、邮箱的验证,建立一个test类调用方法来注册微博。
- 实验问题分析:
问题1:
public static int checkname(Set<User> user,String cname){
int i=0;
Iterator<User> cuser=user.iterator();
while(cuser.hasNext())
{
if(cuser.next().getName().equals(cname)){
System.out.println("用户名已存在");
i=1;
}
else{
System.out.println("用户名注册成功");
}
}
return i;
}
原因:刚开始定义方法为void型的
解决方案:初始为void ,没有返回,无法使重新输入,将类型改为int型是返回值来判断是否重新输入
问题2:
String pat = "^[0-9]{5,10}+@qq.+(com|cn|edu|net|gov|org)$";
原因:不知该怎样用正则表达式来写邮箱的验证
解决方案:通过看书以及从网上查阅资料用[0-9]{5,10}来表示可以为5到10个数字。
(三)代码托管
- 码云commit历史截图