Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-30-2008, 10:34 PM
Member
 
Join Date: Apr 2008
Posts: 1
Javanoob828282 is on a distinguished road
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);
}
}
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 04-30-2008, 11:25 PM
Senior Member
 
Join Date: Jul 2007
Posts: 1,022
hardwired is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Constructor with enum bugger New To Java 2 01-04-2008 12:21 PM
Calling constructor of parent class from a constructor Java Tip Java Tips 0 12-19-2007 10:10 AM
Calling constructor of same class from a constructor Java Tip Java Tips 0 12-19-2007 10:01 AM
problems with asigning elements of an array to a constructor rednessc New To Java 1 12-14-2007 08:25 AM
Constructor Help bluegreen7hi New To Java 2 11-15-2007 06:44 AM


All times are GMT +3. The time now is 11:21 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org