青岛理工大学-实验2:类的封装性和继承性设计_第1页
青岛理工大学-实验2:类的封装性和继承性设计_第2页
青岛理工大学-实验2:类的封装性和继承性设计_第3页
青岛理工大学-实验2:类的封装性和继承性设计_第4页
青岛理工大学-实验2:类的封装性和继承性设计_第5页
已阅读5页,还剩3页未读, 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、课程实验报告课程 名称java应用技术班级计算112实验日期2013.9.26姓名学号实验成绩实验 名称实验2:类的封装性和继承性设计实验目的及要求1. 掌握java类的概念;2. 熟练运用类的属性和方法。实 验 环 境操作系统:windowside: ecl ipse实验内容(1)有理数四则运算。编写一个java应用程序,实现两个有理数的四则运算,要求有理数用分子和分母的形 式表示。结果要求用“分子/分母”的形式表示。(2)表演最终得分。声明一个average接口,英中约定求平均值的方法(参数为一个数组);声明first>second类实现average接口,分别给出求平均值的方法实现

2、。a. 类first +的功能为全部数值相加后求平均值。b. 类second中的功能为去掉一个最高分和一个最低分,再将总分求平均值。调试过程及实验结果1、规定数据的输入格式必须在运算符的两边加一个空格。解题思想:定义一个类:包含分子,分母属性,和四则运算的方法还有一个私有的方 法gcd(int, int):求两个整数的最大公约数。然后对输入数据进行处理分离出两个操 作数,通过构造器形成该类的对象,传入类方法。请输入两个有理数的表达式,最后读入运算符(运算符号两边各有一个空格)12/5 * 11/3the result is 44/5请输入两个有理数的表达式,最后读入运算符(运算符号两边各有一个

3、空格)12 + 16/5|the result is 76/5请输入两个有理数的表达式,最后读入运算符(运算符号两边各有一个空格)19/9 / 15/9the result is 19/15请输入两个有理数的表达式,最后读入运算符(运算符号两边各有一个空格)1 - 15/8(the result is -7/8请输入两个有理数的表达式,最后读入运算符(运算符号两边各有一个空格)15 - 45the result is -302、定义一个接口 average 请有一个方法:averge(double )定义两个类first,second分别事项average接口.英屮second自己实现一个私有

4、方法,用来把一组数据中的最高和最低分数清零(前提是:分数不能为零)请输入一组分数(s c o厂r )123456789 10隹掉最高最低分后的平均数:5.0加权平均数:4.0睛輛人一纽分数(.score;15.235 12.365 1.023 156.2 12.3|11.256 15.213 15.246 18.2365 15.265结果:去掉最高最低分后的平均数:14.0加权平均数:h.0总结实验的结果在普通的情况下,能够计算出正确的结果。对于大数据可能会出现溢出的 结果。本次实验掌握了写类的一些经验,和类实现接口的经验。附 录1、import java.util. seanner;impo

5、rt java.lang.exception;import java. iang. numberformatexception;public class algrithm private long fenzi=0;private long fenmu=0;public algrithmo /构造器pub lie algr it hm(long fen zi, long fen mu) /构造器long x = ged(fenzi, fenmu);this, fenzi = (fenzi/x);this, fenmu = (fenmu/x);private static long gcd(lon

6、g n, long m) /返回最大公约数long r=n%m;while(r != 0) n 二 m;m=r;r二;return m;private long getfenzi () /取分子return fenzi;private long getfenniuo /取分目return fenmu;publie static algrithmadd(algrithm addend, algrithm augend)throws exceptioni/求加与被加数分母的乘积long product = addend, getfenmu() * augend. getfenmu();/adden

7、d分子与augend分母的乘积long addproduct = addend. getfen/i() * augend. geteenmu();/augend的分子与addend的分母的乘积long augproduct = augend. getfenzi () * addend. getfenmu(); long greatestdivisor = ged(addproduct+augproduct, product); returnnewalgrithm(addproduct+augproduct)/greatestdivisor, product/greatestdivisor);/

8、被减数减数public static algrithmsubtraction(algrithm minuend , algrithm subtrahend ) /求减与被减数分母的乘积long product = minuendgetfenmu() subtrahend. getfenmuo ;/求minuend分子与subtrhend的分母的乘积long minproduct = minuend. getfentsi ()*subtrahend getfenmu();/求subtrahend分子与minuend的分母的乘积long subproduct = subtrahend. getfe

9、nzi () * minuend. getfenmu();long greatestdivisor = ged(minproduct - subproduct), product); returnnewalgri thm (mi nproductsubproduct)/greatestdivisor, product/greatestdivisor);public static algrithmmultil (algrithm multiplicand , algrithm multiplier) /计算multiplicand分母的乘积和multiplier的分母的乘积long produc

10、t = multiplicand, getfenmu()*mul tipi ier. getfenmu();/计算multiplicand的分子和multiplier的分子的乘积long otherproduct =multiplicand. getfenzi () * multiplier. getfenzi ();long greatestdivisor =ged (product, otherproduct);product =product/greatestdivisor;otherproduct =otherproduct/greatestdi vi sor;return new a

11、lgrithm(otherproduct, product);public static algrithmdivision(algrithm dividend, algrithm divisor) /计算dividend的分子和divisor的分母的乘积long product = dividendgetfenzi () * divisor. getfenmu ();/计算dividend的分母和divisor的分子的乘积long otherproduct = dividendgetfenmu () * divisor.getfenzi (); long greatestdivisor =gc

12、d(product, otherproduct);product = product/greatestdivisor;otherproduct = otherproduct/greatestdivisor;return now algrithm(product,otherproduct);publ ic void palyo /显示有理数system out. print(the result is “);systemout. print (thisfenzi );if (this. fenmu != 1)system. out> print in (,/, + +this. fenmu

13、);/* param args*/public static void main(string args) throws exceptionisystem, out. print in ("请输入两个有理数的表达式,最后读入运算符(运算符号两边各有一个空格);scanner read = new scanner (system, in) ;/读入表达式字符串iong number = new iong4;string expr = read.nextline ();expr. trim();int index = expr. indexof (,);int t二index;/提供给笫

14、二个操作数使用string first = expr substring(0, index);/system, outprintln(first: " + first);string shenyu = exprsubstring(index+l); shenyu = shenyu. trim() ;/去掉两头的空格 /system, outprintin("shenyu:" + shenyu);char fuhao = shenyu. charat (0);index = shenyu. indexof ('');/剩下的字符串/system, o

15、utprintln(index: + index);string second = shenyu. substring(index +1);seco nd = seco nd. trim();/system, out. pr in tin (,zseco nd: + second);/对第一个操作数进行处理index = first, indexof ('/');if(index = -1)number0=long parselong(first);number1=1;elsenumber0=long. parselong(first, substring(0, index);

16、int length = first.length ();numberl=long parselong(first. substring(index+1, length);/对第二个操作数进行处理index = second. indexof(');if(index = -1) number2=long. parselong (second);number3=1;else number2=long. parselong(second. substring(0, index);number3=long parselong(second, substring(index+l, second

17、. length();algrithm x = now algrithm(number0, number1);algrithm y = new algrithm(number2, number3); switch(fuhao) case ': algrithm. division(x, y). paly 0;break; case:algrithmmultil(x, y ) paly() ;break;case ':algrithm subtraction(x, y). paly();break;case ' +' :algrithm. add(x, y). p

18、aly () ;break; default :break;2、/接口public interface average public abstract double average(double array); /first类public class first implements average public double average(double array) int sum=0;for(int i=0 ; i<array.length;i+) sum+=arrayi;return sum/array.length;import java utilseanner;public class second implements average private void did(double x) doubl

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论