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):
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.
public void setValue (long value): Same as previous method; except the input type is long integer.
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.
public HugeInt clone (): creates a copy of the integer stored in the linked list and returns the result as another HugeInt.
public long log (): counts and returns the number of digits stored in the linked list.
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.
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.
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.
public HugeInt add (HugeInt h): adds h with the integer stored in the linked list and returns the result as another HugeInt.
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
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.
In this assignment, you are asked to implement a Java class named HugeInt for storing huge...
***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...
Copy the program DigitPlay.java and implement the TBI (To Be Implemented) method named play(Result r). Your program cannot contain any import statements. Your play(Result r) method cannot call any other methods. The output of your program must match the following. number: 5 # of digits: 1 smallest digit: 5 largest digit: 5 digit range: 0 digit sum: 5 digit average: 5.0 digit product: 5 number: 42 # of digits: 2 smallest digit: 2 largest digit: 4 digit range: 2 digit...
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 (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...
This class implements a doubly linked list in which the nodes are of the class DLNode. This class must implement the interface DLListADT.java that specifies the public methods in this class. The header for this class will then be public class DLList implements DLListADT This class will have three private instance variables: • private DLNode front. This is a reference to the first node of the doubly linked list. • private DLNode rear. This is a reference to the last...
*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 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:...
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
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...
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...