Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 21st, 2012  |  syntax: None  |  size: 2.78 KB  |  hits: 10  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Numbers Won't Increment
  2. public class TestBigNaturalSimple {
  3.  
  4. public static void main(String[] args) {
  5.     BigNatural b1 = new BigNatural(); // default constructor
  6.     BigNatural b2 = new BigNatural(23); // one-argument int constructor
  7.     BigNatural b3 = new BigNatural("346"); // one-argument String constructor
  8.     BigNatural b4 = new BigNatural(b2); // one-argument BigNatural
  9.                                         // constructor
  10.  
  11.     b1.increment();
  12.     b3.decrement();
  13.  
  14.     System.out.println(b1.toString()); // should print out 1
  15.     System.out.println(b4.toString()); // should print out 23
  16. }
  17. }
  18.        
  19. public class BigNatural {
  20.  
  21. private String num;
  22.  
  23. public BigNatural(String input) {
  24.     num = input;
  25. }
  26.  
  27.  
  28. public BigNatural(BigNatural input) {
  29.     num = input.toString();
  30. }
  31.  
  32. public BigNatural(Integer input) {
  33.     num = input.toString();
  34. }
  35.  
  36. public BigNatural() {
  37.     Integer i = 0;
  38.     num = i.toString();
  39. }
  40.  
  41. public void increment() {
  42.     Integer first = 0;
  43.     Character ch = num.charAt(num.length()-1);
  44.     Integer last = Character.digit(ch, 10);
  45.  
  46.     if (num.length() > 1)
  47.     {
  48.         if (last < 9) {
  49.             last++;
  50.         }
  51.         else
  52.             {
  53.             if (num.length() >= 2)
  54.             {
  55.                 last = 0;
  56.                 String temp = new String(num.substring(0, num.length()-2));
  57.                 increment();
  58.             }
  59.             else
  60.             {
  61.                 last++;
  62.             }
  63.         }
  64.     }
  65.     else
  66.     {
  67.         if (last < 9)
  68.         {
  69.             last++;
  70.         }
  71.         else
  72.         {
  73.             last = 0;
  74.             first = 1;
  75.         }
  76.     }
  77.  
  78.     String t = last.toString();
  79.  
  80.  
  81.     if (first > 0)
  82.     {
  83.     String x = first.toString();
  84.     num.concat(x);
  85.     }
  86.  
  87.     num.concat(t);
  88. }
  89.  
  90. public void decrement() {
  91.     Character ch = num.charAt(num.length()-1);
  92.     Integer last = Character.digit(ch, 10);
  93.  
  94.     if(num.length() > 1)
  95.     {
  96.         if(last == 0)
  97.         {
  98.             String temp = new String(num.substring(0, num.length()-2));
  99.             decrement();
  100.         }
  101.         else
  102.         {
  103.         last--;
  104.         }
  105.     }
  106.     else
  107.     {
  108.         if(last > 0)
  109.         {
  110.             last--;
  111.         }
  112.         else
  113.         {
  114.             last = 0;
  115.         }
  116.     }
  117.  
  118.     String t = last.toString();
  119.     num.concat(t);
  120. }
  121.  
  122.  
  123. public String toString() {
  124.     return num;
  125. }
  126. }
  127.        
  128. public void increment() {
  129.     num = increment(num);
  130. }
  131. private static String increment(String s) {
  132.     if (s.length() <= 0) return "1";
  133.     char ch = s.charAt(s.length() - 1);
  134.     String top = s.substring(0, s.length() - 1);
  135.     return ch < '9' ? top + ++ch : increment(top) + '0';
  136. }
  137.        
  138. num.concat(t);
  139.        
  140. public void increment() {
  141.   add(1);
  142. }
  143.  
  144. public void decrement() {
  145.   add(-1);
  146. }
  147.  
  148. private void add(int i) {
  149.   // Your homework here ...
  150.   // You will have only one function to debug and correct, not 2
  151. }