深入面向对象编程2.docx

上传人:b****5 文档编号:7654627 上传时间:2023-01-25 格式:DOCX 页数:16 大小:183.19KB
下载 相关 举报
深入面向对象编程2.docx_第1页
第1页 / 共16页
深入面向对象编程2.docx_第2页
第2页 / 共16页
深入面向对象编程2.docx_第3页
第3页 / 共16页
深入面向对象编程2.docx_第4页
第4页 / 共16页
深入面向对象编程2.docx_第5页
第5页 / 共16页
点击查看更多>>
下载资源
资源描述

深入面向对象编程2.docx

《深入面向对象编程2.docx》由会员分享,可在线阅读,更多相关《深入面向对象编程2.docx(16页珍藏版)》请在冰豆网上搜索。

深入面向对象编程2.docx

深入面向对象编程2

淮海工学院计算机科学系

实验报告书

课程名:

面向对象程序设计

题目:

实验三深入面向对象编程

(2)

姓名:

班级:

学号:

 

实验三深入面向对象编程

(2)

实验目的和要求

目的:

掌握字符串、时间与数字类的使用,理解并能熟练使用String、StringBuffer、Stringtokenizer、Date、Calendar、SimpleDateFormat、Math、BigInteger类进行程序设计。

要求:

(1)本实验所要完成的各个程序,均基于图形用户界面。

(2)若有输入、输出数据的要求,请尝试利用对话框完成。

[如:

javax.swing.JOptionPane类的静态方法showInputDialog]

实验内容

(1)程序1:

编写程序,尽量多地利用String、StringBuffer、StringTokenizer中的属性和方法。

(2)程序2:

编写程序,练习使用Math类中的常用的静态方法。

(3)程序3:

要求输入两个日期(每个日期均由年、月、日组合而成),并计算输出这两个日期之间的天数间隔。

(4)程序4:

利用BigInteger类练习大整数间的算术运算(加、减、乘、除、取余等)(具体用法参照教材)

 

实验步骤

(1)程序1:

利用String方法输入字符串,用StringBuffer和StringTokenizer方法对输入的字符串进行修改和单词数目统计。

(2)程序2:

使用Math类中的常用的静态方法进行简单的数学运算。

(3)程序3:

利用Date类创建对象,输入两个日期(每个日期均由年、月、日组合而成),利用Calendar类计算输出这两个日期之间的天数间隔。

(4)程序4:

编程输入两个数目,利用BigInteger类练习大整数间的算术运算(加、减、乘、除、取余等)

实验结果(结论)

【程序1代码】

importjava.io.*;

importjavax.swing.*;

importjava.awt.*;

importjava.awt.event.*;

importjava.util.*;

publicclassddyy//程序中的主类

{

publicstaticvoidmain(Stringargs[])

{

newFrameInOut();

}

}

classFrameInOutextendsJFrameimplementsActionListener

{

JButtonbtn1;

JTextAreata;

JPanelp1,p2;

jjccs;

FrameInOut()

{

super("字符串表示");

btn1=newJButton("字符串表示");

btn1.setForeground(Color.red);

btn1.setFont(newFont("微软雅黑",Font.BOLD,12));

ta=newJTextArea(6,25);

ta.setForeground(Color.blue);

ta.setFont(newFont("黑体",Font.BOLD,18));

p1=newJPanel();

p2=newJPanel();

p1.add(btn1);

p2.add(ta);

getContentPane().add(p1);

getContentPane().add(p2);

setLayout(newFlowLayout());

p1.setBackground(Color.blue);

p2.setBackground(Color.yellow);

btn1.addActionListener(this);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置关闭窗口

setSize(450,325);//设置窗口大小

setVisible(true);//显示窗口

Stringa=newString("ILoveChina");

StringBufferb=newStringBuffer("ILoveChina");

b.append("VeryMuch");

StringTokenizerc=newStringTokenizer(a,"");//空格分隔

s=newjjcc(a,b,c);

}

publicvoidactionPerformed(ActionEvente)

{

if(e.getSource()==btn1)

{

ta.setText("");

ta.append(s.toString());

ta.setFont(newFont("微软雅黑",Font.BOLD,15));

ta.setForeground(Color.red);

}

}

}

classjjcc

{

Stringa;

StringBufferb;

StringTokenizerc;

jjcc(Stringa,StringBufferb,StringTokenizerc)//构造函数

{

this.a=a;

this.b=b;

this.c=c;

}

publicStringtoString()

{

Strings="";

s+="字符串A:

"+a+"\n";

s+="字符串的修改B:

"+b+"\n";

s+="字符串A的单词数目:

"+c.countTokens()+"\n";

returns;

}

}

【程序1运行结果】

【程序2代码】

importjava.io.*;

importjavax.swing.*;

importjava.awt.*;

importjava.awt.event.*;

importjava.util.*;

importjava.math.*;

publicclassjsff//程序中的主类

{

publicstaticvoidmain(Stringargs[])

{

newFrameInOut();

}

}

classFrameInOutextendsJFrameimplementsActionListener

{

JButtonbtn1;

JTextAreata;

JPanelp1,p2;

jjccs;

FrameInOut()

{

super("Math的运用");

btn1=newJButton("数字运算");

btn1.setForeground(Color.red);

btn1.setFont(newFont("微软雅黑",Font.BOLD,12));

ta=newJTextArea(6,25);

ta.setForeground(Color.blue);

ta.setFont(newFont("黑体",Font.BOLD,18));

p1=newJPanel();

p2=newJPanel();

p1.add(btn1);

p2.add(ta);

getContentPane().add(p1);

getContentPane().add(p2);

setLayout(newFlowLayout());

p1.setBackground(Color.blue);

p2.setBackground(Color.yellow);

btn1.addActionListener(this);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置关闭窗口

setSize(450,325);//设置窗口大小

setVisible(true);//显示窗口

doublea=Math.sqrt(5);//5的平方根

doubleb=Math.log

(2);//2的自然对数

doublec=Math.sin(3);//正弦值

doubled=Math.pow(2,10);//2的10次幂

doublee=Math.abs(-1.5);//绝对值

doublef=Math.IEEEremainder(12,5);//取余数

doubleg=Math.PI;

s=newjjcc(a,b,c,d,e,f,g);

}

publicvoidactionPerformed(ActionEvente)

{

if(e.getSource()==btn1)

{

ta.setText("");

ta.append(s.toString());

ta.setFont(newFont("微软雅黑",Font.BOLD,15));

ta.setForeground(Color.red);

}

}

}

classjjcc

{

doublea,b,c,d,e,f,g;

jjcc(doublea,doubleb,doublec,doubled,doublee,doublef,doubleg)//构造函数

{

this.a=a;

this.b=b;

this.c=c;

this.d=d;

this.e=e;

this.f=f;

this.g=g;

}

publicStringtoString()

{

Strings="";

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";

returns;

}

}

【程序2运行结果】

【程序3代码】

importjava.io.*;

importjavax.swing.*;

importjava.awt.*;

importjava.awt.event.*;

importjava.util.*;

importjava.text.*;

publicclassabc//程序中的主类

{

publicstaticvoidmain(Stringargs[])

{

newFrameInOut();

}

}

classFrameInOutextendsJFrameimplementsActionListener

{

JButtonbtn;

JTextAreata;

JPanelp1,p2;

students;

FrameInOut()

{

super("日期查询系统");

btn=newJButton("日期间隔天数");

btn.setForeground(Color.red);

btn.setFont(newFont("微软雅黑",Font.BOLD,12));

ta=newJTextArea(6,25);

ta.setForeground(Color.blue);

ta.setFont(newFont("黑体",Font.BOLD,18));

p1=newJPanel();

p2=newJPanel();

p1.add(btn);

p2.add(ta);

getContentPane().add(p1);

getContentPane().add(p2);

setLayout(newFlowLayout());

p1.setBackground(Color.blue);

p2.setBackground(Color.yellow);

btn.addActionListener(this);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置关闭窗口

setSize(450,325);//设置窗口大小

setVisible(true);//显示窗口

Strings1=JOptionPane.showInputDialog("请输入第一个日期xxxx/xx/xx:

");

Strings2=JOptionPane.showInputDialog("请输入第二个日期xxxx/xx/xx:

");

SimpleDateFormatformatter=newSimpleDateFormat("yyyy/MM/dd");

ParsePositionpos=newParsePosition(0);

ParsePositionpos1=newParsePosition(0);

Datedt1=formatter.parse(s1,pos);

Datedt2=formatter.parse(s2,pos1);

longtime1=(dt2.getTime()-dt1.getTime())/(1000*60*60*24);

s=newstudent(s1,s2,time1);

}

publicvoidactionPerformed(ActionEvente)

{

if(e.getSource()==btn)

{

ta.setText("");

ta.append(s.toString());

ta.setFont(newFont("微软雅黑",Font.BOLD,15));

ta.setForeground(Color.red);

}

}

}

classstudent

{

Strings1,s2;

longtime1;

student(Strings1,Strings2,longtime1)//构造函数

{

this.s1=s1;

this.s2=s2;

this.time1=time1;

}

publicStringtoString()

{

Strings="";

s+="输入的第一个日期:

"+s1+"\n";

s+="输入的第二个日期:

"+s2+"\n";

s+="两日期间隔"+time1+"天";

returns;

}

}

【程序3运行结果】

【程序4代码】

importjava.io.*;

importjavax.swing.*;

importjava.awt.*;

importjava.awt.event.*;

importjava.util.*;

importjava.math.*;

publicclasssuanfa//程序中的主类

{

publicstaticvoidmain(Stringargs[])

{

newFrameInOut();

}

}

classFrameInOutextendsJFrameimplementsActionListener

{

JButtonbtn1;

JTextAreata;

JPanelp1,p2;

jjccs;

FrameInOut()

{

super("大整数的运算");

btn1=newJButton("数字运算");

btn1.setForeground(Color.red);

btn1.setFont(newFont("微软雅黑",Font.BOLD,12));

ta=newJTextArea(6,25);

ta.setForeground(Color.blue);

ta.setFont(newFont("黑体",Font.BOLD,18));

p1=newJPanel();

p2=newJPanel();

p1.add(btn1);

p2.add(ta);

getContentPane().add(p1);

getContentPane().add(p2);

setLayout(newFlowLayout());

p1.setBackground(Color.blue);

p2.setBackground(Color.yellow);

btn1.addActionListener(this);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置关闭窗口

setSize(450,325);//设置窗口大小

setVisible(true);//显示窗口

Strings1=JOptionPane.showInputDialog("请输入第一个数字:

");

Strings2=JOptionPane.showInputDialog("请输入第二个数字:

");

BigIntegerb1=newBigInteger(s1);

BigIntegerb2=newBigInteger(s2);

BigIntegerb3=b1.add(b2);//b1+b2

BigIntegerb4=b1.multiply(b2);//b1*b2

BigIntegerb5=b1.subtract(b2);//b1-b2

BigIntegerb6=b1.divide(b2);

BigIntegerb7=b1.remainder(b2);

s=newjjcc(s1,s2,b3,b4,b5,b6,b7);

}

publicvoidactionPerformed(ActionEvente)

{

if(e.getSource()==btn1)

{

ta.setText("");

ta.append(s.toString());

ta.setFont(newFont("微软雅黑",Font.BOLD,15));

ta.setForeground(Color.red);

}

}

}

classjjcc

{

Strings1,s2;

BigIntegerb3,b4,b5,b6,b7;

jjcc(Strings1,Strings2,BigIntegerb3,BigIntegerb4,BigIntegerb5,BigIntegerb6,BigIntegerb7)//构造函数

{

this.s1=s1;

this.s2=s2;

this.b3=b3;

this.b4=b4;

this.b5=b5;

this.b6=b6;

this.b7=b7;

}

publicStringtoString()

{

Strings="";

s+="输入的第一个数字:

"+s1+"\n";

s+="输入的第二个数字:

"+s2+"\n";

s+="两数相加的和是:

"+b3+"\n";

s+="两数相减的差是:

"+b4+"\n";

s+="两数相乘的积是:

"+b5+"\n";

s+="两数相除的商是:

"+b6+"\n";

s+="两数相除的余是:

"+b7+"\n";

returns;

}

}

【程序4的运行结果】

 

实验结果的分析体会:

这次的实验内容比较多,但是相对而言却比较简单,通过这次的实验,我学到了很多。

对于程序1,String类是final的,不可被继承,而StringBuffer可以更改String的类创建的字符串,这是我以前所不知道的,可以说是一大收获。

对于程序2,Math类的用法,因为之前有接触,所以做起来不是很吃力,编程时,我明白了Math.log

(2)的值不是log2,而是ln2,log2的表达方法是Math.Log10

(2),Math类中是静态方法,可以直接通过类名调用。

对于程序3,Date类的用法,是我此次编程出现错误最多的地方,分析了一下,发现原因是ParsePosition的运用错误,通过查资料,这些问题已经得到解决。

对于程序4,BigInteger类在大数之间运算比较方便,变成也比较简单,没有出现很大的问题。

总的来说,本次编程运用的知识比较琐碎,虽然比较简单,但自己还是用了好长时间才调试好,期间有拼写错误,符号错误和程序上的语言错误,但是只要认真就能克服这些困难,通过这次编程,我学到的不只是程序的编译,更是知识的融合,只有把这些琐碎的知识融合在一起,才能真正做到学以致用,才能早日成为Java高手。

 

 

 

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 农林牧渔 > 林学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1