In: Computer Science
in Java please
For this assignment you are to write a class that supports the addition of extra long integers, by using linked-lists. Longer than what is supported by Java's built-in data type, called long.
Your program will take in two strings, consisting of only digits, covert each of them to a linked-list that represents an integer version on that string. Then it will create a third linked-list that represents the sum of both of the linked lists. Lastly, it will print out the result of the sum.
Conceptual Example
For the string: "12", create a list:
head->1->2->null
For the string: "34", create a list:
head->3->4->null
Add the two lists to create a third list:
head->4->6->null
print the resulting linked list as "46"
Where "->" represents a link.
Keep in mind that the conceptual example above is conceptual. It does suggest a certain implementation. However as you read on you will see that you have several options for implementing your solution. You need not use the suggested implementation above.
For this class you are to implement a minimum of three methods. They are:
A method called makeSDList() that takes in a string, consisting only of digits, as an argument, and creates and returns a linked-link representation of the argument string.
The method has the following header:
SDList makeSDList(String s) { }
where s is a string, and SDList is the class name of the linked list.
A method called addLists() that takes takes two lists, adds them together, and creates and returns a list that represents the sum of both argument lists.
The method has the following header:
SDList addLists(SDList c) { }
wherec linked-list of type SDList .
A method called displayList() that takes takes a list, prints the value of each digit of the list.
The method has the following header:
void displayList() { }
Programming Notes
Programming Rules:
Submission Rules:
1. Submit only one Homework5.java file for all test cases. The starter file is names Homework5a.java so you will need rename the file before you begin submitting your solution.
2. Anything submitted to Mimir is considered to be officially submitted to me and is considered to be 100% your work. Even if it is not your last planned submission.
3. Any extra testing code that you wrote and used to do your own testing should be deleted from the file that gets used in the final grading. I emphasize the word deleted. Commenting out code is not sufficient and not considered deleted. It must be completely removed. English comments written to explain your code are perfectly fine.
I have implemented Homework5.java with two classes which represents singly linkedlist where each node of singly linkedlist represents a single digit.
Classes:-
1> SDList :- This class represents a singly linked list.
2> ListNode :- This class represents a node of singly linked list.
Program :-
import java.util.Scanner;
public class Homework5 {
public static void main(String[] args) {
// create reference of SDList class
SDList x, y, z;
String a, b;
Scanner input = new Scanner(System.in); // Create a Scanner object
try{
// get the number from the user
System.out.print("A: ");
a = input.nextLine();
// keep only digits and remove other characters from the String
String first = "";
for(int i=0; i<a.length(); i++){
// append digits only
if(Character.isDigit(a.charAt(i))){
// append into the string
first += String.valueOf(a.charAt(i));
}
}
x = makeSDList(first); // convert first string to a linked list
// get the number from the user
System.out.print("\nB: ");
b = input.nextLine();
// keep only digits and remove other characters from the String
String second = "";
for(int i=0; i<b.length(); i++){
// append digits only
if(Character.isDigit(b.charAt(i))){
// append into the string
second += String.valueOf(b.charAt(i));
}
}
y = makeSDList(second); // convert second string to a linked list
z = x.addLists(y); // add lists x & y and store result in list y
System.out.print("\nA+B: ");
z.displayList(); // call function that displays list z
System.out.println();
}finally{
input.close();
}
}
// This method return singly linkedlist by converting string of digits.
public static SDList makeSDList(String s) {
// first crate a SDList object
SDList sdlist = new SDList();
// store each digit in linkedlist using for loop
for(int i=0; i<s.length(); i++){
// get each digit from the string and convert it into the integer
// first get the character and convert it into the string
String digit = String.valueOf(s.charAt(i));
// and then convert into the integer
int item = Integer.parseInt(digit);
// here, at "0"th iteration we have to create first node as a head
if(i==0){
// create first node as a head node
// call the addFirst() method
sdlist.addFirst(item);
}else{
// when newNode is not a first node
// call the addLast(0 method
sdlist.addLast(item);
}
}
return sdlist;
}
}
// This class represents a Singly Linkedlist
class SDList {
// points to the first node of linkedlist
ListNode head;
// This class reprent a node for linkedlist
class ListNode{
int item;
ListNode next;
public ListNode(){}
// constructor which create a node for likedlist
public ListNode(int item){
// store the number
this.item = item;
this.next = null;
}
}
// This method add a newNode as a first node for the linkedlist
public void addFirst(int item){
// create newNode by creating object of ListNode class
ListNode firstNode = new ListNode(item);
// make newNode as a head
head = firstNode;
}
// This method add a newNode at the end of the linkedlist
public void addLast(int item){
// create newNode by creating object of ListNode class
ListNode newNode = new ListNode(item);
// reach at the last node of linkedlist
ListNode curr = head;
while(curr.next != null){
// goto the next node
curr = curr.next;
}
// here we reach to the last node
// now add the newNode into the curr.next
curr.next = newNode;
}
// This method add the two singly linkedlist
public SDList addLists(SDList c) {
// store head of first list
ListNode list1 = this.head;
// store head of second list
ListNode list2 = c.head;
// create resultant list
SDList resultList = new SDList();
// create head as a "0" for creating list
resultList.head = new ListNode(0);
// store the carry after adding two digits of two linkedlist
int carry=0;
// store the sum of two digits of two linkedlist
int sum;
while(list1 !=null || list2 != null){
/*
In addiition procees, if fist list become null
then add zero(0) with the second list node
*/
if(list1 == null){
// do the addition
sum = carry + 0 + list2.item;
// after addition, go to the next node
list2 = list2.next;
}
else if(list2 == null){
// do the addition
sum = carry + 0 + list1.item;
// after addition, go to the next node
list1 = list1.next;
}
else{
// when two list does not contain null node
// do the addition for node of fist and node of second linkedlist
sum = carry + list1.item + list2.item;
// after addition, go to the next node
list1 = list1.next;
list2 = list2.next;
}
// store carray after addition
carry=sum/10;
// store the answer of addition to the newNode
resultList.addLast(sum % 10);
}
// after addition if the carry is generated then add to the end of list
if(carry > 0){
// addLast() method
resultList.addLast(carry);
}
// now set head to the first added node
resultList.head = resultList.head.next;
// return the resultant list
return resultList;
}
// This method display the whole linkedlist
public void displayList() {
// stor head into the curr
ListNode curr = head;
//display each node using while loop
while(curr != null){
// display the result linkedlist
System.out.print(curr.item);
// goto the next node
curr = curr.next;
}
}
}
Output:-
1> Entered string with only digits.
2> Entered string with space and digits also.
According to the output, we can see that our program give the perfect result of addition of two numbers( singly linkedlist) if the entered string contains any character except the digits that are entered by the user.
I hope you will understand the above program.
Do you feel needful and useful then please upvote me.
Thank you.