• Support PF! Buy your school textbooks, materials and every day products Here!

Java object method

  • Comp Sci
  • Thread starter apiwowar
  • Start date
  • #1
96
0
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:
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?
 

Answers and Replies

  • #2
33,086
4,793
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:
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?
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:
  • #3
96
0
the BigInt means that it returns a BigInt?

and thats true, i didnt consider that yet. thank you
 
  • #4
Hurkyl
Staff Emeritus
Science Advisor
Gold Member
14,916
17
but when i compile it i get an error saying that it expects ( infront of inc.
What would it look like with a parenthesis there?
Code:
public void BigInt(inc ...
Does this suggest what you did wrong?
 
  • #5
33,086
4,793
the BigInt means that it returns a BigInt?

and thats true, i didnt consider that yet. thank you
The compiler/translator is confused. It thinks the method returns a BigInt, but you also have void there. You can't have both.
 
  • #6
96
0
mark was right
 
  • #7
96
0
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:
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:
	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;
		}
	}
 
  • #8
Hurkyl
Staff Emeritus
Science Advisor
Gold Member
14,916
17
cant find the inc method?
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:
class A {
    // Here!
}
Armed with this knowledge, does this give you any ideas of what to look for?
 
  • #9
96
0
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?
 
  • #10
uart
Science Advisor
2,776
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?
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?
 
  • #11
96
0
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
 
  • #12
Hurkyl
Staff Emeritus
Science Advisor
Gold Member
14,916
17
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)
This is C++ only -- I don't think Java allows members to be defined outside of the class body.
 
  • #13
Hurkyl
Staff Emeritus
Science Advisor
Gold Member
14,916
17
in this case is there a b? I'm just trying to work on one instance of the class.
a was "num1" and b was "inc".

but if there was a b it would be found inside the class, is that what you were suggesting?
Right.
 
  • #14
96
0
was i right in writing it as num1.inc();?
 
  • #15
uart
Science Advisor
2,776
9
This is C++ only -- I don't think Java allows members to be defined outside of the class body.
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:
  • #16
33,086
4,793
was i right in writing it as num1.inc();?
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.
 
  • #17
uart
Science Advisor
2,776
9
was i right in writing it as num1.inc();?
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.
 
  • #18
96
0
when i try to do a print statement like

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

BigIntSimpleTest2andClass.java:23: error: method inc in class BigInt cannot be applied to given types;
System.out.println("\n try out inc " + num1.inc());
^
required: BigInt
found: no arguments
reason: actual and formal argument lists differ in length
1 error
its not supposed to have arguments, right?
 
  • #19
uart
Science Advisor
2,776
9
when i try to do a print statement like

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

its not supposed to have arguments, right?
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.
 
  • #20
96
0
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:
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;
		}
	}
}
 
  • #21
33,086
4,793
when i try to do a print statement like

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



its not supposed to have arguments, right?
There are two errors here, one of which was discussed by uart.
1) The inc method doesn't return a value, so the expression num1.inc() does not represent a number. IOW, it's not something that can be displayed in an output statement.
2) The inc method requires an argument of type BigInt. Calling this method without an argument is why the compiler gave you the error it did.

To see what the inc method does, do this:
1. Use println or toString to display the value of a BigInt instance.
2. Call inc on the same BigInt instance.
3. Use println or toString to display the value of a BigInt instance after inc has been called.
 
  • #22
33,086
4,793
Code:
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;
   } 
 .
 .
 .
   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;
      }
   }
}
Your inc method isn't right. The digit array has max (50) elements, with indexes ranging from 0 through 49. If size == 50, the code above is attempting to access the element at index 50, which is outside the array.
 
  • #23
uart
Science Advisor
2,776
9
2) The inc method requires an argument of type BigInt. Calling this method without an argument is why the compiler gave you the error it did.
Yep I should have noticed that. To tell the truth I came to this thread late and didn't read all the posts.

Anyway it brings up a good point about the "philosophy" of how inc() is being implemented here. Since inc() is only a unary operator it really shouldn't take any arguments in an object orientated implementation.

In a procedural implementation you'd want something like inc(num) but in an object orientated implementation I'd really prefer to see num.inc().


Aiwowar, for an example of what I mean regarding the basic outline of how I think you should be approaching this look at the trivial example of doing this with just an simple integer.
Code:
public class Trivial {
	
	public static class Dumb {
		int x;
		
		public Dumb(int xi) { //constructor
			x = xi;
		}		
		public void AddOne() {
			x=x+1;
		}
		public void ShowMe() {
			System.out.println(x);
		}
	}

   public static void main(String args[]) {
      Dumb num = new Dumb(3);
      num.ShowMe();
      num.AddOne();
      num.ShowMe();
   }
}
 
Last edited:
  • #24
33,086
4,793
That's a good point, uart. If inc is a member method of the BigInt class, it shouldn't need a parameter. The method could be invoked on a BigInt instance.

apiwowar, were you given the signature of the inc method (i.e., were you told that it should be a void method and that it should take a BigInt parameter)? Or were you asked to write a method that would increment a BigInt instance?

If the instructions were not very specific, I would write the method as uart recommends.
 

Related Threads for: Java object method

  • Last Post
Replies
23
Views
4K
  • Last Post
Replies
11
Views
647
  • Last Post
Replies
0
Views
2K
Replies
1
Views
2K
  • Last Post
Replies
1
Views
2K
Replies
1
Views
1K
Replies
5
Views
1K
Top