1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Java object method

  1. Sep 19, 2011 #1
    Thers a class called BigInt and it has a size and digit. size is the number of digits in the integer that is entered and digit is an array that holds the integer.

    theres a method which is supposed to just add one to the BigInt so i figured that i could just add one to the last element of the BigInt array. am i right in thinking that?

    also, this is what i have so far:

    Code (Text):
    public void BigInt inc(BigInt num)
        {
       
       
        }
     
    but when i compile it i get an error saying that it expects ( infront of inc. the method isnt supposed to return anything. Im not sure why its getting that error, is it because the method is empty?
     
  2. jcsd
  3. Sep 19, 2011 #2

    Mark44

    Staff: Mentor

    Your method definition above says that inc doesn't return anything (void) AND it returns a BigInt. If it really isn't supposed to return a value, remove BigInt from between "void" and "inc".

    Also, yes, you can add 1 to the last digit, but be aware that this could cause the number to roll over to a new number that requires more digits.

    For example, if you call inc(99999), the new number has 6 digits instead of 5.
     
    Last edited: Sep 19, 2011
  4. Sep 19, 2011 #3
    the BigInt means that it returns a BigInt?

    and thats true, i didnt consider that yet. thank you
     
  5. Sep 19, 2011 #4

    Hurkyl

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    What would it look like with a parenthesis there?
    Code (Text):
    public void BigInt(inc ...
    Does this suggest what you did wrong?
     
  6. Sep 19, 2011 #5

    Mark44

    Staff: Mentor

    The compiler/translator is confused. It thinks the method returns a BigInt, but you also have void there. You can't have both.
     
  7. Sep 19, 2011 #6
    mark was right
     
  8. Sep 19, 2011 #7
    got another error with the compiler when i tried to test out the method. is that saying that it cant find the inc method? or that it cant pass in num1?

    this is the error

    BigIntSimpleTest.java:29: error: cannot find symbol
    num1 = num1.inc();
    ^
    symbol: method inc()
    location: variable num1 of type BigInt

    this is my test program

    Code (Text):
    import java.util.Scanner;

    public class BigIntSimpleTest
     {
        public static void main(String[] args)
        {

        Scanner input = new Scanner(System.in);
       
        int number1 = input.nextInt();
        int number2 = input.nextInt();
        BigInt num1 = new BigInt(number1);
           
        BigInt num2 = new BigInt(number2);
       
        BigInt num3 = new BigInt();
       
       
        System.out.println("Num1 " + num1);
        System.out.println("Num2 " + num2);
        num3 = num1.plus(num2);
        System.out.println("Num1 + Num2 = " + num3);
       
        System.out.println("\ncompare num1 and num2 " + num1.compareTo(num2));
       
        System.out.println("\n the product of num1 and num2 is " + num1.times(num2));
       
       
        num1 = num1.inc();
        System.out.print("added one to number1 " + num1);
        }
    }
    this is my inc method, i know its wrong i just want to be able to get it to add one to 25 or something where i dont have to worry about increasing the size and then work out the bugs.

    Code (Text):
        public void inc(BigInt num)
        {
            int carry = 0;
            num.digit[size] = num.digit[size] + 1;
       
            if(num.digit[size] > 9)
            {
            num.digit[size] = num.digit[size] % 10;
            carry = num.digit[size] / 10;
            num.size++;
            }
       
            if(carry != 0)
            {
            num.digit[size + 1] = num.digit[size + 1] + carry;
            }
        }
     
  9. Sep 19, 2011 #8

    Hurkyl

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    This is a matter of scope -- how the compiler looks for symbols.

    When you write a.b where a is an instance of class A, where does your compiler look for b? The first place it looks is things defined in the following place:
    Code (Text):

    class A {
        // Here!
    }
     
    Armed with this knowledge, does this give you any ideas of what to look for?
     
  10. Sep 19, 2011 #9
    in this case is there a b? I'm just trying to work on one instance of the class.
    but if there was a b it would be found inside the class, is that what you were suggesting?
     
  11. Sep 19, 2011 #10

    uart

    User Avatar
    Science Advisor

    Yes, if a method is defined outside of the primary class definition then you need to use the scope resolution operator "::". As for example,

    void BigInt::inc(BigInt num)


    I think that this is probably what you where intending in your initial post when you wrote,

    void BigInt inc(BigInt num), you just got the syntax wrong by leaving out the "::".


    BTW. Can you clarify one point. Is BigInt something that you're writing (from scratch), or is it something you're extending?
     
  12. Sep 19, 2011 #11
    i dont quite understand what you mean. the inc method is inside the BigInt class

    and its a lab assignment, we were given just the framework and had to come up with the constructors and methods
     
  13. Sep 19, 2011 #12

    Hurkyl

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    This is C++ only -- I don't think Java allows members to be defined outside of the class body.
     
  14. Sep 19, 2011 #13

    Hurkyl

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    a was "num1" and b was "inc".

    Right.
     
  15. Sep 19, 2011 #14
    was i right in writing it as num1.inc();?
     
  16. Sep 19, 2011 #15

    uart

    User Avatar
    Science Advisor

    Whoops, I only looked at the code in the OP and thought it was c++. I guess I should have read a bit further. :redface:
     
    Last edited: Sep 19, 2011
  17. Sep 19, 2011 #16

    Mark44

    Staff: Mentor

    Yes, as long as inc is a method that is defined in your BigInt class. This is what Hurkyl was alluding to a few posts ago.
     
  18. Sep 19, 2011 #17

    uart

    User Avatar
    Science Advisor

    Yes but you only need the "num1.inc();" by itself, not "num1 = num1.inc();". The function inc returns void, so you don't want to be trying to make that assignment on the LHS.
     
  19. Sep 20, 2011 #18
    when i try to do a print statement like

    Code (Text):
    System.out.println("\n try out inc " + num1.inc());
    i get this error

    its not supposed to have arguments, right?
     
  20. Sep 20, 2011 #19

    uart

    User Avatar
    Science Advisor

    BigNum method "inc()" doesn't return anything for println to print.

    BigNum will need some methods to display it's value, something like "num1.show()" or "num1.toString()" etc. If BigNum doesn't already have some methods like this then you need to write them.
     
  21. Sep 20, 2011 #20
    how would i write that, i would pass num1 over to the toString method? but then how would it get changed by the inc method?

    this is my program for reference

    Code (Text):
    import java.util.Scanner;


    public class BigIntSimpleTest2andClass
    {
      public static void main(String[] args)
      {


        Scanner input = new Scanner(System.in);
        int number1 = input.nextInt();
        int number2 = input.nextInt();
        BigInt num1 = new BigInt(number1);
        BigInt num2 = new BigInt(number2);
        BigInt num3 = new BigInt();
       
        System.out.println("Num1 " + num1);
        System.out.println("Num2 " + num2);
        num3 = num1.plus(num2);
        System.out.println("Num1 + Num2 = " + num3);
        System.out.println("\ncompare num1 and num2 " + num1.compareTo(num2));
        System.out.println("\n the product of num1 and num2 is " + num1.times(num2));
         System.out.println("\n try out inc " + num1.inc());
      }
    }


    class BigInt
    {
       private int digit[];         // represent the integer as an array of digits
       private int size;            // number of digits in the integer
       private final int max = 50;  // maximum number of digits in the integer
    public BigInt()
    {     // default constructor
        size = 50;
        digit = new int[max];
        digit[0] = 0;
    }
    // constructor with initial String value
    public BigInt(String num)
    {  
      size = num.length();
      digit = new int[max];
      for (int ct = size - 1; ct >= 0; ct --)
      {
        digit[ct] = Integer.parseInt(num.substring(size - ct - 1, size - ct));
      }
    }
    // constructor with initial integer value  
    public BigInt(int num)
    {  
      size = Integer.toString(num).length();
      digit = new int[max];
      int rem = num;
      for ( int ct = 0; ct <= size - 1; ct++)
      {
      digit[ct] = rem % 10;
      rem = rem / 10;
      }
    }
    // copy constructor
    public BigInt(BigInt num)
    {  
      BigInt copy = new BigInt();//Create copy object
      copy.size = this.size;//declare and initialize with BigInt nums size
      for(int i = 0; i < this.size; i++)//copy the digits in each index
        copy.digit[i] = this.digit[i];
    }
    // override Object’s version
    public String toString()
    {
      String intString = "";
      for (int ct = size - 1; ct >= 0; ct --)
      {
        intString = intString + String.valueOf(digit[ct]);
      }
      return intString;
    }

    public int compareTo(BigInt other)
    {
      int compare = 0;
      int stop = 0;
      if(this.size > other.size)
      {
        stop = this.size;
      }
      else
      {
      stop = other.size;
      }
     
      if(this.size > other.size)
      {
        compare = 1;// return  1 if this greater than other
      }
      else if(this.size < other.size)
      {
        compare = -1;// return -1 if this less than other
      }
      else if(this.size == other.size)
      { // return  0 if both equal
        for(int i = 0; i <= stop;i++)
        {
          if(this.digit[i] > other.digit[i])
          {
            compare = 1;
          }
          else if (this.digit[i] < other.digit[i])
          {
            compare = -1;
          }
          else  
           continue;
        }
      }
      else
      {
        compare = 0;
      }
     
      return compare;                                                                  
    }
     
    // add two BigInt’s
    public BigInt plus(BigInt arg)
    {
      BigInt sum = new BigInt();//Create sum object
      //declare and intialize carry
      int carry = 0;
      int i, temp;
    //change size of sum
      if(this.size > arg.size)
        sum.size = this.size;
      else
        sum.size = arg.size;


      //add two BigInts
      for(i = 0; i <= sum.size - 1; i++)
      {
        temp = this.digit[i] + arg.digit[i] + carry;
        //System.out.println(i + " Temp is " + temp);
        sum.digit[i] = temp % 10;
        carry = temp / 10;
        //System.out.println(i + "Carry is " + carry);
        //System.out.println("sum.digit[" + i +"] is " + sum.digit[i]);
      }
      if (carry != 0)
      {
        sum.digit[sum.size] = carry;
        sum.size++;          
      }                  
      return sum;
    }


    // multiply two BigInts
    public BigInt times(BigInt other)
    {
      //Declare i and initialize carry
      int i,j, count, temp, addZero;
      int carry = 0;
    //create product object
      BigInt product = new BigInt();
      product.size = 0;
     
    //set size of product
      int intervals = this.size * other.size;
     
      BigInt ans[] = new BigInt[this.size];
      for(i = 0; i < this.size; i++)
      {
         ans[i] = new BigInt();
         ans[i].size = 0;
      }
    //Loops to run through elements of array and multiply
      i= 0;
      count = 0;
      while(count <= intervals - 1)
      {
       
        // add 0's
        for(addZero = 0; addZero < i; addZero++)
        {
            ans[i].digit[addZero] = 0;  
            ans[i].size++;      
        }
       
        for(j = 0; j <= (other.size-1); j++)
        {
          temp = this.digit[i] * other.digit[j] + carry;
          ans[i].digit[j+addZero] = temp%10;
          carry = temp / 10;
          //System.out.println(i + " Temp is " + temp);
          //System.out.println(i + " carry is " + carry);
          //System.out.println("sum.digit[" + i +"] is " + product.digit[i]);
         
          ans[i].size++;
          count++;
        }
        i++;
      }
         
      if (carry != 0)
      {
        ans[i-1].digit[ans[i-1].size] = carry;
        ans[i-1].size++;          
      }                
         
      for(int addAll = 0; addAll < i; addAll++)
      {
        product = product.plus(ans[addAll]);        
      }
     
      return product;
     
    }
       //private void times10() { // I found this useful, but you might not.
                              // Not required, but if you do write it be careful
                                // since it changes the BigInt. You might prefer  
                                // writing a version that creates a new BigInt
       
    //}
        public void inc(BigInt num)
        {
            int carry = 0;
            num.digit[size] = num.digit[size] + 1;
       
            if(num.digit[size] > 9)
            {
            num.digit[size] = num.digit[size] % 10;
            carry = num.digit[size] / 10;
            num.size++;
            }
       
            if(carry != 0)
            {
            num.digit[size + 1] = num.digit[size + 1] + carry;
            }
        }
    }


     
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Java object method
Loading...