Hi, I have to implement a class that supports arbitrary sized naturalnumbers. He
ID: 3610786 • Letter: H
Question
Hi,I have to implement a class that supports arbitrary sized naturalnumbers. Here's what has to be done:
Say the class is named BigNatural (its the default package). I needto provide 4 constructors.
1. A defaultconstructor that initializes the natural number to 0.
2. A one argument constructor that takes anint argument (assume constructor will never becalled with a negative argument)
3. A one argument constructor that takes aString argument (must be formatted like output ofthe toString method listed below (i.e. as "1" or "435234" (nocommas, decimals, leading zeros, etc.)))
4. A one argument constructor that takes aBigNatural argument.
It also must contain the following methods:
1.increment - increases value of number by 1 andreturns nothing
2. decrement - decreases value of number by 1 (ifpossible i.e. x > 0) and returns nothing
3. toString - returns string representing thevalue of the natural number (i.e. "435234")
Here's what I have so far:
-------------------------------------------------------------------------------------------------
public class BigNatural()
{
private int natural;
private String str;
private BigNatural b;
public int BigNatural() //defaultconstructor
{
natural = 0;
}
public int BigNatural (int x) //int argument
{
natural = x;
}
public String BigNatural (String x) //Stringargument
{
str = x;
}
public BigNatural BigNatural (BigNatural x)//BigNatural argument
{
b = x;
}
public void increment () //increment
{
natural = natural + 1;
}
public void decrement () //decrement
{
natural = natural - 1;
}
}
-------------------------------------------------------------------------------------------------
Here's an example of how the program is to be tested, etc.:
-------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------
I have a few questions.
1. First of all, in the test file, whenb3.decrement() is called and b3 is a string ofnatural numbers, how can I decrement that? Do I have to convert thestring into an integer, decrement it, and then convert it back to astring?
2. Can I declare a variable as private BigNaturalb even though BigNatural is not originally part of javabut is my creation?
3. For the one argument BigNatural constructor, can you just dopublic BigNatural BigNatural (BigNatural x)?
4. I am confused on how to write the toStringmethod. Can I just do String str = "" +natural where natural is an integer? Also, what way would I goabout removing all the commas, leading zeros, etc.? Have a whileloop of the length of the string and do some sort of comparecommand that would remove everything that is not aninteger?
Sorry for the long post. Any help would be greatlyappreciated!
Explanation / Answer
/* 1) yes, you have to convert the string to Integer and decrement orincrement it ...convert it aggaiin. The second approach is, convert the string into integer and storethat value in natural. So whatever constructor you use, u will havethe integer value in natural. So increment and decrement willaccess natural. 2) You can declare Bignatural b; even if Bignatural is yourcreation. 3) COnstructors dont return anything.. so just declare constructeras public BigNatural(BigNatural b); 4) Integer class of java has an inbuilt toString() method toconvert int to string. use this in your toString() method. You can run a loop which iterates for length of string and removeleading zeroes and commas etc The complete code is given below */ import java.io.*; import java.lang.*; import java.util.*; class BigNatural { private int natural; private String str; private BigNatural b; public BigNatural() //default constructor { natural = 0; } public BigNatural (int x) //int argument { natural = x; } public BigNatural (String x) //Stringargument { str = x; natural=Integer.parseInt(x); } public BigNatural (BigNatural x) //BigNaturalargument { b = x; natural= x.natural; } public void increment () //increment { natural = natural +1; //System.out.println(natural); } public void decrement () //decrement { if(natural>0) natural = natural -1; //System.out.println(natural); } public String toString() { returnInteger.toString(natural); } } public class TestBigNaturalSimple { public static void main(String[] args) { BigNatural b1 = new BigNatural(); // default constructor //System.out.println(b1.toString()); BigNatural b2 = new BigNatural(23); // one-argument intconstructor //System.out.println(b2.toString()); BigNatural b3 = new BigNatural("346"); // one-argument Stringconstructor //System.out.println(b3.toString()); BigNatural b4 = new BigNatural(b2); // one-argument BigNaturalconstructor //System.out.println(b4.toString()); b1.increment(); b3.decrement(); System.out.println(b1.toString()); // should print out 1 System.out.println(b4.toString()); // should print out 23 } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.