Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In java, the magnitude of (non-negative) integers is limited. For example, we ar

ID: 3659234 • Letter: I

Question

In java, the magnitude of (non-negative) integers is limited. For example, we are not able to add numbers like 18,274,364,583,929,273,748,525 and 8,129,498,165,026,350,236 because int variables cannot hold such large values- certainly not their sum.THE problem of adding two such larger values can be overcome if we treat the numbers as strings of numerals, store the numbers corresponding to these numerals on twoSTACKS,and then perform addition by popping numbers from the stacks.Thepseudocode for this algorithm is as follows:

(Have to fill in the statements)

addLargeNumbers (x, y)

{

Read the numerals of the first number x and store the numbers corresponding to them on one stack;

Read the numerals of the second number y and store the numbers corresponding to them on another stack;

result = 0;

while (atleast on stack is not empty)

{

pop a number from each nonempty stack and add them to result;

push the unit part on the "result stack";

store carry in result;

}

push carry on the "result stack" of it is not zero;

pop numbers from the result stack and display them;

}

WRITE a program that generalizes this process and allows for the addition of "n" (non-negative) integer numbers with the use ofnoperand stacks. The program should work in both of the following modes:

a)Command line mode: the user gives the program a collection of numbers to add on the command line. This might look like

$java prog2 1,000,000,000,000 30,523 2400,000

The program would then output the following and quit:

1,000,000,000,000+30,523+2,470,000=1,000,002,500,523

b)Batch mode: The user gives the program the name of the file containing any number of groups of integers to be added. (In the file, each integer will sit on its own line, and each group will end with an "=" on a separte line). The program then proceses one addition at a time. It prints each result on the screen in order. The program continues this way until the end of the file is reached, and then quits. Note that batch mode will be indicated with a "-f" (file) switch as the first command line argument

$java prog2 -f additions.txt

The file name should be additions.txt

HERE IS WHAT I KNOW:

I know that I have to use "list of stacks (of integers)". I know the declration is

List<Stack<Integer>> the_stack_list;

I know that i have to use a method to carry out the addition algorithm. The method header is

public static AdditionRetVal addLargeNumbers(List<String>the_numbers)

I know that the AdditionRetVal is a record type and has following parameters

boolean success; //Indication the success or failure of the addtion

String result; //The resulting sum of the addition, if successful

String error_message; //indicationg the type of error, if not successful

This type of of return record is needed because it is possible that one or more numbers are in an incorrect format and cannot be added. If the addition is successful, then the field "success" should be given the value true and the field "result" should contain the computed sum. Otherwise "success" is false and the field "error_message" should contain an inappropriate error message to be displayed to the user.

LASTLY: THE PROGRAM SHOULD HANDLE THE FOLLOWING ERRORS

1) "Bad commas": Since the numbers can be large the user can include commas to group every three digits in the standard manner. If the numbers contain any comma at all it should be formatted. Commas can are not necessary and can be omitted

2)"Non number": The string "Hello1000" is not a number and cannot be added

3) "Negative numbers": In this program we will not be handling negative numbers, so should be rejected

4) "Numbers starting with 0": The only number allowed to start with zero is 0 itself. For example 01933 is not acceptable.


Explanation / Answer

Your java File name should be LargeNumbersAddition.java

In the Command Line navigate to the folder of the bove file and execute

javac LargeNumbersAddition.java

to compile the program.

To run the program use this Command

For reading from file addition.txt (Note addition.txt file shoild be present)

java LargeNumbersAddition -f addition.txt

For directly giving numbers

java LargeNumbersAddition 1000000 30000000 500000000

NOTE: IF your using an IDE like Eclipse then see the documentation about how to run command line programs.

CLASS

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.Scanner;
import java.util.Stack;

public class LargeNumbersAddition
{

public static void main(String[] args)
{
if(args[0].equals("-f"))
fileHandling(args);
else
{
ArrayList> list = new ArrayList>();
for(int i=0; i< args.length; i++)
{
String line = args[i];
try
{
Stack stack = getStack(line);
list.add(stack);
System.out.print(line);
if(i != args.length -1)
System.out.print(" + ");
}
catch(InputMismatchException e)
{
System.err.println(e.getMessage());
}
}
AddLargeNumbers(list);
}


}

public static void AddLargeNumbers(ArrayList> list)
{
if(list.isEmpty())
{
System.err.println("No Valid Numbers are Entered. Terminating Program");
System.exit(1);
}

Stack result = new Stack();
result = list.get(0);

for(int i=1; i < list.size(); i++)
{
Stack temp = AddTwoLargeNumbers(result, list.get(i));
if(i != list.size() -1)
{
result = new Stack();
while(!temp.isEmpty())
{
result.add(temp.pop());
}
}
else
{
result = temp;
}
}

System.out.print(" = ");
int i = result.size()%3 == 0 ? 3 : result.size()%3;
while(!result.isEmpty())
{
System.out.print(result.pop());
i--;
if(i == 0)
{
if(!result.isEmpty())
{
System.out.printf(",");
i=3;
}
}
}

System.out.println();
}

public static Stack AddTwoLargeNumbers(Stack stack1,
Stack stack2)
{
Stack result = new Stack();
int carryover = 0;
while(!stack1.isEmpty() || !stack2.isEmpty())
{
int sum =0;
if(stack1.isEmpty())
{
sum = stack2.pop();
}
else if(stack2.isEmpty())
{
sum = stack1.pop();
}
else
{
sum = stack1.pop() + stack2.pop();
}
sum += carryover;
result.add(sum%10);
carryover = sum/10;
}
if(carryover != 0)
result.add(carryover);
return result;
}


public static Stack getStack(String line)
{
if(line.substring(0, 1).equals("-"))
{
throw new InputMismatchException("(" + line + "is a negative number." +
"Discarding this number)");
}
if(!line.matches("[0-9][0-9,]*"))
{
throw new InputMismatchException("(" + line + "is not a number." +
"Discarding this number)");
}
if(line.contains(","))
{
for(int i=line.length()-4; i>0; i= i-4)
{
if(!line.substring(i, i+1).equals(","))
throw new InputMismatchException("(" + line + " is not in correct format. " +
"Discarding this number)");
}
}

Stack stack = new Stack();

for(int i=0; i< line.length(); i++)
{
String element = line.substring(i,i+1);
if(!element.equals(","))
{
stack.add(Integer.parseInt(element));
}
}

return stack;
}

public static void fileHandling(String[] array)
{
if(array.length != 2)
{
System.err.println("No fileName given in Command line argument." +
" Terminating Program");
System.exit(1);
}
String fileName = array[1];

try
{
Scanner fileInput = new Scanner(new File(fileName));
ArrayList> list = new ArrayList>();

while(fileInput.hasNext())
{
String line = fileInput.nextLine();
if(line.equals("="))
{
AddLargeNumbers(list);
list = new ArrayList>();
}
else
{
try
{
Stack stack = getStack(line);
list.add(stack);
System.out.print(line + " + ");
}
catch(InputMismatchException e)
{
System.err.println(e.getMessage());
}
}
}
}
catch (FileNotFoundException e)
{
System.err.println("File "" + fileName + "" not Found." +
" Terminating Program");
System.exit(1);
}
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote