Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

1. Submit SumBigInts.java with a main method plus at least three other methods that help you solve this in a well structured form. 2. Define

image text in transcribed

1. Submit SumBigInts.java with a main method plus at least three other methods that help you solve this in a well structured form.

2. Define "interactive" in the text as interacting with input from a File as used in test code below. So the provided main() below MUST work.

3. Use MAX_DIGITS to determine the length of int[ ] arrays. I will change this upon testing. Maybe just start with code provided below:

// This program reads an input file that contains sequences of integers to // be added together. The program reads them as Strings so that it can // process large integers. Reset the constant DIGITS to allow it to handle // larger integers. import java.io.*; import java.util.*; public class SumBigInts { public static final int MAX_DIGITS = 25; public static void main(String[] args) throws FileNotFoundException { Scanner input = new Scanner(new File("sum.txt")); processFile(input); } ... // more methods follow for i/o and add and ..... } And here's the test text file to read plus the expected output to System.out image text in transcribed 

But remember:

DO NOT import BigInteger nor BigDecimal for your final answer, and

DO NOT use System.in for console input, just read a File, and

You are going to write a program that adds together large integers. The built-in type int has a maximum value of 2,147,483,647. Anything larger will cause what is known as overflow. Java also has a type called long that has a larger range, but even values of type long can be at most 9,223,372,036,854,775,807. The approach you are to implement is to store each integer in an array of digits, with one digit per array element. We will be using arrays of length 25, so we will be able to store integers up to 25 digits long. We have to be careful in how we store these digits. Consider, for example, storing the numbers 38423 and 27. If we store these at the "front" of the array with the leading digit of each number in index 0 of the array, then when we go to add these numbers together, we're likely to add them like this: 38423 27 Thus, we would be adding 3 and 2 in the first column and 8 and 7 in the second column Obviously this won't give the right answer. We know from elementary school arithmetic that we have to shift the second number to the right to make sure that we have the appropriate columns lined up: 38423 27 To simulate this right-shifting of values, we will store each value as a sequence of exactly 25 digits, but we'll allow the number to have leading 0's. For example, the problem above is converted into: 0000000000000000000038423 0000000000000000000000027 Now the columns line up properly and we have plenty of space at the front in case we have even longer numbers to add to these You should solve this problem using arrays that are exactly 25 digits long. Certain bugs can be solved by stretching the array to something like 26 digits, but it shouldn't be necessary to do that and you would lose style points if your arrays require more than 25 digits The choice of 25 for the number of digits is arbitrary (a magic number), so you should introduce a class constant that you use throughout that would make it easy to modify your code to operate with a different number of digits The Java class libraries include classes called BigInteger and BigDecimal that use a strategy similar to what we are asking you to implement in this program. You are not allowed to solve this problem using Biglnteger or BigDecimal. You must solve it using arrays of digits You will again be expected to use good style throughout your program and to comment each method and procedure, maybe even have more comments than code, that would be GOOD You are going to write a program that adds together large integers. The built-in type int has a maximum value of 2,147,483,647. Anything larger will cause what is known as overflow. Java also has a type called long that has a larger range, but even values of type long can be at most 9,223,372,036,854,775,807. The approach you are to implement is to store each integer in an array of digits, with one digit per array element. We will be using arrays of length 25, so we will be able to store integers up to 25 digits long. We have to be careful in how we store these digits. Consider, for example, storing the numbers 38423 and 27. If we store these at the "front" of the array with the leading digit of each number in index 0 of the array, then when we go to add these numbers together, we're likely to add them like this: 38423 27 Thus, we would be adding 3 and 2 in the first column and 8 and 7 in the second column Obviously this won't give the right answer. We know from elementary school arithmetic that we have to shift the second number to the right to make sure that we have the appropriate columns lined up: 38423 27 To simulate this right-shifting of values, we will store each value as a sequence of exactly 25 digits, but we'll allow the number to have leading 0's. For example, the problem above is converted into: 0000000000000000000038423 0000000000000000000000027 Now the columns line up properly and we have plenty of space at the front in case we have even longer numbers to add to these You should solve this problem using arrays that are exactly 25 digits long. Certain bugs can be solved by stretching the array to something like 26 digits, but it shouldn't be necessary to do that and you would lose style points if your arrays require more than 25 digits The choice of 25 for the number of digits is arbitrary (a magic number), so you should introduce a class constant that you use throughout that would make it easy to modify your code to operate with a different number of digits The Java class libraries include classes called BigInteger and BigDecimal that use a strategy similar to what we are asking you to implement in this program. You are not allowed to solve this problem using Biglnteger or BigDecimal. You must solve it using arrays of digits You will again be expected to use good style throughout your program and to comment each method and procedure, maybe even have more comments than code, that would be GOOD

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

Database Processing Fundamentals Design And Implementation

Authors: KROENKE DAVID M.

1st Edition

8120322258, 978-8120322257

More Books

Students also viewed these Databases questions