Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. //Roman.java    MrG 2013.0125
  2. public class Roman extends Object implements Comparable
  3. {  
  4.     private int num;
  5.  
  6.     public Roman(int num)
  7.     {
  8.         this.num=num;
  9.     }
  10.    
  11.     public String toString()
  12.     {
  13.         return "" + intToRom(num);
  14.     }
  15.  
  16.     private String intToRom(int num)
  17.     {
  18.         int ones = num % 10;
  19.         int tens = num/10;
  20.  
  21.         String temp = "";
  22.  
  23.         if(ones==1) temp += "i";
  24.         if(ones==2) temp += "ii";
  25.         if(ones==3) temp += "iii";
  26.         if(ones==4) temp += "iv";
  27.         if(ones==5) temp += "v";
  28.         if(ones==6) temp += "vi";
  29.         if(ones==7) temp += "vii";
  30.         if(ones==8) temp += "viii";
  31.         if(ones==9) temp += "ix";
  32.  
  33.         if(tens==1) temp = "x " + temp;
  34.         if(tens==2) temp = "xx " + temp;
  35.         if(tens==3) temp = "xxx " + temp;
  36.         if(tens==4) temp = "xl " + temp;
  37.         if(tens==5) temp = "l " + temp;
  38.         if(tens==6) temp = "lx " + temp;
  39.         if(tens==7) temp = "lxx " + temp;
  40.         if(tens==8) temp = "lxxx " + temp;
  41.         if(tens==9) temp = "xc " + temp;
  42.  
  43.         return temp;
  44.     }
  45.  
  46.     public int getNum()
  47.     {
  48.         return num;
  49.     }
  50.  
  51.     public int compareTo(Object other)
  52.     {
  53.         Roman temp = (Roman)other;
  54.         return this.getNum()-temp.getNum();
  55.     }
  56.  
  57.     public boolean equals(Object other)
  58.     {
  59.         Roman temp = (Roman)other;
  60.         return this.compareTo(temp)==0;
  61.     }
  62.  
  63.     public Roman add(Roman other)
  64.     {
  65.         return new Roman(this.num+other.getNum());
  66.     }
  67.  
  68.     public Roman sub(Roman other)
  69.     {
  70.         return new Roman(this.num-other.getNum());
  71.     }
  72. }