一、实验目的及要求
1.实验目的
通过学习了解本章重点讲解了将数据和对数据的操作封装在类中,通过抽象从具体的实例中抽取出共同的性质形成类的概念,再由类创建具体的对象,然后对象调用方法产生的行为以达到程序所要实现的目的,本实验是有理数的类封装,有理数的分子、分母以及四则运算,本实验用Rational类实现对有理数的封装。
2.实验要求
1)掌握类与对象的概念,能够创建类和对象。
2)理解类和对象的区别,掌握构造函数的使用,熟悉通过对象名引用实例的方法和属性。
3)能够熟练掌握类的定义,熟悉属性、构造函数、方法的作用。
4)编写程序对基本概念进行验证。
二、实验内容
分数的加法:
分数的减法:
分数的乘法: 分数乘分数时,用分子相乘的积做分子,分母相乘的积做分母,能约分的先约分。
分数的除法:
三、实验结果:
四、源程序
Rational 类
package javaDemo;
public class Rational {
//定义私有型分子、分母
private int numerator = 0;
private int denominator = 1;
public Rational() { //无参数的构造函数,创建一个Rational对象时,分子分母默认为0和1
this(1,1);
}
public Rational(int numerator,int denominator) { //有参构造函数,允许在创建Rational对象时指定分子和分母
int gcd = gcd(numerator,denominator); //求最大公约数
this.numerator = ((denominator > 0)?1:-1)*numerator/gcd;
this.denominator = Math.abs(denominator) / gcd;
}
private int gcd(int n,int d) {
int n1 = Math.abs(n);
int n2 = Math.abs(d);
int gcd = 1;
for (int i = 1;i <= n1 && i <= n2;i++) {
if(n1 % i == 0 && n2 % i == 0) {
gcd = i;
}
}
return gcd;
}
public int getNumerator(){
return numerator;
}
public int getDenominator(){
return denominator;
}
public void setNumerator(int numerator){
this.numerator = numerator;
}
public void setDenominator(int Denominator) {
this.denominator = Denominator;
}
//分数的加法
public Rational add(Rational r) {
int n = numerator * r.getDenominator() + denominator * r.getNumerator();
int d = denominator * r.getDenominator();
return new Rational(n,d);
}
//分数的减法
public Rational sub(Rational r) {
int n = numerator * r.getDenominator() - denominator * r.getNumerator();
int d = denominator * r.getDenominator();
return new Rational(n,d);
}
//分数的乘法
public Rational muti(Rational r) {
int n = numerator * r.getNumerator();
int d = denominator * r.getDenominator();
return new Rational(n,d);
}
//分数的除法
public Rational div(Rational r) {
int n = numerator * r.getDenominator();
int d = denominator * r.getNumerator();
return new Rational(n,d);
}
public String toString() {
if(denominator == 1) {
return numerator + "";
}else{
return numerator + "/" + denominator;
}
}
//重写的equals方法,该方法用于判断2个Rational对象是否相等
//它接收一个Object类型的参数,然后将其强制转化为Rational类,然后通过调用substract方法计算差值
public boolean equals(Object parm1) {
if((this.sub((Rational) (parm1))).getNumerator() == 0) {
return true;
} else{
return false;
}
}
public int compareTo(Object o) {
if ((this.sub((Rational) o)).getNumerator() > 0) {
return 1;
} else if((this.sub((Rational) o)).getNumerator() > 0) {
return -1;
}else {
return 0;
}
}
public double doubleValue() {
return numerator * 1.0 / denominator;
}
public float floatValue() {
return (float) doubleValue();
}
public int intValue() {
return (int) doubleValue();
}
public long longValue() {
return (long) doubleValue();
}
}
TeatRational类
package javaDemo;
import java.util.Scanner;
public class TestRational {
public static void main(String[] args) {
int n = 10;
int k = 1;
Rational sum = new Rational();
sum.setNumerator(0);
Rational item = new Rational();
item.setNumerator(2);
item.setDenominator(1);
while(k<=n){
sum = sum.add(item);
k++;
int numerator = item.getNumerator();
int denominator = item.getDenominator();
item.setNumerator(numerator+denominator);
item.setDenominator(numerator);
}
int a ,b ;
a = sum.getNumerator();
b = sum.getDenominator();
Rational r1 = new Rational(1,5);
Rational r2 = new Rational(3,2);
Rational r3 = new Rational(1,4);
System.out.println(r1 + "+" + r2 + "=" + r1.add(r2));
System.out.println(r1 + "-" + r2 + "=" + r1.sub(r2));
System.out.println(r1 + "*" + r2 + "=" + r1.muti(r2));
System.out.println(r1 + "/" + r2 + "=" + r1.div(r2));
System.out.println("2/1+3/2+5/3+8/5+13/8+...的前" + n + "项和为:");
System.out.println("用分数表示:");
System.out.println(a+"/"+b);
double doubleResult = (a*1.0)/b;
System.out.println("用小数表示:");
System.out.println(doubleResult);
}
}