已阅读5页,还剩2页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
福建农林大学计算机与信息学院实验报告系 计算机专业年级12级成绩姓名学号实验室 机号实验时间教师签字实验(三) 类和对象一、实验目的和要求1. 掌握类的构造函数的重载2. 深入理解类和对象3. 学习NetBeans中UML项目的创建类并生成相应代码的方法二、实验内容和原理设计一个复数类,能够完成复数之间的基本运算,重写方法toString(),使其能输出此复数(形式为: 实部 + 虚部i)。要求设计的复数类必须有三个构造函数,分别为无参数、1个参数和2个参数的构造函数,完成的基本运算包括两个复数的加、减、乘、除法和变换了共轭复数。三、实验环境1. 硬件环境:2. 软件环境:JDK1.5四、算法描述及实验步骤1. 算法描述(可以用类图、流程图、伪代码或源程序描述)package complex;import java.util.Scanner;public class Complex private double realPart; private double imaginaryPart; public Complex() realPart = 0; imaginaryPart = 0; public Complex(double imaginary) imaginaryPart = imaginary; realPart = 0; public Complex(double real, double imaginary) imaginaryPart = imaginary; realPart = real; public double getrealPart() return realPart; public void setRealPart(double val) realPart = val; public double getimaginaryPart() return imaginaryPart; public void setlmaginaryPart(double val) imaginaryPart = val; public Complex plus(Complex complex) Complex add = new Complex(); add.realPart = realPart + complex.realPart; add.imaginaryPart = imaginaryPart + complex.imaginaryPart; return add; public Complex minus(Complex complex) Complex minus = new Complex(); minus.realPart = realPart - complex.realPart; minus.imaginaryPart = imaginaryPart - complex.imaginaryPart; return minus; public Complex times(Complex complex) Complex times = new Complex(); times.realPart = realPart * complex.realPart - imaginaryPart * complex.imaginaryPart; times.imaginaryPart = realPart * complex.imaginaryPart + imaginaryPart * complex.realPart; return times; public Complex divideby(Complex complex) Complex divide = new Complex(); divide.realPart = (realPart * complex.realPart - imaginaryPart * complex.imaginaryPart) / (complex.imaginaryPart * complex.imaginaryPart + complex.realPart * complex.realPart); divide.imaginaryPart = imaginaryPart * complex.realPart - realPart * complex.imaginaryPart; return divide; public Complex conjugate() Complex conjugate = new Complex(); conjugate.realPart = realPart; conjugate.imaginaryPart = -imaginaryPart; return conjugate; public void tostring() if (imaginaryPart = 1) System.out.println(realPart + + i); if (imaginaryPart = -1) System.out.println(realPart + -i); if (imaginaryPart != 1 & imaginaryPart != (-1) System.out.println(realPart + + ( + imaginaryPart + )i); public static void main(String args) Scanner input = new Scanner(System.in); System.out.println(复数a的实部和虚部分别是:); double areal = input.nextDouble(); double aimagine = input.nextDouble(); System.out.println(复数b的实部和虚部分别是:); double breal = input.nextDouble(); double bimagine = input.nextDouble(); Complex a = new Complex(areal, aimagine); Complex b = new Complex(breal, bimagine); Complex c = a.plus(b); System.out.print(a+b=); c.tostring(); Complex d = a.minus(b); System.out.print(a-b=); d.tostring(); Complex e = a.times(b); System.out.print(a*b=); e.tostring(); Complex f = a.divideby(b); System.out.print(a/b=); f.tostring(); System.out.println(a的共轭复数为:); a.conjugate().tostring(); System.out.println(b的共轭复数为:); b.conjugate().tostring(); 2. 实验步骤l 创建一个UML项目,并设计类Complex如下图l 创建一个Java应用项目l 把UML项目中的Complex自动生成代码到Java应用项目中l 实现Complex类中的方法l 进行编译l 进行测试,使用的测试用例:输入:预期输出:五、调试过程1. 编译过程记录算法实现中发现的语法错误及改正2. 调试过程记录算法实现中发现的逻辑错误及改正,对每个测试用例
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- GB/T 48460.1-2026微细气泡技术家庭应用第1部分:基于气泡粒径和浓度评价微细气泡花洒性能的测试方法
- 2026及未来5年干红瓶项目可行性研究报告(市场调查与数据分析)
- 密云中考数学试题及答案
- 2026年自媒体营销报告:行业现状与市场拓展策略
- 2026年行业进出口报关行业报告
- 2026年礼品行业市场细分研究报告:礼品市场品牌影响力分析001
- 2026年行业远程会议市场容量报告:协同办公市场潜力分析
- 2026年新材料行业渠道拓展与库存管理报告
- 2026年超净技术市场创新应用报告
- 2026年共享经济行业宏观经济影响与市场潜力分析报告
- 2026年中石化笔试题库(含答案)
- 2026年厨师人员考试模拟试卷(含答案)
- 小学美术四年级社团课教学设计《孵蛋的大白鹅》综合材料创作实践
- 高中美术必修《中国书法和中国画的密切关系》教学设计
- 工程决算编制办法说明
- 2026秋苏教版八年级上册生物全册每课知识点清单
- 2026秋沪科版(五四学制)八年级物理上册全册每课知识点清单
- 2026中银数字服务(南宁)有限公司招聘笔试模拟试题及答案详解
- RoHS 3.0 指令 2024-1020-EU 中文版(完整译文 + 新旧对比 + 2027 年实施时间表)
- 既有线施工安全注意事项培训课件
- 20S515 钢筋混凝土及砖砌排水检查井
评论
0/150
提交评论