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

What: a series of files containing your solution In this lab, you will learn the

ID: 3919488 • Letter: W

Question

What: a series of files containing your solution In this lab, you will learn the basics of user defined data types, object-oriented programming, pointers, reference and exception handling. Problem: Write a program that prompts the user for the name of two files each containing a single line that represents a decimal integer call them m and n, keep in mind that these integers could be very large in absolute value, so they might not be stored neither as a long nor int variables. You should: a) Handle erroneous input graciously. b) Include a class named Biglnteger where you define an integer as a linked list, along with integer operations such as addition and subtraction [multiplication and division will grant you extra points] c) Test your program for each operation and store the result of each operation as a single decimal number in a separate file containing only one line. 1/ Describe how you want to solve this problem: write this description as a comment at the top of your java file. The description should be free of typos and grammatically correct. 2/ Put together a Java code that addresses the above problem. Each instruction of your code should be commented to explain what it does and why the whole piece of code is an actual solution to the above problem

Explanation / Answer

import java.io.File;

import java.io.FileNotFoundException;

import java.util.*;

// Defines a class BigIntLinkedList

public class BigIntLinkedList

{

// To store two numbers

String numberOne, numberTwo;

// Method to read file for two big number in string form  

void acceptData(String fileName)

{

// Declares Scanner class object

Scanner sc = null;

// try block begins

try

{

// Creates the object to read file contents

sc = new Scanner(new File(fileName));

// Accepts data

numberOne = sc.next();

numberTwo = sc.next();

}// End of try block

// Catch block to handle File not found exception

catch(FileNotFoundException fe)

{

System.out.println("Unable to open the file " + fileName);

}// End of catch block

// Closes the scanner

sc.close();

}// End of method

// Method to add two big integer numbers

void add(String numberOne, String numberTwo)

{

// Creates Integer type linked list objects for two numbers and answer

LinkedList<Integer> firstNumber = new LinkedList<Integer>();

LinkedList<Integer> secondNumber = new LinkedList<Integer>();

LinkedList<Integer> answer = new LinkedList<Integer>();

  

// Store number of digits in number one and two

int lengthOne = numberOne.length(), lengthTwo = numberTwo.length();

  

// Loops till end of the length of the first number length

for (int counter = lengthOne - 1; counter >= 0; counter--)

// Converts each character from first number to integer and adds it to linked list first number

firstNumber.add(numberOne.charAt(counter) - '0');

  

// Loops till end of the length of the second number length

for (int counter = lengthTwo - 1; counter >= 0; counter--)

// Converts each character from first number to integer and adds it to linked list first number

secondNumber.add(numberTwo.charAt(counter) - '0');

  

// Calculates the biggest number length using conditional operator

int length = lengthOne > lengthTwo ? lengthOne : lengthTwo;

int carry = 0;

  

// Loops till length

for (int counter = 0; counter < length; counter++)

{

// To store each digit of two numbers

int digitOne = 0, digitTwo = 0;

  

// try block begins

try

{

// Extracts a digit from linked list for first number

digitOne = firstNumber.get(counter);

}// End of try block

// catch block begins to handle Exception

catch(Exception e)

{

e.printStackTrace();

}// End of catch block

// try block begins

try

{

// Extracts a digit from linked list for second number

digitTwo = secondNumber.get(counter);

}// End of try block

// catch block begins to handle Exception

catch(Exception e)

{

e.printStackTrace();

}// End of catch block

  

// Adds two digits and stores the result

int digitSum = digitOne + digitTwo + carry;

// Adds the remainder of digitSum to answer linked list

answer.add(digitSum % 10);

// Calculates the quotient

carry = digitSum / 10;

}// End of for loop

  

// Loops till carry is not equals to zero

while (carry != 0)

{

// Calculates remainder of carry and adds it to answer linked list

answer.add(carry % 10);

// Calculates quotient

carry /= 10;

}

// Displays the answer

System.out.print(" Addition Answer = ");

// Loops till end of the answer linked list in reverse order

for (int counter = answer.size() - 1; counter >= 0; counter--)

System.out.print(answer.get(counter));

System.out.println();

}// End of method

// Method to subtract two big integer numbers

void sub(String numberOne, String numberTwo)

{

// Creates Integer type linked list objects for two numbers and answer

LinkedList<Integer> firstNumber = new LinkedList<Integer>();

LinkedList<Integer> secondNumber = new LinkedList<Integer>();

LinkedList<Integer> answer = new LinkedList<Integer>();

LinkedList<Integer> tmporary = new LinkedList<Integer>();

  

// Store number of digits in number one and two

int lengthOne = numberOne.length(), lengthTwo = numberTwo.length();

String s1 = numberOne, s2 = numberTwo;

// Initializes sign of the number to false

boolean sign = false;

  

// Checks if length of first number is less than the length of the second number

// or first number and second number length is same

if (lengthOne < lengthTwo || (lengthOne == lengthTwo && numberOne.compareTo(numberTwo) < 0))

{

// Assigns second number to s1

s1 = numberTwo;

// Assigns first number to s2

s2 = numberOne;

// Sets the sign to true

sign = true;

}// End of if condition

// Stores the length of the string s1 (second number)

lengthOne = s1.length();

  

// Loops string s2 length is not equals to string one length

while (s2.length() != lengthOne)

// Converts to integer

s2 = "0" + s2;

  

// To store digits in lists

// Loops till length one

for (int counter = lengthOne - 1; counter >= 0; counter--)

{

// Converts each character at current index position to integer and adds it to linked list first number

firstNumber.add(s1.charAt(counter) - '0');

// Calculates 9 complement of second number and adds it to linked list second number

secondNumber.add('9' - s2.charAt(counter));

}// End of for loop

  

// To store the carry

int carry = 0;

// Loops till first number length

for (int counter = 0; counter < lengthOne; counter++)

{

// To store digit value of each number

int digitOne = 0, digitTwo = 0;

// try block begins

try

{

// Extracts current digit from first number from from linked list first number

digitOne = firstNumber.get(counter);

}// End of try block

// catch block begins to handle Exception

catch(Exception e)

{

e.printStackTrace();

}// End of catch block

// try block begins

try

{

// Extracts current digit from second number from linked list second number

digitTwo = secondNumber.get(counter);

}// End of try block

// catch block begins to handle Exception

catch(Exception e)

{

e.printStackTrace();

}// End of catch block

// Adds two digits and carry and stores the result

int digitSum = digitOne + digitTwo + carry;

// Adds the remainder of digitSum to temporary linked list

tmporary.add(digitSum % 10);

// Calculates the quotient

carry = digitSum / 10;

}// End of for loop

  

// Adding carry and storing in answer list

for (int counter = 0; counter < lengthOne; counter++)

{

// Calculates the carry sum

int sum = tmporary.get(counter) + carry;

// Calculates the remainder of sum and stores it in answer

answer.add(sum % 10);

// Calculates the quotient for carry

carry = sum / 10;

}// End of for loop

  

// Displays the result

System.out.print(" Difference = ");

// Checks if first number and second number is same

if (s1.equals(s2))

System.out.print("0 ");

// Otherwise

else

{

// Checks if sign is true then display negative sign

if (sign)

System.out.print("-");

  

int counter;

// Stops printing leading zeroes

// Loops till answer linked list size reverse order

for (counter = answer.size() - 1; counter >= 0; counter--)

// Checks if current number is not zero

if (answer.get(counter) != 0)

break;

// Loops till zero

for (; counter >= 0; counter--)

// Displays current node digit data from answer linked list

System.out.print(answer.get(counter));

System.out.println();

}// End of else

}// End of method

// main method definition

public static void main(String[] ss)

{   

// BigIntLinkedList class object created

BigIntLinkedList bll = new BigIntLinkedList();

// Accepts two numbers for addition

System.out.print(" Adding Two Large Numbers Using Linked Lists Test ");

// Calls the method to accept two numbers from file

bll.acceptData("BigIntegerOne.txt");

System.out.println("First Number: " + bll.numberOne + " Second Number: " + bll.numberTwo);

// Calls the method to add two numbers

bll.add(bll.numberOne, bll.numberTwo);

  

// Accepts two numbers for subtraction

System.out.print(" Subtracting Two Large Numbers Using Linked Lists Test ");

// Calls the method to accept two numbers from file

bll.acceptData("BigIntegerOne.txt");

System.out.println("First Number: " + bll.numberOne + " Second Number: " + bll.numberTwo);

// Calls the method to subtract two numbers

bll.sub(bll.numberOne, bll.numberTwo);

}// End of main method

}// End of class

Sample Output:


Adding Two Large Numbers Using Linked Lists Test
First Number: 22223339999
Second Number: 11112229999

Addition Answer = 33335569998


Subtracting Two Large Numbers Using Linked Lists Test
First Number: 22223339999
Second Number: 11112229999

Difference = 11111110000

BigIntegerOne.txt file contents

22223339999
11112229999

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