commit f3009d068ef32c749f2c4d6212a84757f9aa1989 Author: Jaanus Poeial Date: Thu Sep 22 12:39:36 2016 +0300 initial setup diff --git a/Lfraction/Lfraction.html b/Lfraction/Lfraction.html new file mode 100644 index 0000000..717a1f0 --- /dev/null +++ b/Lfraction/Lfraction.html @@ -0,0 +1,605 @@ + + + + + + +Lfraction + + + + + + + +
+ + + + + +
+ + + +
+

Class Lfraction

+
+
+ +
+
    +
  • +
    +
    All Implemented Interfaces:
    +
    java.lang.Comparable<Lfraction>
    +
    +
    +
    +
    public class Lfraction
    +extends java.lang.Object
    +implements java.lang.Comparable<Lfraction>
    +
    This class presents fractions of form n/d where n and d are long integer + numbers. Basic operations and arithmetics for fractions are provided.
    +
  • +
+
+
+
    +
  • + +
      +
    • + + +

      Constructor Summary

      + + + + + + + + +
      Constructors 
      Constructor and Description
      Lfraction(long a, + long b) +
      Constructor.
      +
      +
    • +
    + +
      +
    • + + +

      Method Summary

      + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
      Methods 
      Modifier and TypeMethod and Description
      java.lang.Objectclone() +
      Clone of the fraction.
      +
      intcompareTo(Lfraction m) +
      Comparision of fractions.
      +
      LfractiondivideBy(Lfraction m) +
      Quotient of fractions.
      +
      booleanequals(java.lang.Object m) +
      Equality test.
      +
      LfractionfractionPart() +
      Extract fraction part of the (improper) fraction + (a proper fraction without the integer part).
      +
      longgetDenominator() +
      Public method to access the denominator field.
      +
      longgetNumerator() +
      Public method to access the numerator field.
      +
      inthashCode() +
      Hashcode has to be equal for equal fractions.
      +
      longintegerPart() +
      Integer part of the (improper) fraction.
      +
      Lfractioninverse() +
      Inverse of the fraction.
      +
      static voidmain(java.lang.String[] param) +
      Main method.
      +
      Lfractionminus(Lfraction m) +
      Difference of fractions.
      +
      Lfractionopposite() +
      Opposite of the fraction.
      +
      Lfractionplus(Lfraction m) +
      Sum of fractions.
      +
      Lfractiontimes(Lfraction m) +
      Multiplication of fractions.
      +
      doubletoDouble() +
      Approximate value of the fraction.
      +
      static LfractiontoLfraction(double f, + long d) +
      Double value f presented as a fraction with denominator d > 0.
      +
      java.lang.StringtoString() +
      Conversion to string.
      +
      static LfractionvalueOf(java.lang.String s) +
      Conversion from string to the fraction.
      +
      +
        +
      • + + +

        Methods inherited from class java.lang.Object

        +finalize, getClass, notify, notifyAll, wait, wait, wait
      • +
      +
    • +
    +
  • +
+
+
+
    +
  • + +
      +
    • + + +

      Constructor Detail

      + + + +
        +
      • +

        Lfraction

        +
        public Lfraction(long a,
        +         long b)
        +
        Constructor.
        +
        Parameters:
        a - numerator
        b - denominator > 0
        +
      • +
      +
    • +
    + +
      +
    • + + +

      Method Detail

      + + + +
        +
      • +

        main

        +
        public static void main(java.lang.String[] param)
        +
        Main method. Different tests.
        +
      • +
      + + + +
        +
      • +

        getNumerator

        +
        public long getNumerator()
        +
        Public method to access the numerator field.
        +
        Returns:
        numerator
        +
      • +
      + + + +
        +
      • +

        getDenominator

        +
        public long getDenominator()
        +
        Public method to access the denominator field.
        +
        Returns:
        denominator
        +
      • +
      + + + +
        +
      • +

        toString

        +
        public java.lang.String toString()
        +
        Conversion to string.
        +
        +
        Overrides:
        +
        toString in class java.lang.Object
        +
        Returns:
        string representation of the fraction
        +
      • +
      + + + +
        +
      • +

        equals

        +
        public boolean equals(java.lang.Object m)
        +
        Equality test.
        +
        +
        Overrides:
        +
        equals in class java.lang.Object
        +
        Parameters:
        m - second fraction
        +
        Returns:
        true if fractions this and m are equal
        +
      • +
      + + + +
        +
      • +

        hashCode

        +
        public int hashCode()
        +
        Hashcode has to be equal for equal fractions.
        +
        +
        Overrides:
        +
        hashCode in class java.lang.Object
        +
        Returns:
        hashcode
        +
      • +
      + + + +
        +
      • +

        plus

        +
        public Lfraction plus(Lfraction m)
        +
        Sum of fractions.
        +
        Parameters:
        m - second addend
        +
        Returns:
        this+m
        +
      • +
      + + + +
        +
      • +

        times

        +
        public Lfraction times(Lfraction m)
        +
        Multiplication of fractions.
        +
        Parameters:
        m - second factor
        +
        Returns:
        this*m
        +
      • +
      + + + +
        +
      • +

        inverse

        +
        public Lfraction inverse()
        +
        Inverse of the fraction. n/d becomes d/n.
        +
        Returns:
        inverse of this fraction: 1/this
        +
      • +
      + + + +
        +
      • +

        opposite

        +
        public Lfraction opposite()
        +
        Opposite of the fraction. n/d becomes -n/d.
        +
        Returns:
        opposite of this fraction: -this
        +
      • +
      + + + +
        +
      • +

        minus

        +
        public Lfraction minus(Lfraction m)
        +
        Difference of fractions.
        +
        Parameters:
        m - subtrahend
        +
        Returns:
        this-m
        +
      • +
      + + + +
        +
      • +

        divideBy

        +
        public Lfraction divideBy(Lfraction m)
        +
        Quotient of fractions.
        +
        Parameters:
        m - divisor
        +
        Returns:
        this/m
        +
      • +
      + + + +
        +
      • +

        compareTo

        +
        public int compareTo(Lfraction m)
        +
        Comparision of fractions.
        +
        +
        Specified by:
        +
        compareTo in interface java.lang.Comparable<Lfraction>
        +
        Parameters:
        m - second fraction
        +
        Returns:
        -1 if this < m; 0 if this==m; 1 if this > m
        +
      • +
      + + + +
        +
      • +

        clone

        +
        public java.lang.Object clone()
        +                       throws java.lang.CloneNotSupportedException
        +
        Clone of the fraction.
        +
        +
        Overrides:
        +
        clone in class java.lang.Object
        +
        Returns:
        new fraction equal to this
        +
        Throws:
        +
        java.lang.CloneNotSupportedException
        +
      • +
      + + + +
        +
      • +

        integerPart

        +
        public long integerPart()
        +
        Integer part of the (improper) fraction.
        +
        Returns:
        integer part of this fraction
        +
      • +
      + + + +
        +
      • +

        fractionPart

        +
        public Lfraction fractionPart()
        +
        Extract fraction part of the (improper) fraction + (a proper fraction without the integer part).
        +
        Returns:
        fraction part of this fraction
        +
      • +
      + + + +
        +
      • +

        toDouble

        +
        public double toDouble()
        +
        Approximate value of the fraction.
        +
        Returns:
        numeric value of this fraction
        +
      • +
      + + + +
        +
      • +

        toLfraction

        +
        public static Lfraction toLfraction(double f,
        +                    long d)
        +
        Double value f presented as a fraction with denominator d > 0.
        +
        Parameters:
        f - real number
        d - positive denominator for the result
        +
        Returns:
        f as an approximate fraction of form n/d
        +
      • +
      + + + +
        +
      • +

        valueOf

        +
        public static Lfraction valueOf(java.lang.String s)
        +
        Conversion from string to the fraction. Accepts strings of form + that is defined by the toString method.
        +
        Parameters:
        s - string form (as produced by toString) of the fraction
        +
        Returns:
        fraction represented by s
        +
      • +
      +
    • +
    +
  • +
+
+
+ + +
+ + + + + +
+ + + + diff --git a/Lfraction/Lfraction.java b/Lfraction/Lfraction.java new file mode 100644 index 0000000..c7b3778 --- /dev/null +++ b/Lfraction/Lfraction.java @@ -0,0 +1,164 @@ +import java.util.*; + +/** This class presents 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 { + + /** Main method. Different tests. */ + public static void main (String[] param) { + // TODO!!! Your debugging tests here + } + + // TODO!!! instance variables here + + /** Constructor. + * @param a numerator + * @param b denominator > 0 + */ + public Lfraction (long a, long b) { + // TODO!!! + } + + /** Public method to access the numerator field. + * @return numerator + */ + public long getNumerator() { + return 0L; // TODO!!! + } + + /** Public method to access the denominator field. + * @return denominator + */ + public long getDenominator() { + return 1L; // TODO!!! + } + + /** Conversion to string. + * @return string representation of the fraction + */ + @Override + public String toString() { + return ""; // TODO!!! + } + + /** Equality test. + * @param m second fraction + * @return true if fractions this and m are equal + */ + @Override + public boolean equals (Object m) { + return false; // TODO!!! + } + + /** 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) { + return null; // TODO!!! + } + + /** Multiplication of fractions. + * @param m second factor + * @return this*m + */ + public Lfraction times (Lfraction m) { + return null; // TODO!!! + } + + /** 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) { + return null; // TODO!!! + } + + /** 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 + */ + public int compareTo (Lfraction m) { + return 0; // TODO!!! + } + + /** 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 0.; // TODO!!! + } + + /** 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!!! + } +} + diff --git a/Lfraction/allclasses-frame.html b/Lfraction/allclasses-frame.html new file mode 100644 index 0000000..ccb4f3f --- /dev/null +++ b/Lfraction/allclasses-frame.html @@ -0,0 +1,19 @@ + + + + + + +All Classes + + + + +

All Classes

+
+ +
+ + diff --git a/Lfraction/allclasses-noframe.html b/Lfraction/allclasses-noframe.html new file mode 100644 index 0000000..a588627 --- /dev/null +++ b/Lfraction/allclasses-noframe.html @@ -0,0 +1,19 @@ + + + + + + +All Classes + + + + +

All Classes

+
+ +
+ + diff --git a/Lfraction/constant-values.html b/Lfraction/constant-values.html new file mode 100644 index 0000000..7b0e173 --- /dev/null +++ b/Lfraction/constant-values.html @@ -0,0 +1,112 @@ + + + + + + +Constant Field Values + + + + + + + + + + +
+

Constant Field Values

+

Contents

+
+ + + + + + diff --git a/Lfraction/deprecated-list.html b/Lfraction/deprecated-list.html new file mode 100644 index 0000000..4914a4a --- /dev/null +++ b/Lfraction/deprecated-list.html @@ -0,0 +1,112 @@ + + + + + + +Deprecated List + + + + + + + +
+ + + + + +
+ + +
+

Deprecated API

+

Contents

+
+ +
+ + + + + +
+ + + + diff --git a/Lfraction/help-doc.html b/Lfraction/help-doc.html new file mode 100644 index 0000000..7294f8e --- /dev/null +++ b/Lfraction/help-doc.html @@ -0,0 +1,209 @@ + + + + + + +API Help + + + + + + + +
+ + + + + +
+ + +
+

How This API Document Is Organized

+
This API (Application Programming Interface) document has pages corresponding to the items in the navigation bar, described as follows.
+
+
+
    +
  • +

    Package

    +

    Each package has a page that contains a list of its classes and interfaces, with a summary for each. This page can contain six categories:

    +
      +
    • Interfaces (italic)
    • +
    • Classes
    • +
    • Enums
    • +
    • Exceptions
    • +
    • Errors
    • +
    • Annotation Types
    • +
    +
  • +
  • +

    Class/Interface

    +

    Each class, interface, nested class and nested interface has its own separate page. Each of these pages has three sections consisting of a class/interface description, summary tables, and detailed member descriptions:

    +
      +
    • Class inheritance diagram
    • +
    • Direct Subclasses
    • +
    • All Known Subinterfaces
    • +
    • All Known Implementing Classes
    • +
    • Class/interface declaration
    • +
    • Class/interface description
    • +
    +
      +
    • Nested Class Summary
    • +
    • Field Summary
    • +
    • Constructor Summary
    • +
    • Method Summary
    • +
    +
      +
    • Field Detail
    • +
    • Constructor Detail
    • +
    • Method Detail
    • +
    +

    Each summary entry contains the first sentence from the detailed description for that item. The summary entries are alphabetical, while the detailed descriptions are in the order they appear in the source code. This preserves the logical groupings established by the programmer.

    +
  • +
  • +

    Annotation Type

    +

    Each annotation type has its own separate page with the following sections:

    +
      +
    • Annotation Type declaration
    • +
    • Annotation Type description
    • +
    • Required Element Summary
    • +
    • Optional Element Summary
    • +
    • Element Detail
    • +
    +
  • +
  • +

    Enum

    +

    Each enum has its own separate page with the following sections:

    +
      +
    • Enum declaration
    • +
    • Enum description
    • +
    • Enum Constant Summary
    • +
    • Enum Constant Detail
    • +
    +
  • +
  • +

    Tree (Class Hierarchy)

    +

    There is a Class Hierarchy page for all packages, plus a hierarchy for each package. Each hierarchy page contains a list of classes and a list of interfaces. The classes are organized by inheritance structure starting with java.lang.Object. The interfaces do not inherit from java.lang.Object.

    +
      +
    • When viewing the Overview page, clicking on "Tree" displays the hierarchy for all packages.
    • +
    • When viewing a particular package, class or interface page, clicking "Tree" displays the hierarchy for only that package.
    • +
    +
  • +
  • +

    Deprecated API

    +

    The Deprecated API page lists all of the API that have been deprecated. A deprecated API is not recommended for use, generally due to improvements, and a replacement API is usually given. Deprecated APIs may be removed in future implementations.

    +
  • +
  • +

    Index

    +

    The Index contains an alphabetic list of all classes, interfaces, constructors, methods, and fields.

    +
  • +
  • +

    Prev/Next

    +

    These links take you to the next or previous class, interface, package, or related page.

    +
  • +
  • +

    Frames/No Frames

    +

    These links show and hide the HTML frames. All pages are available with or without frames.

    +
  • +
  • +

    All Classes

    +

    The All Classes link shows all classes and interfaces except non-static nested types.

    +
  • +
  • +

    Serialized Form

    +

    Each serializable or externalizable class has a description of its serialization fields and methods. This information is of interest to re-implementors, not to developers using the API. While there is no link in the navigation bar, you can get to this information by going to any serialized class and clicking "Serialized Form" in the "See also" section of the class description.

    +
  • +
  • +

    Constant Field Values

    +

    The Constant Field Values page lists the static final fields and their values.

    +
  • +
+This help file applies to API documentation generated using the standard doclet.
+ +
+ + + + + +
+ + + + diff --git a/Lfraction/index-all.html b/Lfraction/index-all.html new file mode 100644 index 0000000..c6770bd --- /dev/null +++ b/Lfraction/index-all.html @@ -0,0 +1,273 @@ + + + + + + +Index + + + + + + + +
+ + + + + +
+ + +
C D E F G H I L M O P T V  + + +

C

+
+
clone() - Method in class Lfraction
+
+
Clone of the fraction.
+
+
compareTo(Lfraction) - Method in class Lfraction
+
+
Comparision of fractions.
+
+
+ + + +

D

+
+
divideBy(Lfraction) - Method in class Lfraction
+
+
Quotient of fractions.
+
+
+ + + +

E

+
+
equals(Object) - Method in class Lfraction
+
+
Equality test.
+
+
+ + + +

F

+
+
fractionPart() - Method in class Lfraction
+
+
Extract fraction part of the (improper) fraction + (a proper fraction without the integer part).
+
+
+ + + +

G

+
+
getDenominator() - Method in class Lfraction
+
+
Public method to access the denominator field.
+
+
getNumerator() - Method in class Lfraction
+
+
Public method to access the numerator field.
+
+
+ + + +

H

+
+
hashCode() - Method in class Lfraction
+
+
Hashcode has to be equal for equal fractions.
+
+
+ + + +

I

+
+
integerPart() - Method in class Lfraction
+
+
Integer part of the (improper) fraction.
+
+
inverse() - Method in class Lfraction
+
+
Inverse of the fraction.
+
+
+ + + +

L

+
+
Lfraction - Class in <Unnamed>
+
+
This class presents fractions of form n/d where n and d are long integer + numbers.
+
+
Lfraction(long, long) - Constructor for class Lfraction
+
+
Constructor.
+
+
+ + + +

M

+
+
main(String[]) - Static method in class Lfraction
+
+
Main method.
+
+
minus(Lfraction) - Method in class Lfraction
+
+
Difference of fractions.
+
+
+ + + +

O

+
+
opposite() - Method in class Lfraction
+
+
Opposite of the fraction.
+
+
+ + + +

P

+
+
plus(Lfraction) - Method in class Lfraction
+
+
Sum of fractions.
+
+
+ + + +

T

+
+
times(Lfraction) - Method in class Lfraction
+
+
Multiplication of fractions.
+
+
toDouble() - Method in class Lfraction
+
+
Approximate value of the fraction.
+
+
toLfraction(double, long) - Static method in class Lfraction
+
+
Double value f presented as a fraction with denominator d > 0.
+
+
toString() - Method in class Lfraction
+
+
Conversion to string.
+
+
+ + + +

V

+
+
valueOf(String) - Static method in class Lfraction
+
+
Conversion from string to the fraction.
+
+
+C D E F G H I L M O P T V 
+ +
+ + + + + +
+ + + + diff --git a/Lfraction/index.html b/Lfraction/index.html new file mode 100644 index 0000000..931810d --- /dev/null +++ b/Lfraction/index.html @@ -0,0 +1,65 @@ + + + + + + +Generated Documentation (Untitled) + + + + + + +<noscript> +<div>JavaScript is disabled on your browser.</div> +</noscript> +<h2>Frame Alert</h2> +<p>This document is designed to be viewed using the frames feature. If you see this message, you are using a non-frame-capable web client. Link to <a href="Lfraction.html">Non-frame version</a>.</p> + + + diff --git a/Lfraction/overview-tree.html b/Lfraction/overview-tree.html new file mode 100644 index 0000000..c09f609 --- /dev/null +++ b/Lfraction/overview-tree.html @@ -0,0 +1,121 @@ + + + + + + +Class Hierarchy + + + + + + + +
+ + + + + +
+ + +
+

Hierarchy For All Packages

+
+
+

Class Hierarchy

+
    +
  • java.lang.Object +
      +
    • Lfraction (implements java.lang.Comparable<T>)
    • +
    +
  • +
+
+ +
+ + + + + +
+ + + + diff --git a/Lfraction/package-frame.html b/Lfraction/package-frame.html new file mode 100644 index 0000000..9d28c3f --- /dev/null +++ b/Lfraction/package-frame.html @@ -0,0 +1,20 @@ + + + + + + +&lt;Unnamed&gt; + + + + +

<Unnamed>

+
+

Classes

+ +
+ + diff --git a/Lfraction/package-list b/Lfraction/package-list new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/Lfraction/package-list @@ -0,0 +1 @@ + diff --git a/Lfraction/package-summary.html b/Lfraction/package-summary.html new file mode 100644 index 0000000..7976b26 --- /dev/null +++ b/Lfraction/package-summary.html @@ -0,0 +1,126 @@ + + + + + + + + + + + + + + + +
+

Package <Unnamed>

+
+
+
    +
  • + + + + + + + + + + + + +
    Class Summary 
    ClassDescription
    Lfraction +
    This class presents fractions of form n/d where n and d are long integer + numbers.
    +
    +
  • +
+
+ + + + + + diff --git a/Lfraction/package-tree.html b/Lfraction/package-tree.html new file mode 100644 index 0000000..365a6f3 --- /dev/null +++ b/Lfraction/package-tree.html @@ -0,0 +1,121 @@ + + + + + + + Class Hierarchy + + + + + + + +
+ + + + + +
+ + +
+

Hierarchy For Package <Unnamed>

+
+
+

Class Hierarchy

+
    +
  • java.lang.Object +
      +
    • Lfraction (implements java.lang.Comparable<T>)
    • +
    +
  • +
+
+ +
+ + + + + +
+ + + + diff --git a/Lfraction/resources/background.gif b/Lfraction/resources/background.gif new file mode 100644 index 0000000..f471940 Binary files /dev/null and b/Lfraction/resources/background.gif differ diff --git a/Lfraction/resources/tab.gif b/Lfraction/resources/tab.gif new file mode 100644 index 0000000..1a73a83 Binary files /dev/null and b/Lfraction/resources/tab.gif differ diff --git a/Lfraction/resources/titlebar.gif b/Lfraction/resources/titlebar.gif new file mode 100644 index 0000000..17443b3 Binary files /dev/null and b/Lfraction/resources/titlebar.gif differ diff --git a/Lfraction/resources/titlebar_end.gif b/Lfraction/resources/titlebar_end.gif new file mode 100644 index 0000000..3ad78d4 Binary files /dev/null and b/Lfraction/resources/titlebar_end.gif differ diff --git a/Lfraction/stylesheet.css b/Lfraction/stylesheet.css new file mode 100644 index 0000000..0aeaa97 --- /dev/null +++ b/Lfraction/stylesheet.css @@ -0,0 +1,474 @@ +/* Javadoc style sheet */ +/* +Overall document style +*/ +body { + background-color:#ffffff; + color:#353833; + font-family:Arial, Helvetica, sans-serif; + font-size:76%; + margin:0; +} +a:link, a:visited { + text-decoration:none; + color:#4c6b87; +} +a:hover, a:focus { + text-decoration:none; + color:#bb7a2a; +} +a:active { + text-decoration:none; + color:#4c6b87; +} +a[name] { + color:#353833; +} +a[name]:hover { + text-decoration:none; + color:#353833; +} +pre { + font-size:1.3em; +} +h1 { + font-size:1.8em; +} +h2 { + font-size:1.5em; +} +h3 { + font-size:1.4em; +} +h4 { + font-size:1.3em; +} +h5 { + font-size:1.2em; +} +h6 { + font-size:1.1em; +} +ul { + list-style-type:disc; +} +code, tt { + font-size:1.2em; +} +dt code { + font-size:1.2em; +} +table tr td dt code { + font-size:1.2em; + vertical-align:top; +} +sup { + font-size:.6em; +} +/* +Document title and Copyright styles +*/ +.clear { + clear:both; + height:0px; + overflow:hidden; +} +.aboutLanguage { + float:right; + padding:0px 21px; + font-size:.8em; + z-index:200; + margin-top:-7px; +} +.legalCopy { + margin-left:.5em; +} +.bar a, .bar a:link, .bar a:visited, .bar a:active { + color:#FFFFFF; + text-decoration:none; +} +.bar a:hover, .bar a:focus { + color:#bb7a2a; +} +.tab { + background-color:#0066FF; + background-image:url(resources/titlebar.gif); + background-position:left top; + background-repeat:no-repeat; + color:#ffffff; + padding:8px; + width:5em; + font-weight:bold; +} +/* +Navigation bar styles +*/ +.bar { + background-image:url(resources/background.gif); + background-repeat:repeat-x; + color:#FFFFFF; + padding:.8em .5em .4em .8em; + height:auto;/*height:1.8em;*/ + font-size:1em; + margin:0; +} +.topNav { + background-image:url(resources/background.gif); + background-repeat:repeat-x; + color:#FFFFFF; + float:left; + padding:0; + width:100%; + clear:right; + height:2.8em; + padding-top:10px; + overflow:hidden; +} +.bottomNav { + margin-top:10px; + background-image:url(resources/background.gif); + background-repeat:repeat-x; + color:#FFFFFF; + float:left; + padding:0; + width:100%; + clear:right; + height:2.8em; + padding-top:10px; + overflow:hidden; +} +.subNav { + background-color:#dee3e9; + border-bottom:1px solid #9eadc0; + float:left; + width:100%; + overflow:hidden; +} +.subNav div { + clear:left; + float:left; + padding:0 0 5px 6px; +} +ul.navList, ul.subNavList { + float:left; + margin:0 25px 0 0; + padding:0; +} +ul.navList li{ + list-style:none; + float:left; + padding:3px 6px; +} +ul.subNavList li{ + list-style:none; + float:left; + font-size:90%; +} +.topNav a:link, .topNav a:active, .topNav a:visited, .bottomNav a:link, .bottomNav a:active, .bottomNav a:visited { + color:#FFFFFF; + text-decoration:none; +} +.topNav a:hover, .bottomNav a:hover { + text-decoration:none; + color:#bb7a2a; +} +.navBarCell1Rev { + background-image:url(resources/tab.gif); + background-color:#a88834; + color:#FFFFFF; + margin: auto 5px; + border:1px solid #c9aa44; +} +/* +Page header and footer styles +*/ +.header, .footer { + clear:both; + margin:0 20px; + padding:5px 0 0 0; +} +.indexHeader { + margin:10px; + position:relative; +} +.indexHeader h1 { + font-size:1.3em; +} +.title { + color:#2c4557; + margin:10px 0; +} +.subTitle { + margin:5px 0 0 0; +} +.header ul { + margin:0 0 25px 0; + padding:0; +} +.footer ul { + margin:20px 0 5px 0; +} +.header ul li, .footer ul li { + list-style:none; + font-size:1.2em; +} +/* +Heading styles +*/ +div.details ul.blockList ul.blockList ul.blockList li.blockList h4, div.details ul.blockList ul.blockList ul.blockListLast li.blockList h4 { + background-color:#dee3e9; + border-top:1px solid #9eadc0; + border-bottom:1px solid #9eadc0; + margin:0 0 6px -8px; + padding:2px 5px; +} +ul.blockList ul.blockList ul.blockList li.blockList h3 { + background-color:#dee3e9; + border-top:1px solid #9eadc0; + border-bottom:1px solid #9eadc0; + margin:0 0 6px -8px; + padding:2px 5px; +} +ul.blockList ul.blockList li.blockList h3 { + padding:0; + margin:15px 0; +} +ul.blockList li.blockList h2 { + padding:0px 0 20px 0; +} +/* +Page layout container styles +*/ +.contentContainer, .sourceContainer, .classUseContainer, .serializedFormContainer, .constantValuesContainer { + clear:both; + padding:10px 20px; + position:relative; +} +.indexContainer { + margin:10px; + position:relative; + font-size:1.0em; +} +.indexContainer h2 { + font-size:1.1em; + padding:0 0 3px 0; +} +.indexContainer ul { + margin:0; + padding:0; +} +.indexContainer ul li { + list-style:none; +} +.contentContainer .description dl dt, .contentContainer .details dl dt, .serializedFormContainer dl dt { + font-size:1.1em; + font-weight:bold; + margin:10px 0 0 0; + color:#4E4E4E; +} +.contentContainer .description dl dd, .contentContainer .details dl dd, .serializedFormContainer dl dd { + margin:10px 0 10px 20px; +} +.serializedFormContainer dl.nameValue dt { + margin-left:1px; + font-size:1.1em; + display:inline; + font-weight:bold; +} +.serializedFormContainer dl.nameValue dd { + margin:0 0 0 1px; + font-size:1.1em; + display:inline; +} +/* +List styles +*/ +ul.horizontal li { + display:inline; + font-size:0.9em; +} +ul.inheritance { + margin:0; + padding:0; +} +ul.inheritance li { + display:inline; + list-style:none; +} +ul.inheritance li ul.inheritance { + margin-left:15px; + padding-left:15px; + padding-top:1px; +} +ul.blockList, ul.blockListLast { + margin:10px 0 10px 0; + padding:0; +} +ul.blockList li.blockList, ul.blockListLast li.blockList { + list-style:none; + margin-bottom:25px; +} +ul.blockList ul.blockList li.blockList, ul.blockList ul.blockListLast li.blockList { + padding:0px 20px 5px 10px; + border:1px solid #9eadc0; + background-color:#f9f9f9; +} +ul.blockList ul.blockList ul.blockList li.blockList, ul.blockList ul.blockList ul.blockListLast li.blockList { + padding:0 0 5px 8px; + background-color:#ffffff; + border:1px solid #9eadc0; + border-top:none; +} +ul.blockList ul.blockList ul.blockList ul.blockList li.blockList { + margin-left:0; + padding-left:0; + padding-bottom:15px; + border:none; + border-bottom:1px solid #9eadc0; +} +ul.blockList ul.blockList ul.blockList ul.blockList li.blockListLast { + list-style:none; + border-bottom:none; + padding-bottom:0; +} +table tr td dl, table tr td dl dt, table tr td dl dd { + margin-top:0; + margin-bottom:1px; +} +/* +Table styles +*/ +.contentContainer table, .classUseContainer table, .constantValuesContainer table { + border-bottom:1px solid #9eadc0; + width:100%; +} +.contentContainer ul li table, .classUseContainer ul li table, .constantValuesContainer ul li table { + width:100%; +} +.contentContainer .description table, .contentContainer .details table { + border-bottom:none; +} +.contentContainer ul li table th.colOne, .contentContainer ul li table th.colFirst, .contentContainer ul li table th.colLast, .classUseContainer ul li table th, .constantValuesContainer ul li table th, .contentContainer ul li table td.colOne, .contentContainer ul li table td.colFirst, .contentContainer ul li table td.colLast, .classUseContainer ul li table td, .constantValuesContainer ul li table td{ + vertical-align:top; + padding-right:20px; +} +.contentContainer ul li table th.colLast, .classUseContainer ul li table th.colLast,.constantValuesContainer ul li table th.colLast, +.contentContainer ul li table td.colLast, .classUseContainer ul li table td.colLast,.constantValuesContainer ul li table td.colLast, +.contentContainer ul li table th.colOne, .classUseContainer ul li table th.colOne, +.contentContainer ul li table td.colOne, .classUseContainer ul li table td.colOne { + padding-right:3px; +} +.overviewSummary caption, .packageSummary caption, .contentContainer ul.blockList li.blockList caption, .summary caption, .classUseContainer caption, .constantValuesContainer caption { + position:relative; + text-align:left; + background-repeat:no-repeat; + color:#FFFFFF; + font-weight:bold; + clear:none; + overflow:hidden; + padding:0px; + margin:0px; +} +caption a:link, caption a:hover, caption a:active, caption a:visited { + color:#FFFFFF; +} +.overviewSummary caption span, .packageSummary caption span, .contentContainer ul.blockList li.blockList caption span, .summary caption span, .classUseContainer caption span, .constantValuesContainer caption span { + white-space:nowrap; + padding-top:8px; + padding-left:8px; + display:block; + float:left; + background-image:url(resources/titlebar.gif); + height:18px; +} +.overviewSummary .tabEnd, .packageSummary .tabEnd, .contentContainer ul.blockList li.blockList .tabEnd, .summary .tabEnd, .classUseContainer .tabEnd, .constantValuesContainer .tabEnd { + width:10px; + background-image:url(resources/titlebar_end.gif); + background-repeat:no-repeat; + background-position:top right; + position:relative; + float:left; +} +ul.blockList ul.blockList li.blockList table { + margin:0 0 12px 0px; + width:100%; +} +.tableSubHeadingColor { + background-color: #EEEEFF; +} +.altColor { + background-color:#eeeeef; +} +.rowColor { + background-color:#ffffff; +} +.overviewSummary td, .packageSummary td, .contentContainer ul.blockList li.blockList td, .summary td, .classUseContainer td, .constantValuesContainer td { + text-align:left; + padding:3px 3px 3px 7px; +} +th.colFirst, th.colLast, th.colOne, .constantValuesContainer th { + background:#dee3e9; + border-top:1px solid #9eadc0; + border-bottom:1px solid #9eadc0; + text-align:left; + padding:3px 3px 3px 7px; +} +td.colOne a:link, td.colOne a:active, td.colOne a:visited, td.colOne a:hover, td.colFirst a:link, td.colFirst a:active, td.colFirst a:visited, td.colFirst a:hover, td.colLast a:link, td.colLast a:active, td.colLast a:visited, td.colLast a:hover, .constantValuesContainer td a:link, .constantValuesContainer td a:active, .constantValuesContainer td a:visited, .constantValuesContainer td a:hover { + font-weight:bold; +} +td.colFirst, th.colFirst { + border-left:1px solid #9eadc0; + white-space:nowrap; +} +td.colLast, th.colLast { + border-right:1px solid #9eadc0; +} +td.colOne, th.colOne { + border-right:1px solid #9eadc0; + border-left:1px solid #9eadc0; +} +table.overviewSummary { + padding:0px; + margin-left:0px; +} +table.overviewSummary td.colFirst, table.overviewSummary th.colFirst, +table.overviewSummary td.colOne, table.overviewSummary th.colOne { + width:25%; + vertical-align:middle; +} +table.packageSummary td.colFirst, table.overviewSummary th.colFirst { + width:25%; + vertical-align:middle; +} +/* +Content styles +*/ +.description pre { + margin-top:0; +} +.deprecatedContent { + margin:0; + padding:10px 0; +} +.docSummary { + padding:0; +} +/* +Formatting effect styles +*/ +.sourceLineNo { + color:green; + padding:0 30px 0 0; +} +h1.hidden { + visibility:hidden; + overflow:hidden; + font-size:.9em; +} +.block { + display:block; + margin:3px 0 0 0; +} +.strong { + font-weight:bold; +} diff --git a/README.md b/README.md new file mode 100644 index 0000000..fec0961 --- /dev/null +++ b/README.md @@ -0,0 +1,57 @@ +# README # + +See on neljanda kodutöö failide hoidla, mida saab kasutada ainult algseks lugemiseks. +Töötamiseks looge endale isiklik repositoorium, näiteks privaatne 'fork' bitbucket serverisse, millest saate luua klooni oma arvutisse. + +## Näidete kasutamine käsurealt ## +#### Kompileerimine: #### + +``` +#!bash + +javac -cp src src/Lfraction.java +``` + +#### Käivitamine: #### + +``` +#!bash + +java -cp src Lfraction +``` + + +### Testide kasutamine ### +#### Testi kompileerimine: #### + +``` +#!bash + +javac -encoding utf8 -cp 'src:test:test/junit-4.12.jar:test/hamcrest-core-1.3.jar' test/LfractionTest.java + +``` +Sama Windows aknas (koolonite asemel peavad olema semikoolonid): + +``` +#!bash + +javac -encoding utf8 -cp 'src;test;test/junit-4.12.jar;test/hamcrest-core-1.3.jar' test/LfractionTest.java + + +``` + +#### Testi käivitamine: #### + +``` +#!bash + +java -cp 'src:test:test/junit-4.12.jar:test/hamcrest-core-1.3.jar' org.junit.runner.JUnitCore LfractionTest +``` + +Sama Windows aknas (koolonite asemel semikoolonid): + +``` +#!bash + +java -cp 'src;test;test/junit-4.12.jar;test/hamcrest-core-1.3.jar' org.junit.runner.JUnitCore LfractionTest +``` diff --git a/description.html b/description.html new file mode 100644 index 0000000..48ad223 --- /dev/null +++ b/description.html @@ -0,0 +1,69 @@ + + + +i231.fraction.v2 + + + +Write an implementation (class Lfraction) for an abstract data +type "Fraction", where the fraction consists of two long integers: +numerator and denominator. Always convert +fractions to a form, where denominator is strictly positive and +the fraction is reduced. By default all operations (described on webpage +http://www.itcollege.ee/~jpoial/algoritmid/Lfraction/index.html ) must be exact +and use integer arithmetics only (e.g. do not use floating point arithmetics +to compare two fractions). +

+Remark 1. In addition to public methods listed you may need +a method to find the greatest common divisor of two integers to reduce +the fractions. +
+Remark 2. Operations must not modify operands, a new fraction must be +created for the result. +
+Remark 3. Methods toString and valueOf must +be dual: method valueOf must be able to interpret all +strings generated by the method toString . +
+Remark 4. If an error occurs (division by zero, illegal string, etc.) +the program must throw an exception of class RuntimeException +(or any appropriate subclass of RuntimeException). +
+Remark 5. Sign of a fraction is in numerator and integer numbers +expressed as fractions must have denominator 1, e.g. +3 is 3/1 , -5 is (-5)/1 and 0 is 0/1 . +

+Realiseerida abstraktne andmetüüp "murd pikkade täisarvude paarina". +
+Murdu esindab objekt klassist Lfraction, mis sisaldab murru +lugejat (ingl.k. numerator) ja murru nimetajat (ingl.k. denominator) +pikkade täisarvudena (teha nii, et nimetaja oleks alati rangelt positiivne +ning murd taandatud). +
+Realiseerida kõik operatsioonid, mis on kirjeldatud programmitoorikus (ja +veebilehel http://www.itcollege.ee/~jpoial/algoritmid/Lfraction/index.html ). +Murdude võrdlus peab olema täpne, s.t. toetuma arvutustele täisarvudega, +mitte reaalarvuliste lähiväärtustega. +

+Märkus 1. Lisaks loetletud avalikele meetoditele on teil tõenäoliselt +tarvis murru taandamise meetodit ja võib-olla ka kahe täisarvu +suurima ühisteguri leidmise meetodit. +
+Märkus 2. Reeglina ei tohi meetodid muuta objekti, millele neid rakendatakse +(näit. liitmisel ei tohi muuta liidetavaid, tuleb summa jaoks luua uus murd). +
+Märkus 3. Sõne teisendamisel murruks peab valueOf-meetod +olema kooskõlas toString-meetodiga (peab oskama tõlgendada +niisugust sõnet, mida toString tagastab). +
+Märkus 4. Veasituatsioonid (sõne, mida ei saa teisendada; nulliga jagamine +jt.) peavad tekitama erindi klassist RuntimeException või mõnest +selle klassi sobivast alamklassist. +
+Märkus 5. Murru märk käib lugeja juurde ning täisarvude nimetaja on 1 (ka +arvu 0 puhul). +

+ + + diff --git a/src/Lfraction.java b/src/Lfraction.java new file mode 100644 index 0000000..c7b3778 --- /dev/null +++ b/src/Lfraction.java @@ -0,0 +1,164 @@ +import java.util.*; + +/** This class presents 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 { + + /** Main method. Different tests. */ + public static void main (String[] param) { + // TODO!!! Your debugging tests here + } + + // TODO!!! instance variables here + + /** Constructor. + * @param a numerator + * @param b denominator > 0 + */ + public Lfraction (long a, long b) { + // TODO!!! + } + + /** Public method to access the numerator field. + * @return numerator + */ + public long getNumerator() { + return 0L; // TODO!!! + } + + /** Public method to access the denominator field. + * @return denominator + */ + public long getDenominator() { + return 1L; // TODO!!! + } + + /** Conversion to string. + * @return string representation of the fraction + */ + @Override + public String toString() { + return ""; // TODO!!! + } + + /** Equality test. + * @param m second fraction + * @return true if fractions this and m are equal + */ + @Override + public boolean equals (Object m) { + return false; // TODO!!! + } + + /** 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) { + return null; // TODO!!! + } + + /** Multiplication of fractions. + * @param m second factor + * @return this*m + */ + public Lfraction times (Lfraction m) { + return null; // TODO!!! + } + + /** 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) { + return null; // TODO!!! + } + + /** 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 + */ + public int compareTo (Lfraction m) { + return 0; // TODO!!! + } + + /** 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 0.; // TODO!!! + } + + /** 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!!! + } +} + diff --git a/src/TextIO.java b/src/TextIO.java new file mode 100644 index 0000000..a388710 --- /dev/null +++ b/src/TextIO.java @@ -0,0 +1,552 @@ + +/* + The file defines a class TextIO, which provides a simple interface + to Java's standard console input and output. This class defines + several static methods for reading and writing + values of various type. + + This class will only work with standard, interactive applications. + When it is used in such an application, System.out and System.in + should not be used directly, since the TextIO class thinks it has + exclusive control of System.out and System.in. (Actually, using + System.out will probably not cause any problems, but don't use + System.in.) + + To use this class in your program, simply include the compiled class + file TextIO.class in the same directory with the class file for your + main program. (If you are using a development environment such as + CodeWarrior or Visual J++, you can include the source file, + TextIO.java in your project.) You can then use all the public static methods + from the TextIO class in your program. (In your programs, the names + of the methods must be prefaced with "TextIO." For example, you should + use the name TextIO.getln() rather than simply getln().) + + (This class is for use with my on-line introductory java textbook, + which is available at http://math.hws.edu/eck/cs124/notes/index.html.) + + Written by: David Eck + Department of Mathematics and Computer Science + Hobart and William Smith Colleges + Geneva, NY 14456 + Email: eck@hws.edu + WWW: http://math.hws.edu/eck/ + + July 16, 1998 + + Modified February, 2000; getChar() now skips blanks and CR's, and getAnyChar() + can be used to read the next char even if it's a blank or CR. + +*/ +import java.io.*; + +public class TextIO { + + // *************************** I/O Methods ********************************* + + // Methods for writing the primitive types, plus type String, + // to the console, with no extra spaces. + // + // Note that the real-number data types, float + // and double, a rounded version is output that will + // use at most 10 or 11 characters. If you want to + // output a real number with full accuracy, use + // "TextIO.put(String.valueOf(x))", for example. + + public static void put(int x) { put(x,0); } // Note: also handles byte and short! + public static void put(long x) { put(x,0); } + public static void put(double x) { put(x,0); } // Also handles float. + public static void put(char x) { put(x,0); } + public static void put(boolean x) { put(x,0); } + public static void put(String x) { put(x,0); } + + + // Methods for writing the primitive types, plus type String, + // to the console,followed by a carriage return, with + // no extra spaces. + + public static void putln(int x) { put(x,0); newLine(); } // Note: also handles byte and short! + public static void putln(long x) { put(x,0); newLine(); } + public static void putln(double x) { put(x,0); newLine(); } // Also handles float. + public static void putln(char x) { put(x,0); newLine(); } + public static void putln(boolean x) { put(x,0); newLine(); } + public static void putln(String x) { put(x,0); newLine(); } + + + // Methods for writing the primitive types, plus type String, + // to the console, with a minimum field width of w, + // and followed by a carriage return. + // If output value is less than w characters, it is padded + // with extra spaces in front of the value. + + public static void putln(int x, int w) { put(x,w); newLine(); } // Note: also handles byte and short! + public static void putln(long x, int w) { put(x,w); newLine(); } + public static void putln(double x, int w) { put(x,w); newLine(); } // Also handles float. + public static void putln(char x, int w) { put(x,w); newLine(); } + public static void putln(boolean x, int w) { put(x,w); newLine(); } + public static void putln(String x, int w) { put(x,w); newLine(); } + + + // Method for outputting a carriage return + + public static void putln() { newLine(); } + + + // Methods for writing the primitive types, plus type String, + // to the console, with minimum field width w. + + public static void put(int x, int w) { dumpString(String.valueOf(x), w); } // Note: also handles byte and short! + public static void put(long x, int w) { dumpString(String.valueOf(x), w); } + public static void put(double x, int w) { dumpString(realToString(x), w); } // Also handles float. + public static void put(char x, int w) { dumpString(String.valueOf(x), w); } + public static void put(boolean x, int w) { dumpString(String.valueOf(x), w); } + public static void put(String x, int w) { dumpString(x, w); } + + + // Methods for reading in the primitive types, plus "words" and "lines". + // The "getln..." methods discard any extra input, up to and including + // the next carriage return. + // A "word" read by getlnWord() is any sequence of non-blank characters. + // A "line" read by getlnString() or getln() is everything up to next CR; + // the carriage return is not part of the returned value, but it is + // read and discarded. + // Note that all input methods except getAnyChar(), peek(), the ones for lines + // skip past any blanks and carriage returns to find a non-blank value. + // getln() can return an empty string; getChar() and getlnChar() can + // return a space or a linefeed ('\n') character. + // peek() allows you to look at the next character in input, without + // removing it from the input stream. (Note that using this + // routine might force the user to enter a line, in order to + // check what the next character is.) + // Acceptable boolean values are the "words": true, false, t, f, yes, + // no, y, n, 0, or 1; uppercase letters are OK. + // None of these can produce an error; if an error is found in input, + // the user is forced to re-enter. + // Available input routines are: + // + // getByte() getlnByte() getShort() getlnShort() + // getInt() getlnInt() getLong() getlnLong() + // getFloat() getlnFloat() getDouble() getlnDouble() + // getChar() getlnChar() peek() getAnyChar() + // getWord() getlnWord() getln() getString() getlnString() + // + // (getlnString is the same as getln and is only provided for consistency.) + + public static byte getlnByte() { byte x=getByte(); emptyBuffer(); return x; } + public static short getlnShort() { short x=getShort(); emptyBuffer(); return x; } + public static int getlnInt() { int x=getInt(); emptyBuffer(); return x; } + public static long getlnLong() { long x=getLong(); emptyBuffer(); return x; } + public static float getlnFloat() { float x=getFloat(); emptyBuffer(); return x; } + public static double getlnDouble() { double x=getDouble(); emptyBuffer(); return x; } + public static char getlnChar() { char x=getChar(); emptyBuffer(); return x; } + public static boolean getlnBoolean() { boolean x=getBoolean(); emptyBuffer(); return x; } + public static String getlnWord() { String x=getWord(); emptyBuffer(); return x; } + public static String getlnString() { return getln(); } // same as getln() + public static String getln() { + StringBuffer s = new StringBuffer(100); + char ch = readChar(); + while (ch != '\n') { + s.append(ch); + ch = readChar(); + } + return s.toString(); + } + + + public static byte getByte() { return (byte)readInteger(-128L,127L); } + public static short getShort() { return (short)readInteger(-32768L,32767L); } + public static int getInt() { return (int)readInteger((long)Integer.MIN_VALUE, (long)Integer.MAX_VALUE); } + public static long getLong() { return readInteger(Long.MIN_VALUE, Long.MAX_VALUE); } + + public static char getAnyChar(){ return readChar(); } + public static char peek() { return lookChar(); } + + public static char getChar() { // skip spaces & cr's, then return next char + char ch = lookChar(); + while (ch == ' ' || ch == '\n') { + readChar(); + if (ch == '\n') + dumpString("? ",0); + ch = lookChar(); + } + return readChar(); + } + + public static float getFloat() { + float x = 0.0F; + while (true) { + String str = readRealString(); + if (str.equals("")) { + errorMessage("Illegal floating point input.", + "Real number in the range " + Float.MIN_VALUE + " to " + Float.MAX_VALUE); + } + else { + Float f = null; + try { f = Float.valueOf(str); } + catch (NumberFormatException e) { + errorMessage("Illegal floating point input.", + "Real number in the range " + Float.MIN_VALUE + " to " + Float.MAX_VALUE); + continue; + } + if (f.isInfinite()) { + errorMessage("Floating point input outside of legal range.", + "Real number in the range " + Float.MIN_VALUE + " to " + Float.MAX_VALUE); + continue; + } + x = f.floatValue(); + break; + } + } + return x; + } + + public static double getDouble() { + double x = 0.0; + while (true) { + String str = readRealString(); + if (str.equals("")) { + errorMessage("Illegal floating point input", + "Real number in the range " + Double.MIN_VALUE + " to " + Double.MAX_VALUE); + } + else { + Double f = null; + try { f = Double.valueOf(str); } + catch (NumberFormatException e) { + errorMessage("Illegal floating point input", + "Real number in the range " + Double.MIN_VALUE + " to " + Double.MAX_VALUE); + continue; + } + if (f.isInfinite()) { + errorMessage("Floating point input outside of legal range.", + "Real number in the range " + Double.MIN_VALUE + " to " + Double.MAX_VALUE); + continue; + } + x = f.doubleValue(); + break; + } + } + return x; + } + + public static String getWord() { + char ch = lookChar(); + while (ch == ' ' || ch == '\n') { + readChar(); + if (ch == '\n') + dumpString("? ",0); + ch = lookChar(); + } + StringBuffer str = new StringBuffer(50); + while (ch != ' ' && ch != '\n') { + str.append(readChar()); + ch = lookChar(); + } + return str.toString(); + } + + public static boolean getBoolean() { + boolean ans = false; + while (true) { + String s = getWord(); + if ( s.equalsIgnoreCase("true") || s.equalsIgnoreCase("t") || + s.equalsIgnoreCase("yes") || s.equalsIgnoreCase("y") || + s.equals("1") ) { + ans = true; + break; + } + else if ( s.equalsIgnoreCase("false") || s.equalsIgnoreCase("f") || + s.equalsIgnoreCase("no") || s.equalsIgnoreCase("n") || + s.equals("0") ) { + ans = false; + break; + } + else + errorMessage("Illegal boolean input value.", + "one of: true, false, t, f, yes, no, y, n, 0, or 1"); + } + return ans; + } + + // ***************** Everything beyond this point is private ******************* + + // ********************** Utility routines for input/output ******************** + + private static InputStream in = System.in; // rename standard input stream + private static PrintStream out = System.out; // rename standard output stream + + private static String buffer = null; // one line read from input + private static int pos = 0; // position of next char in input line that has + // not yet been processed + + + private static String readRealString() { // read chars from input following syntax of real numbers + StringBuffer s=new StringBuffer(50); + char ch=lookChar(); + while (ch == ' ' || ch == '\n') { + readChar(); + if (ch == '\n') + dumpString("? ",0); + ch = lookChar(); + } + if (ch == '-' || ch == '+') { + s.append(readChar()); + ch = lookChar(); + while (ch == ' ') { + readChar(); + ch = lookChar(); + } + } + while (ch >= '0' && ch <= '9') { + s.append(readChar()); + ch = lookChar(); + } + if (ch == '.') { + s.append(readChar()); + ch = lookChar(); + while (ch >= '0' && ch <= '9') { + s.append(readChar()); + ch = lookChar(); + } + } + if (ch == 'E' || ch == 'e') { + s.append(readChar()); + ch = lookChar(); + if (ch == '-' || ch == '+') { + s.append(readChar()); + ch = lookChar(); + } + while (ch >= '0' && ch <= '9') { + s.append(readChar()); + ch = lookChar(); + } + } + return s.toString(); + } + + private static long readInteger(long min, long max) { // read long integer, limited to specified range + long x=0; + while (true) { + StringBuffer s=new StringBuffer(34); + char ch=lookChar(); + while (ch == ' ' || ch == '\n') { + readChar(); + if (ch == '\n') + dumpString("? ",0); + ch = lookChar(); + } + if (ch == '-' || ch == '+') { + s.append(readChar()); + ch = lookChar(); + while (ch == ' ') { + readChar(); + ch = lookChar(); + } + } + while (ch >= '0' && ch <= '9') { + s.append(readChar()); + ch = lookChar(); + } + if (s.equals("")){ + errorMessage("Illegal integer input.", + "Integer in the range " + min + " to " + max); + } + else { + String str = s.toString(); + try { + x = Long.parseLong(str); + } + catch (NumberFormatException e) { + errorMessage("Illegal integer input.", + "Integer in the range " + min + " to " + max); + continue; + } + if (x < min || x > max) { + errorMessage("Integer input outside of legal range.", + "Integer in the range " + min + " to " + max); + continue; + } + break; + } + } + return x; + } + + private static String realToString(double x) { + // Goal is to get a reasonable representation of x in at most + // 10 characters, or 11 characters if x is negative. + if (Double.isNaN(x)) + return "undefined"; + if (Double.isInfinite(x)) + if (x < 0) + return "-INF"; + else + return "INF"; + if (Math.abs(x) <= 5000000000.0 && Math.rint(x) == x) + return String.valueOf( (long)x ); + String s = String.valueOf(x); + if (s.length() <= 10) + return s; + boolean neg = false; + if (x < 0) { + neg = true; + x = -x; + s = String.valueOf(x); + } + if (x >= 0.00005 && x <= 50000000 && (s.indexOf('E') == -1 && s.indexOf('e') == -1)) { // trim x to 10 chars max + s = round(s,10); + s = trimZeros(s); + } + else if (x > 1) { // construct exponential form with positive exponent + long power = (long)Math.floor(Math.log(x)/Math.log(10)); + String exp = "E" + power; + int numlength = 10 - exp.length(); + x = x / Math.pow(10,power); + s = String.valueOf(x); + s = round(s,numlength); + s = trimZeros(s); + s += exp; + } + else { // constuct exponential form + long power = (long)Math.ceil(-Math.log(x)/Math.log(10)); + String exp = "E-" + power; + int numlength = 10 - exp.length(); + x = x * Math.pow(10,power); + s = String.valueOf(x); + s = round(s,numlength); + s = trimZeros(s); + s += exp; + } + if (neg) + return "-" + s; + else + return s; + } + + private static String trimZeros(String num) { // used by realToString + if (num.indexOf('.') >= 0 && num.charAt(num.length() - 1) == '0') { + int i = num.length() - 1; + while (num.charAt(i) == '0') + i--; + if (num.charAt(i) == '.') + num = num.substring(0,i); + else + num = num.substring(0,i+1); + } + return num; + } + + private static String round(String num, int length) { // used by realToString + if (num.indexOf('.') < 0) + return num; + if (num.length() <= length) + return num; + if (num.charAt(length) >= '5' && num.charAt(length) != '.') { + char[] temp = new char[length+1]; + int ct = length; + boolean rounding = true; + for (int i = length-1; i >= 0; i--) { + temp[ct] = num.charAt(i); + if (rounding && temp[ct] != '.') { + if (temp[ct] < '9') { + temp[ct]++; + rounding = false; + } + else + temp[ct] = '0'; + } + ct--; + } + if (rounding) { + temp[ct] = '1'; + ct--; + } + // ct is -1 or 0 + return new String(temp,ct+1,length-ct); + } + else + return num.substring(0,length); + + } + private static void dumpString(String str, int w) { // output string to console + for (int i=str.length(); i= 0x20 && (int)str.charAt(i) != 0x7F) // no control chars or delete + out.print(str.charAt(i)); + else if (str.charAt(i) == '\n' || str.charAt(i) == '\r') + newLine(); + } + + private static void errorMessage(String message, String expecting) { + // inform user of error and force user to re-enter. + newLine(); + dumpString(" *** Error in input: " + message + "\n", 0); + dumpString(" *** Expecting: " + expecting + "\n", 0); + dumpString(" *** Discarding Input: ", 0); + if (lookChar() == '\n') + dumpString("(end-of-line)\n\n",0); + else { + while (lookChar() != '\n') + out.print(readChar()); + dumpString("\n\n",0); + } + dumpString("Please re-enter: ", 0); + readChar(); // discard the end-of-line character + } + + private static char lookChar() { // return next character from input + if (buffer == null || pos > buffer.length()) + fillBuffer(); + if (pos == buffer.length()) + return '\n'; + return buffer.charAt(pos); + } + + private static char readChar() { // return and discard next character from input + char ch = lookChar(); + pos++; + return ch; + } + + private static void newLine() { // output a CR to console + out.println(); + out.flush(); + } + + private static boolean possibleLinefeedPending = false; + + private static void fillBuffer() { // Wait for user to type a line and press return, + // and put the typed line into the buffer. + StringBuffer b = new StringBuffer(); + out.flush(); + try { + int ch = in.read(); + if (ch == '\n' && possibleLinefeedPending) + ch = in.read(); + possibleLinefeedPending = false; + while (ch != -1 && ch != '\n' && ch != '\r') { + b.append((char)ch); + ch = in.read(); + } + possibleLinefeedPending = (ch == '\r'); + if (ch == -1) { + System.out.println("\n*** Found an end-of-file while trying to read from standard input!"); + System.out.println("*** Maybe your Java system doesn't implement standard input?"); + System.out.println("*** Program will be terminated.\n"); + throw new RuntimeException("End-of-file on standard input."); + } + } + catch (IOException e) { + System.out.println("Unexpected system error on input."); + System.out.println("Terminating program."); + System.exit(1); + } + buffer = b.toString(); + pos = 0; + } + + private static void emptyBuffer() { // discard the rest of the current line of input + buffer = null; + } + + +} // end of class TextIO diff --git a/test/Aout.java b/test/Aout.java new file mode 100644 index 0000000..0ca2f88 --- /dev/null +++ b/test/Aout.java @@ -0,0 +1,261 @@ + +/** Helper methods. + * Arrays converted to array expressions (toString(...)) + * and prettyprinting (toPrettyString(...)). + * @author Jaanus + */ +public class Aout { + + /** Just testing. */ + public static void main (String[] args) { + System.out.println (toString (new String[] + {null, "", "0", "a\nb", " ", " ", "\t", "möäüÕga", "\"", "\\", "\'"})); + } // main + + /** Conversion for int[][] . */ + public static String toString (int[][] m) { + if (m == null) return "(int[][])null"; + StringBuffer sb = new StringBuffer(); + sb.append ("new int[][]{"); + for (int i=0; i < m.length; i++) { + if (i > 0) sb.append (", "); + if (m[i] == null) + sb.append ("null"); + else { + sb.append ("{"); + for (int j=0; j < m[i].length; j++) { + if (j > 0) sb.append (", "); + sb.append (String.valueOf (m[i][j])); + } // for j + sb.append ("}"); + } + } // for i + sb.append ("}"); + return sb.toString(); + } // toString int[][] + + /** Conversion for double[][] . */ + public static String toString (double[][] m) { + if (m == null) return "(double[][])null"; + StringBuffer sb = new StringBuffer(); + sb.append ("new double[][]{"); + for (int i=0; i < m.length; i++) { + if (i > 0) sb.append (", "); + if (m[i] == null) + sb.append ("null"); + else { + sb.append ("{"); + for (int j=0; j < m[i].length; j++) { + if (j > 0) sb.append (", "); + sb.append (String.valueOf (m[i][j])); + } // for j + sb.append ("}"); + } + } // for i + sb.append ("}"); + return sb.toString(); + } // toString double[][] + + /** Conversion for int[] . */ + public static String toString (int[] m) { + if (m == null) return "(int[])null"; + StringBuffer sb = new StringBuffer(); + sb.append ("new int[]{"); + for (int i=0; i < m.length; i++) { + if (i > 0) sb.append (", "); + sb.append (String.valueOf (m[i])); + } // for i + sb.append ("}"); + return sb.toString(); + } // toString int[] + + /** Conversion for double[] . */ + public static String toString (double[] m) { + if (m == null) return "(double[])null"; + StringBuffer sb = new StringBuffer(); + sb.append ("new double[]{"); + for (int i=0; i < m.length; i++) { + if (i > 0) sb.append (", "); + sb.append (String.valueOf (m[i])); + } // for i + sb.append ("}"); + return sb.toString(); + } // toString double[] + + /** Conversion for int . */ + public static String toString (int n) { + return String.valueOf (n); + } // toString int + + /** Conversion for double . */ + public static String toString (double d) { + return String.valueOf (d); + } // toString double + + /** Conversion for String . */ + public static String toString (String s) { + if (s == null) + return "null"; + StringBuffer tmp = new StringBuffer(); + for (int k=0; k < s.length(); k++) { + char c = s.charAt (k); + switch (c) { + case '\n': { tmp.append ("\\n"); break; } + case '\t': { tmp.append ("\\t"); break; } + case '\b': { tmp.append ("\\b"); break; } + case '\f': { tmp.append ("\\f"); break; } + case '\r': { tmp.append ("\\r"); break; } + case '\\': { tmp.append ("\\\\"); break; } + case '\'': { tmp.append ("\\\'"); break; } + case '\"': { tmp.append ("\\\""); break; } + // TODO!!! add more escapes if needed + default: tmp.append (c); + } // switch + } // for k + return "\"" + tmp.toString() + "\""; + } // toString String + + /** Conversion for String[] . */ + public static String toString (String[] m) { + if (m == null) + return "(String[])null"; + StringBuffer sb = new StringBuffer(); + sb.append ("new String[]{"); + for (int i=0; i < m.length; i++) { + if (i > 0) + sb.append (", "); + sb.append (toString (m[i])); + } // for i + sb.append ("}"); + return sb.toString(); + } // toString String[] + + /** Double number as string with the given length. + * @param d argument + * @param len length + * @return d as string + */ + public static String fString (double d, int len) { + if (len<1) + return ""; + // pad on ruum punkti ja v6imaliku miinusm2rgi jaoks + int pad = 1 + ((d<0)?1:0); + // loga on t2isosa numbrikohtade arv + int loga = (int)Math.max (0., Math.log10 (Math.abs (d))) + 1; + // kk on punkti j2rel olevate kohtade arv + int kk = (int)Math.max (len-pad-loga, 0); + String fs = "%" + String.valueOf (len) + "." + + String.valueOf (kk) + "f"; + String res = ""; + try { + res = String.format ((java.util.Locale)null, fs, d); + } catch (IllegalArgumentException e) { + res = String.valueOf (d); + } // try + return res; + } // fString + + /** Prettyprint for double[][] . + * @param m array to print + * @param fs format string for element + * @return m array as multiline string + */ + public static String toPrettyString (double[][] m, String fs) { + String nl = System.getProperty ("line.separator"); + if (m == null) + return "nullpointer instead of this matrix" + nl; + // throw new NullPointerException ("(double[][])null"); // alternative + if (m.length == 0) + return "this matrix is empty" + nl; + StringBuffer sb = new StringBuffer(nl); + for (int i=0; i < m.length; i++) { + if (m[i] == null) + sb.append ("nullpointer instead of this row" + nl); + else { + if (m[i].length == 0) + sb.append ("this row is empty"); + else { + for (int j=0; j < m[i].length; j++) { + String elem = ""; + if (fs == null || fs.length() < 1) { + // TODO!!! keera siit, kui tahad pilti muuta + elem = fString (m[i][j], 6) + "\t"; + } else { + try { + elem = String.format ((java.util.Locale)null, + fs, m[i][j]) + " "; // remove space if needed + } catch (IllegalArgumentException e) { + elem = fString (m[i][j], 6) + "\t"; + } // try + } + sb.append (elem); + } // for j + } // nonempty row + sb.append (nl); + } // non-null row + } // for i + return sb.toString(); + } // toPrettyString double[][] + + /** Version of double[][] prettyprint without format string. */ + public static String toPrettyString (double[][] m) { + return toPrettyString (m, null); + } // toPrettyString double[][] + + /** Prettyprint for int[][] . + * @param m array to print + * @param fs format string for element + * @return m array as a multiline string + */ + public static String toPrettyString (int[][] m, String fs) { + String nl = System.getProperty ("line.separator"); + if (m == null) + return "nullpointer instead of this matrix" + nl; + // throw new NullPointerException ("(double[][])null"); // alternative + if (m.length == 0) + return "this matrix is empty" + nl; + StringBuffer sb = new StringBuffer(nl); + for (int i=0; i < m.length; i++) { + if (m[i] == null) + sb.append ("nullpointer instead of this row" + nl); + else { + if (m[i].length == 0) + sb.append ("this row is empty"); + else { + for (int j=0; j < m[i].length; j++) { + String elem = ""; + if (fs == null || fs.length() < 1) + fs = "%5d"; // TODO!!! keera siit, kui vaja + try { + elem = String.format ((java.util.Locale)null, + fs, m[i][j]) + " "; // remove space if needed + } catch (IllegalArgumentException e) { + elem = String.valueOf (m[i][j]) + "\t"; + } // try + sb.append (elem); + } // for j + } // nonempty row + sb.append (nl); + } // non-null row + } // for i + return sb.toString(); + } // toPrettyString int[][] + + /** Version of int[][] prettyprint without format string. */ + public static String toPrettyString (int[][] m) { + return toPrettyString (m, null); + } // toPrettyString int[][] + + /** Prettyprint for String[] . */ + public static String toPrettyString (String[] m) { + String nl = System.getProperty ("line.separator"); + if (m == null) return "(String[])null"; + StringBuffer sb = new StringBuffer(); + for (int i=0; i < m.length; i++) { + sb.append (toString (m[i]) + nl); + } // for i + return sb.toString(); + } // toPrettyString String[] + +} // Aout + diff --git a/test/LfractionTest.java b/test/LfractionTest.java new file mode 100644 index 0000000..3c5b245 --- /dev/null +++ b/test/LfractionTest.java @@ -0,0 +1,335 @@ + +import static org.junit.Assert.*; +import org.junit.Test; +import java.util.*; + +/** Testklass. + * @author Jaanus + */ +public class LfractionTest { + + @Test (timeout=1000) + public void testPlus() { + Lfraction f1 = new Lfraction (2, 5); + Lfraction f2 = new Lfraction (4, 15); + Lfraction sum = f1.plus(f2); + assertEquals ("Wrong sum: <" + f1 + "> + <" + f2 + ">", + new Lfraction (2, 3), sum); + Lfraction sm2 = sum.plus (sum); + assertEquals ("Wrong sum.plus(sum): <2/3> + <2/3>", + new Lfraction (4, 3), sm2); + assertEquals ("Do not change the arguments of the sum", + new Lfraction (2, 3), sum); + f1 = new Lfraction (-1, 250); + f2 = new Lfraction (-1, 375); + sum = f1.plus(f2); + assertEquals ("Wrong sum: <" + f1 + "> + <" + f2 + ">", + new Lfraction (-1, 150), sum); + f1 = new Lfraction (1, 221); + f2 = new Lfraction (1, 323); + sum = f1.plus (f2); + assertEquals ("Wrong sum: <" + f1 + "> + <" + f2 + ">", + new Lfraction (32, 4199), sum); + f1 = new Lfraction (1, 39203); + f2 = new Lfraction (1, 41989); + sum = f1.plus (f2); + assertEquals ("Wrong sum: <" + f1 + "> + <" + f2 + ">", + new Lfraction (408, 8271833), sum); + f1 = new Lfraction (-2, 5); + f2 = new Lfraction (2, 5); + sum = f1.plus(f2); + assertEquals ("Wrong sum: <" + f1 + "> + <" + f2 + ">", + new Lfraction (0, 1), sum); + } + + @Test (timeout=1000) + public void testTimes() { + Lfraction f1 = new Lfraction (2, 5); + Lfraction f2 = new Lfraction (4, 15); + Lfraction prd = f1.times (f2); + assertEquals ("Wrong product: <" + f1 + "> * <" + f2 + ">", + new Lfraction (8, 75), prd); + f1 = new Lfraction (-3, 5); + f2 = new Lfraction (-5, 7); + prd = f1.times (f2); + assertEquals ("Wrong product: <" + f1 + "> * <" + f2 + ">", + new Lfraction (3, 7), prd); + Lfraction pr2 = prd.times (prd); + assertEquals ("Wrong prd.times(prd): <3/7> * <3/7>", + new Lfraction (9, 49), pr2); + assertEquals ("Do not change the arguments of the product", + new Lfraction (3, 7), prd); + f1 = new Lfraction (0, 1); + f2 = new Lfraction (2, 3); + prd = f1.times (f2); + assertEquals ("Wrong product: <" + f1 + "> * <" + f2 + ">", + new Lfraction (0, 1), prd); + f1 = new Lfraction (3, 2); + f2 = new Lfraction (2, 3); + prd = f1.times (f2); + assertEquals ("Wrong product: <" + f1 + "> * <" + f2 + ">", + new Lfraction (1, 1), prd); + f1 = new Lfraction (3, 5); + f2 = new Lfraction (5, 7); + prd = f1.times (f2); + assertTrue ("Result must be reduced: 3/5 * 5/7 -> 3/7", + prd.getDenominator()==7); + } + + @Test (expected=RuntimeException.class) + public void testCreateZeroDenominator() { + Lfraction f = new Lfraction (1, 0); + } + + @Test (expected=RuntimeException.class) + public void testZeroInverse() { + Lfraction f = new Lfraction (0, 1); + f.inverse(); + } + + @Test (timeout=1000) + public void testClone() { + Lfraction f1 = new Lfraction (2, 5); + Lfraction f2 = null; + try { + f2 = (Lfraction)f1.clone(); + } catch (CloneNotSupportedException e) {}; + assertNotSame ("clone must differ from original", f2, f1); + assertEquals ("clone must be equal to original", f1, f2); + f1 = f2.plus(f1); + assertEquals ("clone must be independent from original", + new Lfraction (2, 5), f2); + } + + @Test (timeout=1000) + public void testToString() { + String s = new Lfraction (1, 4).toString(); + assertTrue (s + " must represent quarter", + (s.indexOf('1') < s.indexOf('4')) && (s.indexOf('1') >= 0)); + s = new Lfraction (-1, 5).toString(); + assertTrue (s + " does not contain minus", s.indexOf('-') >= 0); + } + + @Test (expected=RuntimeException.class) + public void testDivideByZero() { + Lfraction f1 = new Lfraction (1, 5); + Lfraction f2 = new Lfraction (0, 1); + Lfraction q = f1.divideBy (f2); + } + + @Test (timeout=1000) + public void testMinus() { + Lfraction f1 = new Lfraction (2, 5); + Lfraction f2 = new Lfraction (4, 15); + Lfraction dif = f1.minus (f2); + assertEquals ("Wrong difference: <" + f1 + "> - <" + f2 + ">", + new Lfraction (2, 15), dif); + Lfraction df2 = dif.minus (dif); + assertEquals ("Wrong difference: <2/15> - <2/15>", + new Lfraction (0, 1), df2); + assertEquals ("Do not change the arguments of the difference", + new Lfraction (2, 15), dif); + f1 = new Lfraction (-2, 5); + f2 = new Lfraction (-4, 15); + dif = f1.minus (f2); + assertEquals ("Wrong difference: <" + f1 + "> - <" + f2 + ">", + new Lfraction (-2, 15), dif); + } + + @Test (timeout=1000) + public void testDivideBy() { + Lfraction f1 = new Lfraction (-2, 7); + Lfraction f2 = new Lfraction (-1, 14); + Lfraction f = f1.divideBy (f2); + assertEquals ("Wrong quotient: <" + f1 + "> / <" + f2 + ">", + new Lfraction (4, 1), f); + f = f2.divideBy (f1); + assertEquals ("Wrong quotient: <" + f1 + "> / <" + f2 + ">", + new Lfraction (1, 4), f); + Lfraction f3 = f.divideBy (f); + assertEquals (f.toString() + " divided by itself", + new Lfraction (1, 1), f3); + assertEquals ("Do not change the arguments of the quotient", + new Lfraction (1, 4), f); + } + + @Test (timeout=1000) + public void testOpposite() { + Lfraction f1 = new Lfraction (1, 6); + Lfraction f2 = f1.opposite(); + assertEquals ("Wrong opposite", new Lfraction (-1, 6), f2); + assertEquals ("Do not change the argument of opposite", + new Lfraction (1, 6), f1); + f1 = new Lfraction (-4, 75); + f2 = f1.opposite(); + assertEquals ("Wrong opposite", new Lfraction (4, 75), f2); + f1 = new Lfraction (0, 1); + f2 = f1.opposite(); + assertEquals ("zero must be neutral to opposite", f1, f2); + } + + @Test (timeout=1000) + public void testInverse() { + Lfraction f1 = new Lfraction (2, 3); + Lfraction f2 = f1.inverse(); + assertEquals ("Wrong inverse ", new Lfraction (3, 2), f2); + assertEquals ("Do not change the argument of inverse", + new Lfraction (2, 3), f1); + f1 = new Lfraction (-4, 75); + f2 = f1.inverse(); + assertEquals ("Wrong inverse", new Lfraction (-75, 4), f2); + assertTrue ("Denominator must always be positive", + f2.getDenominator() > 0); + f1 = new Lfraction (1, 1); + f2 = f1.inverse(); + assertEquals ("1 must be neutral to inverse", f1, f2); + } + + @Test (timeout=1000) + public void testGetters() { + Lfraction f1 = new Lfraction (2, 3); + long num = f1.getNumerator(); + assertEquals ("wrong numerator ", 2, num); + f1 = new Lfraction (-4, 75); + num = f1.getNumerator(); + assertEquals ("Wrong numerator", -4, num); + f1 = new Lfraction (0, 7); + num = f1.getNumerator(); + assertEquals ("Wrong numerator", 0, num); + f1 = new Lfraction (2, 3); + long den = f1.getDenominator(); + assertEquals ("wrong denominator ", 3, den); + f1 = new Lfraction (-4, 75); + den = f1.getDenominator(); + assertEquals ("Wrong denominator", 75, den); + } + + @Test (timeout=1000) + public void testIntegerPart() { + Lfraction f1 = new Lfraction (2, 3); + long i = f1.integerPart(); + assertEquals ("wrong integer part ", 0, i); + f1 = new Lfraction (3, 2); + i = f1.integerPart(); + assertEquals ("wrong integer part ", 1, i); + f1 = new Lfraction (32, 3); + i = f1.integerPart(); + assertEquals ("wrong integer part ", 10, i); + f1 = new Lfraction (33, 3); + i = f1.integerPart(); + assertEquals ("wrong integer part ", 11, i); + f1 = new Lfraction (-33, 3); + i = f1.integerPart(); + assertEquals ("wrong integer part ", -11, i); + } + + @Test (timeout=1000) + public void testLfractionPart() { + Lfraction f1 = new Lfraction (2, 3); + Lfraction i = f1.fractionPart(); + assertEquals ("wrong fraction part ", new Lfraction (2, 3), i); + f1 = new Lfraction (3, 2); + i = f1.fractionPart(); + assertEquals ("wrong fraction part ", new Lfraction (1, 2), i); + f1 = new Lfraction (32, 3); + i = f1.fractionPart(); + assertEquals ("wrong fraction part ", new Lfraction (2, 3), i); + f1 = new Lfraction (33, 3); + i = f1.fractionPart(); + assertEquals ("wrong fraction part ", new Lfraction (0, 1), i); + f1 = new Lfraction (-33, 3); + i = f1.fractionPart(); + assertEquals ("wrong fraction part ", new Lfraction (0, 1), i); + f1 = new Lfraction (-5, 4); + i = f1.fractionPart(); + assertTrue ("wrong fraction part " + i.toString() + + " for " + f1.toString(), + i.equals (new Lfraction (-1, 4)) || i.equals (new Lfraction (3, 4))); + } + + @Test (timeout=1000) + public void testEquals() { + Lfraction f1 = new Lfraction (2, 5); + Lfraction f2 = new Lfraction (4, 10); + assertTrue ("2/5 must be equal to 4/10", f1.equals (f2)); + assertFalse ("2/5 is not 3/5", f1.equals (new Lfraction (3, 5))); + f1 = new Lfraction (12345678901234567L, 1L); + f2 = new Lfraction (12345678901234568L, 1L); + assertFalse ("12345678901234567/1 is not 12345678901234568/1", + f1.equals (f2)); + } + + @Test (timeout=1000) + public void testCompareTo() { + Lfraction f1 = new Lfraction (2, 5); + Lfraction f2 = new Lfraction (4, 7); + assertTrue ("2/5 must be less than 4/7", f1.compareTo (f2) < 0); + assertTrue ("2/5 must be equal to 4/10", + f1.compareTo (new Lfraction (4, 10)) == 0); + assertTrue ("4/7 must be greater than 2/5", f2.compareTo (f1) > 0); + f1 = new Lfraction (-2, 5); + f2 = new Lfraction (-4, 7); + assertTrue ("-2/5 must be greater than -4/7", f1.compareTo (f2) > 0); + assertTrue ("-2/5 must be equal to -4/10", + f1.compareTo (new Lfraction (-4, 10)) == 0); + assertTrue ("-4/7 must be less than -2/5", f2.compareTo (f1) < 0); + f1 = new Lfraction (12345678901234567L, 1L); + f2 = new Lfraction (12345678901234568L, 1L); + assertFalse ("12345678901234567/1 is not 12345678901234568/1", + f1.compareTo (f2) == 0); + } + + @Test (timeout=1000) + public void testToLfraction() { + Lfraction f1 = Lfraction.toLfraction (Math.PI, 7); + Lfraction f2 = new Lfraction (22, 7); + assertTrue ("Math.PI must be nearly 22/7", f1.equals (f2)); + f1 = Lfraction.toLfraction (-10., 2); + f2 = new Lfraction (-20, 2); + assertTrue ("-10. must be -20/2", f1.equals (f2)); + } + + @Test (timeout=1000) + public void testToDouble() { + Lfraction f = new Lfraction (2, 5); + assertEquals ("2/5 must be nearly 0.4", 0.4, f.toDouble(), 0.00001); + f = new Lfraction (-17, 100); + assertEquals ("-17/100 must be nearly -0.17", -0.17, + f.toDouble(), 0.00001); + } + + @Test (timeout=1000) + public void testValueOf() { + Lfraction f = new Lfraction (2, 5); + assertEquals ("valueOf must read back what toString outputs. ", + f, Lfraction.valueOf (f.toString())); + f = new Lfraction (-17, 100); + assertEquals ("valueOf must read back what toString outputs. ", + f, Lfraction.valueOf (f.toString())); + } + + @Test (timeout=1000) + public void testHashCode() { + Lfraction q1 = new Lfraction (1L, 2L); + int h1 = q1.hashCode(); + Lfraction q2 = new Lfraction (1L, 2L); + int h2 = q2.hashCode(); + Lfraction q3 = null; + try { + q3 = (Lfraction)q1.clone(); + } catch (CloneNotSupportedException e) {}; + int h3 = q3.hashCode(); + assertTrue ("hashCode has to be same for equal objects", h1 == h2); + assertTrue ("hashCode has to be same for clone objects", h1 == h3); + assertTrue ("hashCode has to be same for the same object", + h1 == q1.hashCode()); + q2 = new Lfraction (0L, 2L); + h2 = q2.hashCode(); + q3 = new Lfraction (1L, 3L); + h3 = q3.hashCode(); + assertFalse ("hashCode does not depend on numerator", h1 == h2); + assertFalse ("hashCode does not depend on denominator", h1 == h3); + } + +} + diff --git a/test/hamcrest-core-1.3.jar b/test/hamcrest-core-1.3.jar new file mode 100644 index 0000000..9d5fe16 Binary files /dev/null and b/test/hamcrest-core-1.3.jar differ diff --git a/test/junit-4.12.jar b/test/junit-4.12.jar new file mode 100644 index 0000000..3a7fc26 Binary files /dev/null and b/test/junit-4.12.jar differ