版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、实验大纲1 字符统计程序程序运行结果:统计字符源文件:StaChar.javaimport javax.swing.*;/* * 1 字符统计程序 * 利用对话框读入字符串 统计输入字符行中数字字符、英文字母个数. * author 黎明你好 */public class StaCharpublic static void main(String args)String str = JOptionPane.showInputDialog(请输入字符串:);char c = str.toCharArray();int numberCount = 0;int letterCount = 0;for
2、 (int i = 0; i c.length; i+)if (ci 0)numberCount+;else if (ci A & ci a & ci z)letterCount+;String result = 输入内容:n + str + n数字字符: + numberCount + 个; + n字母: + letterCount+ 个;JOptionPane.showMessageDialog(null, result, 结果:, JOptionPane.INFORMATION_MESSAGE);2 找质数程序程序运行结果:输出质数原文件:PrintPrime.javaimport ja
3、vax.swing.JOptionPane;/* * 2 找质数程序,利用对话框读入整数,输出2至这个整数之间的质数. * author 黎明你好 */public class PrintPrimeprivate int number;/ 正整数private String result = ;public PrintPrime()/ 构造方法number = getIntegerNumber(输入整数n, 0);/ 要求是=0的整数if (number 0)return;/ 出现错误,程序结束else/ 如果大于等于2,开始用循环计算结果for (int i = 2; i = number;
4、 i+) / 计算素数和if (isPrimeNumber(i)result += i + ;/ 显示最后的和JOptionPane.showMessageDialog(null, number + 之前所有素数为:n “ + result + ”, 显示结果,JOptionPane.INFORMATION_MESSAGE);/* * 通过图形界面,得到符合规则的正整数的方法 * param message - 在弹出的对话框中,显示提示信息 * param min - 要求此数必须大于等于min * return - 返回符合规则的整数 */public int getIntegerNumb
5、er(String message, int min)String str = JOptionPane.showInputDialog(null, message, 提示信息,JOptionPane.INFORMATION_MESSAGE);int number = -1;trynumber = Integer.parseInt(str); / 得到输入的正整数catch( Exception e )JOptionPane.showMessageDialog(null, 输入非数字字符n程序结束, 错误警告, JOptionPane.ERROR_MESSAGE);return -1; / 输入
6、的不是数字字符,程序结束if (number min)JOptionPane.showMessageDialog(null, 输入的数不符合规则,不是正整数n程序结束, 错误警告,JOptionPane.ERROR_MESSAGE);return -1; / 输入的数不是大于2的正整数时候,程序结束elsereturn number;/* * 判断是否是素数的方法 * param n - 需要判断的数 * return - 是素数返回true,否则返回false */public boolean isPrimeNumber(int n)for (int i = 2; i n; i+)if (n
7、 % i = 0)return false;return true;/* main方法 */public static void main(String args)new PrintPrime();3 类的继承定义,包括几何形状类Shape、圆形类Circle.、矩形类Rectangle/* * 几何图形类,抽象类 */abstract class Shapepublic float area()return 0.0f;/* * 圆形类 */class Circle extends Shapeprivate float R;public Circle(float r)R = r;public
8、float area()return (float) (Math.PI * R * R);/* * 矩形类 */class Rectangle extends Shapeprivate float w, h;public Rectangle(float w, float h)this.w = w;this.h = h;public float area()return w * h;public class Work11_3public static void main(String args)Circle c;Rectangle r;c = new Circle(2.0f);r = new R
9、ectangle(3.0f, 5.0f);System.out.println(圆面积 + returnArea(c);System.out.println(长方形面积 + returnArea(r);static float returnArea(Shape s)return s.area();4 数组排序程序源文件:Work11_4.javaimport javax.swing.*;import java.util.*;/* * 4 数组排序程序. * 输入整数序列,对输入的整数进行排序,输出结果. * author 黎明你好 */public class Work11_4public s
10、tatic final int RISE = 0;public static final int LOWER = 1;public static void main(String args)String str = JOptionPane.showInputDialog(请输入字符串:);StringTokenizer token = new StringTokenizer(str, ,.;: );int mode = Work11_4.RISE;/ 排列模式,默认为升序排列int count = token.countTokens();/ 输入的整数的个数int array = new in
11、tcount;int index = 0;while (token.hasMoreTokens()tryarrayindex = Integer.parseInt(token.nextToken();index+;catch( Exception e )JOptionPane.showMessageDialog(null, 输入非法字符, 错误警告, JOptionPane.ERROR_MESSAGE);return;/ 输入非法字符时候,直接结束程序sort(array, mode);/ 按mode模式,进行排序String result = new String();String mode
12、String = new String();if (mode = Work11_4.RISE)modeString = 升序排列结果为:;if (mode = Work11_4.LOWER)modeString = 降序排列结果为:;for (int i = 0; i array.length; i+)result = result + arrayi + ,;if (result != null)JOptionPane.showMessageDialog(null, result, modeString, JOptionPane.INFORMATION_MESSAGE);/* * 给数组排序的
13、方法 * param array - 需要排序的数组 * param mode - 排序的模式,可以为RISE,LOWER */public static void sort(int array, int mode)for (int i = 0; i array.length; i+)for (int j = 0; j arrayj + 1)int temp = arrayj;arrayj = arrayj + 1;arrayj + 1 = temp;if (mode = Work11_4.LOWER & arrayj arrayj + 1)int temp = arrayj;arrayj =
14、 arrayj + 1;arrayj + 1 = temp;5 字符串处理程序,括号匹配程序运行结果:括号匹配检测源文件:CheckBrackets.javaimport java.awt.*;import java.awt.event.*;import javax.swing.*;import unit9.MyFileFilter;import java.io.*;/* * 5 字符串处理程序. * 输入程序的源程序代码行,找出可能存在圆括号,花括号不匹配的错误. * author 黎明你好 */public class CheckBrackets extends JFrame implem
15、ents ActionListener, ItemListenerprivate static final long serialVersionUID = 1L;private JFileChooser fileChooser;private JButton openFileButton;private JComboBox comboBox;private JTextField showRowStringField;private JTextField showMessageField;private JTextArea textArea;private JPanel northPanel,
16、control_panel;private String rowString;private File file = null;public CheckBrackets()super(检测圆、花括号匹配程序);fileChooser = new JFileChooser(System.getProperty(user.dir);openFileButton = new JButton(打开文件);showRowStringField = new JTextField();showMessageField = new JTextField(20);textArea = new JTextArea
17、();comboBox = new JComboBox();northPanel = new JPanel();control_panel = new JPanel();rowString = new String1000;fileChooser.addChoosableFileFilter(new MyFileFilter(txt);/文件筛选textArea.setLineWrap(true);showRowStringField.setEditable(false);showRowStringField.setBackground(Color.WHITE);showMessageFiel
18、d.setEditable(false);showMessageField.setBackground(Color.WHITE);openFileButton.addActionListener(this);comboBox.addItemListener(this);comboBox.addItem(请选择);control_panel.add(openFileButton);control_panel.add(new JLabel( 选择代码行:);control_panel.add(comboBox);control_panel.add(new JLabel(检测结果:);control
19、_panel.add(showMessageField);northPanel.setLayout(new GridLayout(2, 1, 10, 10);northPanel.add(control_panel);northPanel.add(showRowStringField);this.add(northPanel, BorderLayout.NORTH);this.add(new JScrollPane(textArea), BorderLayout.CENTER);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.s
20、etBounds(50, 50, 550, 500);this.setVisible(true);this.validate();public void actionPerformed(ActionEvent e)showMessageField.setText();int message = fileChooser.showOpenDialog(this);if (message = JFileChooser.APPROVE_OPTION)file = fileChooser.getSelectedFile();comboBox.removeAllItems();comboBox.addIt
21、em(请选择);readFile(file);public void itemStateChanged(ItemEvent e)int index = comboBox.getSelectedIndex();if (index = 1)showRowStringField.setText(rowStringindex - 1);char c = rowStringindex - 1.toCharArray();int count1 = 0;int count2 = 0;for (int i = 0; i c.length; i+)if (ci = )count1+;if (ci = )coun
22、t1-;if (ci = ()count2+;if (ci = )count2-;System.out.println(大括号 + count1 + ,小括号: + count2);if (count1 != 0)showMessageField.setText(第 + index + 行,大括号匹配错误);else if (count2 != 0)showMessageField.setText(第 + index + 行,小括号()匹配错误);else if (count1 != 0 & count2 != 0)showMessageField.setText(第 + index + 行,
23、大、小括号都匹配错误);else if (count1 = 0 & count2 = 0)showMessageField.setText(括号匹配正确);public void readFile(File f)if (f != null)tryFileReader file = new FileReader(f);BufferedReader in = new BufferedReader(file);String s = new String();int i = 0;textArea.setText();while (s = in.readLine() != null)textArea.a
24、ppend(i + 1) + : + s + n);rowStringi = s;comboBox.addItem(i + 1);i+;catch( Exception e )System.out.println( + e.toString();public static void main(String args)new CheckBrackets();6 计算器程序。程序运行结果:简易计算器程序原文件: Calculator.javaimport java.awt.*;import java.awt.event.*;import javax.swing.*;/* * 计算器程序. * 三个
25、文本框,加、减、乘、除按钮 在前两个文本框分别输入两个运算数 点击按钮后,在第三个文本框中显示计算结果. * author 黎明你好 */public class Calculator extends JFrame implements ActionListenerprivate static final long serialVersionUID = 1L;private JTextField oneField, twoField, resultField;private JButton addButton, subtractButton, multiplyButton, divideBut
26、ton, cleanButton;private JPanel panel1, panel2, panel3;public Calculator()super(简易计算器);oneField = new JTextField(10);twoField = new JTextField(10);resultField = new JTextField(20);addButton = new JButton(+);subtractButton = new JButton();multiplyButton = new JButton(*);divideButton = new JButton(/);
27、cleanButton = new JButton(CE);panel1 = new JPanel();panel2 = new JPanel();panel3 = new JPanel();oneField.setHorizontalAlignment(JTextField.RIGHT);twoField.setHorizontalAlignment(JTextField.RIGHT);resultField.setHorizontalAlignment(JTextField.RIGHT);resultField.setEditable(false);resultField.setBackg
28、round(Color.WHITE);resultField.setForeground(Color.RED);panel3.setLayout(new GridLayout(1, 4, 5, 5);panel3.add(addButton);panel3.add(subtractButton);panel3.add(multiplyButton);panel3.add(divideButton);panel3.add(cleanButton);addButton.addActionListener(this);subtractButton.addActionListener(this);mu
29、ltiplyButton.addActionListener(this);divideButton.addActionListener(this);cleanButton.addActionListener(this);panel1.add(new JLabel(输入x:);panel1.add(oneField);panel2.add(new JLabel(输入y:);panel2.add(twoField);this.setLayout(new FlowLayout();this.add(panel1);this.add(panel2);this.add(panel3);this.add(
30、resultField);this.setBounds(200, 100, 300, 200);this.setVisible(true);this.validate();this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);public void actionPerformed(ActionEvent e)double x = 0;double y = 0;tryx = Double.parseDouble(oneField.getText();y = Double.parseDouble(twoField.getText();catch(
31、NumberFormatException e1 )resultField.setText(请输入数字字符);if (e.getSource() = addButton)resultField.setText(X + Y = + (x + y);else if (e.getSource() = subtractButton)resultField.setText(X - Y = + (x - y);else if (e.getSource() = multiplyButton)resultField.setText(X Y = + (x * y);else if (e.getSource()
32、= divideButton)if (y = 0)resultField.setText(除数不能为0);elseresultField.setText(X Y = + (x / y);if (e.getSource() = cleanButton)oneField.setText();twoField.setText();resultField.setText();public static void main(String args)new Calculator();7 选择框应用程序。程序运行结果:选择框程序源文件: Work11_7.javaimport java.awt.*;impo
33、rt java.awt.event.*;import javax.swing.*;/* * 7 选择框应用程序. * 使用选择框选择商品,在文本框显示商品的单价、产地等信息. * author 黎明你好 */public class Work11_7 extends JFrame implements ActionListenerprivate static final long serialVersionUID = 1L;private JPanel panel1, panel2;private Goods goods1, goods2, goods3, goods4;private JTe
34、xtField showNameText; / 显示商品名字private JTextField showCostText; / 显示单价private JTextField showPlaceText;/ 显示产地private JTextField showWeightText; / 显示重量private ButtonGroup group;public Work11_7()super(选择框应用程序);panel1 = new JPanel();panel2 = new JPanel();group = new ButtonGroup();goods1 = new Goods(高露洁牙
35、膏, 10.45, 广州, 850);goods2 = new Goods(飘柔洗发露, 16.90, 天津, 1530.5);goods3 = new Goods(老干妈肉酱, 9.80, 贵阳, 210);goods4 = new Goods(可比克薯片, 8.50, 吉林, 45);showNameText = new JTextField(10);showCostText = new JTextField(10);showPlaceText = new JTextField(10);showWeightText = new JTextField(10);addGoods(goods1)
36、;addGoods(goods2);addGoods(goods3);addGoods(goods4);panel2.setLayout(new GridLayout(4, 2);panel2.add(new JLabel(商品名称:);panel2.add(showNameText);panel2.add(new JLabel(商品单价:);panel2.add(showCostText);panel2.add(new JLabel(商品产地:);panel2.add(showPlaceText);panel2.add(new JLabel(商品重量:);panel2.add(showWei
37、ghtText);this.setLayout(new FlowLayout();this.add(panel1);this.add(panel2);this.setBounds(200, 100, 400, 200);this.setVisible(true);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);public void addGoods(Goods goods)JCheckBox box = new JCheckBox(goods.getName();group.add(box);box.addActionListener(
38、this);panel1.add(box);public void actionPerformed(ActionEvent e)String name = e.getActionCommand();if (name.equals(goods1.getName()setGoodsText(goods1);if (name.equals(goods2.getName()setGoodsText(goods2);if (name.equals(goods3.getName()setGoodsText(goods3);if (name.equals(goods4.getName()setGoodsTe
39、xt(goods4);public void setGoodsText(Goods goods)showNameText.setText( + goods.getName();showPlaceText.setText( + goods.getPlace();showCostText.setText( + goods.getCost() + 元);showWeightText.setText( + goods.getWeight() + 克);public static void main(String args)new Work11_7();用到的商品类源文件:Goods.java/* *
40、商品类 */class Goodsprivate String name;/ 商品名称private double cost;/ 商品单价,单位元private String place;/ 商品产地private double weight;/ 商品重量,单位克public Goods(String name, double cost, String place, double weight) = name;this.cost = cost;this.place = place;this.weight = weight;public String getName()retu
41、rn name;public String getPlace()return place;public double getCost()return cost;public double getWeight()return weight;8 菜单应用程序。程序运行结果:菜单练习程序源文件:MenuFrame.javaimport java.awt.*;import java.awt.event.*;import javax.swing.*;/* * 菜单应用程序. * 一个菜单,一个菜单条含三个下拉式菜单,每个下拉式菜单又有2到3个菜单项. * 当选择某个菜单项时,弹出一个对话框显示菜单项的选
42、择信息. * author 黎明你好 */public class MenuFrame extends JFrame implements ActionListenerprivate static final long serialVersionUID = 1L;private JMenuBar menubar;private JMenu file_menu, edit_menu, look_menu, arrangeIcons_menu, tool_menu;private JMenuItem new_item, open_item;private JMenuItem copy_item,
43、paste_item;private JMenuItem refresh_item, byGroup_item, auto_item;private JTextArea textArea;public MenuFrame()super(菜单应用程序);textArea = new JTextArea();menubar = new JMenuBar();file_menu = new JMenu(文件);edit_menu = new JMenu(编辑);look_menu = new JMenu(查看);tool_menu = new JMenu(工具栏);arrangeIcons_menu
44、 = new JMenu(排列图标);new_item = new JMenuItem(新建);open_item = new JMenuItem(打开);copy_item = new JMenuItem(复制);paste_item = new JMenuItem(粘贴);refresh_item = new JMenuItem(刷新);byGroup_item = new JMenuItem(按组排列);auto_item = new JMenuItem(自动排列);menubar.add(file_menu);menubar.add(edit_menu);menubar.add(loo
45、k_menu);file_menu.add(new_item);file_menu.add(open_item);edit_menu.add(copy_item);edit_menu.add(paste_item);look_menu.add(refresh_item);look_menu.add(tool_menu);look_menu.add(arrangeIcons_menu);arrangeIcons_menu.add(byGroup_item);arrangeIcons_menu.add(auto_item);new_item.addActionListener(this);open
46、_item.addActionListener(this);copy_item.addActionListener(this);paste_item.addActionListener(this);refresh_item.addActionListener(this);byGroup_item.addActionListener(this);auto_item.addActionListener(this);this.setJMenuBar(menubar);this.add(textArea, BorderLayout.CENTER);this.setBounds(50, 50, 250,
47、 200);this.setVisible(true);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);public void actionPerformed(ActionEvent e)String name = e.getActionCommand();JOptionPane.showMessageDialog(this, name + 背选中, 提示信息, JOptionPane.INFORMATION_MESSAGE);public static void main(String args)new MenuFrame();9 多线
48、程应用程序。程序运行结果:多线程程序源文件ThreadFrame.javaimport java.awt.BorderLayout;import java.awt.event.*;import javax.swing.*;* * 多线程应用程序. * 爸爸和妈妈不断往盘子中放桃子. * 三个儿子不断吃盘子汇总的桃子,吃的速度不一样. * 不能同时操作盘子,盘子最多可以放5个桃子. * author 黎明你好 */public class ThreadFrame extends JFrame implements Runnable, ActionListenerprivate static final long serialVersionUID = 1L;
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 图书仓库改造:装饰材料拆改步骤
- 项目管理基础知识与实践应用
- 职业生涯规划报告:职场沟通与人际关系建设
- 金融风险管理及创新策略研究报告
- 大数据技术与应用前景研究
- 合规文件编制作战攻略企业的法律文件如何确保规范合法性
- 大学生影视制作完全手册
- 工厂自动化设备维保管理方案
- 网络安全防护技能进阶教程
- 科研机构实验室设备采购与安装技术指南
- 驾考宝典2025全部试题(附答案)
- 2025广东省建筑安全员-C证考试(专职安全员)题库附答案
- 审核岗位笔试题目及答案
- 图书出版流程图解
- 供应链管理流程及风险控制
- 赌场合作合同模板范本(3篇)
- 大单元体育教学设计解读
- 高压压力管道安全培训课件
- 江苏省苏州市昆山市秀峰中学2024-2025学年七年级上学期第一次月考语文试题(解析版)
- QC/T 531-2025汽车视镜
- 体检中心业务知识培训课件
评论
0/150
提交评论