Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- /** This class represents fractions of form n/d where n and d are long integer
- * numbers. Basic operations and arithmetics for fractions are provided.
- */
- public class Lfraction implements Comparable<Lfraction> {
- private static long numerator;
- private static long denominator;
- /** Main method. Different tests. */
- public static void main (String[] param) {
- // TODO!!! Your debugging tests here
- }
- /** Constructor.
- * @param a numerator
- * @param b denominator > 0
- */
- public Lfraction (long a, long b) {
- long numerator = a;
- long denominator = Math.abs(b);
- }
- /** Public method to access the numerator field.
- * @return numerator
- */
- public long getNumerator() {
- return Lfraction.numerator;
- }
- /** Public method to access the denominator field.
- * @return denominator
- */
- public long getDenominator() {
- return Lfraction.denominator;
- }
- /** Conversion to string.
- * @return string representation of the fraction
- */
- @Override
- public String toString() {
- return null; // TODO!!!
- }
- /** Equality test.
- * @param m second fraction
- * @return true if fractions this and m are equal
- */
- @Override
- public boolean equals (Object m) {
- if ((minus((Lfraction)(m))).getNumerator() == 0)
- return true;
- else
- return false;
- }
- /** Hashcode has to be equal for equal fractions.
- * @return hashcode
- */
- @Override
- public int hashCode() {
- return 0; // TODO!!!
- }
- /** Sum of fractions.
- * @param m second addend
- * @return this+m
- */
- public Lfraction plus (Lfraction m) {
- long a = numerator * m.getDenominator() + denominator * m.getNumerator();
- long b = denominator * m.getDenominator();
- return new Lfraction(a, b);
- }
- /** Multiplication of fractions.
- * @param m second factor
- * @return this*m
- */
- public Lfraction times (Lfraction m) {
- long a = numerator * m.getNumerator();
- long b = denominator * m.getDenominator();
- return new Lfraction(a, b);
- }
- /** Inverse of the fraction. n/d becomes d/n.
- * @return inverse of this fraction: 1/this
- */
- public Lfraction inverse() {
- return null; // TODO!!!
- }
- /** Opposite of the fraction. n/d becomes -n/d.
- * @return opposite of this fraction: -this
- */
- public Lfraction opposite() {
- return null; // TODO!!!
- }
- /** Difference of fractions.
- * @param m subtrahend
- * @return this-m
- */
- public Lfraction minus (Lfraction m) {
- long a = numerator * m.getDenominator() - denominator * m.getNumerator();
- long b = denominator * m.getDenominator();
- return new Lfraction(a, b);
- }
- /** Quotient of fractions.
- * @param m divisor
- * @return this/m
- */
- public Lfraction divideBy (Lfraction m) {
- return null; // TODO!!!
- }
- /** Comparision of fractions.
- * @param m second fraction
- * @return -1 if this < m; 0 if this==m; 1 if this > m
- */
- @Override
- public int compareTo (Lfraction m) {
- if (this.minus(m).getNumerator() > 0)
- return 1;
- else if (this.minus(m).getNumerator() < 0)
- return -1;
- else
- return 0;
- }
- /** Clone of the fraction.
- * @return new fraction equal to this
- */
- @Override
- public Object clone() throws CloneNotSupportedException {
- return null; // TODO!!!
- }
- /** Integer part of the (improper) fraction.
- * @return integer part of this fraction
- */
- public long integerPart() {
- return 0L; // TODO!!!
- }
- /** Extract fraction part of the (improper) fraction
- * (a proper fraction without the integer part).
- * @return fraction part of this fraction
- */
- public Lfraction fractionPart() {
- return null; // TODO!!!
- }
- /** Approximate value of the fraction.
- * @return numeric value of this fraction
- */
- public double toDouble() {
- return numerator * 1.0 / denominator;
- }
- /** Double value f presented as a fraction with denominator d > 0.
- * @param f real number
- * @param d positive denominator for the result
- * @return f as an approximate fraction of form n/d
- */
- public static Lfraction toLfraction (double f, long d) {
- return null; // TODO!!!
- }
- /** Conversion from string to the fraction. Accepts strings of form
- * that is defined by the toString method.
- * @param s string form (as produced by toString) of the fraction
- * @return fraction represented by s
- */
- public static Lfraction valueOf (String s) {
- return null; // TODO!!!
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement