Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

JAVA!! PLEASE READ BEFORE YOU SOLVE. I ONLY NEED THE PART THAT SAYS IS DUE JANUARY 23RD, I WILL BOLD IT BELOW. ALSO PLEASE TYPE

JAVA!! PLEASE READ BEFORE YOU SOLVE. I ONLY NEED THE PART THAT SAYS IS DUE JANUARY 23RD, I WILL BOLD IT BELOW. ALSO PLEASE TYPE AND DO NOT HANDWRITE. THANK YOU. AGAIN ALL THAT NEEDS TO BE DONE AT THIS POINT IS TO MAKE THE BIGINT CONSTRUCTORS AND THE toString() method.

(10 points) January 23, Wednesday: BigInt constructors and toString() method. Consider a String argument, an integer argument, and a BigInt argument to the constructor. Create a RuntimeException class that throws an exception when the input string does not hold a properly formed integer.

I will also add the demo for the constructor and toString() methods below.

public class BigIntConstructorDemo { public static void main(String[] args) { //BigInt b1 = new BigInt("1000000"); //BigInt b1 = new BigInt("+0"); //BigInt b1 = new BigInt("-0"); //BigInt b1 = new BigInt("5stt7");// error //BigInt b1 = new BigInt("+");// error //BigInt b1 = new BigInt("-");// error //BigInt b1 = new BigInt(" 500");// error BigInt b1 = new BigInt("44444444445555555555666666666677777777770000000000"); System.out.println("b1 is " + b1); } }

BigInt class

The purpose of the BigInt class is to solve the problem using short methods that work together to solve the operations of add, subtract multiply and divide. A constructor can call a method called setSignAndRemoveItIfItIsThere(). It receives the string that was sent to the constructor and sets a boolean variable positive to true or false and then returns a string without the sign that can then be processed by the constructor to store the string in an ArrayList. A public add method does not add. It calls helper methods to do the work. A public multiply method does not multiply. It calls two private methods. One is a multiply method that calls other methods to do the actual multiplication. A public divide method does not divide. It calls many private helper methods that do the work.

This is a challenging project that requires information hiding to be successful.

There are two main programs that you must use. On my website is a demo for the constructor and toString() methods. This demo must be run when you are working on the constructor and toString() method. There is a second BigInt demo for add, subtract, multiply and divide. This demo must be used when you test addition, subtraction, multiplication, division, and modulus.

(10 points) January 23, Wednesday: BigInt constructors and toString() method. Consider a String argument, an integer argument, and a BigInt argument to the constructor. Create a RuntimeException class that throws an exception when the input string does not hold a properly formed integer.

(20 points) February 4 Monday: BigInt addition and subtraction. (10 points) February 11, Monday: BigInt multiplication.

(20 points) February 18, Monday: BigInt division and modulus.

When you solve this division problem use appropriate SOP() lines to demonstrate debugging your program.

BigInt Constructor and toString Considerations

Constructors initialize the private data at the point of declaration. The constructor has a String parameter. Java code must analyze this string and if it correctly represents a BigInt convert the characters to integers and store them in an ArrayList or an array. Example strings your constructor must correctly deal with are: 100, -5, -0, +0, +, -, 56DDD8, 456 The last four examples do not represent an integer and must be rejected. A BigInt consists of a boolean sign and the array or ArrayList 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. Instance variables can be seen throughout the class. You will need other String, int and double variables that will be declared in methods and will be local to that method. These local variables can be passed as arguments to other methods. The algorithm could be:

  1. Read a String
  2. If the string has a or + as the first character remove it and set the sign. . Using a very descriptive name the method might be called setSignAndRemoveItIfItIsThere(num). If the sign is the only character in the string end the program.
  3. 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 method isDigit() 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.
  4. Think of the indexes of the ArrayList values going right to left starting with 0. 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 ArrayList this happens automatically with the add method. Example: bigNum.add(value). (See ArrayList documentation. I will put some on my website.)
  5. 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.
  6. 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.
  7. 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) == '-')

if(num.charAt(i) >= 0 && num.charAt(i) <= 9) it is a digit.

substring(start) returns a new string having the same characters as the substring that begins at index startof 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

given int value = 5;

given num is 1234

num = num + value;// num now is the string 12345

Other String operations may be useful.

You can also test if a character is a digit with Character.isDigit()

Do not put a main method in the BigInt class. On my website are two main classes. One is to test the constructor and toString method and one is to test the operations add, subtract, multiply, divide, and modulus of the class. The main classes are in the same folder as the BigInt class.

Do not use an instance of the Scanner class for keyboard input. The constructor provides the strings for input.

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

Expert Performance Indexing In SQL Server

Authors: Jason Strate, Grant Fritchey

2nd Edition

1484211189, 9781484211182

More Books

Students also viewed these Databases questions