4.2 实验练习
2013-03-20 21:54
369 查看
4.2.1 机动车的类封装
![](http://img.my.csdn.net/uploads/201303/20/1363784557_5346.jpg)
4.2.2 有理数的类封装
class Vehicle{ double speed=0.0; int power=0; void speedUp(int s){ speed=s+speed; } void speedDown(int d){ speed=speed-d; } void setPower(int p){ power=p; } int getPower(){ return power; } double getSpeed(){ return speed; } } public class User{ public static void main(String args[]){ Vehicle car1,car2; car1=new Vehicle(); car2=new Vehicle(); car1.setPower(128); car2.setPower(76); System.out.println("car1的功率是:"+car1.getPower()); System.out.println("car2的功率是:"+car2.getPower()); car1.speedUp(80); car2.speedUp(80); System.out.println("car1目前的速度:"+car1.getSpeed()); System.out.println("car2目前的速度:"+car2.getSpeed()); car1.speedDown(10); car2.speedDown(20); System.out.println("car1目前的速度:"+car1.getSpeed()); System.out.println("car2目前的速度:"+car2.getSpeed()); } }
![](http://img.my.csdn.net/uploads/201303/20/1363784557_5346.jpg)
4.2.2 有理数的类封装
class Rational{ int numerator=1; int denominator=1; void setNumerator(int a){ int c=f(Math.abs(a),denominator); numerator=a/c; denominator=denominator/c; if(numerator<0&&denominator<0){ numerator=-numerator; denominator=-denominator; } } void setDenominator(int b){ int c=f(numerator,Math.abs(b)); numerator=numerator/c; denominator=b/c; if(numerator<0&&denominator<0){ numerator=-numerator; denominator=-denominator; } } int getNumerator(){ return numerator; } int getDenominator(){ return denominator; } int f(int a,int b){ if(a==0){ return 1; } if(a<b){ int c=a; a=b; b=c; } int r=a%b; while(r!=0){ a=b; b=r; r=a%b; } return b; } Rational add(Rational r){ int a=r.getNumerator(); int b=r.getDenominator(); int newNumerator=numerator*b+denominator*a; int newDenominator=denominator*b; Rational result=new Rational(); result.setNumerator(newNumerator); result.setDenominator(newDenominator); return result; } Rational sub(Rational r){ int a=r.getNumerator(); int b=r.getDenominator(); int newNumerator=numerator*b-denominator*a; int newDenominator=denominator*b; Rational result=new Rational(); result.setNumerator(newNumerator); result.setDenominator(newDenominator); return result; } Rational muti(Rational r){ int a=r.getNumerator(); int b=r.getDenominator(); int newNumerator=numerator*a; int newDenominator=denominator*b; Rational result=new Rational(); result.setNumerator(newNumerator); result.setDenominator(newDenominator); return result; } Rational div(Rational r){ int a=r.getNumerator(); int b=r.getDenominator(); int newNumerator=numerator*b; int newDenominator=denominator*a; Rational result=new Rational(); result.setNumerator(newNumerator); result.setDenominator(newDenominator); return result; } } public class Computer{ public static void main(String args[]){ Rational r1=new Rational(); r1.setNumerator(1); r1.setDenominator(5); Rational r2=new Rational(); r2.setNumerator(3); r2.setDenominator(2); Rational result=r1.add(r2); int a=result.getNumerator(); int b=result.getDenominator(); System.out.println("1/5+3/2 = "+a+"/"+b); result=r1.sub(r2); a=result.getNumerator(); b=result.getDenominator(); System.out.println("1/5-3/2 = "+a+"/"+b); result=r1.muti(r2); a=result.getNumerator(); b=result.getDenominator(); System.out.println("1/5×3/2 = "+a+"/"+b); result=r1.div(r2); a=result.getNumerator(); b=result.getDenominator(); System.out.println("1/5÷3/2 = "+a+"/"+b); } }
![](http://img.my.csdn.net/uploads/201303/20/1363787978_6550.jpg)
相关文章推荐
- XCode4.2中练习Object-C
- hp uft 实验练习
- 实验三 页面布局练习
- 实验报告撰写练习1
- 实验1-顺序结构 —— C语言实验——格式化输出(常量练习)(1111)
- 实验:练习convolution和pooling的使用(该博主的很多微博都注重实验)
- 实验 7 综合练习 ok
- 练习系统 实验一 谁拿了最多的奖学金
- 实验 7 综合练习 第三题
- 2.2 实验练习
- 练习系统 实验二 不吉利日期
- python 实验6 字符串练习(二)
- 学习数组类时,碰到typeof(),对其实验引起的一连串知识点、学习小结和思考练习(涉及成员对象变量、成员常量、抽象类Type等知识)
- 啊哈C——学习4.2练习
- 练习系统 实验二 循环数
- 实验 13 综合练习三
- 实验教学内容(16课时)实验2选择语句练习
- 2.C#实验五:ADO.NET数据库操作练习详解
- JAVA第三周抽象类的学习练习实验一
- 实验1_练习使用editplus编写运行JAVA应用程序