Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

BigInt Constructor and toString Considerations Constructors initialize the private data at the point of declaration. The constructor has a String argument. Java code must analyze

image text in transcribed
BigInt Constructor and toString Considerations Constructors initialize the private data at the point of declaration. The constructor has a String argument. Java code must analyze this string and if it correctly represents a BigInt convert the characters to integers and store them in an AnayList or an array. Example strings your constructor must correctly deal with are: "100", "-5", "-0", "+0", "+", "56DDD8" The last two examples do not represent an integer and must be rejected. A BigInt consists of a boolean sign and the array or Array List holding the absolute value of the number. If an array is used a third variable holding the size of the BigInt must also be included. There should be no other instance variables. The algorithm could be: Read a String if the siring has a - or + as the first character remove it and set the sign. (The String substring() method can do this. If this is the only character in the siring end the program.) Use a for loop to work through the string from the end of the string to the front. Store the string characters as integers. The Character wrapper class can make this conversion or the character can be cast as an integer remembering to subtract 48 from the ASCII value. If a character other than a digit is encountered end the program. With the string "549" the 9 can be placed int location 0, the 4 in location 1 and the 5 in location 2. With an AnayList this happens automatically with the add method. BigInt.add(index). (See ArrayList documentation. I will put some on blackboard.) The toString() method creates and returns a String with the sign if it is negative and then adding the ArrayList integers starting from the back of the list to the front. I used a private helper method to test for the sign in the string input, set the sign and to make sure the sign was not the only character in the input string You might consider doing this yourself. You must be familiar with the String methods to turn a string into integers that can be stored in an array. You then need to take the integers stored in an array and turn them back into a string in the toString() method. important reminders: String num = "-1234";//creates a string of 5 characters int i num.length();//strings know their length. i has a value of 5 the string method charAt returns the character value at the specified location if(num.charAt(0) = '+' || num.charAt(0) = '-') substring(start) returns a new siring having the same characters as the substring that begins at index start of this string through to the end of the string. Index numbers begin at 0. Example: num = num.substring(1) will assign the string "1234" to num Other String operations may be useful

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Transact SQL Cookbook Help For Database Programmers

Authors: Ales Spetic, Jonathan Gennick

1st Edition

1565927567, 978-1565927568

More Books

Students also viewed these Databases questions

Question

What is DDL?

Answered: 1 week ago