201521123103 《java学习笔记》 第十周学习总结

1、本周学习总结

1.1 以你喜欢的方式(思惟导图或其余)概括总结异常与多线程相关内容。

2、书面做业

本次PTA做业题集异常、多线程java

一、finally 题目4-2

1.1 截图你的提交结果(出现学号)

1.2 4-2中finally中捕获异常须要注意什么?

答:只有try执行后才会执行finally,不管在try块的哪一个地方返回,finally块中的代码必定会被执行。4-2这里用resource.close();释放资源,并且finally中的异常要在finally中用try/catch捕获。百度查到说finally中最好不要使用return,若是finally中return了, 则执行finally中的reutrn 语句,而后程序结束,不会执行try块中的return。连接:http://1194867672-qq-com.iteye.com/blog/1571561

二、用异常改进ArrayIntegerStack 题目4-3

2.1 截图你的提交结果(出现学号)

2.2 实验总结

答:该题只是对以前的代码进行了修改:
     若是栈满,if(top== arr.length){ throw new FullStackException();}抛出栈满的异常。
     若是栈空,if(top==0){ throw new EmptyStackException();}抛出栈满的异常
     把之前代码的arr[]改为题目中的arrStack[]就好了

三、自定义异常 题目5-4

3.1 截图你的提交结果(出现学号)

3.2 自定义异常有哪几个关键点?

答:自定义异常,须要继承已有的父类。Checked Exception继承Exception类,Uncheck Exception继承RuntimeException类。自定义的异常在抛出时要明确告诉用户异常的缘由,好比:throw new IllegalNameException("the first char of name must not be digit, name="+name);

四、读取文件并组装对象 实验任务书中中的题目3:读取文件并组装对象,给出关键代码(需出现你的学号)

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.NoSuchElementException;
import java.util.Scanner;

public class ReadFile201521123103 {
    public static void main(String[] args)  throws FileNotFoundException{
        Scanner in = null;
        ArrayList<User> student=new ArrayList<User>();
        try{
            int count = 0;
            in = new Scanner(new File("身份证号.txt"));//为myfile.txt这个File建立一个扫描器in
            while(in.hasNextLine()){
                String line = in.nextLine();//读出myfile.txt的下一行
                count++;
                @SuppressWarnings("resource")
                Scanner lineScanner = new Scanner(line);//为每一行创建一个扫描器
                //System.out.println("lineScanner="+lineScanner);
                lineScanner.useDelimiter(" ");//使用空格做为分隔符
                //System.out.println(line);
                String a1 = lineScanner.next();//姓名
                String a2 = lineScanner.next();//身份证号
                String a3 = lineScanner.next();//性别
                try{
                    if(a1.length() == 0 || a1.length() > 3)
                    {
                        throw new IllegalArgumentException("a1.length = " + a1.length());
                    }
                    if(a2.length() == 0)
                    {
                        throw new IllegalArgumentException("a2.length = " + a2.length());
                    }
                    if(!a3.equals("男") && !a3.equals("女"))
                    {
                        throw new IllegalArgumentException("性别出错:"+a3);
                    }
                    String a4 = lineScanner.next();//年龄
                    String a5 = lineScanner.next();//地址
                    while(lineScanner.hasNext()){//谨防地址只有一段
                        a5 += lineScanner.next();
                    }
                    student.add(new User(a1,a2,a3,Integer.parseInt(a4),a5));
                }catch(IllegalArgumentException e)
                {
                    System.out.println("抛出异常:"+e+"。错误在文件的第"+count+"行:"+line);
                }
                catch(NoSuchElementException e)
                {
                    System.out.println("抛出异常:"+e+"。错误在文件的第"+count+"行:"+line);
                }        
                
            } 
            Collections.sort(student,new Comparator<User>(){
                @Override
                public int compare(User o1, User o2) {
                                    return o1.getAge()-o2.getAge();
                }       
            });
            for (User user : student) {
                System.out.println(user.toString());
            }
        }catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            System.out.println(e);
        } finally {
            if (in != null) {
                in.close();
            }
        }
    }
}
class User{
    private String name;
    private String id;
    private String gender;
    private int age;
    private String address;
    
    public User(String name, String id, String gender, int age, String address) {
        super();
        this.name = name;
        this.id = id;
        this.gender = gender;
        this.age = age;
        this.address = address;
    }
    @Override
    public String toString() {
        return "User [name=" + name + ", id=" + id + ", gender=" + gender + ", age=" + age + ", address=" + address
                + "]";
    }
    public int getAge() {
        // TODO Auto-generated method stub
        return age;
    }
    
}

五、学会使用Eclipse进行调试 观看相关调试视频

5.1 简述使用Eclipse进行调试须要几步?

答:(1)在正确的地方设置断点(2)启动调试模式,Eclipse有一个专门的debug perspective(3)运行程序,使用F五、F六、F7快捷键进行调试(4)查看状态值

5.2 调试时F5, F6, F7快键键各有什么不一样?什么状况该使用哪一个快捷键?

答:F5:Step into 跳入函数或方法  调试某个函数内部的代码,进入函数内部要使用F5快捷键
     F6: Step over 跳过   一行一行调试,不进入函数内部
     F7: Step return 跳出某函数

5.3 除了Eclipse的调试器,你还使用什么方法调试程序?

答:使用System.out.println输出值来进行调试。

六、题集多线程

6.1 程序填空3-一、3-2。(截图提交结果,出现你的学号)

6.2 函数4-1(Thread)、4-2(Runnable)(截图提交结果,出现你的学号)


6.3 函数4-3(Runnable与匿名内部类)(截图提交结果,出现你的学号),并使用Labmda表达式改写。

import java.util.Arrays;

public class Main4 {

    public static void main(String[] args){
        // TODO Auto-generated method stub
         final String mainThreadName = Thread.currentThread().getName();
         Thread t1 = new Thread(()->{
            
                 System.out.println(mainThreadName);
                 System.out.println(Thread.currentThread().getName());
                 System.out.println(Arrays.toString(Thread.class.getInterfaces()));
             
         });
         t1.start();
    }
}

6.4 实验总结

答:3-1  守护线程:用t1.setDaemon(true);使线程成为守护线程,若是全部前台线程死亡,守护线程自动结束。
     3-2  线程让步:用t1.join()方法让处于运行状态的线程调用另外一个线程,等其余线程执行完再执行本线程。
     4-1  编写MyThread类继承自Thread,按题目要求编写public void run()方法覆盖Thread类的run()方法。
     4-2  这个题错了好屡次,一直说没有终止线程,问同窗,要捕获异常来终止try{Thread.sleep(1); } catch(InterruptedException e){ }还有题目要求对每一个传入的word只能检查一遍。因此要if(word!=null) this.word=null;
     4-3  PPT中和Runnable与任务后的一个选择题同样,用到匿名内部类

七、源代码阅读:多线程程序BounceThread

7.1 哪一个类是支持多线程的类,它实现了什么接口。这个类作了些什么?

答:BallRunnable是支持多线程的类,它实现了Runnable接口,该类调用小球移动的方法move(),生成小球的位置移动,利用repaint()对界面进行重画,用Thread.sleep()使其睡眠一小段时间,造成小球移动的现象。

7.2 Ball.java这个程序只作了两件事,这两件事分别是什么?

答:getShape()得到小球的大小和move(Rectangle2D bounds)小球移动的位置坐标变化,小球移动。

7.3 BallComponent也只作了两件事,这两件事分别是什么?

答:add(Ball b)加小球和paintComponent(Graphics g)画小球。

7.4 BounceThread中,何时启动了新线程?

答:点击start按钮的时候,调用addBall()方法,加入一个小球,启动一个新线程。

7.5 这个程序是如何实现?一个大体的执行流程是什么?

答;创建一个图形界面,点击start按钮就出现一个小球,而后让小球移动,等小球移动到必定位置中止,再次点击start按钮,增长一个小球,重复以前操做。

八、购物车系统中的多线程

8.1 购物车系统中可能存在哪些多线程问题?

答:一个用户登陆能够当作一个线程,当多个用户一块儿购物时,就启动了多个线程,也就是多线程问题。

3、码云上代码提交记录

题目集:异常、多线程(3-1, 3-2, 4-1, 4-2, 4-3)git

3.1. 码云代码提交记录

在码云的项目中,依次选择“统计-Commits历史-设置时间段”, 而后搜索并截图多线程

相关文章
相关标签/搜索