Results 1 to 2 of 2
  1. #1
    Join Date
    Apr 2008
    Posts
    1
    Rep Power
    0

    Default Array Constructor

    I am trying to write code for a class that has a 40element array in it and i want to use the 2nd set of code to just input the numbers and print it out for now. I have to do a bunch with the ints later but i cannot figure out how to write the constructor.
    I am not sure if I have given enough information for someone to help, but I do not know what else to do. I have spents hours looking at this. Thanks for your help.


    public class HugeInteger
    {
    //Instance Variables
    int[] hugeArray=new int [41];
    //constructor
    HugeInteger(
    {

    }//End Constructor
    }//End Class

    *************************
    {
    public static void main(String args[])
    {
    // smaller numbers
    HugeInteger i = new HugeInteger(9999);
    HugeInteger j = new HugeInteger(1);
    HugeInteger k = new HugeInteger(-9999);
    HugeInteger l = new HugeInteger(-1);

    System.out.println(i);
    }
    }

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    public class HugeIntegerTest
    {
        public static void main(String[] args)
        {
            // smaller numbers
            HugeInteger hugeInt = new HugeInteger();
            hugeInt.addElement(9999);
            hugeInt.addElement(1);
            hugeInt.addElement(-9999);
            hugeInt.addElement(-1);
    
            System.out.println(hugeInt);
        }
    }
    
    class HugeInteger
    {
        //Instance Variables
        int numberOfElements = 40;
        // The jvm initializes all elemnts to zero by default.
        int[] hugeArray=new int [numberOfElements];
        int index = 0;
    
        //constructor
        HugeInteger()
        {
            // Check assertion above:
             for(int i = 0; i < hugeArray.length; i++) {
                 System.out.print(hugeArray[i]);
                 if(i < hugeArray.length-1)
                     System.out.print(", ");
                 else
                     System.out.println();
             }
        }//End Constructor
    
        public void addElement(int n)
        {
            // Assign element at index to the local variable/
            // arguemnt value "n".
            hugeArray[index] = n;
            // Increment index.
            index++;
            // If the array is full, ie, index > 39
            // do something...
            if(index > numberOfElements-1) {
                System.out.println("array is full!");
                // Next call to this method will cause an
                // ArrayIndexoutOfBoundsException.
                // Start over:
                index = 0;
            }
        }
    
        public String toString()
        {
            return java.util.Arrays.toString(hugeArray);
        }
    }//End Class

Similar Threads

  1. Constructor with enum
    By bugger in forum New To Java
    Replies: 2
    Last Post: 01-04-2008, 12:21 PM
  2. Replies: 0
    Last Post: 12-19-2007, 10:10 AM
  3. Replies: 0
    Last Post: 12-19-2007, 10:01 AM
  4. Replies: 1
    Last Post: 12-14-2007, 08:25 AM
  5. Constructor Help
    By bluegreen7hi in forum New To Java
    Replies: 2
    Last Post: 11-15-2007, 06:44 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •