马凯军201771010116《面向对象与程序设计Java》

实验十八  总复习

实验时间 2018-12-30

1、实验目的与要求

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

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

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

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

(5) 综合编程练习。

2、实验内容和步骤

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

马凯军201771010116《面向对象与程序设计Java》_第1张图片

任务2:综合编程练习

练习1:设计一个用户信息采集程序,要求如下:

    (1) 用户信息输入界面如下图所示:

 马凯军201771010116《面向对象与程序设计Java》_第2张图片

(1)用户点击提交按钮时,用户输入信息显示控制台界面;

(2)用户点击重置按钮后,清空用户已输入信息;

(3)点击窗口关闭,程序退出。

程序如下:

按 Ctrl+C 复制代码
import java.awt.EventQueue; import javax.swing.JFrame; public class Mian { public static void main(String[] args) { EventQueue.invokeLater(() -> { DemoJFrame page = new DemoJFrame(); }); } }
按 Ctrl+C 复制代码
按 Ctrl+C 复制代码
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(); } }
按 Ctrl+C 复制代码
按 Ctrl+C 复制代码
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);//将组件移到新的位置 } }
按 Ctrl+C 复制代码

程程序运行结果如下:

马凯军201771010116《面向对象与程序设计Java》_第3张图片

马凯军201771010116《面向对象与程序设计Java》_第4张图片

练习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 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) { 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 ) { 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 {

    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"; } }
复制代码

 马凯军201771010116《面向对象与程序设计Java》_第5张图片

 

练习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; } }
复制代码

程序运行结果:

马凯军201771010116《面向对象与程序设计Java》_第6张图片马凯军201771010116《面向对象与程序设计Java》_第7张图片

三:实验总结

任务3:本学期课程已结束,请汇总《面向对象程序设计课程学习进度条》的数据,统计个人专业能力提升的数据。并从学习内容、学习方法、学习心得几个方面进行课程学习总结,也希望你对课程的不足提出建议和意见。

      在大一的时候接触了C语言,刚拿到书的时候,相对于高中的书厚了很多,虽然学的不怎么好,但最后还是学完了。这学期当拿到相当于两个C语言的Java时,感觉还是C语言好呀(哈哈),这一学期下来在老师的讲解中,学长的不断辅导里,对这门新的编程语言有了认识和了解。也学习到了很多知识, 也对Java语言产生了一些兴趣,总体来说,Java语言我们这学期学习了java的基本程序设计结构、对象与类、继承、接口、泛型程序设计以及图形用户界面几个大块。在学习过程中,老师主要采用课堂讲解,实验课上通过自己运行成型来强化理论知识。有时候线运行程序进行预习,在课堂讲解。此外,在课堂上老师给我们请了学长(学霸),通过学长给我们演示以及讲解程序,我也学到了很多东西。我知道,短短一学期的学习,来学习一门语言课程是远远不够的,虽然课程结束了,但我还会继续学习这门语言,提升自己的JAVA语言技能。在老师和学长的帮助下,我才对java这门课有了更好的了解和掌握,非常感谢老师和学长!

你可能感兴趣的:(马凯军201771010116《面向对象与程序设计Java》)