Great Deal! Get Instant $10 FREE in Account on First Order + 10% Cashback on Every Order Order Now

Task back to top Large Integers addition using Linked Lists The largest value of an integer number in any programming language is limited by the number of bits used to represent the integer. However,...

1 answer below »

Task

back to top

Large Integers addition using Linked Lists

The largest value of an integer number in any programming language is limited by the number of bits used to represent the integer. However, you can represent an integer using linked lists, so the value of the integer is limited only by the available memory. Your task is to design and implement an Abstract Data Type (ADT) for storing integer numbers in linked lists. There are two different strategies for storing the integers: by storing one digit in each node or by storing multiple digits in each node. We shall call the first strategy Single-digit Integer Linked List (SILL), and the second strategy Multi-digit Integer Linked List (MILL). In this assignment, you need to implement only Single-digit Integer Linked List (SILL).

Single-digit Integer Linked List (SILL)

In this strategy, each node in the list stores one digit, and the value of the integer is a concatenation of all the nodes in the list. For example, the value XXXXXXXXXXis stored in a list that has 11 nodes:

23007999000

Obviously, SILL is inefficient in memory utilisation, as each node stores only one digit. The simplest way to improve the storage efficiency is to store multiple digits in each node, called MILL.

What to Implement (Tasks):

Task 1 (20 marks):

Read two integers from a file (input.txt) which has two lines. The first line stores the first integer and the second line stores the second integer.

You may assume that

  • the integer is entered as Most Significant Bit First
  • it could be negative or positive. A number without a ‘-‘ sign is assumed to be a positive number.

If the input file contains invalid values, the program must catch and report the errors. The program must also check if users have passed a command line parameter before attempting to open the file. If it has read the file successfully, then it must inform users. Regardless of the outcome, the program must then display a simple menu as described in Task 4.

Task 2 (15 marks)

Store the two integers using Single-digit Integer Linked List (SILL) and display the integers read. So the following linked list

23007999000

must be displayed as XXXXXXXXXXIt must also display the number of nodes in the list (here 11)

Task 3 (35 marks)

Write an addition operation for the SILL. Your program should perform addition operation in the digit level (one digit in the node of a linked list) and store the result in an another SILL. See below an example:

SILL1:

999

SILL2:

11

SILL3 = SILL1+SILL2

1010

Task 4 (15 marks)

Write a driver program to test all the functionality that you have implemented. The driver program should have a simple menu to allow users to:
1. Accept the name of an input file. If the file exists, the program should read it. The program must inform users if it has read the file successfully or failed, in which case, it must inform users why it has failed to read the file. (task 1)
2. Display integers stored in SILL (task 2).
3. Display the results of the addition arithmetic operation in SILL (task 3).
4. Exit.

This section concludes the description of the tasks required. Other than functionalities, it is also important the code is designed and written to maximize maintainability. The following section describes aspects that affects code maintainability

Design, Presentation and Conventions

You should NOT use java BigInteger.

You need to think about the design of your program to allow code reuse. The SILL Class should make use of a linked list class to store the digits; you may use Java’s LinkedList class or implement your own. You should also consider the number of lines in each method; as a guideline a method should not exceed 25 lines.

Presentation is mainly formatting for aesthetic, while conventions cover issues like naming of classes, methods and variables as well as coding standards. We will use Google’s Java Style Guide as described inhttps://google.github.io/styleguide/javaguide.html

Rationale

back to top

This assessment task will assess the following learning outcome/s:

  • be able to apply a variety of abstract data structures to the solution of welldefined problems.
  • be able to implement selected data structures in the Java language.
  • be able to design wellstructured solutions to programming problems and implement these solutions in the Java language.

This assignment requires you to use the Java programming language to

  • Read input from text files.
  • Use linked lists to represent large integers.
  • Write an arithmetic operation for large integers (NOT allowed to use Java BigInteger).
  • Write a simple text based menu/user interface.
  • Comments Java programs using Javadoc style/convention.

Marking criteria and standards

back to top

Assignment will be marked out of 100 but will be scaled to assignment weight 15%

CriteriaHD XXXXXXXXXX%)DI (74-85%)CR (65-74%)PS (50-64%)FL(0-49%)
Functionality [85 marks]

Demonstrates highly developed capability to implement program specification by correctly implementing all tasks specified; with a program is robust and correctly handles exceptions and errors.

All arithmetic operations are correct for negative and positive numbers. Error messages are easy to understand.

Demonstrates a well-developed capability to implement program specification by implementing all tasks; with a program that correctly handles most exceptions and errors. Error messages are generally easy to understand.
Demonstrates moderate ability to implement program specification by implementing more than half of the tasks specified. The program has some exception and error handling. The error messages provided are clear but require further explanation.

Demonstrates minimal ability to implement program specification by implementing only task 1 and/or 2.

Very little or no exception and error handling.

Demonstrates very little or almost no understanding of implementing the program specification.

No error messages or cryptic error messages.

Design [10 marks]Shows in-depth understanding of software maintainability by implementing best practices of object-oriented design including information hiding and reusable classes.Shows good understanding of software maintainability by implementing most good practices of object-oriented design including information hiding and reusable classes.Shows moderate understanding of software maintainability by implementing some good practices of object-oriented design including information hiding and reusable classes.Shows minimal understanding of software maintainability.
Some classes and methods are poorly designed.
Shows little or no understanding of software maintainability. Many classes and methods are poorly designed.
Presentation and Conventions [5 marks]

The code is extremely well organized, aesthetically pleasing and easy to follow. Uses JavaDoc style to comment each class and method. Comments each variable and blocks of code, and comments for the code are meaningful (focus on why rather than what). May skip a few variables that are obvious (such as those used in loops).

Names of all the classes, modules and variables are meaningful.

Subscribes to all good coding conventions.

The code is fairly easy to read and mostly aesthetically pleasing. One to two names of the classes, modules and variables may be inappropriate. Uses Javadoc style to comment most classes and methods. Comments most variables and blocks of code.

Subscribes to most good coding conventions as described in the style guide above.

The code is somewhat readable. Three to four names of the classes, modules and variables maybe inappropriate. Uses Javadoc style to comment many of the classes and methods. Comments many variables and blocks of code

Subscribes to some coding conventions as described in the style guide above.

The code is partially organized and somewhat difficult to read. More than four names of the classes, modules and variables maybe appropriate. Uses Javadoc style to comment some of the classes and methods. Comments some variables and blocks of code.

Subscribes to a few coding conventions as described in the style guide above.

Code is disorganized (such as poorly indented). Poor choice of names for many of the classes, modules and variables. Little or no internal comments in the source code.

Subscribes to little or no coding conventions as described in the style guide above.

Presentation

back to top

What to submit:

  1. Java source code (all *.java files only) in the correct directory structure (as dictated by the package structures).
  2. Documentation (Word file preferable) describing how to compile and run your program and any assumptions you have made (not more than 200 words). This documentation is not graded but it will help the markers in assessing your submission.

Requirements

back to top

To complete this assignment you might need to have covered material up to and including the topicArrays, Linked Lists and ADT .


Answered Same Day Apr 01, 2021

Solution

Samrakshini R answered on Apr 02 2021
138 Votes

Addition/.DS_Store
__MACOSX/Addition/._.DS_Store
Addition
in/.myfile.txt.swp
Addition
in/.DS_Store
__MACOSX/Addition
in/._.DS_Store
Addition
in/LinkedListATN.class
public synchronized class LinkedListATN {
LinkedListATN$node head1;
LinkedListATN$node head2;
LinkedListATN$node result;
int ca
y;
LinkedListATN$node cur;
public void LinkedListATN();
void printlist(LinkedListATN$node);
void push(int, int);
void addsamesize(LinkedListATN$node, LinkedListATN$node);
void propogateca
y(LinkedListATN$node);
int getsize(LinkedListATN$node);
void addlists();
public static void main(String[]);
}
Addition
in/LinkedListATN$node.class
synchronized class LinkedListATN$node {
int val;
LinkedListATN$node next;
public void LinkedListATN$node(LinkedListATN, int);
}
Addition/.classpath

    
    
    
Addition/.settings/org.eclipse.jdt.core.prefs
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=e
o
org.eclipse.jdt.core.compiler.problem.enumIdentifier=e
o
org.eclipse.jdt.core.compiler.source=1.8
Addition/.project

     Addition
    
    
    
    
        
             org.eclipse.jdt.core.javabuilde
            
            
        
    
    
         org.eclipse.jdt.core.javanature
    
Addition/myfile.txt
12345
12345
__MACOSX/Addition/._myfile.txt
Addition/src/.DS_Store
__MACOSX/Addition/src/._.DS_Store
Addition/src/LinkedListATN.java
Addition/src/LinkedListATN.java
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
public class LinkedListATN {
    class node  
    { 
        int val; 
        node next; 
  
        public node(int val)  
        { 
            this.val = val; 
        } 
    } 
      
    
 Function to print linked list 
    void printlist(node head)  
    { 
        while (head != null)  
        { 
            System.out.print(head.val + " "); 
            head = head.next; 
        } 
    } 
  
    node head1, head2, result; 
    int ca
y; 
  
    /* A utility function to push a value to linked list *
    void push(int val, int list)  
    { 
        node newnode = new node(val); 
        if (list == 1)  
        { 
            newnode.next = head1; 
            head1 = newnode; 
        }  
        else if (list == 2)  
        { 
            newnode.next = head2; 
            head2 = newnode; 
        }  
        else 
        { 
            newnode.next = result; 
            result = newnode; 
        } 
  
    } 
  
    
 Adds two linked lists of same size represented by 
    
 head1 and head2 and returns head of the resultant  
    
 linked list. Ca
y is propagated while returning  
    
 from the recursion 
    void addsamesize(node n, node m)  
    { 
        
 Since the function assumes linked lists are of  
        
 same size, check any of the two head pointers 
        if (n == null) 
            return; 
  
        
 Recursively add remaining nodes and get the ca
y 
        addsamesize(n.next, m.next); 
  
        
 add digits of cu
ent nodes and propagated ca
y 
        int sum = n.val + m.val + ca
y; 
        ca
y = sum / 10; 
        sum = sum % 10; 
  
        
 Push this to result list 
        push(sum, 3); 
  
    } 
  
    node cur; 
  
    
 This function is called after the smaller list is  
    
 added to the bigger lists's sublist of same size.  
    
 Once the right sublist is added, the ca
y must be  
    
 added to the left side of larger list to get the  
    
 final result. 
    void propogateca
y(node head1)  
    { 
        
 If difference number of nodes are not traversed, add ca
y 
        if (head1 != cur)  
        { 
            propogateca
y(head1.next); 
            int sum = ca
y + head1.val; 
            ca
y = sum / 10; 
            sum %= 10; 
  
            
 add this node to the front of the result 
            push(sum, 3); 
        } 
    } 
  
    int getsize(node head)  
    { 
        int count = 0; 
        while (head != null)  
        { 
            count++; 
            head = head.next; 
        } 
        return count; 
    } 
  
    
 The main function that adds two linked lists  
    
 represented by head1 and head2. The sum of two  
    
 lists is stored in a list refe
ed by result 
    void addlists()  
    { 
        
 first list is empty 
        if (head1 == null)  
        { 
            result = head2; 
            return; 
        } 
  
        
 first list is empty 
        if (head2 == null)  
        { 
            result = head1; 
            return; 
        } 
  
        int size1 = getsize(head1); 
        int size2 = getsize(head2); 
  
        
 Add same size lists 
        if (size1 == size2)  
        { 
            addsamesize(head1, head2); 
        }  
        else 
        { 
            
 First list should always be larger than second list. 
            
 If not, swap pointers 
            if (size1 < size2)  
            { 
                node temp = head1; 
                head1 = head2; 
                head2 = temp; 
            } 
            int diff = Math.abs(size1 - size2); 
  
            
 move difference number of nodes in first list 
            node temp = head1; 
            while (diff-- >= 0)  
            { 
                cur = temp; 
                temp = temp.next; 
            } 
  
            
 get addition of same size lists 
            addsamesize(cur, head2); 
  
            
 get addition of remaining first list and ca
y 
            propogateca
y(head1); 
        } 
            
 if some ca
y is still there, add a new node to  
            
 the front of the result list. e.g. 999 and 87 
            if (ca
y > 0) 
                push(ca
y, 3); 
          
    } 
  
    
 Driver program to test above functions 
    public static void main(String args[]) 
    { 
        LinkedListATN list = new LinkedListATN(); 
        list.head1 = null; 
        list.head2 = null; 
        list.result = null; 
        list.ca
y = 0; 
        int choice;
        BufferedReader reader;
        
        
        System.out.println("=========== Large integer addition ==========");
        System.out.println("| Enter 1 to Read numbers from File         |");
        System.out.println("| Enter 2 to Display integers stored in SILL|");
        System.out.println("| Enter 3 to Display addition result        |");
        System.out.println("| Enter 4 to Exit                           |");
        System.out.println("=============================================");
        Scanner sc=new Scanner(System.in);
        choice=sc.nextInt();
        
        while(true)
        {
            System.out.println("=========== Large integer addition ==========");
            System.out.println("| Enter 1 to Read numbers from File         |");
            System.out.println("| Enter 2 to Display integers stored in SILL|");
            System.out.println("| Enter 3 to Display addition result        |");
            System.out.println("| Enter 4 to Exit                           |");
            System.out.println("=============================================");
            choice=sc.nextInt();
            switch(choice)
            {
            case 1:
                try {
                reader = new BufferedReader(new FileReader("myfile.txt"));
                String line = reader.readLine();
                
                
create first list
                for (int i = line.length()-1; i >= 0; --i) 
                    list.push(Character.getNumericValue(line.charAt(i)), 1);
                
 read next line
                line = reader.readLine();
                
System.out.println(line);
                
create second list
                for (int i = line.length()-1; i >= 0; --i) 
                   list.push(Character.getNumericValue(line.charAt(i)), 2);
                
System.out.println(line);
                reader.close();
                } catch (IOException e) {
                System.out.println("The required file is not found");
                e.printStackTrace();
                }
                
eak;
            case 2:             
                list.printlist(list.head1);
                System.out.println("Number of nodes: "+list.getsize(list.head1));
                list.printlist(list.head2);
                System.out.println("Number of nodes: "+list.getsize(list.head2));
                
eak;
            case 3:
                list.addlists(); 
                list.printlist(list.result);
                System.out.println(" ");
                
eak;
            case 4:
                System.exit(0);
                
eak;
            default:
                System.out.println("Invalid choice");
            }
        
        }
       
    } 
}
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here