Lab 23 CSIS 10A Fractions The purpose of an object in a programming language is
ID: 3912561 • Letter: L
Question
Lab 23 CSIS 10A
Fractions
The purpose of an object in a programming language is to represent things that cannot be represented using a single primitive data types such as int, double, char, boolean.
For example a String object consists of a list of primitive char data values. And the char data values are encapsulated inside the String object. Another example is the Particle object (from the Golf animation game) used to simulate projectiles. A particle object contains inside the (x,y) coordinates of the particle and the x and y velocities of the particle.
If you were to code an application for a college admissions and records department, you would probability need to represent data about each student enrolled in the college. The student data would consist of: name, student id, address, program of study or major, total number of credit hours taken so far, and probably several other data items. All these data items would be encapsulated into a Student object that is described by the Student class code.
In this lab, you will write a class that describes an object that can be used to represent a fraction – also called a rational number.
A rational number is a number that can be represented as the ratio of two integers. For example, 2/3 is a rational number, and you can think of 7 as a rational number with an implicit 1 in the denominator.
The purpose of this exercise is to write a class definition that includes a variety of methods, including constructors, modifiers.
Directions
Create a class named Rational. A Rational object should have two integer instance variables (fields) to store the numerator and denominator. You should define the fields using the private modifier.
Write a constructor that takes no arguments (called a no-arg constructor) and that assigns the value 0 to the numerator and the value 1 to the denominator. This represents the value 0.
Write a second constructor for your class that takes two arguments and that uses them to initialize the instance variables.
Write an instance method (without the keyword static) named printRational that accesses the instance variables numerator and denominator and prints the fraction in some reasonable format such as
2/3
The method header for printRational should look like
public void printRational()
Create a second class named DemoFraction. Define a main method that creates an object of type Rational using the 2 parameter constructor and prints the object using the printRational method. Remember that to call an instance method you must create an instance of the object and use the variable that contains the reference to the object instance before the method name. l
Rational f = new Rational(4,5);
f.printRational(); // should print 4/5
At this stage, you have a minimal testable program. Run it and, if necessary, debug it.
Write a method called negate. The negate method multiplies the numerator by the value -1 (negative one). This method modifies the object. It should return void. Add code to main to test the new method.
f.negate();
f.printRational(); // should print -4/5
Write a method called invert that inverts the number by swapping the numerator and denominator. Add code to main to test the new method. Remember that the denominator of a fraction should always be positive. After doing the swap, if the denominator is negative, multiply both denominator and numerator by -1 to make the denominator and move the negative sign to the numerator.
f.invert();
f.printRational(); // should print -5/4
Write a method called toDouble that converts the rational number to a double (floating-point number) and returns the result. As always, test the new method.
double t = f.toDouble();
System.out.println(t)); // should print -0.8
Write a method named reduce that reduces a rational number to its lowest terms by finding the greatest common divisor (GCD) of the numerator and denominator and dividing through. This method should be a pure function; it should not modify its instance but instead it should create and return a new Rational object that is the reduced fraction. The code to find the GCD is given below.
Rational g = new Rational(75, 100);
Rational gr = g.reduce();
g.printRational(); // should print 75/100
gr.printRational(); // should print 3/4
/**
* find the GCD Greatest Common Divisor of two integer
* Exmaple: GCD of 24 and 16 is 8, GCD of 24 and 3 is 3
* GCD of 25 and 3 is 1.
* @param a a positive integer
* @param b a positive integer
* @return the gcd of a and b
*/
public static int gcd(int a, int b) {
a = Math.abs(a);
b = Math.abs(b);
// make sure that a >= b
if (a < b) { // swap a and b
int t = b;
b = a;
a = t;
}
int r = a%b;
while (r!=0){
a=b;
b=r;
r=a%b;
}
return b;
}
Write a method called add that returns a new Rational object which is the sum of the current object and the Rational object passed as parameter. This is a pure function; it does not modify the instance variables. The method header should like
public Rational add( Rational a)
There are several ways to add fractions. You can use any one you want, but you should make sure that the result object that is returned is reduced so that the numerator and denominator have no common divisors. [hint: use the gcd method]
Rational a = new Rational(3,2);
Rational b = new Rational(3,8);
Rational sum = a.add(b);
sum.printRational(); // should print 9/4.
Explanation / Answer
//Step 1 Here we create a new class and create instance variable ints numerator and denominator
public class Rational {
//Step 2
int numerator, denominator;
//Step 5 Write a main method that creates a new object with type Rational, sets its instance
//variables to some values, and prints the object
public static void main(String[] args) {
Rational test = new Rational(3,2);
Rational testAdd = new Rational(3,8);
Rational sum=add(test,testAdd);
printRational(sum);
//Step 7 Part 2 Add lines to main to test the new method
//negate(test);
//printRational(test);
//reset(test);
//Step 8 Part 2 Add lines to main to test the new method
//invert(test);
//printRational(test);
//reset(test);
//Step 9 Part 2 As always, test the new method.
//double doubleValue = toDouble(test);
//System.out.println(doubleValue);
//reset(test);
//reduce(test);
//printRational(add(test, testAdd));
}
//Step 3 Here is the constructor that takes no arguments
public Rational() {
this.numerator = 0;
this.denominator = 1;
}
//Step 6 Write a second constructor for your class that takes two arguments and that uses them to initialize the instance variables.
public Rational(int n, int d) {
this.numerator = n;
this.denominator = d;
}
//!! a reset for the purpose of resetting the values after testing !!\
public static void reset(Rational test) {
test.numerator = 10;
test.denominator = 20;
}
//Step 4 Write a method called printRational prints a Rational object
public static void printRational(Rational tPrint) {
System.out.println(tPrint.numerator + "/" + tPrint.denominator);
}
//Step 7 Write a method called negate that reverses the sign of a rational number. This method should be a modifier, so it should return void.
public static void negate(Rational test) {
test.numerator = test.numerator * -1;
if(test.denominator <= 0) {
test.numerator = test.numerator * -1;
test.denominator = test.denominator * -1;
}
}
//Step 8 Step 8 Write a method called invert that inverts the number by swapping the numerator and denominator
public static void invert(Rational test) {
int tempN = test.numerator;
int tempD = test.denominator;
test.numerator = tempD;
test.denominator = tempN;
}
//Step 9 Write a method called toDouble that converts the rational number to a double (floating-point number) and returns the result. This method is a pure function; it does not modify the object.
public static double toDouble(Rational test) {
double retval = (double)test.numerator / (double)test.denominator;
return retval;
}
//Step 10 Step 10 Write a modifier named reduce that reduces a rational number to its lowest terms
public static void reduce(Rational test) {
int remain, num1, num2;
num1 = test.numerator;
num2 = test.denominator;
remain = num1 % num2;
while(remain != 0) {
remain = num1 % num2 ;
num1 = num2;
num2 = remain;
}
int GCD = num1;
Rational ret = new Rational(test.numerator/GCD, test.denominator/GCD);
printRational(ret);
}
//Step 11 Write a method called add that takes two Rational numbers as arguments and returns a new Rational object. The return object should contain the sum of the arguments.
public static Rational add(Rational test, Rational testAdd) {
Rational retAdd = new Rational(0, 0);
if(test.denominator == testAdd.denominator) {
retAdd.numerator = test.numerator + testAdd.numerator;
retAdd.denominator = test.denominator;
return retAdd;
} else {
retAdd.numerator = test.numerator * testAdd.denominator;
retAdd.denominator = test.denominator * testAdd.denominator;
testAdd.numerator = testAdd.numerator * test.denominator;
testAdd.denominator = testAdd.denominator * test.denominator;
retAdd.numerator += testAdd.numerator;
int remain, num1, num2;
num1 = retAdd.numerator;
num2 = retAdd.denominator;
remain = num1 % num2;
while(remain != 0) {
remain = num1 % num2 ;
num1 = num2;
num2 = remain;
}
int GCD = num1;
retAdd.numerator = retAdd.numerator/GCD;
retAdd.denominator = retAdd.denominator/GCD;
return retAdd;
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.