All pastes #28654 Raw Edit

Untitled

public text v1 · immutable
#28654 ·published 2005-11-14 12:57 UTC
rendered paste body
import java.io.*;
import java.util.*;
import java.math.*;
abstract class Abstraction {  
  Implementor implementor; 
  public Implementor getImplementor() {
    return this.implementor;
  }
  public abstract void operation();
}
class RefinedAbstraction1 extends Abstraction{  
  public RefinedAbstraction1(Implementor implementor) {
    this.implementor = implementor;
  }
  public void operation(){
    Implementor implementor = this.getImplementor();
    implementor.operationImp();
  }
}
class RefinedAbstraction2 extends Abstraction{  
  public RefinedAbstraction2(Implementor implementor) {
    this.implementor = implementor;
  }
  public void operation(){
    Implementor implementor = this.getImplementor();
    implementor.operationImp();
  }
}
abstract class Implementor{  
  public abstract void operationImp();
}
class ConcreteImplementor1 extends Implementor{ 
	int o, n; 
  ConcreteImplementor1(int o, int n) {this.o=o; this.n=n;}
  public void operationImp(){
	double kv = Math.sqrt(n);
	double kub =Math.pow( n , 1.0/3.0 );
	double atv =Math.pow(n,-1);
	System.out.println("");
	if (o == 0) 
        	System.out.println("Skaiciaus " +n+ " kvadratine saknis(double) yra -> " +kv);
	else {
		if (o == 1)
			System.out.println("Skaiciaus " +n+ " kubine saknis(double) yra -> " +kub);
		else {
        		if (o == 2) 
				System.out.println("Skaiciui " +n+ " atvirkscias(double) yra -> " +atv);
			else 
				System.out.println("Klaida.Blogai pasirinkote!");
		}
	}	
  }
}
class ConcreteImplementor2 extends Implementor{  
 int o, n;
  ConcreteImplementor2(int o, int n) {this.o=o; this.n=n;}
  public void operationImp(){
	
   	double kv = Math.sqrt(n);
	double kub =Math.pow( n , 1.0/3.0 );
	double atv =Math.pow(n,-1);
	BigDecimal rez = new BigDecimal(kv);
	BigDecimal rez2= new BigDecimal(kub);
	BigDecimal rez3= new BigDecimal(atv);
	System.out.println("");
	if (o == 0) 
        	System.out.println("Skaiciaus " +n+ " kvadratine saknis(BigDecimal) yra -> " +rez);
	else {
		if (o == 1)
			System.out.println("Skaiciaus " +n+ " kubine saknis(BigDecimal) yra -> " +rez2);
		else {
        		if (o == 2) 
				System.out.println("Skaiciui " +n+ " atvirkscias(BigDecimal) yra -> " +rez3);
			else 
				System.out.println("Klaida.Blogai pasirinkote!");
		}
	}
  }
}
class TestBridge {            
  public static void testConcreteImplementor1(int op, int nr){
    RefinedAbstraction1 refinedAbstraction = 
      new RefinedAbstraction1(new ConcreteImplementor1(op, nr));
    refinedAbstraction.operation();
  }
  public static void testConcreteImplementor2(int op, int nr){
    RefinedAbstraction2 refinedAbstraction = 
      new RefinedAbstraction2(new ConcreteImplementor2(op, nr));
    refinedAbstraction.operation();       
  }   
  public static void main(String[] arg){
	int  t, op, nr;
		t  = new Integer(arg[0]).intValue();
		op = new Integer(arg[1]).intValue();
		nr = new Integer(arg[2]).intValue();
		if (t == 0)  
        	 testConcreteImplementor1(op, nr);
		else {
		 if (t == 1) 
        	  testConcreteImplementor2(op, nr);
		 else
	          System.out.println("Klaida");
	}
  }
}