Rational,

[ Pobierz całość w formacie PDF ]
public class Rational{private int licznik;private int mianownik;public Rational(int l, int m){licznik =l;mianownik=m;}public Rational add(Rational val){int l =this.licznik*val.mianownik+val.licznik*this.mianownik;int m =this.mianownik*val.mianownik;return new Rational(l,m);}public Rational mul(Rational val){int l =this.licznik*val.licznik;int m =this.mianownik*val.mianownik;return new Rational(l,m);}public Rational sub(Rational val){int l=this.licznik*val.mianownik-val.licznik*this.mianownik;int m =this.mianownik*val.mianownik;return new Rational(l,m);}public Rational div(Rational val){int l =this.licznik*val.mianownik;int m=this.mianownik*val.licznik;return new Rational(l,m);}public boolean equals(Rational r){return (compare(this,r)==0);}public int compareTo(Rational r){return compare(this,r);}public String toString(){return licznik + "/" +mianownik;}private static int compare(Rational l, Rational r) {int subtr = l.licznik * r.mianownik - l.mianownik * r.licznik;int sign = (l.mianownik * r.mianownik > 0 ? 1 : -1);if (subtr == 0) {return 0;}if (subtr * sign < 0) {return -1;}return 1;}public static void main(String[] args){Rational r0=new Rational(1,2),r1=new Rational(2,4),r2=new Rational(1,4),r3=new Rational(-1,3),r4,r5,r6,r7;System.out.println("Wprowadzaone Liczby:");System.out.println("r0 = " + r0);System.out.println("r1 = " + r1);System.out.println("r2 = " + r2);System.out.println("r3 = " + r3);System.out.println("Wykonywanie operacji:");r4 = r1.add(r2);r5 = r3.sub(r2);r6 = r2.mul(r3);r7 = r2.div(r3);System.out.println("r1 + r2 = " + r4);System.out.println("r3 - r2 = " + r5);System.out.println("r2 * r3 = " + r6);System.out.println("r2 / r3 = " + r7);System.out.println("Por?wnanie");System.out.println("r2 < r1 : "+ (r2.compareTo(r1) == -1 ? "TAK" : "NIE"));System.out.println("r1 = r2 : " + r2.equals(r1));}} [ Pobierz całość w formacie PDF ]

  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • tejsza.htw.pl
  •