标签:信息 多线程 fileinput vat editable ram ide i++ 回顾
1、实验目的与要求
(1) 综合掌握java基本程序结构;
(2) 综合掌握java面向对象程序设计特点;
(3) 综合掌握java GUI 程序设计结构;
(4) 综合掌握java多线程编程模型;
(5) 综合编程练习。
2、实验内容和步骤
任务1:填写课程课后调查问卷,网址:https://www.wjx.cn/jq/33108969.aspx。
任务2:综合编程练习
练习1:设计一个用户信息采集程序,要求如下:
(1) 用户信息输入界面如下图所示:
1)用户点击提交按钮时,用户输入信息显示控制台界面;
(2)用户点击重置按钮后,清空用户已输入信息;
(3)点击窗口关闭,程序退出。
import java.awt.EventQueue; import javax.swing.JFrame; public class Main { public static void main(String[] args) { EventQueue.invokeLater(() -> { DemoJFrame page = new DemoJFrame(); }); } }
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); } }
import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.border.*; public class DemoJFrame extends JFrame { public DemoJFrame() { JPanel panel1 = new JPanel(); panel1.setPreferredSize(new Dimension(700, 45)); panel1.setLayout(new GridLayout(1, 4)); JLabel label1 = new JLabel("Name:"); JTextField j1 = new JTextField(""); JLabel label2 = new JLabel("Qualification:"); JComboBox<Object> j2 = new JComboBox<>(); j2.addItem("chuzhong"); j2.addItem("gaozhong"); j2.addItem("undergraduate"); panel1.add(label1); panel1.add(j1); panel1.add(label2); panel1.add(j2); JPanel panel2 = new JPanel(); panel2.setPreferredSize(new Dimension(700, 65)); panel2.setLayout(new GridLayout(1, 4)); JLabel label3 = new JLabel("Address:"); JTextArea j3 = new JTextArea(); JLabel label4 = new JLabel("Hobby:"); JPanel p = new JPanel(); p.setLayout(new GridLayout(3, 1)); p.setBorder(BorderFactory.createLineBorder(null)); JCheckBox c1 = new JCheckBox("Reading"); JCheckBox c2 = new JCheckBox("Singing"); JCheckBox c3 = new JCheckBox("Dancing"); p.add(c1); p.add(c2); p.add(c3); panel2.add(label3); panel2.add(j3); panel2.add(label4); panel2.add(p); JPanel panel3 = new JPanel(); panel3.setPreferredSize(new Dimension(700, 150)); FlowLayout flowLayout1 = new FlowLayout(FlowLayout.LEFT, 70, 40); panel3.setLayout(flowLayout1); JLabel label5 = new JLabel("Sex:"); JPanel p1 = new JPanel(); p1.setLayout(new GridLayout(2,1)); p1.setBorder(BorderFactory.createLineBorder(null)); ButtonGroup bu = new ButtonGroup(); JRadioButton jr1 = new JRadioButton("Male"); JRadioButton jr2 = new JRadioButton("Female"); bu.add(jr1); bu.add(jr2); p1.add(jr1); p1.add(jr2); panel3.add(label5); panel3.add(p1); add(panel1); add(panel2); add(panel3); JPanel panel4 = new JPanel(); panel4.setPreferredSize(new Dimension(700, 150)); JButton b1 = new JButton("Validate"); panel4.add(b1); JButton b2 = new JButton("Reset"); panel4.add(b2); add(panel4); FlowLayout flowLayout = new FlowLayout(); this.setLayout(flowLayout); this.setTitle("Students Detail"); this.setBounds(300, 300, 800, 400); this.setVisible(true); this.setDefaultCloseOperation(DISPOSE_ON_CLOSE); b1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { // TODO 自动生成的方法存根 String xueli = j2.getSelectedItem().toString(); System.out.println("Name:" + j1.getText()); System.out.println("Qualification:" + xueli); String hobbystring = "Hobby:"; if (c1.isSelected()) { hobbystring += "Reading"; } if (c2.isSelected()) { hobbystring += "Singing"; } if (c3.isSelected()) { hobbystring += "Dancing"; } System.out.println("Address:" + j3.getText()); if (jr1.isSelected()) { System.out.println("Sex:Male"); } if (jr2.isSelected()) { System.out.println("Sex:Female"); } System.out.println(hobbystring); } }); b2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { // TODO 自动生成的方法存根 j1.setText(null); j3.setText(null); j2.setSelectedIndex(0); c1.setSelected(false); c2.setSelected(false); c3.setSelected(false); bu.clearSelection(); } }); } public static void main(String args[]) { new DemoJFrame(); } }
结果如下:
练习2:采用GUI界面设计以下程序:
l 编制一个程序,将身份证号.txt 中的信息读入到内存中;
l 按姓名字典序输出人员信息;
l 查询最大年龄的人员信息;
l 查询最小年龄人员信息;
l 输入你的年龄,查询身份证号.txt中年龄与你最近人的姓名、身份证号、年龄、性别和出生地;
l 查询人员中是否有你的同乡。
l 输入身份证信息,查询所提供身份证号的人员信息,要求输入一个身份证数字时,查询界面就显示满足查询条件的查询结果,且随着输入的数字的增多,查询匹配的范围逐渐缩小。
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"; } }
j结果如下:
练习3:采用GUI界面设计以下程序
l 编写一个计算器类,可以完成加、减、乘、除的操作
l 利用计算机类,设计一个小学生100以内数的四则运算练习程序,由计算机随机产生10道加减乘除练习题,学生输入答案,由程序检查答案是否正确,每道题正确计10分,错误不计分,10道题测试结束后给出测试总分;
l 将程序中测试练习题及学生答题结果输出到文件,文件名为test.txt。
package 答题; import java.awt.*; import java.awt.event.*; import java.util.*; import java.util.Random; import javax.swing.*; public class Exam 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 Exam("测试").setVisible(true); } public Exam(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); } } } } }
任务3:本学期课程已结束,请汇总《面向对象程序设计课程学习进度条》的数据,统计个人专业能力提升的数据。并从学习内容、学习方法、学习心得几个方面进行课程学习总结,也希望你对课程的不足提出建议和意见。
学习总结:
1、OOPS主要优点:
好处:提高了可复用性
~多态性:多态性是一种(编程时)对于不同基础形式(数据类型)呈现相同接口的能力。这意味着类有不同的功能,同时共享一个通用的接口,可以通过传递特定类别的参考动态调用。典型的例子是Shape类和所有可以继承它(方形,圆形,十二面体,不规则的多边形,图示等)的类。每个类都有一个 Draw ()方法。
好处如下:
基类的行为(即方法)和状态(即变量)可以在其派生子类中访问。继承的主要优点是,它提供了代码重用和避免重复继承的正式机制,这也会导致紧耦合的设计,因为如果你要更改的超类,你必须知道子类的所有细节。
好处:
提高了可复用性
创建了逻辑上的"is a"关系,如 Dog is a animal
模块化代码
避免重复
回顾我们的学习任务条,上面的数据显示我们每次作业代码的增加量和学习的进度,感觉一学期下来,且不说实实在在掌握了多少,但也下了功夫在上面,看着每周自己读译的代码渐渐变多,内心有着充实感,在复习阶段也可以去翻翻以前的作业来加固知识。一学期快结束了,在老师和学长的帮助下,我们也对java这门课有了了解和掌握,希望以后也可以去自己学习这方面的知识,不要丢了忘了。最后,再次感谢我们的老师!(送花^-^)
201771010135 杨蓉庆《面对对象程序设计(java)》第十八周学习总结
标签:信息 多线程 fileinput vat editable ram ide i++ 回顾
原文地址:https://www.cnblogs.com/YRQY/p/10186599.html