This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java BigNumber

 
Jose Carrillo
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello well I have to make a BigNumner class that store 100 digits using and array where each element of the array will represent a digit. my question is I have declare the array to 100 elements and then I have to incialized to zero and I do not have any idea how to do that this my code so far:
Public class GiantNumber
{
private short [] digitsArray;

public GiantNumber()
{
digitsArray = new short[100];
}
public GiantNumber1()
{
//here I have to include a default constructor that initializes our number to 0
}
}
I really apreciated
 
Sadanand Murthy
Ranch Hand
Posts: 382
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jose Carrillo:
Hello well I have to make a BigNumner class that store 100 digits using and array where each element of the array will represent a digit. my question is I have declare the array to 100 elements and then I have to incialized to zero and I do not have any idea how to do that this my code so far:
Public class GiantNumber
{
private short [] digitsArray;

public GiantNumber()
{
digitsArray = new short[100];
}
public GiantNumber1()
{
//here I have to include a default constructor that initializes our number to 0
}
}
I really apreciated


Looks like you are jumping into coding without quite grasping the java programming concepts. Please take a look at the Sun's Java Tutorial.
For a class called GiantNumber you cannot have a constructor called GiantNumber1. The constructor name must be exactly the same as the class name. So you will need to use overloading concepts to create multiple constructors for a class.
You reference an element in an array by using the form 'myArray[i]' which will access the i'th element of the array called myArray. If the array is made of primitives, you can assign each element of the array value 0 (or anything else) by using the form 'myArray[i] = 0;'. Since in your case you have 100 array elements, you will need to use a loop (a for loop fits great for this).
A final point - arrays in java are 0 based.
That is why I said at the outset that you need to go through the tutorial.
 
Jason Menard
Sheriff
Posts: 6450
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Primitives are automatically initialized to 0. Each element in an array of primitives is likewise initialized to 0. Therefore you do not need to do any initialization on your array of shorts if you simply want each element to be 0.

Was this your question or did I misunderstand you?
As a side note, you cannot have two empty constructors, and any constructor you do have must have the same name as your class. So given your code, you cannot have the constructor GiantNumber1().
[ February 03, 2004: Message edited by: Jason Menard ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic