Advertisement
calcpage

APCS2012 CH7_NewRoman.java

Feb 1st, 2013
300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 2.86 KB | None | 0 0
  1. //NewRoman.java MrG 2013.0125
  2. public class NewRoman extends Object implements Comparable
  3. {  
  4.     private int num;
  5.     private int[] digits;
  6.  
  7.     public NewRoman(int num)
  8.     {
  9.         this.num=num;
  10.  
  11.         int temp=num;
  12.         digits = new int[5];
  13.         for(int i=0; i<(int)Math.log(num)/Math.log(10)+1; i++)
  14.         {
  15.             digits[i]=temp%10;
  16.             temp=temp/10;
  17.         }
  18.     }
  19.    
  20.     public String toString()
  21.     {
  22.         return "" + intToRom(num);
  23.     }
  24.  
  25.     private String intToRom(int num)
  26.     {
  27.         String temp = "";
  28.  
  29.         if(digits[0]==1){temp="i"+temp;}
  30.         if(digits[0]==2){temp="ii"+temp;}
  31.         if(digits[0]==3){temp="iii"+temp;}
  32.         if(digits[0]==4){temp="iv"+temp;}
  33.         if(digits[0]==5){temp="v"+temp;}
  34.         if(digits[0]==6){temp="vi"+temp;}
  35.         if(digits[0]==7){temp="vii"+temp;}
  36.         if(digits[0]==8){temp="viii"+temp;}
  37.         if(digits[0]==9){temp="ix"+temp;}
  38.  
  39.         if(digits[1]==1){temp="x "+temp;}
  40.         if(digits[1]==2){temp="xx "+temp;}
  41.         if(digits[1]==3){temp="xxx "+temp;}
  42.         if(digits[1]==4){temp="xl "+temp;}
  43.         if(digits[1]==5){temp="l "+temp;}
  44.         if(digits[1]==6){temp="lx "+temp;}
  45.         if(digits[1]==7){temp="lxx "+temp;}
  46.         if(digits[1]==8){temp="lxxx "+temp;}
  47.         if(digits[1]==9){temp="xc "+temp;}
  48.  
  49.         if(digits[2]==1){temp="c "+temp;}
  50.         if(digits[2]==2){temp="cc "+temp;}
  51.         if(digits[2]==3){temp="ccc "+temp;}
  52.         if(digits[2]==4){temp="cd "+temp;}
  53.         if(digits[2]==5){temp="d "+temp;}
  54.         if(digits[2]==6){temp="dc "+temp;}
  55.         if(digits[2]==7){temp="dcc "+temp;}
  56.         if(digits[2]==8){temp="dccc "+temp;}
  57.         if(digits[2]==9){temp="cm "+temp;}
  58.  
  59.         if(digits[3]==1){temp="m "+temp;}
  60.         if(digits[3]==2){temp="mm "+temp;}
  61.         if(digits[3]==3){temp="mmm "+temp;}
  62.         if(digits[3]==4){temp="mV "+temp;}
  63.         if(digits[3]==5){temp="V "+temp;}
  64.         if(digits[3]==6){temp="Vm "+temp;}
  65.         if(digits[3]==7){temp="Vmm "+temp;}
  66.         if(digits[3]==8){temp="Vmmm "+temp;}
  67.         if(digits[3]==9){temp="mX "+temp;}
  68.  
  69.         if(digits[4]==1){temp="X "+temp;}
  70.         if(digits[4]==2){temp="XX "+temp;}
  71.         if(digits[4]==3){temp="XXX "+temp;}
  72.         if(digits[4]==4){temp="XL "+temp;}
  73.         if(digits[4]==5){temp="L "+temp;}
  74.         if(digits[4]==6){temp="LX "+temp;}
  75.         if(digits[4]==7){temp="LXX "+temp;}
  76.         if(digits[4]==8){temp="LXXX "+temp;}
  77.         if(digits[4]==9){temp="XM "+temp;}
  78.  
  79.         return temp;
  80.     }
  81.  
  82.     public int getNum()
  83.     {
  84.         return num;
  85.     }
  86.  
  87.     public int compareTo(Object other)
  88.     {
  89.         NewRoman temp = (NewRoman)other;
  90.         return this.getNum()-temp.getNum();
  91.     }
  92.  
  93.     public boolean equals(Object other)
  94.     {
  95.         NewRoman temp = (NewRoman)other;
  96.         return this.compareTo(temp)==0;
  97.     }
  98.  
  99.     public NewRoman add(NewRoman other)
  100.     {
  101.         return new NewRoman(this.num+other.getNum());
  102.     }
  103.  
  104.     public NewRoman sub(NewRoman other)
  105.     {
  106.         return new NewRoman(this.num-other.getNum());
  107.     }
  108.  
  109.     public NewRoman mul(NewRoman other)
  110.     {
  111.         return new NewRoman(this.num*other.getNum());
  112.     }
  113.  
  114.     public NewRoman exp(NewRoman other)
  115.     {
  116.         return new NewRoman((int)Math.pow(this.num,other.getNum()));
  117.     }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement