Question

Hi, I'm having a lot of troubles with this assignment for my Data class. The assignment...

Hi, I'm having a lot of troubles with this assignment for my Data class. The assignment is the following;

A singly-linked list can be used to store large integers one digit at a time. For example, the integer 1234 could be stored in a list by storing 1 in the first node, 2 in the second node, 3 in the third node, and 4 in the last node. However, for this assignment you may find it more useful to store the digits backwards; that is, store 4 in the first node, 3 in the second node, 2 in the third node, and 1 in the last node. You will see why this is the case shortly.

Write a program that reads two positive integers that are of arbitrary length and then outputs the sum of the two numbers. Your program will read the digits as values of type char so that the number 1234 is read as the four characters '1', '2', '3', and '4'. As the characters are read they are changed to values of type int and stored in a list. (Now you can see the first reason why storing the digits “backwards” has an advantage.) After the first number has been read your program reads the second number, storing it in a second list.

Your program will perform the addition by implementing the usual paper-and-pencil addition algorithm. (Now you should discover the second reason why storing the digits “backwards” is advantageous.) The result of the addition is stored in a list and the result is then written to the screen. Include a loop that allows the user to continue to do more additions until the user says the program should end.

Of course, your multiple precision addition code should be formulated as a function that accepts a pair of lists, returning a list as its result (the sum of the two inputs).

0 0
Add a comment Improve this question Transcribed image text
Answer #1

MyData.java

package HomeworkLib2;

import java.util.LinkedList;
import java.util.Scanner;

public class MyData {
  
   public static void main(String args[]){
       Scanner sc = new Scanner(System.in);
      
       while(!sc.nextLine().equals("Stop")){
           //Build-in list can be used as a singly-linked list only with addFirst(), removeFirst()
           LinkedList<Integer> list1 = new LinkedList<Integer>();
           LinkedList<Integer> list2 = new LinkedList<Integer>();
           LinkedList<Integer> list3 = new LinkedList<Integer>();
          
           String num1 = sc.nextLine();
           String num2 = sc.nextLine();
           char num1Chars[] = num1.toCharArray();
           char num2Chars[] = num2.toCharArray();
          
           //Add digits to the list
           for(int i=0;i<num1Chars.length;i++){
               list1.addFirst(Character.getNumericValue(num1Chars[i]));
           }
           for(int i=0;i<num2Chars.length;i++){
               list2.addFirst(Character.getNumericValue(num2Chars[i]));
           }
          
           //Addition Algorithm
           int temp = list1.getFirst()+list2.getFirst();
           if((list1.getFirst()+list2.getFirst()) > 9)
               list3.addFirst( (list1.removeFirst()+list2.removeFirst())%10 );   // If greater than 9, only right digit should be added
           else
               list3.addFirst( (list1.removeFirst()+list2.removeFirst()) );
          
           for(int i=1;i<num1Chars.length;i++){
               if(temp > 9){
                   if((list1.getFirst()+list2.getFirst()+1) > 9){
                       temp = list1.getFirst()+list2.getFirst()+1;
                       list3.addFirst( (list1.removeFirst()+list2.removeFirst()+1)%10 );
                   }
                   else{
                       temp = list1.getFirst()+list2.getFirst()+1;
                       list3.addFirst( (list1.removeFirst()+list2.removeFirst()+1) );
                   }
               }else{
                   if((list1.getFirst()+list2.getFirst()) > 9){
                       temp = list1.getFirst()+list2.getFirst();
                       list3.addFirst( (list1.removeFirst()+list2.removeFirst())%10 );
                   }
                   else{
                       temp = list1.getFirst()+list2.getFirst();
                       list3.addFirst( (list1.removeFirst()+list2.removeFirst()) );
                   }
               }
           }
          
           for(int i=0;i<num1Chars.length;i++)
               System.out.print(list3.get(i));
       }
   }

}

Note that I have used the built-in linkedlist which can be used as a singly-linkedlist with methods addFirst(), removeFirst().

This program stops whenever a user type "Stop".

Add a comment
Know the answer?
Add Answer to:
Hi, I'm having a lot of troubles with this assignment for my Data class. The assignment...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT