• 孔维滢《面向对象程序设计(java)》课程学习总结


    实验十八  总复习

    1、实验目的与要求              

    (1) 综合掌握java基本程序结构;

    (2) 综合掌握java面向对象程序设计特点;

    (3) 综合掌握java GUI 程序设计结构;

    (4) 综合掌握java多线程编程模型;

    (5) 综合编程练习。

    2、实验内容和步骤

    任务1:填写课程课后调查问卷,网址:https://www.wjx.cn/jq/33108969.aspx。

    任务2:综合编程练习

    练习1:

     

    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.*;
    import javax.swing.*;
    
    public class Yonghu 
    {
       public static void main(String[] args)
       {
          EventQueue.invokeLater(() -> {
             JFrame frame = new FrameTest();
             frame.setTitle("信息采集");
             frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
             frame.setVisible(true);
          });
       }
    }
    
    
    
    class FrameTest extends JFrame
    {
        
        private JPanel panel1;
        private JTextArea text1,text2;
        private JRadioButton JRadioButton1,JRadioButton2;
        private ButtonGroup ButtonGroup1;
        private JLabel JLabel1;
        private JCheckBox checkboxb1,checkboxb2,checkboxb3;
        private JComboBox<String> JComboBox1;
        private JButton Button1,Button2;
        
        
       public FrameTest()
       {
          setSize(700,500);
          panel1=new JPanel();
          panel1.setLayout(null);
    
          ButtonGroup1=new ButtonGroup();
          JRadioButton1=new JRadioButton("Male",false);   
          JRadioButton1.setBounds(150,330, 80, 50);
          JRadioButton2=new JRadioButton("Female",false); 
          JRadioButton2.setBounds(150,300, 80,50);
          ButtonGroup1.add(JRadioButton1);
          ButtonGroup1.add(JRadioButton2);
          
          addJLabel("sex:",100,300);
          addJLabel("name:",100,50);
          addJLabel("address:",100,150);
          addJLabel("Qualification:",400,50);
          addJLabel("Hobby:",400,150);
          
          
          text1=new JTextArea(1,1);
          text1.setBounds(150,70, 120, 30);
          text1.setLineWrap(true);
          text2=new JTextArea(5,3);
          text2.setBounds(150,160, 130, 100);
          text2.setLineWrap(true);
          
          
          checkboxb1=new JCheckBox("Reading");
          checkboxb1.setBounds(450,160,100,30);
          checkboxb2=new JCheckBox("Dancing");
          checkboxb2.setBounds(450,180,100,30);
          checkboxb3=new JCheckBox("Singing");
          checkboxb3.setBounds(450,200,100,30);
    
          
          JComboBox1=new JComboBox<>();
          JComboBox1.addItem("Graduate");
          JComboBox1.addItem("Sinior");
          JComboBox1.addItem("Junior");
          JComboBox1.setBounds(500,65, 100, 20);
          
          Button1 = new JButton("提交");Button1.setBounds(200, 400, 100, 35);
          Button2 = new JButton("重置");Button2.setBounds(400, 400, 100, 35);
    
          Button1.addActionListener(new Action1());
          Button2.addActionListener(new Action2());
          
          panel1.add(checkboxb1);
          panel1.add(checkboxb2);
          panel1.add(checkboxb3);
          panel1.add(Button1);
          panel1.add(Button2);
          panel1.add(JComboBox1);
          panel1.add(text1);
          panel1.add(text2);
          panel1.add(JRadioButton1);
          panel1.add(JRadioButton2);
          add(panel1);
          
          
       }
       
    public void addJLabel(String n,int a,int b)
       {
           JLabel1 = new JLabel(n);
           JLabel1.setBounds(a,b,100,50);
           panel1.add(JLabel1);
       }
       
       private class Action1 implements ActionListener
       {
       public void actionPerformed(ActionEvent event)
           {        
           System.out.println("name:"+text1.getText()+"
    "+"address:"+text2.getText());
           System.out.println("Qualification:"+JComboBox1.getSelectedItem());
           System.out.println("Hobby:");
           if(checkboxb1.isSelected()==true)System.out.print(checkboxb1.getText());
           if(checkboxb2.isSelected()==true)System.out.print(checkboxb2.getText());
           if(checkboxb3.isSelected()==true)System.out.print(checkboxb3.getText());
           System.out.println("
    "+"sex:");
           if(JRadioButton1.isSelected()==true)System.out.println(JRadioButton1.getText());
           if(JRadioButton2.isSelected()==true)System.out.println(JRadioButton2.getText());
           System.out.println("
    ");
           }
       } 
       private class Action2 implements ActionListener
       {
       public void actionPerformed(ActionEvent event)
           {        
           text1.setText(null);
           text2.setText(null);
           checkboxb1.setSelected(false);
           checkboxb2.setSelected(false);
           checkboxb3.setSelected(false);
           ButtonGroup1.clearSelection();
           JComboBox1.setSelectedIndex(0);
           }
       }   
    }
    

     

      

          

          重置后:

          

    练习2:

    package ShenF;
    
    import java.awt.Dimension;
    import java.awt.EventQueue;
    import java.awt.Toolkit;
    
    import javax.swing.JFrame;
    
    public class Main {
    
         public static void main (String args[])
            {
                 Toolkit t=Toolkit.getDefaultToolkit();
                Dimension s=t.getScreenSize(); 
                EventQueue.invokeLater(() -> {
                    JFrame frame = new Main1();
                    frame.setBounds(0, 0,(int)s.getWidth(),(int)s.getHeight());
                    frame.setTitle("身份查询");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setVisible(true);
                 });        
            }
    }
    

      

    package ShenF;
    
    import java.awt.BorderLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.util.*;
    import java.util.Timer;
    import javax.swing.*;
    
    public class Main1 extends JFrame {
    	private static ArrayList<Person> Personlist;
    
    	Scanner scanner = new Scanner(System.in);
    	File file = new File("E:\身份证号.txt");
    
    	private JPanel Panel;
    	private JLabel JLabel1;
    	private JButton Button, Button2, Button3;
    	private JTextArea text, text1, text2, text3;
    	boolean tru = true;
    
    	public Main1() {
    
    		Panel = new JPanel();
    		Panel.setLayout(null);
    		Button = new JButton("1:按姓名字典序输出人员信息");
    		Button2 = new JButton("2:查询最大年龄与最小年龄人员信息");
    		Button3 = new JButton("查询相近年龄");
    		JLabel1 = new JLabel("输入身份证号或者地址查询");
    		JLabel1.setBounds(900, 50, 400, 30);
    
    		text = new JTextArea(30, 80);
    		text.setBounds(50, 180, 700, 700);
    		text1 = new JTextArea(1, 30);
    		text1.setBounds(900, 80, 400, 30);
    		text2 = new JTextArea(30, 80);
    		text2.setBounds(900, 180, 700, 700);
    		text3 = new JTextArea(30, 80);
    		text3.setBounds(420, 100, 200, 40);
    
    		Button.addActionListener(new Action());
    		Button.setBounds(50, 50, 300, 40);
    		Button2.addActionListener(new Action1());
    		Button2.setBounds(50, 100, 300, 40);
    		Button3.addActionListener(new Action2());
    		Button3.setBounds(650, 100, 120, 40);
    		Panel.add(JLabel1);
    		Panel.add(Button);
    		Panel.add(Button2);
    		Panel.add(Button3);
    		Panel.add(text);
    		Panel.add(text2);
    		Panel.add(text1);
    		Panel.add(text3);
    		add(Panel);
    
    		Timer timer = new Timer();
    		TimerTask timeTask = new TimerTask() {
    
    			@Override
    			public void run() {
    				// TODO Auto-generated method stub
    				text2.setText(null);
    				String place = text1.getText().toString().trim();
    				for (int i = 0; i < Personlist.size(); i++) {
    
    					String Str = (String) Personlist.get(i).getbirthplace();
    					if (Str.contains(place) && !place.equals("")) {
    						text2.append(Personlist.get(i).toString());
    					}
    				}
    				for (int i = 0; i < Personlist.size(); i++) {
    
    					String Str = (String) Personlist.get(i).getID();
    					if (Str.contains(place) && !place.equals("")) {
    						text2.append(Personlist.get(i).toString());
    					}
    				}
    			}
    		};
    		timer.schedule(timeTask, 0, 100);
    
    		Personlist = new ArrayList<>();
    		try {
    			FileInputStream fis = new FileInputStream(file);
    			BufferedReader in = new BufferedReader(new InputStreamReader(fis));
    			String temp = null;
    			while ((temp = in.readLine()) != null) {
    				Scanner linescanner = new Scanner(temp);
    				linescanner.useDelimiter(" ");
    				String name = linescanner.next();
    				String ID = linescanner.next();
    				String sex = linescanner.next();
    				String age = linescanner.next();
    				String place = linescanner.nextLine();
    				Person Person = new Person();
    				Person.setname(name);
    				Person.setID(ID);
    				Person.setsex(sex);
    				int a = Integer.parseInt(age);
    				Person.setage(a);
    				Person.setbirthplace(place);
    				Personlist.add(Person);
    
    			}
    		} catch (FileNotFoundException e) {
    			System.out.println("查找信息失败");
    			e.printStackTrace();
    		} catch (IOException e) {
    			System.out.println("信息读取有误");
    			e.printStackTrace();
    		}
    	}
    
    	private class Action implements ActionListener {
    		public void actionPerformed(ActionEvent event) {
    			text.setText(null);
    			Collections.sort(Personlist);
    			text.append(Personlist.toString());
    		}
    	}
    
    	private class Action1 implements ActionListener {
    		public void actionPerformed(ActionEvent event) {
    			text.setText(null);
    			int max = 0, min = 100;
    			int j, k1 = 0, k2 = 0;
    			for (int i = 1; i < Personlist.size(); i++) {
    				j = Personlist.get(i).getage();
    				if (j > max) {
    					max = j;
    					k1 = i;
    				}
    				if (j < min) {
    					min = j;
    					k2 = i;
    				}
    			}
    			text.append("年龄最大:   " + Personlist.get(k1) + "
    " + "年龄最小:  " + Personlist.get(k2));
    		}
    	}
    
    	private class Action2 implements ActionListener {
    		public void actionPerformed(ActionEvent event) {
    			text.setText(null);
    			int a = Integer.parseInt(text3.getText().toString().trim());
    			int d_value = a - Personlist.get(agenear(a)).getage();
    
    			for (int i = 0; i < Personlist.size(); i++) {
    				int p = Personlist.get(i).getage() - a;
    
    				if (p == d_value || -p == d_value)
    					text.append(Personlist.get(i).toString());
    			}
    		}
    
    	}
    
    	public static int agenear(int age) {
    
    		int j = 0, min = 53, d_value = 0, k = 0;
    		for (int i = 0; i < Personlist.size(); i++) {
    			d_value = Personlist.get(i).getage() - age;
    			if (d_value < 0)
    				d_value = -d_value;
    			if (d_value < min) {
    				min = d_value;
    				k = i;
    			}
    		}
    		return k;
    	}
    }
    

      

    package ShenF;
    
    public class Person implements Comparable<Person> {
    	private String name;
    	private String ID;
    	private int age;
    	private String sex;
    	private String birthplace;
    
    	public String getname() {
    		return name;
    	}
    
    	public void setname(String name) {
    		this.name = name;
    	}
    
    	public String getID() {
    		return ID;
    	}
    
    	public void setID(String ID) {
    		this.ID = ID;
    	}
    
    	public int getage() {
    
    		return age;
    	}
    
    	public void setage(int age) {
    		this.age = age;
    	}
    
    	public String getsex() {
    		return sex;
    	}
    
    	public void setsex(String sex) {
    		this.sex = sex;
    	}
    
    	public String getbirthplace() {
    		return birthplace;
    	}
    
    	public void setbirthplace(String birthplace) {
    		this.birthplace = birthplace;
    	}
    
    	public int compareTo(Person o) {
    		return this.name.compareTo(o.getname());
    
    	}
    
    	public String toString() {
    		return name + "	" + sex + "	" + age + "	" + ID + "	" + birthplace + "
    ";
    
    	}
    }
    

      练习3:

    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    import java.util.Random;
    import javax.swing.*;
     
    public class Text extends JFrame {
        JPanel p=new JPanel();
        JLabel timeLabel=new JLabel();
          
        JLabel[] label1=new JLabel[10];
        JLabel[] label2=new JLabel[10];
        JLabel[] label3=new JLabel[10];
        JLabel[] label4=new JLabel[10];
        JLabel[] label5=new JLabel[10];
        JTextField[] field=new JTextField[10];
        JLabel[] label6=new JLabel[10]; 
        String[] btn_name= {"开始","重置","提交","重考"};
        JButton[] btn=new JButton[4];
        Panel2 panel2=null;
        int ExamCount=0;
        JLabel examLabel=new JLabel();
        double[] result=new double[10];
        public static void main(String[] args) {
            new Text("测试").setVisible(true);
        }
          
        public Text(String title) {
            setTitle(title);
            setLocationRelativeTo(null);
            setDefaultCloseOperation(3);
            setSize(400,500);
            setResizable(false);
            setForeground(Color.blue);
            add(new Panel1(),BorderLayout.NORTH);
            panel2=new Panel2();
            add(new JScrollPane(panel2));
            add(new Panel3(),BorderLayout.WEST);       
        }
     
        int rightResultCount=0;
        public void startExam() {
            int num1=0;
            int num2=0;
            String[] quots= {"+","-","*","/"};
            String quot=null;
            Random ran=null;
            ran=new Random(System.currentTimeMillis());
            Box box=Box.createVerticalBox();
              
            for(int i=0;i<10;i++) {
                num1=ran.nextInt(100)+1;
                num2=ran.nextInt(100)+1;
                int n=ran.nextInt(4);
                quot=new String(quots[n]);
                switch(quot) {
                case "+":
                    result[i]=num1+num2;
                    break;
                case "-":
                    result[i]=num1-num2;
                    break;
                case "*":
                    result[i]=num1*num2;
                    break;
                case "/":
                    result[i]=num1/(num2*1.0);
                    result[i]=Math.round(result[i]*100)/100.0;
                    break;
                }
                  
                label1[i]=new JLabel("第"+(i+1)+"题:");
                label2[i]=new JLabel(num1+"");
                  
                label3[i]=new JLabel(quot);
                label4[i]=new JLabel(num2+"");
                label5[i]=new JLabel("=");
                field[i]=new JTextField();
                field[i].setPreferredSize(new Dimension(60,20));
                field[i].addKeyListener(new KeyAdapter() {
                    public void keyTyped(KeyEvent ee) {
                        if((ee.getKeyChar()>'9' || ee.getKeyChar()<'0') && ee.getKeyChar()!=45 && ee.getKeyChar()!='.') {
                            ee.consume();
                        }
                    }
                });
                label6[i]=new JLabel("");  
                Box hbox=Box.createHorizontalBox();
                hbox.add(label1[i]);       
                hbox.add(Box.createHorizontalStrut(20));
                hbox.add(label2[i]);  
                hbox.add(Box.createHorizontalStrut(5));
                hbox.add(label3[i]); 
                hbox.add(Box.createHorizontalStrut(5));
                hbox.add(label4[i]);     
                hbox.add(Box.createHorizontalStrut(5));
                hbox.add(label5[i]);   
                hbox.add(Box.createHorizontalStrut(5));
                hbox.add(field[i]);
                hbox.add(Box.createHorizontalStrut(20));
                hbox.add(label6[i]);
                box.add(hbox);
                box.add(Box.createVerticalStrut(20));
            }
            panel2.add(box);
            panel2.validate();
        }
        int submitCount=0;
        class Listener implements ActionListener{
            public void actionPerformed(ActionEvent e) {
                JButton button=(JButton)e.getSource();
                if(button==btn[0]) {
                    startExam();
                    ExamCount++;
                    btn[0].setEnabled(false);
                      
                    for(int i=1;i<4;i++) {
                        btn[i].setEnabled(true);
                    }
                }
                if(button==btn[1]) {
                    for(int i=0;i<10;i++) {
                        field[i].setText("");
                    }
                }
                if(button==btn[2] ) {
                    rightResultCount=0;
                    btn[2].setEnabled(false);
                    double yourResult=0;
                    for(int i=0;i<10;i++) {
                        try {
                            yourResult=Double.parseDouble(field[i].getText().trim());
                        }catch(Exception ee) {}
                          
                        if(yourResult==result[i]) {
                            rightResultCount++;
                            label6[i].setText("V");
                            label6[i].setForeground(Color.BLUE);
                              
                        }else {
                            label6[i].setText("X");
                            label6[i].setForeground(Color.RED);
                              
                        }
                    }
                    examLabel.setText("你答对了 "+rightResultCount+
                            " 道题,答错了"+(10-rightResultCount)+" 道题!"+
                            "考试得分是: "+rightResultCount*10+" 分!");
                }
                if(button==btn[3]) {
                    btn[2].setEnabled(true);
                    panel2.removeAll();
                    startExam();
                    ExamCount++;
                    btn[3].setEnabled(false);
                    for(int i=0;i<10;i++) {
                        field[i].setText("");
                        label6[i].setText("");
                    }
                    panel2.repaint();
                }
                if(btn[2].isEnabled()==false && btn[3].isEnabled()==false) {
                    btn[1].setEnabled(false);
                }
            }
        }
          
        class Panel1 extends JPanel{
            public Panel1() {
                setPreferredSize(new Dimension(350,120));
                setLayout(new GridLayout(3,1,10,10));
                JTextArea area=new JTextArea("点击“开始”开始答题,答案中有小数的,保留2位!");
                area.setLineWrap(true);
                area.setEditable(false);
                add(area);
                add(examLabel);
                p.add(timeLabel);
                add(p);
            }
        }
      
        class Panel2 extends JPanel{
            public Panel2() {
                setPreferredSize(new Dimension(400,600));  
            }
        }
          
        class Panel3 extends JPanel{
            public Panel3() {
                setPreferredSize(new Dimension(50,100));
                setBackground(Color.LIGHT_GRAY);
                for(int i=0;i<4;i++) {
                    btn[i]=new JButton(btn_name[i]);
                    btn[i].addActionListener(new Listener());
                    add(btn[i]);
                    if(i>0) {
                        btn[i].setEnabled(false);
                    }
                }
            }
        }
    }
    

      

    实验总结:

        一个学期的java学习已经结束,我发现相较于很多同学来说,我的能力还是不够,我还需比其他同学更加努力。而且我深刻的感到,很多学习是需要耐心和实践才能认识到自己的不足,才能不断完善的。在以后的学习里,我会继续深入学习,不断完善自己的知识面,学会活学活用。

       还有对我帮助很多的老师和学长表示感谢。

    意见和建议:

       因为专业课较多,所以如果实验作业在周四下午发布,周天下午五点提交,在这之间时间比较紧张。

       其次,我认为老师课堂教学+助教线上演示+网上教学+章末答疑这种教学方式很新颖也很有用,尤其是助教对我的帮助很大,因为我们在学习中遇到的问题,学长可能也经历过,所以能更好的解答,因为是同龄人,所以交流起来也更自然。还有每周提交完作业后,周一课上的答疑对我帮助也很大。觉得老师可以在以后的学妹学弟们身上继续使用这种教学方式,个人觉得很好。

     

  • 相关阅读:
    数学是最好的语言
    人类的语言--自然语言--》逻辑语言
    为什么大脑喜欢看图?
    思考是调用大脑存储的上下文对输入信息进行处理的过程
    Redis的相关问题总结
    TP5 关联模型使用(嵌套关联、动态排序以及隐藏字段)
    array_column 函数, 以及在PHP5.5之下的替代方法
    thinkphp在app接口开发过程中的通讯安全认证
    PHP开发APP接口实现--基本篇
    分布式与集群的区别是什么?
  • 原文地址:https://www.cnblogs.com/Weiron/p/10199485.html
Copyright © 2020-2023  润新知