1、自考JAVA语言程序设计一课后习题答案与源代码实验大纲实验大纲1 字符统计程序程序运行结果:统计字符源文件:StaChar.javaimport javax.swing.*;/* * 1 字符统计程序 * 利用对话框读入字符串 统计输入字符行中数字字符、英文字母个数. * author 黎明你好 */public class StaChar public static void main(String args) String str = JOptionPane.showInputDialog(请输入字符串:); char c = str.toCharArray(); int numberCo
2、unt = 0; int letterCount = 0; for (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 找质数
3、程序程序运行结果:输出质数原文件:PrintPrime.javaimport javax.swing.JOptionPane;/* * 2 找质数程序,利用对话框读入整数,输出2至这个整数之间的质数. * author 黎明你好 */public class PrintPrime private int number;/ 正整数 private String result = ; public PrintPrime()/ 构造方法 number = getIntegerNumber(输入整数n, 0);/ 要求是=0的整数 if (number 0) return;/ 出现错误,程序结束 el
4、se / 如果大于等于2,开始用循环计算结果 for (int i = 2; i = number; i+) / 计算素数和 if (isPrimeNumber(i) result += i + ; / 显示最后的和 JOptionPane.showMessageDialog(null, number + 之前所有素数为:n “ + result + ”, 显示结果, JOptionPane.INFORMATION_MESSAGE); /* * 通过图形界面,得到符合规则的正整数的方法 * param message - 在弹出的对话框中,显示提示信息 * param min - 要求此数必须
5、大于等于min * return - 返回符合规则的整数 */ public int getIntegerNumber(String message, int min) String str = JOptionPane.showInputDialog(null, message, 提示信息, JOptionPane.INFORMATION_MESSAGE); int number = -1; try number = Integer.parseInt(str); / 得到输入的正整数 catch( Exception e ) JOptionPane.showMessageDialog(null
6、, 输入非数字字符n程序结束, 错误警告, JOptionPane.ERROR_MESSAGE); return -1; / 输入的不是数字字符,程序结束 if (number min) JOptionPane.showMessageDialog(null, 输入的数不符合规则,不是正整数n程序结束, 错误警告, JOptionPane.ERROR_MESSAGE); return -1; / 输入的数不是大于2的正整数时候,程序结束 else return number; /* * 判断是否是素数的方法 * param n - 需要判断的数 * return - 是素数返回true,否则返回
7、false */ public boolean isPrimeNumber(int n) for (int i = 2; i n; i+) if (n % i = 0) return false; return true; /* main方法 */ public static void main(String args) new PrintPrime(); 3 类的继承定义,包括几何形状类Shape、圆形类Circle.、矩形类Rectangle/* * 几何图形类,抽象类 */abstract class Shape public float area() return 0.0f; /* *
8、 圆形类 */class Circle extends Shape private float R; public Circle(float r) R = r; public float area() return (float) (Math.PI * R * R); /* * 矩形类 */class Rectangle extends Shape private float w, h; public Rectangle(float w, float h) this.w = w; this.h = h; public float area() return w * h; public clas
9、s Work11_3 public static void main(String args) Circle c; Rectangle r; c = new Circle(2.0f); r = new Rectangle(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.
10、*;import java.util.*;/* * 4 数组排序程序. * 输入整数序列,对输入的整数进行排序,输出结果. * author 黎明你好 */public class Work11_4 public static final int RISE = 0; public static final int LOWER = 1; public static void main(String args) String str = JOptionPane.showInputDialog(请输入字符串:); StringTokenizer token = new StringTokenizer
11、(str, ,.;: ); int mode = Work11_4.RISE;/ 排列模式,默认为升序排列 int count = token.countTokens();/ 输入的整数的个数 int array = new intcount; int index = 0; while (token.hasMoreTokens() try arrayindex = Integer.parseInt(token.nextToken(); index+; catch( Exception e ) JOptionPane.showMessageDialog(null, 输入非法字符, 错误警告, J
12、OptionPane.ERROR_MESSAGE); return;/ 输入非法字符时候,直接结束程序 sort(array, mode);/ 按mode模式,进行排序 String result = new String(); String modeString = new String(); if (mode = Work11_4.RISE) modeString = 升序排列结果为:; if (mode = Work11_4.LOWER) modeString = 降序排列结果为:; for (int i = 0; i array.length; i+) result = result
13、+ arrayi + ,; if (result != null) JOptionPane .showMessageDialog(null, result, modeString, JOptionPane.INFORMATION_MESSAGE); /* * 给数组排序的方法 * 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
14、arrayj + 1) int temp = arrayj; arrayj = arrayj + 1; arrayj + 1 = temp; if (mode = Work11_4.LOWER & arrayj arrayj + 1) int temp = arrayj; arrayj = arrayj + 1; arrayj + 1 = temp; 5 字符串处理程序,括号匹配程序运行结果:括号匹配检测源文件:CheckBrackets.javaimport java.awt.*;import java.awt.event.*;import javax.swing.*;import unit
15、9.MyFileFilter;import java.io.*;/* * 5 字符串处理程序. * 输入程序的源程序代码行,找出可能存在圆括号,花括号不匹配的错误. * author 黎明你好 */public class CheckBrackets extends JFrame implements ActionListener, ItemListener private static final long serialVersionUID = 1L; private JFileChooser fileChooser; private JButton openFileButton; priv
16、ate JComboBox comboBox; private JTextField showRowStringField; private JTextField showMessageField; private JTextArea textArea; private JPanel northPanel, control_panel; private String rowString; private File file = null; public CheckBrackets() super(检测圆、花括号匹配程序); fileChooser = new JFileChooser(Syst
17、em.getProperty(user.dir); openFileButton = new JButton(打开文件); showRowStringField = new JTextField(); showMessageField = new JTextField(20); textArea = new JTextArea(); comboBox = new JComboBox(); northPanel = new JPanel(); control_panel = new JPanel(); rowString = new String1000; fileChooser.addChoo
18、sableFileFilter(new MyFileFilter(txt);/文件筛选 textArea.setLineWrap(true); showRowStringField.setEditable(false); showRowStringField.setBackground(Color.WHITE); showMessageField.setEditable(false); showMessageField.setBackground(Color.WHITE); openFileButton.addActionListener(this); comboBox.addItemList
19、ener(this); comboBox.addItem(请选择); control_panel.add(openFileButton); control_panel.add(new JLabel( 选择代码行:); control_panel.add(comboBox); control_panel.add(new JLabel(检测结果:); control_panel.add(showMessageField); northPanel.setLayout(new GridLayout(2, 1, 10, 10); northPanel.add(control_panel); northP
20、anel.add(showRowStringField); this.add(northPanel, BorderLayout.NORTH); this.add(new JScrollPane(textArea), BorderLayout.CENTER); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setBounds(50, 50, 550, 500); this.setVisible(true); this.validate(); public void actionPerformed(ActionEvent e)
21、showMessageField.setText(); int message = fileChooser.showOpenDialog(this); if (message = JFileChooser.APPROVE_OPTION) file = fileChooser.getSelectedFile(); comboBox.removeAllItems(); comboBox.addItem(请选择); readFile(file); public void itemStateChanged(ItemEvent e) int index = comboBox.getSelectedInd
22、ex(); 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 = ) count1-; if (ci = () count2+; if (ci = ) count2-; System.out.println(大括号 + count1 + ,小括号: + c
23、ount2); if (count1 != 0) showMessageField.setText(第 + index + 行,大括号匹配错误); else if (count2 != 0) showMessageField.setText(第 + index + 行,小括号()匹配错误); else if (count1 != 0 & count2 != 0) showMessageField.setText(第 + index + 行,大、小括号都匹配错误); else if (count1 = 0 & count2 = 0) showMessageField.setText(括号匹配正确
24、); public void readFile(File f) if (f != null) try FileReader file = new FileReader(f); BufferedReader in = new BufferedReader(file); String s = new String(); int i = 0; textArea.setText(); while (s = in.readLine() != null) textArea.append(i + 1) + : + s + n); rowStringi = s; comboBox.addItem(i + 1)
25、; 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.*;/* * 计算器程序. * 三个文本框,加、减、乘、除按钮 在前两个文本框分别输入两个运算数 点击按钮后,在第三个文本框中显示计算结果. * aut
26、hor 黎明你好 */public class Calculator extends JFrame implements ActionListener private static final long serialVersionUID = 1L; private JTextField oneField, twoField, resultField; private JButton addButton, subtractButton, multiplyButton, divideButton, 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(/); cleanButton = new JButton
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1