201 Assignment: Adding Large Integers Description This is a large final assignme
ID: 3697496 • Letter: 2
Question
201 Assignment: Adding Large Integers Description This is a large final assignment that I am giving you relatively little direction on, so get started now. In any computer representation, there is a largest positive integer. In Java, the long primitive data type (standing for long integer) is used to represent large integers. The largest long is 19 digits and has a value of 9223372036854775807. In this assignment, you will write a program that reads large integers from a file and adds them. These integers will all be larger than the largest long How to Store These Large Integers Obviously, these integers cannot be read in or added as longs because they are too big! So you will read each integer in as a String and convert that String to an array of ints that represents all the digits in the large number. For example, suppose my large integer is: 35443933471129477556102 First, read it in as a String 354439334711294 77556102" Then, go through the String character by character converting each character to an int between 0 and 9 and storing each digit in an array of ints of the required length. In this example, the array would have a length of 23 and might look like this [3, 5, 4, 4, 3, 9, 3, 3, 4, 7, 1, 1, 2, 9, 4, 7, 7,5, 5, 6, 1, 0, 2] 0 1 2 3 4 5 6 789 10 11 12 13 14 15 16 17 18 19 20 21 22 or like this [3, 5, 4, 4, 3, 9, 3, 3, 4, 7, 1, 1, 2, 9, 4, 7, 7, 5, 5, 6, 1, 0, 2] 22 21 20 19 1 817 16 15 14 13 12 11 10 9 87 6 5 4 3 2 1 0 That is, you need to decide whether to store the most significant digit (the 3 in this case) at position 0 in the array, or in the last position of the array. Also, don't forget that it is easy to convert a char that represents a digit between 0 and 9 to an integer between 0 and 9. As an example: '7-'0' 7. That is, the char '7' has a character code that is a difference of 7 from the character code for the char '0' Input/Output Your program should prompt the user for the name of the input file and keep prompting the user until he/she gives the name of a file that exists. The format of the file is one large integer per line. Read these large integers two at a time and write their sum to an output file. Your program should also prompt the user for the name of the output file. Of course, the output file may (or may not) exist. You should read integers two at a time until there are no more pairs of integers. For example, you might have the following input file 8989898989898989898989898989898987 23128392038283094930273845 382309 834209384234820349823049808230984023984208Explanation / Answer
ode:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package ideone;
/**
*
* @author 302320
*/
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Scanner;
class Ideone {
public static void main(String[] args) throws FileNotFoundException, IOException {
Scanner input = new Scanner(System.in);
BufferedReader in = new BufferedReader(new FileReader("Input.txt"));
String num1,num2;
int[] xDigits = null;
int[] yDigits = null;
int[] result = new int[51];
while (((num1= in.readLine()) != null)&& (num2= in.readLine()) != null)
{
xDigits = stringToIntArray(num1);
yDigits = stringToIntArray(num2);
result=add_int(xDigits,yDigits);
reverseArray(result);
System.out.println("Sum is ");
print(result);
System.out.println("");
}
input.close();
}
private static void reverseArray(int[] result) {
for (int i = 0; i < result.length / 2; i++) {
int temp = result[i];
result[i] = result[result.length - i - 1];
result[result.length - i - 1] = temp;
}
}
public static int[] stringToIntArray(String n) {
char[] charArray = n.toCharArray();
int[] result = new int[50];
int resultIndex = 0;
for (int i = (charArray.length - 1); i >= 0; i--) {
result[resultIndex] = Integer.parseInt("" + charArray[i]);
resultIndex++;
}
return result;
}
private static int[] add_int(int[] xDigits, int[] yDigits) {
int carryOver = 0;
int[] result1 = new int[51];
for (int i = 0; i < 50; i++) {
int j = xDigits[i] + yDigits[i] + carryOver;
if (j > 9) {
result1[i] = j % 10;
carryOver = 1;
} else {
result1[i] = j;
carryOver = 0;
}
}
return result1;
}
private static void print(int[] result) {
boolean print = false;
for (int i : result) {
if (!print && i == 0) {
continue;
} else {
print = true;
}
System.out.print(i);
}
}
}
Input:
999999999999999999999999
11111111111111111
8989898989898989898989898989
23128392094930273845
Output:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.