Question

In this assignment, you are asked to implement a Java class named HugeInt for storing huge...

In this assignment, you are asked to implement a Java class named HugeInt for storing huge integers and performing mathematical operations on them. This class must have a private variable of type LinkedList〈Byte〉 which stores digits of a huge integer and ten of the following public methods (choose ten methods out of 12):

  1. public void setValue (int value): gets an integer (like 1234) as input parameter and stores its digits in the linked list in the same order as they appear in the input integer; e.g. for input equal to 1234, digit 1 is placed at the beginning of the list; while digit 4 is the placed at the end.

  2. public void setValue (long value): Same as previous method; except the input type is long integer.

  3. public int getValue (): returns −1 if the number stored in the linked list is too large to be fit in a Java int variable and returns the integer value of the stored number otherwise.

  4. public HugeInt clone (): creates a copy of the integer stored in the linked list and returns the result as another HugeInt.

  5. public long log (): counts and returns the number of digits stored in the linked list.

  6. public HugeInt modExp (long n): calculates the remainder of the stored integer when

    dividing it by 10n and returns the result in the form of another HugeInt.

  7. public HugeInt quotientExp (long n): calculates the quotient of the integer stored in the linked list when dividing it by 10n and returns the result in the form of another HugeInt.

  8. public HugeInt timesExp (long n): calculate the product of 10n and the integer stored in the linked lists and returns the result in the form of another HugeInt.

  9. public HugeInt add (HugeInt h): adds h with the integer stored in the linked list and returns the result as another HugeInt.

  10. public void addSet (HugeInt h): adds h with the integer stored in the linked list and writes the result back on the linked list.

1

11. public int recursiveModNine (): returns the remainder of the integer stored in the linked list when dividing it by 9. The easiest way to find the remainder of an integer like 235 when dividing it by nine is to calculate the remainder of 2+3+5 instead. Use this idea to implement recursiveModNine method in a recursive manner. Method recur- siveModNine must be able to calculate the remainder of a huge integer like 1011111 − 1 when dividing it by 9. For example, recursiveModNine (1011111 − 1) first sums the digits of the input integer and then calls itself for the calculated sum. It does the same operations recursively until it reaches a one-digit integer. See below for details:

recursiveModeNine (1011111 − 1) =recursiveModeNine (99 . . . 9)? ?? ?

11111 times =recursiveModeNine (99999)

=recursiveModeNine (45) =recursiveModeNine (9) =0;

12. public int recursiveModThree (): returns the remainder of the integer stored in the linked list when dividing it by 3. To implement this method, use the same idea men- tioned for the previous method.

Submissions

You need to submit a .zip file compressing the Java source file(s) of your program (.javafiles).

2

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

HugeInt Class

import java.util.LinkedList;

import java.lang.Math.*;

public class HugeInt{

private LinkedList<Integer> list = new LinkedList<>();

public LinkedList<Integer> getList() {
   return list;
}

public void setList(LinkedList<Integer> list) {
   this.list = list;
}

/*gets an integer (like 1234) as input parameter and stores its digits in the linked list
* in the same order as they appear in the input integer; e.g. for input equal to 1234,
*/
public void setValue(int value) {
  
   String str = Integer.toString(value);
  
   int length = str.length();
  
   int i =0;
  
   this.list.clear();
  
   while(i<length) {
      
       this.list.add(Character.getNumericValue(str.charAt(i)));
       i++;
      
   }
  
}

/*gets an long (like 1234) as input parameter and stores its digits in the linked list
* in the same order as they appear in the input long; e.g. for input equal to 1234,
*/

public void setValue(long value) {
  
   String str =Long.toString(value);
  
   int length = str.length();
  
   int i =0;
  
   this.list.clear();
  
   while(i<length) {
      
       this.list.add(Character.getNumericValue(str.charAt(i)));
       i++;
      
   }
  
}

/* This Method will return the long from the digits stored in linkedlist */

public long getInteger() {
  
   int len = this.list.size();
   long num = 0;
   int i=0;
  
   while(i<len) {
      
       num = num + (long)(this.list.get(i)*Math.pow(10,len-i-1));
       i++;
      
   }
   return num;
  
}


/*returns −1 if the number stored in the linked list is too large to be fit in a Java
int variable and returns the integer value of the stored number otherwise.*/

public int getValue() {
  
   int len = this.list.size();
   long num = 0;
   int i=0;
  
   while(i<len) {
      
       num = num + (long)(this.list.get(i)*Math.pow(10,len-i-1));
       i++;
      
   }
  
   if(num>2147483647)
       return -1;
   else
       return (int) num;
  
  
}


public HugeInt clone() {
  
   return this;
}

public long log() {
  
   long count = this.list.size();
  
   return count;
}

public HugeInt modExp(long n) {
  
   long divisor = 10 * n;
   long num = getInteger();
   LinkedList<Integer> list1 = new LinkedList<>();
   HugeInt newhugeint = new HugeInt();
   long result = num%divisor;
  
String str = Long.toString(result);
  
   int length = str.length();
  
   int i =0;
  
  
   while(i<length) {
      
       list1.add(Character.getNumericValue(str.charAt(i)));
       i++;
      
   }
  
   newhugeint.setList(list1);
  
   return newhugeint;
  
}

public HugeInt quotientExp(long n) {
  
   long divisor = 10 * n;
   long num = getInteger();
   LinkedList<Integer> list1 = new LinkedList<>();
   HugeInt newhugeint = new HugeInt();
   long result = num/divisor;
  
String str = Long.toString(result);
  
   int length = str.length();
  
   int i =0;
  
  
   while(i<length) {
      
       list1.add(Character.getNumericValue(str.charAt(i)));
       i++;
      
   }
  
   newhugeint.setList(list1);
  
   return newhugeint;
  
}

public HugeInt timesExp(long n) {
  
   long multiplier = 10 * n;
   long num = getInteger();
   LinkedList<Integer> list1 = new LinkedList<>();
   HugeInt newhugeint = new HugeInt();
   long result = num * multiplier;
  
String str = Long.toString(result);
  
   int length = str.length();
  
   int i =0;
  
  
   while(i<length) {
      
       list1.add(Character.getNumericValue(str.charAt(i)));
       i++;
      
   }
  
   newhugeint.setList(list1);
  
   return newhugeint;
  
}

public HugeInt add(HugeInt h) {
  
   long num1 = h.getInteger();
   long num = getInteger();
   LinkedList<Integer> list1 = new LinkedList<>();
   HugeInt newhugeint = new HugeInt();
   long result = num + num1;
  
String str = Long.toString(result);
  
   int length = str.length();
  
   int i =0;
  
  
   while(i<length) {
      
       list1.add(Character.getNumericValue(str.charAt(i)));
       i++;
      
   }
  
   newhugeint.setList(list1);
  
   return newhugeint;
  
}

public void addset(HugeInt h) {
  
   long num1 = h.getInteger();
   long num = getInteger();

   long result = num + num1;
  
String str =Long.toString(result);
  
   int length = str.length();
  
   int i =0;
  
   this.list.clear();
  
   while(i<length) {
      
       this.list.add(Character.getNumericValue(str.charAt(i)));
       i++;
      
   }
  

}

}

I tried to write the driver class Which was not asked in the question. Just to help you get it more.

If you want more driver code to drive HugeInt class. Just comment the requirement.

package com.mindtree.sample;

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

public class HugeIntDriver {

   public static void main(String[] args) {
       // TODO Auto-generated method stub
      
       Scanner sc = new Scanner(System.in);
      
       HugeInt hugeint = new HugeInt();
      
      
       // Driver code for setValue(int value) Method
      
       System.out.println("Enter an integer");
      
       int intvalue = sc.nextInt();
      
       hugeint.setValue(intvalue);
      
       LinkedList<Integer> list1 = hugeint.getList();
      
       for(int i : list1)
           System.out.print(i + " ");
      
      
       // Driver code for setValue(long value) Method
      
       System.out.println("\nEnter an Long integer");
      
long longvalue = sc.nextLong();
      
       hugeint.setValue(longvalue);
      
       list1 = hugeint.getList();
      
       for(int i : list1)
           System.out.print(i + " ");
      
      
       // Driver code for getValue Method
      
       intvalue = hugeint.getValue();
      
       System.out.println("\ngetValue method returned value is " + intvalue);
      
      
       // Driver code for clone() Method
       HugeInt hugeint1 = hugeint.clone();
      
       list1 = hugeint1.getList();
      
       for(int i : list1)
           System.out.print(i + " ");
      
      
       // Driver code for log() Method
       long count = hugeint.log();
      
       System.out.println("\nCount of digits in linked list is " + count);
      
      
      
      
      
      

   }

}

If you left with any doubt feel free to ask.

Add a comment
Know the answer?
Add Answer to:
In this assignment, you are asked to implement a Java class named HugeInt for storing huge...
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
  • ***JAVA: Please make "Thing" movies. Objective In this assignment, you are asked to implement a bag...

    ***JAVA: Please make "Thing" movies. Objective In this assignment, you are asked to implement a bag collection using a linked list and, in order to focus on the linked list implementation details, we will implement the collection to store only one type of object of your choice (i.e., not generic). You can use the object you created for program #2 IMPORTANT: You may not use the LinkedList class from the java library. Instead, you must implement your own linked list...

  • Java, Please implement the way the interface specifies. Part A:Radix Sort Implement a radix sort as...

    Java, Please implement the way the interface specifies. Part A:Radix Sort Implement a radix sort as described in the last section of Chapter 7 in your text. It should handle variable amounts of data and variable numbers of digits in the key values. Use testing to ensure radix sort works for at least three examples of different input sizes and various max digit length. I need to implement the radix sort using the below java interface. /** * <h1><LeastSignificantDigit Radix...

  • Implement the following class in Java: Class name: People Constructor Summary: public People (Str...

    Implement the following class in Java: Class name: People Constructor Summary: public People (String name) Methods: public People(String name) public void setName(String name) public String getName() Class name: Truck Constructor Summary: public Truck (int licensePlate, int onBoard, People people) Initially, the truck does not contain any on board the vehicle. If the number of people on board the vehicle is a negative number, the value of onBoard should be stored as zero. Methods: public int getLicensePlate() Returns license plate of...

  • *Java* Hi. I need some help with creating generic methods in Java. Write a program GenMethods...

    *Java* Hi. I need some help with creating generic methods in Java. Write a program GenMethods that has the following generic methods: (1) Write the following method that returns a new ArrayList. The new list contains the nonduplicate (i.e., distinct) elements from the original list. public static ArrayList removeDuplicates(ArrayList list) (2) Write the following method that shuffles an ArrayList. It should do this specifically by swapping two indexes determined by the use of the random class (use Random rand =...

  • in java Write a class named Palindrome.java and Write a method isPalindrome that takes an IntQueue...

    in java Write a class named Palindrome.java and Write a method isPalindrome that takes an IntQueue as a parameter and that returns whether or not the numbers in the queue represent a palindrome (true if they do, false otherwise). A sequence of numbers is considered a palindrome if it is the same in reverse order. For example, suppose a Queue called q stores this sequence of values: front [3, 8, 17, 9, 17, 8, 3] back Then the following call:...

  • Using Java You are given a Node class and a List class: public class Node {...

    Using Java You are given a Node class and a List class: public class Node {    int   data;     Node next; } public class List {     Node first; } You are also given a Stack class. The following functions are available for use: public class Stack { public boolean isEmpty(){}; public void push(int n){}; public int pop(){};} Write a Java method snglyRevToStck that pushes the data found in a linked list t in reverse order into the stack...

  • Java. Can’t use for or while loops, thank you! Practice by writing a recursive method that...

    Java. Can’t use for or while loops, thank you! Practice by writing a recursive method that returns the number of digits in the integer passed to it as an argument of type int. Allow for both positive and negative arguments. For example, -120 has three digits. Hint: Dividing a number by 10 removes the last digit from the number

  • In Java programming language Please write code for the 6 methods below: Assume that Student class...

    In Java programming language Please write code for the 6 methods below: Assume that Student class has name, age, gpa, and major, constructor to initialize all data, method toString() to return string reresentation of student objects, getter methods to get age, major, and gpa, setter method to set age to given input, and method isHonors that returns boolean value true for honors students and false otherwise. 1) Write a method that accepts an array of student objects, and n, the...

  • Please answer this programming question as fast as possible. Will upvote Predict the output of this...

    Please answer this programming question as fast as possible. Will upvote Predict the output of this next question. ookmarks People /courses/34463/quizzes/213587/take 15 pts Question 1 Let class Hugelnt be implemented in the following way. In this class, digits is a linked list that contains the digits of an integer that can be very large and have thousands of digits public class Hugelnt public LinkedList (Integer) digits; public Hugelnt0 digits - new LinkedList (Integer) O: public void printWithThousandsSeperator) // your code...

  • a Java code Complete the provided code by adding a method named sum() to the LinkedList...

    a Java code Complete the provided code by adding a method named sum() to the LinkedList class. The sum() method should calculate the sum of all of the positive numbers stored in the linked list. The input format is the number of items in the list, followed by each of the items, all separated by spaces. Construction of the linked list is provided in the template below. The output should print the sum of the positive values in the list....

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
ADVERTISEMENT