Project Setup: Create a new project in eclipse called Homework04. In your src folder, create a package called hw04. Right-click the src folder and choose new -> package. All of your source code files for this project will go in the hw04 package. Right-click the package and choose new -> Class. Create two classes in your hw04 package, and name them: CustomBigInteger CBITester These two classes are described below. The CustomBigInteger Class: Be sure to name your class exactly CustomBigInteger. Data Fields:private int[] digits Your class shall have a data field called digits. This data field shall be an array of integer values. This data field shall also be private with no getters or setters. Each index of this array will store the digits of our CustomBigInteger. No other data fields are necessary / allowed in this class. All other variables must be local variables. Constructors:public CustomBigInteger(String value) This constructor shall take a String whose characters should all be numbers. You may assume when the code is tested the given String will ONLY contain integer numbers. The String can be any length. This constructor shall call the private parseBigInt method to finish the object initialization. No other constructors are needed or allowed. Methods:private void parseBigInt(String value):The job of this method is to take the String of digits stored in value and store each digit into your digits array data field. It should store one digit PER index of the array. Example: String "435678765456" would need to be processed so that each digit ends up in the data field array: {4,3,5,6,7,8,7,6,5,4,5,6} where 4 is at index 0, 3 is at index 1, 5 is at index 2, and so on.... HINT 1: The length of the string is == the number of digits, so this should tell you how big your array should be. HINT 2: To convert a string number into an actual int type, you can use the Integer.parseInt() method. You will need to convert each individual digit of the string into an integer type and store each individual digit in the digits array data field. public CustomBigInteger add(CustomBigInteger other):This method accepts a CustomBigInteger object (other) as a parameter and adds the parameter object's number to the calling object's number. See Below for discussion on calling object vs parameter object. This method should return the sum result as a new CustomBigInteger object. Keep in mind that this is not as simple as converting both strings into integers and adding them together. The point of this class is to enable REALLY BIG integers to work. Everything has to be done digit by digit. Think about how you would add two numbers together by hand, then work out how to implement this process programmatically. (Don't forget to carry the 1!) HINT: As your program is finding the sums of each digit, store the intermediate results in a String. You can then pass this String to the constructor of a new CustomBigInteger object when you want to return the final result. Calling Object vs Parameter Object: Consider the following: CustomBigInteger cbi1 = new CustomBigInteger("74687654567876545678123456789"); CustomBigInteger cbi2 = new CustomBigInteger("567890987656"); cbi1.add(cbi2); <----NOTE THIS LINE When the instance method add() is invoked, the calling object is cbi1 and the parameter object is cbi2. How do you access the arrays of each object inside of the add method? Well, if the parameter is called other, then you access its array by saying other.digits. To access the array of the calling object, you need to use this.digits. public String toString(): This method should return a string representation of your class. It should return a string which contains the digits of the number stored in that class. Basically you need to take your array and convert it back into the original string format, then return this string. The CBITester Class: Be sure to name your class exactly CBITester. This class shall contain your main method. This class should demonstrate that ALL functionality of your program is working. Make sure to test with small numbers as well as really large numbers. Include the following test cases: 2 + 2 = 4 99999 + 9 = 100008 97654345678909876545743658 + 9787963247908676445789098 = 107442308926818552991532756 MAKE SURE IT IS TESTED!
Project Setup:
Create a new project in eclipse called Homework04.
In your src folder, create a package called hw04.
Right-click the src folder and choose new -> package.
All of your source code files for this project will go in the hw04 package.
Right-click the package and choose new -> Class.
Create two classes in your hw04 package, and name them:
CustomBigInteger
CBITester
These two classes are described below.
The CustomBigInteger Class:
Be sure to name your class exactly CustomBigInteger.
Data Fields:private int[] digits
Your class shall have a data field called digits.
This data field shall be an array of integer values.
This data field shall also be private with no getters or setters.
Each index of this array will store the digits of our CustomBigInteger.
No other data fields are necessary / allowed in this class. All other variables must be local variables.
Constructors:public CustomBigInteger(String value)
This constructor shall take a String whose characters should all be numbers.
You may assume when the code is tested the given String will ONLY contain integer numbers.
The String can be any length.
This constructor shall call the private parseBigInt method to finish the object initialization.
No other constructors are needed or allowed.
Methods:private void parseBigInt(String value):The job of this method is to take the String of digits stored in value and store each digit into your digits array data field. It should store one digit PER index of the array.
Example: String "435678765456" would need to be processed so that each digit ends up in the data field array: {4,3,5,6,7,8,7,6,5,4,5,6} where 4 is at index 0, 3 is at index 1, 5 is at index 2, and so on....
HINT 1: The length of the string is == the number of digits, so this should tell you how big your array should be.
HINT 2: To convert a string number into an actual int type, you can use the Integer.parseInt() method. You will need to convert each individual digit of the string into an integer type and store each individual digit in the digits array data field.
public CustomBigInteger add(CustomBigInteger other):This method accepts a CustomBigInteger object (other) as a parameter and adds the parameter object's number to the calling object's number.
See Below for discussion on calling object vs parameter object.
This method should return the sum result as a new CustomBigInteger object.
Keep in mind that this is not as simple as converting both strings into integers and adding them together. The point of this class is to enable REALLY BIG integers to work. Everything has to be done digit by digit.
Think about how you would add two numbers together by hand, then work out how to implement this process programmatically. (Don't forget to carry the 1!)
HINT: As your program is finding the sums of each digit, store the intermediate results in a String. You can then pass this String to the constructor of a new CustomBigInteger object when you want to return the final result.
Calling Object vs Parameter Object:
Consider the following:
CustomBigInteger cbi1 = new CustomBigInteger("74687654567876545678123456789");
CustomBigInteger cbi2 = new CustomBigInteger("567890987656");
cbi1.add(cbi2); <----NOTE THIS LINE
When the instance method add() is invoked, the calling object is cbi1 and the parameter object is cbi2.
How do you access the arrays of each object inside of the add method? Well, if the parameter is called other, then you access its array by saying other.digits. To access the array of the calling object, you need to use this.digits.
public String toString():
This method should return a string representation of your class.
It should return a string which contains the digits of the number stored in that class.
Basically you need to take your array and convert it back into the original string format, then return this string.
The CBITester Class:
Be sure to name your class exactly CBITester.
This class shall contain your main method.
This class should demonstrate that ALL functionality of your program is working.
Make sure to test with small numbers as well as really large numbers.
Include the following test cases:
2 + 2 = 4
99999 + 9 = 100008
97654345678909876545743658 + 9787963247908676445789098 = 107442308926818552991532756
MAKE SURE IT IS TESTED!
Trending now
This is a popular solution!
Step by step
Solved in 5 steps with 4 images