Chapter 7, Programming Project #1, page 514. I\'ve attached a file sum.txtPrevie
ID: 3764131 • Letter: C
Question
Chapter 7, Programming Project #1, page 514. I've attached a file sum.txtPreview the documentView in a new window of 14 lines of integers. Your program must output the sum of each line (14 answers). Here's a picture of suggested console output: Sum.JPG I suggest you start by getting your program to work with just the first 3 lines of data (3 answers) as those will be easy with plain old int data. Then take the challenge of more lines. When I grade these, I will use different numbers, so do not code just for these exact data. Submit a Class called Sum.java with the following main() method: and a processFile method as used below!!!! import java.io.*; // NO you cannot use BigInteger to do this! import java.util.*; // These are the ONLY imports allowed public class Sum { public static final int DIGITS = 50; // maximum number of digits public static void main(String[] args) throws FileNotFoundException { Scanner input = new Scanner(new File("sum.txt")); // sum.txtPreview the documentView in a new window processFile(input); } // ... lots more code follows ..... Above requires two methods, main and processFile. In addition, I REQUIRE two additional methods in your solution, and at least one of these methods must use an array as a passed parameters (two arrays also OK). For example, you might write some sort of "add" method that does the actual addition of int[] arrays as passed parameters. I say int[] because that's how we do addition, one digit at a time, treat each digit as an element of an int[], and write the rules of addition, you know them well, time to code it!!! I ALSO REQUIRE good form for coding, comments, name, indents, decent logic, etc... as always. I also expect good error handling, as in no problems created by extra blank lines, or lack of blank lines, or handling of bad data input. Program submission requirements And finally I REQUIRE that you do your own work, and I will use JPLAG to check up on this.
(sum.txt):
Explanation / Answer
Complete Program:
// Sum.java
import java.io.*;
import java.util.*;
public class Sum
{
public static final int DIGITS = 50;
public static void main(String[] args) throws FileNotFoundException
{
Scanner input = new Scanner(new File("sum.txt"));
processFile(input);
}
public static void processFile(Scanner input)
{
int lineCount = 0;
while(input.hasNextLine())
{
lineCount++;
String line = input.nextLine();
StringTokenizer tokens = new StringTokenizer(line);
int[] result = new int[DIGITS];
while(tokens.hasMoreTokens())
{
int[] temp = new int[DIGITS];
fill(temp, tokens.nextToken());
add(result, temp);
}
String strResult = "";
int i = result.length - 1;
while(i >= 0 && result[i] == 0)
i--;
while(i >= 0)
{
strResult = strResult + result[i];
i--;
}
if(strResult.length() == 0)
strResult = "0";
System.out.println("Sum at line " + lineCount + ": " + strResult);
}
}
public static void fill(int[] temp, String token)
{
int i = 0;
for(int j = token.length() - 1; j >= 0; j--)
{
temp[i] = Character.getNumericValue(token.charAt(j));
i++;
}
}
public static void add(int[] result, int[] temp)
{
int carry = 0;
for(int i = 0; i < DIGITS; i++)
{
result[i] = result[i] + temp[i] + carry;
if(result[i] < 10)
{
carry = 0;
}
else
{
result[i] = result[i] % 10;
carry = 1;
}
}
}
}
Sample Output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.