Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help creating BigInt add, subtract, multiply, divide and modulus. Below the project instructions I have added what I have so far. If demo

I need help creating BigInt add, subtract, multiply, divide and modulus. Below the project instructions I have added what I have so far. If demo is needed just let me know. Any one would help add, subtract, multiply, or modulus doesn't have to be all of them. Please please please any help would be great!

Thanks!

BigInt classThe purpose of the BigInt class is to solve the problem using short methods that work together to solve the operations of add, subtract multiply, divide, and modulus. 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 helper methods to do the work. I called two 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 20, Wednesday: BigInt constructors and toString() method. Consider a String 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 1 Monday: BigInt addition and subtraction.(10 points) February 8, Monday: BigInt multiplication.(20 points) February 17 Wednesday: BigInt division and modulus. When you solve this division problem use appropriate SOP() lines to demonstrate debugging your program. BigInt Constructor and toString ConsiderationsConstructors initialize the private data at the point of declaration. The constructor has a String parameter. Your Java code must analyze this string and if it correctly represents a BigInt convert the characters to integers and store them in an ArrayList. 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 throw an exceptions and end the program. A BigInt consists of a boolean sign and the ArrayList holding the absolute value of the number. 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 it should throw an exception and 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 throw and exception and 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 am sending you.)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 number = -1234; // creates a string of 5 charactersnumber = number.substring(1) will assign the string 1234 to num given int value = 5;given number is 1234number = number + value;// num now is the string 12345int i = number.length(); // strings know their length. i has a value of 5the string method charAt returns the character value at the specified locationif(number.charAt(0) == '+' || number.charAt(0) == '-')if(number.charAt(i) >= 0 && number.charAt(i) <= 9) it is a digit. substring(start) returns a new string 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.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.

import java.util.ArrayList;

public class BigInt

{

private ArrayList numbers = new ArrayList<>();

private boolean positive = true;

private String setSignAndRemoveIfPresent(String string)

{

Character sign = string.charAt(0);

if (sign.equals('+') || sign.equals('-'))

{

//if only sign is there or empty string

if (string.length() == 1 || sign.equals(' '))

{

throw new InvalidIntegerException("not a correct integer");

}

if (sign.equals('-'))

{// negative number

positive = false;

return string.substring(1, string.length());

}

else if (sign.equals('+'))

{// positive number

positive = true;

return string.substring(1, string.length());

}

else if (sign.equals(' '))

{// case when the string starts with space

throw new InvalidIntegerException("not a correct integer");

}

}

return string;

}

public BigInt()

{

}

public BigInt(String string)

{

String a = setSignAndRemoveIfPresent(string);

for (int i = a.length()- 1; i >= 0; i--)

{

Character c = a.charAt(i);

Boolean value = Character.isDigit(c);

if (value)

{

numbers.add(Integer.parseInt(a.substring(i, i + 1)));

}

else

{

throw new InvalidIntegerException("not a correct integer");

}

}

}

@Override

public String toString()

{

String result = "";

for (int i = numbers.size() - 1; i >= 0; i--)

{

result = result + numbers.get(i);

}

if (!positive)

{

result = "-" + result;

} else if (numbers.size() > 0)

{

result = "+" + result;

}

return result;

}

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

Students also viewed these Databases questions

Question

Question 14 v of145tep 1 of5 01:11:26

Answered: 1 week ago