with integer arrays when i add them i can have code that looks like this
result[0] = array[0] + array2[0]
all the arrays being of integer type
now if my arrays are of Huge type and i would like to get their sum, how would i do that?
this is the code i have but i does not work. i c++ i would have used operator overloading to overload the "+" operator, i now know that there is no operator overloading in java

package HugeInterger;
public class HugeInterger 
{
    /*
      randomly generate the object with random numbers from 1 to 9
    */
    int length = 4;
    int array[][] = new int[length][length];        
    
    public static int[][] Input(int array[][])
    {
        for(int row = 0; row < array.length; row++)
        {
            for(int column = 0;column < array[row].length; column++)
            {
                int randomNumber = 1 + ((int) (Math.random() * 9));
                array[row][column] = randomNumber ;
            }
        }
        return array;

    }
    
    public static void Output(int array[][])
    {
        /*
          print out the results
        */
        for(int row = 0; row < array.length; row++)
        {
            for(int column = 0; column < array[row].length; column++)
            {
                System.out.print(array[row][column]  + "  ");
            }
            System.out.println();
        }
    }
    
    public static HugeInterger Add(HugeInterger array[][],HugeInterger array2[][])
    {
        /*
          I now try to add the two object, this is where my problem is
        */ 
        HugeInterger result[][] = new HugeInterger[array.length][array.length];
        for(int row = 0; row < array.length; row++)
        {
            for(int column = 0; column < array[row].length; column++)
            {
                result[row][column] = array[row][column] + array2[row][column];
            }
        }
     
        return result;
    }
    
}

i get as error as "+" is not defined for my new object type

your method is supposed to return an Object of type HugeInterger, but you're actually returning a 2 dimensional array of HugeIntergers.
I can't say anything about where you call your method, but that 'll probably be a part of it.

Is there any code to create a single HugeInteger, to initialise one, or to perform any operations on single HugeIntegers?
The code above defines a HugeInteger as consisting of a 4x4 array of ordinary ints. Is that right?

yes it does, but considering the code shown above, it's only possible to have a default HugeInterger with the default values for the variables, since there are no (non-default) constructors or setters/getters.

Is there any code to create a single HugeInteger, to initialise one, or to perform any operations on single HugeIntegers?
The code above defines a HugeInteger as consisting of a 4x4 array of ordinary ints. Is that right?

i have included my entire code together with my main function.
this is what i wanted to do
1. create a hugeinteger object that comprises of 4 elements each being an array of 4 elements(hence my 4*4 array).
2. populate the object with random numbers from 1 to 9
3. print the object
4. having two object of hugeinteger type add them and get the results
ie. if i had two 1*2 array of integers and wanted to add the two this is what i would have
for(int i = 0; i < length; i++
{result = array1[0] + array2[0];}

your method is supposed to return an Object of type HugeInterger, but you're actually returning a 2 dimensional array of HugeIntergers.
I can't say anything about where you call your method, but that 'll probably be a part of it.

i do not understand what you mean

package HugeInterger;
public class HugeInterger 
{
    int length = 4;
    int array[][] = new int[length][length];        
    
    public static int[][] Input(int array[][])
    {
        for(int row = 0; row < array.length; row++)
        {
            for(int column = 0;column < array[row].length; column++)
            {
                int randomNumber = 1 + ((int) (Math.random() * 9));
                array[row][column] = randomNumber ;
            }
        }
        return array;

    }
    
    public static void Output(int array[][])
    {
        for(int row = 0; row < array.length; row++)
        {
            for(int column = 0; column < array[row].length; column++)
            {
                System.out.print(array[row][column]  + "  ");
            }
            System.out.println();
        }
    }
    
    public static HugeInterger Add(HugeInterger array[][],HugeInterger array2[][])
    {
        HugeInterger result[][] = new HugeInterger[array.length][array.length];
        for(int row = 0; row < array.length; row++)
        {
            for(int column = 0; column < array[row].length; column++)
            {
                /*
                 * the line below is where i get the error
                 * the erro is "bad operant type for Hugeinteger
                 */
                result[row][column] = array[row][column] + array2[row][column];
            }
        }
     
        return result;
    }
    
}


package HugeInterger;
public class HugeIntegerMain 
{
    public static void main(String[] args) 
    {
        
        int length = 4;
        int [][]array = new int[length][length];
        HugeInterger HugeIntergerOne = new HugeInterger();
        HugeInterger HugeIntergerTwo = new HugeInterger();
        
        /*
         * this will store the resulting array from any method that returns an
         * array
         */
        HugeInterger Results = new HugeInterger();
        
        
        System.out.println("HugeIntergerOne is:");
        HugeIntergerOne.Input(array);
        HugeIntergerOne.Output(array);
        
        System.out.println("HugeIntergerTwo is:");
        HugeIntergerTwo.Input(array);
        HugeIntergerTwo.Output(array);
        
        
        
        
        
        
       

    }
}

you're not populating or changing anything to your instances of HugeInterger, static goes to the class, not to an instance. as for my remark:

public static HugeInterger Add(HugeInterger array[][],HugeInterger array2[][])

here you set HugeInterger as the return type of your method

HugeInterger result[][] = new HugeInterger[array.length][array.length];
        // rest of your method body
        return result;

in the first line of this snippet, you declare result als being a two dimensional array of HugeInterger objects.
the last line returns the two dimensional array you declared earlier.
I assume your code doesn't compile?

in the first line of this snippet, you declare result als being a two dimensional array of HugeInterger objects.
the last line returns the two dimensional array you declared earlier.
I assume your code doesn't compile?

yes it does not compile, i assume from my coding you can see that i am not that efficient in java.
assuming two hugeinterger type that looks like
array1 = [1,2,3,4] and
array2 =[1,2,3,4]
i would like to have a method to add the two to get a result like this
result =[2,4,6,8]
where result at index 0 is the summation of array1 at index 0 and array2 at index 0.
the method should take in two object of hugeinterger type as arguments. i suppose it would look like this:
add(hugeinterger hugeintergerOne,hugeinterger hugeintergerTwo)
{
//function definition
}

Edited 4 Years Ago by liphoso: clarity

You are getting confused between HugeInteger objects and arrays. As you described it a HI object consists of a 4x4 array. The array is part of the internal implementation of the HI class. Inside your HI class you deal with the arrays, but outside it the arrays should be invisible. Outside your HI class you just deal with HI's - you don't know or care whether they are 4x4 arrays or a List of 16 values, or anything else - that's HI's own business.
You are also getting tangled up with the syntax for declaring arrays. One of the great mistakes they made when creating the Java language was to allow two different syntaxes. you can say int[] myArray or int myArray[] The first version is the preferred one - it says "I have an array of ints, and I call it myArray". The second version compiles to the same result, but when you read it it seems to say "I have an array of myArrays, type int".
In your code you say things like HugeInterger array[][] I guess you are thinking that a HI is an array and combine those two ideas in one place, but that's not what it does. Write that same code using the preferred format and its much clearer: HugeInteger[][] array ie you declare a 2-D array of HI's, and you call that "array". That's where your code goes all wrong - each HI is internally a 4x4 array, but you then create 4x4 arrays of HI's.

Here's what I would do:
1. Think of the int[][] array in HI private.
2. In every method signature that has an array as a parameter or return type, either delete the array completely, or replace it with a single HI. eg

public static HugeInterger Add(HugeInterger array[][],HugeInterger array2[][]) // wrong
public static HugeInterger Add(HugeInterger hi1,HugeInterger hi2)  // much better

Edited 4 Years Ago by JamesCherrill: n/a

that's impossible for several reasons:
1. you declare your array as an two dimensional array, not a one dimensional arra.
2. all the methods in your HugeInterger class are static, which means you will not be changing any of the instance variables values. if you change the value for one instance of HugeInterger, you'll change it for all of them, since you are changing it on class scope, not instance scope.
3. if you want to get a new HugeInterger instance returned from that method, you'll have to change the code in that method to:

HugeInterger result = new HugeInterger();
... return result

yes, you can't just go result[x][y] = ...
but it's a start.

you'll have to rethink your entire usage of the HugeInterger class, create setters and getters and other function methods that are not static, but can operate on instance level.

try that, and post any error messages you get here, also those you get on compile time, it'll make it easier for us to help you out.

Hi stultuske. Can I suggest a little pause for a design question before going too far down this track? Your suggestions of getters/setters etc are perfectly sensible, but there are strong reasons for making BI immutable (like String or BigDecimal)
http://docs.oracle.com/javase/tutorial/essential/concurrency/immutable.html
http://docs.oracle.com/javase/tutorial/essential/concurrency/imstrat.html
and, most relevant here, is that ultimately the code is simpler for immutable objects.

If you want to guide the OP along the getter/setter route then I won't create any more confusion, but I think you should consider this... J

well, yes, my point was just, that if he wants his HugeInterger instances to have values, he'll need at least one way to actually set that value.

yes it could be a setter, or, to make immutable a bit possible, a constructor. I just used setter as an example.

if he adds a constructor taking a two dimensional array as a parameter, and creates a new instance of HugeInterger in his add method, no problem there.

I just assumed that the OP would be more familiar with the use of setters than the creation of an immutable type :)

I'm really sorry about the confusion. There are two ways to approach this and it would be even more confusing if we mixed them up in our advice. Since stultuske has done the most work in this thread then he has the right to make the call - immutable or mutable stultuske? Once that's decided we can get you back on track.
Either way, please spend a little time with my "essay" on arrays a couple of posts ago. That's something you will have to get straight in your own mind a.s.a.p.

first thing you'll need is a constructor, which takes a two dimensional array as parameter, to instantiate your HugeInterger (you can also add a validation to see whether the lengths of your arrays are, in fact, the length you've foreseen: 4)

as for mutable or immutable: I think this most depends on whether liphoso is familiar with the concept, and what/how he wants to implement his code

Edited 4 Years Ago by stultuske: answer to JCherrill 's comment added

... or to keep things really simple - you already have some code to fill in a random HI, so why not a constructor with no arguments that just creates an HI filled with random values? You can always do the one that takes (and validates) values later.

Edited 4 Years Ago by JamesCherrill: n/a

This question has already been answered. Start a new discussion instead.