int[] data = {}; //data array store digits in this natural number /** * THIS IS
ID: 3904843 • Letter: I
Question
int[] data = {}; //data array store digits in this natural number
/**
* THIS IS A HELPER method which converses a digit character to its integer value.
* @param c is character and assumed be one of the following '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
* @return an integer value of character in c
*/
private int toInt(char c)
{
return (int)c - (int)'0';
}
/**
* A parameterized constructor
* @param initData is a String containing the digits of this NaturalNumber
* instantiate the data array to correct size and store the digits from initData
* as an integer in array data.
*
* The constructor should use its setter to set each digit to
*/
public NaturalNumber(String initData)
{
// write code
return;
}
/**
* A setter : setting an integer between 0 to 9 to a digit specified by parameter digitIndex to the data array
* @param digitIndex is the index to data array
* @param value is an integer between 0 and 9
*
* setter validation:
* @param digitIndex must be between 0 and data.length - 1 AND @param value must be between 0 and 9
* otherwise, this setter should not do anything to the data array.
*/
public void setDigit(int digitIndex, int value)
{
//write code
return;
}
/**
* A getter : getting a digit from this NaturalNumber.
* @param digitIndex is the index to the data array.
* @return the integer value specified by @param digitIndex if digitIndex is between 0 and data.length - 1
* otherwise return -1
*/
public int getDigit(int digitIndex)
{
//write code
}
/**
* A compare method to compare this NaturalNumber to other NaturalNumber.
* @param other an instance of another NaturalNumber class to compare this NaturalNumber
* @return 0 if this and other are equal
* 1 if this is bigger than other
* -1 if this is smaller than other
*
* ALGORITHM:
* 1) Strip leading zeros from both this and other natural number
* 2) See if one is longer. If it is, it is bigger.
* 3) If not, take the leftmost digit of both numbers, and compare them. If one is bigger, then that number is bigger.
* 4) Otherwise, continue comparing digits until you reach the rightmost one. If they are the same, the numbers are the equals.
*/
public int compareTo(NaturalNumber other)
{
//write code
}
/**
*
* @return number of digits in this NaturalNumber
*/
public int numberOfDigits()
{
return data.length;
}
/**
* converse this NaturalNumber to a string
*/
public String toString()
{
String s = "";
for(int i = 0; i < numberOfDigits() ; i = i + 1)
{
s = s + (getDigit(i) + "");
}
return s;
}
}
Explanation / Answer
NaturalNumber.java
class NaturalNumber
{
int[] data = {};
private int toInt(char c)
{
return (int)c - (int)'0';
}
public NaturalNumber(String initData) //Parameterized constructor
{
data = new int[initData.length()]; //Length of data = length of string
for(int i = 0; i < initData.length(); i++) //Loop from 0 to length - 1
setDigit(i, toInt(initData.charAt(i))); //Set each digit using setter
return;
}
public void setDigit(int digitIndex, int value) //Setter
{
if(digitIndex >= 0 && digitIndex <= numberOfDigits() - 1 && value >=0 && value <= 9) //If does not follow, nothing happens
data[digitIndex] = value; //Set the digit
return;
}
public int getDigit(int digitIndex) //Getter
{
if(digitIndex >= 0 && digitIndex <= numberOfDigits() - 1) //Return data[digitIndex] if digitIndex is correct
return(data[digitIndex]);
else //Otherwise return -1
return(-1);
}
public int compareTo(NaturalNumber other) //Compares 2 NaturalNumber objects and returns -1, 0 or 1 based on data[]
{
int pos;
//First remove the leading 0's. For that, find the 1st non zero digit and then save the array from
//that position to end in a temporary array and then copy it to data[]
for(pos = 0; pos < numberOfDigits() && this.data[pos] == 0; pos++); //Loop to find 1st non 0 digit
int[] temp = new int[numberOfDigits() - pos]; //Temporary array of size data.length - pos
if(pos != 0)
{
for(int i = 0; i < temp.length; i++) //Copy to temporary array
temp[i] = this.data[pos + i];
this.data = new int[temp.length]; //Re initialize data[]
for(int i = 0; i < numberOfDigits(); i++) //Copy to data[]
this.data[i] = temp[i];
}
//Do the same for other
for(pos = 0; pos < other.numberOfDigits() && other.data[pos] == 0; pos++);
temp = new int[other.numberOfDigits() - pos];
if(pos != 0)
{
for(int i = 0; i < temp.length; i++)
temp[i] = other.data[pos + i];
other.data = new int[temp.length];
for(int i = 0; i < other.numberOfDigits(); i++)
other.data[i] = temp[i];
}
//Now compare
if(this.numberOfDigits() > other.numberOfDigits()) //Compare length
return(1);
else if(other.numberOfDigits() > this.numberOfDigits())
return(-1);
else
{
for(int i = 0; i < numberOfDigits(); i++) //Compare each digit of the array.
//Control will come here only when both arrays have same length
{
if(this.data[i] > other.data[i])
return(1);
else if(other.data[i] > this.data[i])
return(-1);
}
}
return(0); //Control will come here only if both the arrays are equal, then return 0
}
public int numberOfDigits()
{
return(data.length);
}
public String toString()
{
String s = "";
for(int i = 0; i < numberOfDigits() ; i = i + 1)
s = s + (getDigit(i) + "");
return(s);
}
}
A test class with 3 test runs is also provided to verify the NaturalNaumber class.
TestNaturalNumber.java
public class TestNaturalNumber
{
public static void main(String[] args)
{
System.out.println("Test Run 1:");
NaturalNumber m = new NaturalNumber("123");
NaturalNumber n = new NaturalNumber("0011");
System.out.println("m = " + m);
System.out.println("n = " + n);
System.out.println(m.compareTo(n));
System.out.println("-------------------------------------------------");
System.out.println("Test Run 2:");
m = new NaturalNumber("0022");
n = new NaturalNumber("00222");
System.out.println("m = " + m);
System.out.println("n = " + n);
System.out.println(m.compareTo(n));
System.out.println("-------------------------------------------------");
System.out.println("Test Run 3:");
m = new NaturalNumber("00124");
n = new NaturalNumber("124");
System.out.println("m = " + m);
System.out.println("n = " + n);
System.out.println(m.compareTo(n));
}
}
Output:
Test Run 1:
m = 123
n = 0011
1
-------------------------------------------------
Test Run 2:
m = 0022
n = 00222
-1
-------------------------------------------------
Test Run 3:
m = 00124
n = 124
0
Kindly give a thumbs up, if found useful. Comment for queries. :)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.