BigInteger Problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gemacjr1201
    New Member
    • Nov 2006
    • 14

    #1

    BigInteger Problem

    I am creating a GUI for class and need help with my program. My program takes 2 big integers and adds them together. First how do I declare my 2 Big Integers and then add them together? I have read the API docs , but still need help. Thanks!
    Code:
    import java.math.BigInteger;
    public class Integer  {
        private BigInteger IntegerONE; // both entered as strings from GUI
        private BigInteger IntegerTWO;
        
       
        public Integer() {
         
        }
    
    
        public Integer( BigInteger IntegerONE, BigInteger IntegerTWO){
            this.IntegerONE = IntegerONE;   // I am not sure here?
            this.IntegerTWO = IntegerTWO;
        }
        public BigInteger getIntegerONE(){
            return IntegerONE;
        }
        public BigInteger getIntegerTWO(){
            return IntegerTWO;
        }
     
     public BigInteger add(BigInteger val){
          
          return(val);       // Psuedocode says(this + val)
        }
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    The design for that class is incorrect: your class isn't an Integer (there already is
    a core class named Integer so your class' name is confusing). Your class is a
    Two Integer Manipulator (let's call it TIM). All it can do is add two BigIntegers;
    BigIntegers know how to add another BigInteger to themselves so you hardly
    need any code:

    [code=java]
    public class TIM {
    private BigInteger i1;
    private BigInteger i2;
    //
    public TIM() { this(BigInteger .ZERO, BigInteger.ZERO ); }
    public TIM(BigInteger i1, BigInteger i2) {
    this.i1= i1;
    this.i2= i2;
    }
    //
    public BigInteger manipulate() { return i1.add(i2); }
    }
    [/code]

    That is all there is to it.

    kind regards,

    Jos

    Comment

    • gemacjr1201
      New Member
      • Nov 2006
      • 14

      #3
      Originally posted by JosAH
      The design for that class is incorrect: your class isn't an Integer (there already is
      a core class named Integer so your class' name is confusing). Your class is a
      Two Integer Manipulator (let's call it TIM). All it can do is add two BigIntegers;
      BigIntegers know how to add another BigInteger to themselves so you hardly
      need any code:

      [code=java]
      public class TIM {
      private BigInteger i1;
      private BigInteger i2;
      //
      public TIM() { this(BigInteger .ZERO, BigInteger.ZERO ); }
      public TIM(BigInteger i1, BigInteger i2) {
      this.i1= i1;
      this.i2= i2;
      }
      //
      public BigInteger manipulate() { return i1.add(i2); }
      }
      [/code]

      That is all there is to it.

      kind regards,

      Jos
      Thanks you so much!!

      Comment

      Working...