Question

In: Computer Science

Using Java Although the long data type can store large integers, it cannot store extremely large...

Using Java

Although the long data type can store large integers, it cannot store extremely large values such as an integer with 100 digits. Create a HugeNumber class that uses a linked list of single digits to represent non-negative integers of arbitrary length. The class should include a method to add a new most significant digit to the existing number so that longer and longer numbers can be created. Also add methods to reset the number and to return the value of the huge integer as a String (is this toString?) along with appropriate constructor and accessor methods.

In addition to the requirements given above, implement an inner class iterator which will allow you to sequence through the digits of the HugeNumber one at a time.

Decide if the HugeNumber contains no digits, then converting it to a String should return a “-1” or an empty string and document your decision.

Submit program files for your HugeNumber class and of your class containing main. Include at least test cases for 3 huge numbers, each with 12 or more digits. Make sure to demonstrate the methods to reset the HugeNumber, to return the number as a String, and to iterate through the digits of the HugeNumber one at a time.

Solutions

Expert Solution

I have written the complete Java code for given conditions. Few Points to note are:

1) toString() : This function cannot be used here to copy the contents of linked list in a string as it will keep the elements separately only as shown below:

The number is: [9, 2, 7, 6, 1, 0, 4, 7, 7, 6, 5, 4, 2]

which obviously we don't want as it seems in your question.

So, to print the the complete huge number as a whole, we need to add elements of linked list in a string using a loop as shown below:

String returnNumber()   //return the number as string
   {
       if(ll.size() == 0)   // check if the linked list is empty
           return "";   // return empty string if true
      
       String number = "";
      
       for(int digit : ll)   //copy the elements of the linked list in a string
       {
           number = number + digit;
       }
      
       return number;   // return the string of huge number
   }

2) You will need to import the java.util.* package to use the LinkedList class in Java as shown below:

import java.util.*;

3) Complete Code is as follows:

import java.util.*;

class HugeNumber{
  
   LinkedList<Integer> ll = new LinkedList<Integer>();   //empty linked list
  
   void addMSD(int n)   //adding elements to most significant digit
   {
       ll.addFirst(n);
   }
  
   void resetNumber()   //reset the string
   {
       ll.clear();
       System.out.println("success.");
   }
  
   String returnNumber()   //return the number as string
   {
       if(ll.size() == 0)   // check if the linked list is empty
           return "";   // return empty string if true
      
       String number = "";
      
       for(int digit : ll)   //copy the elements of the linked list in a string
       {
           number = number + digit;
       }
      
       return number;   // return the string of huge number
   }
  
   void iterateElements()
   {
       //Inner class iterator
       class ListIterator
       {  
           void iterate()
           {
               Iterator<Integer> it = ll.iterator();
              
               System.out.print("Iterating the elements of linked list: ");
               while(it.hasNext())       //iterate the elements one by one
               {
                   int digit = (Integer)it.next();
                   System.out.print(digit + " ");
               }
               System.out.println();
           }
       }
       //calling the inner class function
       ListIterator itr = new ListIterator();
       itr.iterate();
   }
}

//class containing main function
public class Number {

   public static void main(String[] args) {
      
       //create objects for 3 huge number
       HugeNumber number1 = new HugeNumber();   //create object of class HugeNumber
       HugeNumber number2 = new HugeNumber();
       HugeNumber number3 = new HugeNumber();
       HugeNumber number4 = new HugeNumber();
      
       //add elements in number 1
       number1.addMSD(2);
       number1.addMSD(4);
       number1.addMSD(5);
       number1.addMSD(6);
       number1.addMSD(7);
       number1.addMSD(7);
       number1.addMSD(4);
       number1.addMSD(0);
       number1.addMSD(1);
       number1.addMSD(6);
       number1.addMSD(7);
       number1.addMSD(2);
       number1.addMSD(9);
      
       //add elements in number 2
       number2.addMSD(1);
       number2.addMSD(2);
       number2.addMSD(3);
       number2.addMSD(4);
       number2.addMSD(4);
       number2.addMSD(5);
       number2.addMSD(6);
       number2.addMSD(7);
       number2.addMSD(8);
       number2.addMSD(5);
       number2.addMSD(9);
       number2.addMSD(0);
       number2.addMSD(4);
       number2.addMSD(8);
      
       //add elements in number 3
       number3.addMSD(6);
       number3.addMSD(2);
       number3.addMSD(7);
       number3.addMSD(2);
       number3.addMSD(0);
       number3.addMSD(0);
       number3.addMSD(0);
       number3.addMSD(6);
       number3.addMSD(3);
       number3.addMSD(7);
       number3.addMSD(3);
       number3.addMSD(2);
      
       //Number 1 operations
       System.out.println("NUMBER 1:");
      
       String number = number1.returnNumber();
       if(number.isEmpty())
       {
           System.out.println("Empty String");
       }
       else
       {
           System.out.println("The number is: " + number);
       }
      
       //Iterate the elements
       number1.iterateElements();
      
       //Reset number 1
       System.out.print("Reset number 1: ");
       number1.resetNumber();
      
       //number 2 operations
       System.out.println("NUMBER 2:");
      
       number = number2.returnNumber();
       if(number.isEmpty())
       {
           System.out.println("Empty String");
       }
       else
       {
           System.out.println("The number is: " + number);
       }
      
       //Iterate the elements
       number2.iterateElements();
      
       //Reset number 2
       System.out.print("Reset number 2: ");
       number2.resetNumber();
      
       //Number 3 operations
       System.out.println("NUMBER 3:");
      
       number = number3.returnNumber();
       if(number.isEmpty())
       {
           System.out.println("Empty String");
       }
       else
       {
           System.out.println("The number is: " + number);
       }
      
       //Iterate the elements
       number3.iterateElements();
      
       //Reset number 2
       System.out.print("Reset number: ");
       number3.resetNumber();
      
       //number 4 operations
       System.out.println("NUMBER 4:");
      
       number = number4.returnNumber();
       if(number.isEmpty())
       {
           System.out.println("Empty String");
       }
       else
       {
           System.out.println("The number is: " + number);
       }
   }

}

Note : Please refer to the screenshot of the code below to understand the indentation of the code

Output:


Related Solutions

Using Java A stack is a type of data collection on which things can be “pushed”...
Using Java A stack is a type of data collection on which things can be “pushed” and “popped”. For example, a stack of plates can have a plate added (or “pushed”) onto the stack, and a plate removed (or “popped”) from the stack. Plates are pushed onto or popped off of the stack one at a time. The last plate pushed onto the stack is the first plate popped off of the stack. This type of structure is known as...
Using survey data to calculate statistics can be extremely valuable, but you must also make sure...
Using survey data to calculate statistics can be extremely valuable, but you must also make sure that the sample and questions are unbiased. Design a pair of questions that are related to the same healthcare issue: one that is unbiased and another that would result in a bias in one direction or the other.   Examples: Do you think that the rate of type II diabetes diagnoses will increase over the next 10 years? Given the large increase in childhood obesity...
Using survey data to calculate statistics can be extremely valuable, but you must also make sure...
Using survey data to calculate statistics can be extremely valuable, but you must also make sure that the sample and questions are unbiased. Design a pair of questions that are related to the same healthcare issue: one that is unbiased and another that would result in a bias in one direction or the other. Examples: Do you think that the rate of type II diabetes diagnoses will increase over the next 10 years? Given the large increase in childhood obesity...
Using survey data to calculate statistics can be extremely valuable, but you must also make sure...
Using survey data to calculate statistics can be extremely valuable, but you must also make sure that the sample and questions are unbiased. Design a pair of questions that are related to the same healthcare issue: one that is unbiased and another that would result in a bias in one direction or the other. Examples: Do you think that the rate of type II diabetes diagnoses will increase over the next 10 years? Given the large increase in childhood obesity...
Using survey data to calculate statistics can be extremely valuable, but you must also make sure...
Using survey data to calculate statistics can be extremely valuable, but you must also make sure that the sample and questions are unbiased. Design a pair of questions that are related to the same healthcare issue: one that is unbiased and another that would result in a bias in one direction or the other.   Examples: Do you think that the rate of type II diabetes diagnoses will increase over the next 10 years? Given the large increase in childhood obesity...
Data Structure in Java The following java method counts how many triples of integers in an...
Data Structure in Java The following java method counts how many triples of integers in an array of n distinct integers sum to zero. public static int count(int[] a) { int n = a.length; int count = 0; for (int i = 0; i < n; i++) { for (int j = i+1; j < n; j++) { for (int k = j+1; k < n; k++) { if (a[i] + a[j] + a[k] == 0) count++; } } }...
Java- Create a new class named Forest that has a 2D array of integers to store...
Java- Create a new class named Forest that has a 2D array of integers to store a forest. Create a private inner class Position to store the row and column of the current cell. Create a Depth First Search Method that uses a stack to remove the current tree and search its neighbors based on the following pseudocode: // given a forest "f" // store positions on fire Stack<Position> cellsToExplore // the forest is aflame! for each tree in the...
Code in Java Create a stack class to store integers and implement following methods: 1) void...
Code in Java Create a stack class to store integers and implement following methods: 1) void push(int num): This method will push an integer to the top of the stack. 2) int pop(): This method will return the value stored in the top of the stack. If the stack is empty this method will return -1. 3) void display(): This method will display all numbers in the stack from top to bottom (First item displayed will be the top value)....
Program in Java Create a stack class to store integers and implement following methods: 1- void...
Program in Java Create a stack class to store integers and implement following methods: 1- void push(int num): This method will push an integer to the top of the stack. 2- int pop(): This method will return the value stored in the top of the stack. If the stack is empty this method will return -1. 3- void display(): This method will display all numbers in the stack from top to bottom (First item displayed will be the top value)....
Program in Java Create a queue class to store integers and implement following methods: 1- void...
Program in Java Create a queue class to store integers and implement following methods: 1- void enqueue(int num): This method will add an integer to the queue (end of the queue). 2- int dequeue(): This method will return the first item in the queue (First In First Out). 3- void display(): This method will display all items in the queue (First item will be displayed first). 4- Boolean isEmpty(): This method will check the queue and if it is empty,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT