Question: int[] data = {}; //data array store digits in this natural number /** * THIS IS A HELPER method which converses a digit character to
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;
}
}
public class Operator
{
/**
* Add two natural numbers
* @param a is a NaturalNumber
* @param b is a NaturalNumber
* @return an instance of a class NaturalNumber which is the result of adding a and b
*/
public static NaturalNumber add(NaturalNumber a, NaturalNumber b)
{
//write code
}
/**
* Subtract two natural numbers
* @param a is a NaturalNumber
* @param b is a NaturalNumber
* @return if a is larger than b, return an instance of a class NaturalNumber
* which is the result of subtracting b from a.
* otherwise return an instance of class NaturalNumber with 0
*/
public static NaturalNumber subtract(NaturalNumber a, NaturalNumber b)
{
//write code
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
