1、深入面向对象编程2淮海工学院计算机科学系实验报告书课程名: 面向对象程序设计 题 目 : 实验三 深入面向对象编程(2) 姓 名 : 班 级: 学 号: 实验三 深入面向对象编程(2) 实验目的和要求目的:掌握字符串、时间与数字类的使用,理解并能熟练使用String、StringBuffer、Stringtokenizer、Date、Calendar、SimpleDateFormat、Math、BigInteger类进行程序设计。要求:(1)本实验所要完成的各个程序,均基于图形用户界面。(2)若有输入、输出数据的要求,请尝试利用对话框完成。如: javax.swing.JOptionPane类
2、的静态方法showInputDialog实验内容(1) 程序1:编写程序,尽量多地利用String 、StringBuffer、StringTokenizer中的属性和方法。(2) 程序2:编写程序,练习使用Math类中的常用的静态方法。(3) 程序3:要求输入两个日期(每个日期均由年、月、日组合而成),并计算输出这两个日期之间的天数间隔。(4) 程序4:利用BigInteger类练习大整数间的算术运算(加、减、乘、除、取余等)(具体用法参照教材)实验步骤(1) 程序1:利用String方法输入字符串,用StringBuffer和StringTokenizer方法对输入的字符串进行修改和单词数
3、目统计。(2) 程序2:使用Math类中的常用的静态方法进行简单的数学运算。(3) 程序3:利用Date类创建对象,输入两个日期(每个日期均由年、月、日组合而成),利用Calendar类计算输出这两个日期之间的天数间隔。(4) 程序4:编程输入两个数目,利用BigInteger类练习大整数间的算术运算(加、减、乘、除、取余等)实验结果(结论)【程序1代码】import java.io.*;import javax.swing.*;import java.awt.*;import java.awt.event.*;import java.util.*;public class ddyy/程序中的
4、主类 public static void main(String args) new FrameInOut(); class FrameInOut extends JFrame implements ActionListener JButton btn1; JTextArea ta; JPanel p1,p2; jjcc s; FrameInOut() super(字符串表示); btn1=new JButton(字符串表示); btn1.setForeground(Color.red); btn1.setFont(new Font(微软雅黑,Font.BOLD,12); ta=new JT
5、extArea(6,25); ta.setForeground(Color.blue); ta.setFont(new Font(黑体,Font.BOLD,18); p1=new JPanel(); p2=new JPanel(); p1.add(btn1); p2.add(ta); getContentPane().add(p1); getContentPane().add(p2); setLayout(new FlowLayout(); p1.setBackground(Color.blue); p2.setBackground(Color.yellow); btn1.addActionL
6、istener(this); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); /设置关闭窗口 setSize(450,325); /设置窗口大小 setVisible(true); /显示窗口 String a=new String(I Love China); StringBuffer b=new StringBuffer(I Love China); b.append( Very Much); StringTokenizer c=new StringTokenizer(a, );/空格分隔 s=new jjcc(a,b,c); public
7、void actionPerformed(ActionEvent e) if (e.getSource()=btn1) ta.setText(); ta.append(s.toString(); ta.setFont(new Font(微软雅黑,Font.BOLD,15); ta.setForeground(Color.red); class jjcc String a; StringBuffer b; StringTokenizer c; jjcc(String a,StringBuffer b,StringTokenizer c) /构造函数 this.a=a; this.b=b; thi
8、s.c=c; public String toString() String s=; s+=字符串A:+a+n; s+=字符串的修改B:+b+n; s+=字符串A的单词数目:+c.countTokens()+n; return s; 【程序1运行结果】【程序2代码】import java.io.*;import javax.swing.*;import java.awt.*;import java.awt.event.*;import java.util.*;import java.math.*;public class jsff/程序中的主类 public static void main(
9、String args) new FrameInOut(); class FrameInOut extends JFrame implements ActionListener JButton btn1; JTextArea ta; JPanel p1,p2; jjcc s; FrameInOut() super(Math的运用); btn1=new JButton(数字运算); btn1.setForeground(Color.red); btn1.setFont(new Font(微软雅黑,Font.BOLD,12); ta=new JTextArea(6,25); ta.setForeg
10、round(Color.blue); ta.setFont(new Font(黑体,Font.BOLD,18); p1=new JPanel(); p2=new JPanel(); p1.add(btn1); p2.add(ta); getContentPane().add(p1); getContentPane().add(p2); setLayout(new FlowLayout(); p1.setBackground(Color.blue); p2.setBackground(Color.yellow); btn1.addActionListener(this); setDefaultC
11、loseOperation(JFrame.EXIT_ON_CLOSE); /设置关闭窗口 setSize(450,325); /设置窗口大小 setVisible(true); /显示窗口 double a=Math.sqrt(5);/5的平方根 double b=Math.log(2);/2的自然对数 double c=Math.sin(3);/正弦值 double d=Math.pow(2,10);/2的10次幂 double e=Math.abs(-1.5);/绝对值 double f=Math.IEEEremainder(12,5);/取余数 double g=Math.PI; s=n
12、ew jjcc(a,b,c,d,e,f,g); public void actionPerformed(ActionEvent e) if (e.getSource()=btn1) ta.setText(); ta.append(s.toString(); ta.setFont(new Font(微软雅黑,Font.BOLD,15); ta.setForeground(Color.red); class jjcc double a,b,c,d,e,f,g; jjcc(double a,double b,double c,double d,double e,double f,double g)
13、/构造函数 this.a=a; this.b=b; this.c=c; this.d=d; this.e=e; this.f=f; this.g=g; public String toString() String s=; s+=5的平方根是:+a+n; s+=2的自然对数:+b+n; s+=弧度为3的正弦值是:+c+n; s+=2的10次幂是:+d+n; s+=-1.5的绝对值是:+e+n; s+=12除以5的余数是:+f+n; s+=常量是:+g+n; return s; 【程序2运行结果】【程序3代码】import java.io.*;import javax.swing.*;impor
14、t java.awt.*;import java.awt.event.*;import java.util.*;import java.text.*;public class abc /程序中的主类 public static void main(String args) new FrameInOut(); class FrameInOut extends JFrame implements ActionListener JButton btn; JTextArea ta; JPanel p1,p2; student s; FrameInOut() super(日期查询系统); btn=new
15、 JButton(日期间隔天数); btn.setForeground(Color.red); btn.setFont(new Font(微软雅黑,Font.BOLD,12); ta=new JTextArea(6,25); ta.setForeground(Color.blue); ta.setFont(new Font(黑体,Font.BOLD,18); p1=new JPanel(); p2=new JPanel(); p1.add(btn); p2.add(ta); getContentPane().add(p1); getContentPane().add(p2); setLayou
16、t(new FlowLayout(); p1.setBackground(Color.blue); p2.setBackground(Color.yellow); btn.addActionListener(this); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); /设置关闭窗口 setSize(450,325); /设置窗口大小 setVisible(true); /显示窗口 String s1=JOptionPane.showInputDialog(请输入第一个日期xxxx/xx/xx:); String s2=JOptionPane.s
17、howInputDialog(请输入第二个日期xxxx/xx/xx:); SimpleDateFormat formatter = new SimpleDateFormat (yyyy/MM/dd); ParsePosition pos = new ParsePosition(0); ParsePosition pos1 = new ParsePosition(0); Date dt1=formatter.parse(s1,pos); Date dt2=formatter.parse(s2,pos1); long time1=( dt2.getTime()-dt1.getTime()/(100
18、0*60*60*24); s=new student(s1,s2,time1); public void actionPerformed(ActionEvent e) if (e.getSource()=btn) ta.setText(); ta.append(s.toString(); ta.setFont(new Font(微软雅黑,Font.BOLD,15); ta.setForeground(Color.red); class student String s1,s2; long time1; student(String s1,String s2,long time1) /构造函数
19、this.s1=s1; this.s2=s2; this.time1=time1; public String toString() String s=; s+=输入的第一个日期:+s1+n; s+=输入的第二个日期:+s2+n; s+=两日期间隔+time1+天; return s; 【程序3运行结果】【程序4代码】import java.io.*;import javax.swing.*;import java.awt.*;import java.awt.event.*;import java.util.*;import java.math.*;public class suanfa /程
20、序中的主类 public static void main(String args) new FrameInOut(); class FrameInOut extends JFrame implements ActionListener JButton btn1; JTextArea ta; JPanel p1,p2; jjcc s; FrameInOut() super(大整数的运算); btn1=new JButton(数字运算); btn1.setForeground(Color.red); btn1.setFont(new Font(微软雅黑,Font.BOLD,12); ta=new
21、 JTextArea(6,25); ta.setForeground(Color.blue); ta.setFont(new Font(黑体,Font.BOLD,18); p1=new JPanel(); p2=new JPanel(); p1.add(btn1); p2.add(ta); getContentPane().add(p1); getContentPane().add(p2); setLayout(new FlowLayout(); p1.setBackground(Color.blue); p2.setBackground(Color.yellow); btn1.addActi
22、onListener(this); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); /设置关闭窗口 setSize(450,325); /设置窗口大小 setVisible(true); /显示窗口 String s1=JOptionPane.showInputDialog(请输入第一个数字:); String s2=JOptionPane.showInputDialog(请输入第二个数字:); BigInteger b1=new BigInteger(s1); BigInteger b2=new BigInteger(s2); BigInteg
23、er b3=b1.add(b2);/b1+b2 BigInteger b4=b1.multiply(b2);/b1*b2 BigInteger b5=b1.subtract(b2);/b1-b2 BigInteger b6=b1.divide(b2); BigInteger b7=b1.remainder(b2); s=new jjcc(s1,s2,b3,b4,b5,b6,b7); public void actionPerformed(ActionEvent e) if (e.getSource()=btn1) ta.setText(); ta.append(s.toString(); ta
24、.setFont(new Font(微软雅黑,Font.BOLD,15); ta.setForeground(Color.red); class jjcc String s1,s2; BigInteger b3,b4,b5,b6,b7; jjcc(String s1,String s2,BigInteger b3,BigInteger b4,BigInteger b5,BigInteger b6,BigInteger b7) /构造函数 this.s1=s1; this.s2=s2; this.b3=b3; this.b4=b4; this.b5=b5; this.b6=b6; this.b7
25、=b7; public String toString() String s=; s+=输入的第一个数字:+s1+n; s+=输入的第二个数字:+s2+n; s+=两数相加的和是:+b3+n; s+=两数相减的差是:+b4+n; s+=两数相乘的积是:+b5+n; s+=两数相除的商是:+b6+n; s+=两数相除的余是:+b7+n; return s; 【程序4的运行结果】实验结果的分析体会:这次的实验内容比较多,但是相对而言却比较简单,通过这次的实验,我学到了很多。对于程序1,String类是final的,不可被继承,而StringBuffer可以更改String的类创建的字符串,这是我以
26、前所不知道的,可以说是一大收获。对于程序2,Math类的用法,因为之前有接触,所以做起来不是很吃力,编程时,我明白了Math.log(2)的值不是log2,而是ln2,log2的表达方法是Math.Log10(2),Math类中是静态方法,可以直接通过类名调用。对于程序3,Date类的用法,是我此次编程出现错误最多的地方,分析了一下,发现原因是ParsePosition的运用错误,通过查资料,这些问题已经得到解决。对于程序4,BigInteger类在大数之间运算比较方便,变成也比较简单,没有出现很大的问题。总的来说,本次编程运用的知识比较琐碎,虽然比较简单,但自己还是用了好长时间才调试好,期间有拼写错误,符号错误和程序上的语言错误,但是只要认真就能克服这些困难,通过这次编程,我学到的不只是程序的编译,更是知识的融合,只有把这些琐碎的知识融合在一起,才能真正做到学以致用,才能早日成为Java高手。
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1