CECS 277
LAB ASSIGNMENT #1
Assigned date: Wed 1/20
Due date: Wed 1/27
10 points
Objectives
- Be familar with classes and objects
- Create a class and objects to implement Rational arithmetic operations.
Prelab
- Read chapter 8 - Classes and objects
- Read the javadoc note posted on the class website
Problem
Create a class called Fraction for performing arithmetic operations with fractions. The class Fraction must have the following data members, contructors and methods:
- Data members:
private
int numerator
int denominator
- Constructors
Default-argument constructor
Two-argument constructor to initialize numerator and denominator
- Accessors (Getters)
public int getNumerator() - return the numerator
public int getDenominator() - return the denominator
- Modifiers (Setters)
public void setNumberator(int value) - set the numerator to value
public void setDenominator(int value) - set the denominator to value
- public inputFraction()
Input the numerator and the denominator from the keyboard
- public String toString()
return a string in the form numerator/denominator
- private int gcd(int m, int n) - return the greatest common divisor
int r;
while(n != 0)
{ r = m % n;
m = n;
n = r;
}
return m;
- Add two Fraction numbers
public Fraction add(Fraction f)
- Subtract two Fraction numbers
pubic void sub(Fraction, f1, Fraction f2)
- Multiply two Fraction numbers
public Fraction mul(Fraction f)
- Divide two Fraction Numbers
public void div(Fraction f1,Fraction f2)
- Return the real value of Fraction f1 divided by Fraction f2
public static double divFraction(Fraction f1,Fraction f2)
Write a main method to testthe class Fraction. All the Fraction objects should must be reduced by using the greatest common denominator.
- Input Fraction object F1
- Input Fraction object F2
- Add the Fraction objects F1 and F2 and store the result in the Fraction object F3. Display the result in the following format:
numerator/demoninator + numberator/denominator = numberator/denominator.
- Subtract the Fraction object F2 from the Fraction object F1 and store the result in the Fraction object F3. Display the result in the following format:
numerator/demoninator - numberator/denominator = numberator/denominator.
- Multiply the Fraction object F1 with the Fraction object F2 and store the result in the object F3. Display the result in the following format:
numerator/demoninator * numberator/denominator = numberator/denominator.
- Divide the Fraction object F1by the Fraction object F2 and store the result in the object F3. Display the result in the following format:
numerator/demoninator / numberator/denominator = numberator/denominator.
- Display the result of dividing the object F1 by the object F2 in a real number.
- Change the numerator of object F1 to 2
- Change the denominator of object F2 to 5
- Display the numerator of F1 and the denominator of F2 in the following format:
Numerator: ______
Denominator: _____
Grading requirements
- A hard copy of your source code, and runtime output.
- Document your program with Javadoc and other comments
- Demonstrate the result to the instructor
- Submit the lab to the Beachboard.