王豔 201771010127《面向對象程序設計(java)》第十八週學習總結

實驗十八  總複習java

實驗時間 2018-12-30編程

1、實驗目的與要求多線程

(1) 綜合掌握java基本程序結構;dom

(2) 綜合掌握java面向對象程序設計特色;工具

(3) 綜合掌握java GUI 程序設計結構;佈局

(4) 綜合掌握java多線程編程模型;學習

(5) 綜合編程練習。測試

2、實驗內容和步驟this

任務1:填寫課程課後調查問卷,網址:https://www.wjx.cn/jq/33108969.aspx。spa

任務2:綜合編程練習

練習1:設計一個用戶信息採集程序,要求以下:

    (1) 用戶信息輸入界面以下圖所示:

 

1)用戶點擊提交按鈕時,用戶輸入信息顯示控制檯界面;

2)用戶點擊重置按鈕後,清空用戶已輸入信息;

3)點擊窗口關閉,程序退出。

程序以下:

import java.awt.EventQueue;

import javax.swing.JFrame;

public class Mian {
    public static void main(String[] args) {
        EventQueue.invokeLater(() -> {
            DemoJFrame page = new DemoJFrame();
        });
    }
}
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.LayoutManager;
import java.awt.Panel;
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.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.ButtonModel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

public class DemoJFrame extends JFrame {
    private JPanel jPanel1;
    private JPanel jPanel2;
    private JPanel jPanel3;
    private JPanel jPanel4;
    private JTextField fieldname;
    private JComboBox comboBox;
    private JTextField fieldadress;
    private ButtonGroup bg;
    private JRadioButton nan;
    private JRadioButton nv;
    private JCheckBox sing;
    private JCheckBox dance;
    private JCheckBox draw;

    public DemoJFrame() {
        // 設置窗口大小
        this.setSize(800, 400);
        // 設置可見性
        this.setVisible(true);
        // 設置標題
        this.setTitle("Student Detail");
        // 設置關閉操做
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        // 設置窗口居中
        WinCenter.center(this);
        // 建立四個面板對象
        jPanel1 = new JPanel();
        setJPanel1(jPanel1);
        jPanel2 = new JPanel();
        setJPanel2(jPanel2);
        jPanel3 = new JPanel();
        setJPanel3(jPanel3);
        jPanel4 = new JPanel();
        setJPanel4(jPanel4);
        // 設置容器爲流式佈局
        FlowLayout flowLayout = new FlowLayout();
        this.setLayout(flowLayout);
        // 將四個面板添加到容器中
        this.add(jPanel1);
        this.add(jPanel2);
        this.add(jPanel3);
        this.add(jPanel4);

    }

    /*
     * 設置面板一
     */
    private void setJPanel1(JPanel jPanel) {
        jPanel.setPreferredSize(new Dimension(700, 45));//設置此組件的首選大小
        // 給面板的佈局設置爲網格佈局 一行4列
        jPanel.setLayout(new GridLayout(1, 4));
        JLabel name = new JLabel("Name:");
        name.setSize(100, 50);
        fieldname = new JTextField("");
        fieldname.setSize(80, 20);
        JLabel study = new JLabel("Qualification:");
        comboBox = new JComboBox();
        comboBox.addItem("Graduate");
        comboBox.addItem("senior");
        comboBox.addItem("Undergraduate");
        jPanel.add(name);
        jPanel.add(fieldname);
        jPanel.add(study);
        jPanel.add(comboBox);

    }

    /*
     * 設置面板二
     */
    private void setJPanel2(JPanel jPanel) {
        jPanel.setPreferredSize(new Dimension(700, 50));
        // 給面板的佈局設置爲網格佈局 一行4列
        jPanel.setLayout(new GridLayout(1, 4));
        JLabel name = new JLabel("Address:");
        fieldadress = new JTextField();
        fieldadress.setPreferredSize(new Dimension(150, 50));
        JLabel study = new JLabel("Hobby:");
        JPanel selectBox = new JPanel();
        selectBox.setBorder(BorderFactory.createTitledBorder(""));
        selectBox.setLayout(new GridLayout(3, 1));
        sing = new JCheckBox("Singing");
        dance = new JCheckBox("Dancing");
        draw = new JCheckBox("Reading");
        selectBox.add(sing);
        selectBox.add(dance);
        selectBox.add(draw);
        jPanel.add(name);
        jPanel.add(fieldadress);
        jPanel.add(study);
        jPanel.add(selectBox);
    }

    /*
     * 設置面板三
     */
    private void setJPanel3(JPanel jPanel) {
        jPanel.setPreferredSize(new Dimension(700, 150));
        FlowLayout flowLayout = new FlowLayout(FlowLayout.LEFT);
        jPanel.setLayout(flowLayout);
        JLabel sex = new JLabel("Sex:");
        JPanel selectBox = new JPanel();
        selectBox.setBorder(BorderFactory.createTitledBorder(""));
        selectBox.setLayout(new GridLayout(2, 1));
        bg = new ButtonGroup();
        nan = new JRadioButton("Male");
        nv = new JRadioButton("Female");
        bg.add(nan);
        bg.add(nv);
        selectBox.add(nan);
        selectBox.add(nv);
        jPanel.add(sex);
        jPanel.add(selectBox);

    }

    /*
     * 設置面板四
     */
    private void setJPanel4(JPanel jPanel) {
        // TODO 自動生成的方法存根
        jPanel.setPreferredSize(new Dimension(700, 150));
        FlowLayout flowLayout = new FlowLayout(FlowLayout.CENTER, 50, 10);
        jPanel.setLayout(flowLayout);
        jPanel.setLayout(flowLayout);
        JButton sublite = new JButton("Validate");
        JButton reset = new JButton("Reset");
        sublite.addActionListener((e) -> valiData());
        reset.addActionListener((e) -> Reset());
        jPanel.add(sublite);
        jPanel.add(reset);
    }

    /*
     * 提交數據
     */
    private void valiData() {
        // 拿到數據
        String name = fieldname.getText().toString().trim();
        String xueli = comboBox.getSelectedItem().toString().trim();
        String address = fieldadress.getText().toString().trim();
        System.out.println(name);
        System.out.println(xueli);
        String hobbystring="";
        if (sing.isSelected()) {
            hobbystring+="Singing   ";
        }
        if (dance.isSelected()) {
            hobbystring+="Dancing   ";
        }
        if (draw.isSelected()) {
            hobbystring+="Reading  ";
        }
        System.out.println(address);
        if (nan.isSelected()) {
            System.out.println("Male");
        }
        if (nv.isSelected()) {
            System.out.println("Female");
        }
        System.out.println(hobbystring);
    }

    /*
     * 重置
     */
    private void Reset() {
        fieldadress.setText(null);
        fieldname.setText(null);
        comboBox.setSelectedIndex(0);
        sing.setSelected(false);
        dance.setSelected(false);
        draw.setSelected(false);
        bg.clearSelection();
    }
}
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.Window;

public class WinCenter {
    public static void center(Window win){
        Toolkit tkit = Toolkit.getDefaultToolkit();//獲取默認工具包
        Dimension sSize = tkit.getScreenSize();//獲取屏幕的大小
        Dimension wSize = win.getSize();
        if(wSize.height > sSize.height){
            wSize.height = sSize.height;
        }
        if(wSize.width > sSize.width){
            wSize.width = sSize.width;
        }
        win.setLocation((sSize.width - wSize.width)/ 2, (sSize.height - wSize.height)/ 2);//將組件移到新的位置
    }
}

程程序運行結果以下:

練習2:採用GUI界面設計如下程序:

編制一個程序,將身份證號.txt 中的信息讀入到內存中;

按姓名字典序輸出人員信息;

查詢最大年齡的人員信息;

查詢最小年齡人員信息;

輸入你的年齡,查詢身份證號.txt中年齡與你最近人的姓名、身份證號、年齡、性別和出生地;

查詢人員中是否有你的同鄉。

輸入身份證信息,查詢所提供身份證號的人員信息,要求輸入一個身份證數字時,查詢界面就顯示知足查詢條件的查詢結果,且隨着輸入的數字的增多,查詢匹配的範圍逐漸縮小。

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.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Scanner;

public class Main{
    private static ArrayList<Student> studentlist;
    public static void main(String[] args) {
        studentlist = new ArrayList<>();
        Scanner scanner = new Scanner(System.in);
        File file = new File("D:\\text");
        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 number = linescanner.next();
                String sex = linescanner.next();
                String age = linescanner.next();
                String province =linescanner.nextLine();
                Student student = new Student();
                student.setName(name);
                student.setnumber(number);
                student.setsex(sex);
                int a = Integer.parseInt(age);
                student.setage(a);
                student.setprovince(province);
                studentlist.add(student);

            }
        } catch (FileNotFoundException e) {
            System.out.println("學生信息文件找不到");
            e.printStackTrace();
        } catch (IOException e) {
            System.out.println("學生信息文件讀取錯誤");
            e.printStackTrace();
        }
        boolean isTrue = true;
        while (isTrue) {
            System.out.println("選擇你的操做,輸入正確格式的選項");
            System.out.println("A.字典排序");
            System.out.println("B.輸出年齡最大和年齡最小的人");
            System.out.println("C.尋找老鄉");
            System.out.println("D.尋找年齡相近的人");
            System.out.println("F.退出");
            String m = scanner.next();
            switch (m) {
            case "A":
                Collections.sort(studentlist);              
                System.out.println(studentlist.toString());
                break;
            case "B":
                 int max=0,min=100;
                 int j,k1 = 0,k2=0;
                 for(int i=1;i<studentlist.size();i++)
                 {
                     j=studentlist.get(i).getage();
                 if(j>max)
                 {
                     max=j; 
                     k1=i;
                 }
                 if(j<min)
                 {
                   min=j; 
                   k2=i;
                 }
                 
                 }  
                 System.out.println("年齡最大:"+studentlist.get(k1));
                 System.out.println("年齡最小:"+studentlist.get(k2));
                break;
            case "C":
                 System.out.println("老家?");
                 String find = scanner.next();        
                 String place=find.substring(0,3);
                 for (int i = 0; i <studentlist.size(); i++) 
                 {
                     if(studentlist.get(i).getprovince().substring(1,4).equals(place)) 
                         System.out.println("老鄉"+studentlist.get(i));
                 }             
                 break;
                 
            case "D":
                System.out.println("年齡:");
                int yourage = scanner.nextInt();
                int near=agenear(yourage);
                int value=yourage-studentlist.get(near).getage();
                System.out.println(""+studentlist.get(near));
                break;
            case "F":
                isTrue = false;
                System.out.println("退出程序!");
                break;
                default:
                System.out.println("輸入有誤");

            }
        }
    }
        public static int agenear(int age) {      
        int j=0,min=53,value=0,k=0;
         for (int i = 0; i < studentlist.size(); i++)
         {
             value=studentlist.get(i).getage()-age;
             if(value<0) value=-value; 
             if (value<min) 
             {
                min=value;
                k=i;
             } 
          }    
         return k;         
      }

}
public class Student implements Comparable<Student> {

    private String name;
    private String number ;
    private String sex ;
    private int age;
    private String province;
   
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getnumber() {
        return number;
    }
    public void setnumber(String number) {
        this.number = number;
    }
    public String getsex() {
        return sex ;
    }
    public void setsex(String sex ) {
        this.sex =sex ;
    }
    public int getage() {

        return age;
        }
        public void setage(int age) {
            // int a = Integer.parseInt(age);
        this.age= age;
        }

    public String getprovince() {
        return province;
    }
    public void setprovince(String province) {
        this.province=province ;
    }

    public int compareTo(Student o) {
       return this.name.compareTo(o.getName());
    }

    public String toString() {
        return  name+"\t"+sex+"\t"+age+"\t"+number+"\t"+province+"\n";
    }    
}

 

 

練習3:採用GUI界面設計如下程序

編寫一個計算器類,能夠完成加、減、乘、除的操做

利用計算機類,設計一個小學生100之內數的四則運算練習程序,由計算機隨機產生10道加減乘除練習題,學生輸入答案,由程序檢查答案是否正確,每道題正確計10分,錯誤不計分,10道題測試結束後給出測試總分;

將程序中測試練習題及學生答題結果輸出到文件,文件名爲test.txt。

程序以下:

import java.util.Scanner;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Random;

public class Demo {
    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);
        Number counter = new Number();
        PrintWriter out = null;
        try {
            out = new PrintWriter("text.txt");
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        int sum = 0;

        for (int i = 1; i <= 10; i++) {

            int a = (int) Math.round(Math.random() * 100);
            int b = (int) Math.round(Math.random() * 100);
            int m = (int) Math.round(Math.random() * 3);
            Random n = new Random();

            switch (m) {
            case 0:
                System.out.println(i + ": " + a + "/" + b + "=");

                while (b == 0) {
                    b = (int) Math.round(Math.random() * 100);
                }

                int c = in.nextInt();
                out.println(a + "/" + b + "=" + c);
                if (c == counter.division(a, b)) {
                    sum += 10;
                    System.out.println("恭喜答案正確");
                } else {
                    System.out.println("抱歉,答案錯誤");
                }

                break;

            case 1:
                System.out.println(i + ": " + a + "*" + b + "=");
                int c1 = in.nextInt();
                out.println(a + "*" + b + "=" + c1);
                if (c1 == counter.multiplication(a, b)) {
                    sum += 10;
                    System.out.println("恭喜答案正確");
                } else {
                    System.out.println("抱歉,答案錯誤");
                }
                break;
            case 2:
                System.out.println(i + ": " + a + "+" + b + "=");
                int c2 = in.nextInt();
                out.println(a + "+" + b + "=" + c2);
                if (c2 == counter.add(a, b)) {
                    sum += 10;
                    System.out.println("恭喜答案正確");
                } else {
                    System.out.println("抱歉,答案錯誤");
                }

                break;
            case 3:
                System.out.println(i + ": " + a + "-" + b + "=");
                int c3 = in.nextInt();
                out.println(a + "-" + b + "=" + c3);
                if (c3 == counter.reduce(a, b)) {
                    sum += 10;
                    System.out.println("恭喜答案正確");
                } else {
                    System.out.println("抱歉,答案錯誤");
                }
                break;

            }

        }
        System.out.println("成績" + sum);
        out.println("成績:" + sum);
        out.close();

    }
}
public class Number {
    private int a;
    private int b;

    public int add(int a, int b) {
        return a + b;
    }

    public int reduce(int a, int b) {
        return a - b;
    }

    public int multiplication(int a, int b) {
        return a * b;
    }

    public int division(int a, int b) {
        if (b != 0)
            return a / b;
        else
            return 0;
    }

}

程序運行結果:

三:實驗總結

任務3:本學期課程已結束,請彙總《面向對象程序設計課程學習進度條》的數據,統計我的專業能力提高的數據。並從學習內容、學習方法、學習心得幾個方面進行課程學習總結,也但願你對課程的不足提出建議和意見。

       這一學期,咱們學習了java這門語言,一開始拿到書以爲那麼厚無從下手,可是通過一學期的學習,對Java語言產生了很大的興趣,整體來講,咱們這學期學習了java的基本程序設計結構、對象與類、繼承、接口、泛型程序設計以及圖形用戶界面幾個大塊。在學習過程當中,老師主要採用課堂講解,實驗課上經過本身運行成型來強化理論知識。有時候線運行程序進行預習,在課堂講解。此外,老師在課堂上請了助教,經過助教給咱們演示以及講解程序,咱們也學到了不少東西。我知道,短短一學期的學習,來學習一門語言課程是遠遠不夠的,雖然課程結束了,但我還會繼續學習這門語言,提高本身的JAVA語言技能。

相關文章
相關標籤/搜索