Advertisement
makispaiktis

ComplexNumbers Polar.java

Feb 24th, 2019 (edited)
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.73 KB | None | 0 0
  1. public class Polar {
  2.    
  3.     double r;
  4.     double arg;
  5.    
  6.     // Constructor
  7.     Polar(){
  8.         r = 0;
  9.         arg = 0;
  10.     }
  11.    
  12.     Polar(double r, double arg){
  13.         if(r ==0){
  14.             System.out.println("Polar coordinates cannot be defined when x = 0 and y =0.");
  15.             return;
  16.         }
  17.        
  18.         else{
  19.             this.r = r;
  20.             this.arg = arg;
  21.         }
  22.     }
  23.    
  24.     // Convert
  25.     public Rectangular toRectangular(){
  26.         return new Rectangular(r * Math.cos(arg), r * Math.sin(arg));
  27.     }
  28.    
  29.     // Methods
  30.     public static Polar multiplyPol(Polar pol1, Polar pol2){
  31.         return  new Polar(pol1.r * pol2.r, pol1.arg + pol2.arg);
  32.     }
  33.    
  34.     public static Polar multiplyPolRect(Polar pol1, Rectangular rect2){
  35.         Polar pol2 = rect2.toPolar();
  36.         return  new Polar(pol1.r * pol2.r, pol1.arg + pol2.arg);
  37.     }
  38.  
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement