public class Hello{
private double realPart;
private double imaginPart;
public Hello(double r,double i){
this.realPart = r;
this.imaginPart = i;
}
public Hello(){
realPart = 0;
imaginPart = 0;
}
public static Hello complexAdd(Hello a,Hello b){
Hello c = new Hello();
c.realPart = a.realPart+b.realPart;
c.imaginPart = a.imaginPart+b.imaginPart;
return c;
}
public static Hello complexminus(Hello a,Hello b){
Hello c = new Hello();
c.realPart = a.realPart-b.realPart;
c.imaginPart = a.imaginPart-b.imaginPart;
return c;
}
public static Hello multiply(Hello a,Hello b){
Hello c = new Hello();
c.realPart = a.realPart*b.realPart - a.imaginPart*b.imaginPart;
c.imaginPart = a.imaginPart*b.realPart + a.realPart* b.imaginPart;
return c;
}
public static Hello divide(Hello a,Hello b){
double d = Math.sqrt(b.realPart*b.realPart) + Math.sqrt(b.imaginPart*b.imaginPart);
Hello c = new Hello();
c.realPart = (a.realPart*b.realPart + a.imaginPart*b.imaginPart) / d;
c.imaginPart = (-a.imaginPart*b.realPart + a.realPart* b.imaginPart) / d;
return c;
}
public static void toString(Hello a){
System.out.println(a.realPart+"+"+a.imaginPart+"i");
}
public static double scale(Hello a){
double d = Math.sqrt(a.realPart*a.realPart + a.imaginPart*a.imaginPart);
return d;
}
public static void main(String[] args){
Hello a,b;
Hello c1 = new Hello(3.5,5.5);
Hello c2 = new Hello(-3.5,1);
Hello.toString(complexAdd(c1,c2));
Hello.toString(complexminus(c1,c2));
Hello.toString(multiply(c1,c2));
Hello.toString(divide(c1,c2));
System.out.println(scale(c1));
}
}
import java.math.BigInteger;
public class Rational extends Number implements Comparable {
private BigInteger numerator;
private BigInteger denominator;
public static void main(String[] args) {
Rational rational1 = new Rational(new BigInteger(14 + ""),
new BigInteger(37 + ""));
Rational rational2 = new Rational(new BigInteger(23 + ""),
new BigInteger(67 + ""));
System.out.println(rational1 + "+" + rational2 + "=" +rational1.add(rational2));
System.out.println(rational1 + "-" + rational2 + "=" +rational1.subtract(rational2));
System.out.println(rational1 + "*" + rational2 + "=" +rational1.multiple(rational2));
System.out.println(rational1 + "/" + rational2 + "=" +rational1.divide(rational2));
System.out.println(rational2 + "is" +rational2.doubleValue());
Rational rational = new Rational(new BigInteger(0 + ""),
new BigInteger(1 + ""));
for (int i = 2; i <=100; i++) {
rational = rational.add(new Rational(new BigInteger(i-1 + ""),
new BigInteger(i + "")));
}
System.out.println("final is" +rational);
System.out.println("value is" +rational.doubleValue());
}
public Rational() {
this(BigInteger.ZERO, BigInteger.ONE);
}
public Rational(BigInteger numerator, BigInteger denominator) {
BigInteger gcd = gcd(numerator, denominator);
this.numerator = ((denominator.compareTo(BigInteger.ZERO)) > 0 ? BigInteger.ONE
: new BigInteger(-1 + "")).multiply(numerator).divide(gcd);
this.denominator = denominator.abs().divide(gcd);
}
public static BigInteger gcd(BigInteger a, BigInteger b) {
BigInteger n1 = a.abs();
BigInteger n2 = b.abs();
BigInteger remainder = n1.remainder(n2);
while (remainder.compareTo(BigInteger.ZERO) > 0) {
n1 = n2;
n2 = remainder;
remainder = n1.remainder(n2);
}
return n2;
}
public BigInteger getNumerator() {
return numerator;
}
public BigInteger getDenominator() {
return denominator;
}
public Rational add(Rational secondRational) {
BigInteger n = numerator.multiply(secondRational.getDenominator()).add(
denominator.multiply(secondRational.getNumerator()));
BigInteger d = denominator.multiply(secondRational.getDenominator());
return new Rational(n, d);
}
public Rational subtract(Rational secondRational) {
BigInteger n = numerator.multiply(secondRational.getDenominator())
.subtract(denominator.multiply(secondRational.getNumerator()));
BigInteger d = denominator.multiply(secondRational.getDenominator());
return new Rational(n, d);
}
public Rational multiple(Rational secondRational) {
BigInteger n = numerator.multiply(secondRational.getNumerator());
BigInteger d = denominator.multiply(secondRational.getDenominator());
return new Rational(n, d);
}
public Rational divide(Rational secondRational) {
BigInteger n = numerator.multiply(secondRational.getDenominator());
BigInteger d = denominator.multiply(secondRational.getNumerator());
return new Rational(n, d);
}
@Override
public boolean equals(Object obj) {
if (this.getNumerator().compareTo(((Rational) obj).getNumerator()) == 0) {
return true;
}
else {
return false;
}
}
@Override
public String toString() {
if (denominator.compareTo(BigInteger.ONE) == 0) {
return numerator.toString();
}
else {
return numerator.toString() + "/" + denominator.toString();
}
}
@Override
public int intValue() {
return numerator.divide(denominator).intValue();
}
@Override
public long longValue() {
return numerator.divide(denominator).longValue();
}
@Override
public float floatValue() {
return numerator.divide(denominator).floatValue();
}
@Override
public double doubleValue() {
return numerator.divide(denominator).doubleValue();
}
@Override
public int compareTo(Object o) {
if (this.getNumerator().compareTo(((Rational) o).getNumerator()) > 0) {
return 1;
}
else if (this.getNumerator().compareTo(((Rational) o).getNumerator()) < 0) {
return -1;
}
else {
return 0;
}
}
}