Question
There is a Java exercise here on lists Do not use Java packages This exercise has a practical problem, numbers are known to be stored
There is a Java exercise here on lists
Do not use Java packages
This exercise has a practical problem, numbers are known to be stored in memory cells
The Int or Long type is limited in size due to the size of the memory. But sometimes you have to use certain applications in very large numbers, such as: 423,158,635,544,245. This number cannot be kept in a long memory cell because of its large size.
One way to deal with saving very large numbers is through a linked list. Therefore we can define a linked list in which each member will contain one digit from the number, as long as the number itself is not stored as a number but as a collection of digits one after the other.
In this exercise I need to set up a linked list for saving very large numbers.
In this exercise the use of positive and zero numbers (not negative numbers).
And the linked listing must be one-way !!
Please pay attention to all parts of the exercise
Please help me write Class BigDidit with API
In the BigDidit class. This class represents a linked list of digits that represent a very large number.
I have a Node class that defines the basic link of the list.
And also has a List class to build the BigDidit class
Attention should be paid to the representation of the number. Because this is a linked list. There are two options to represent the number from end to beginning or from beginning to end
One should choose here the best option and consider which representation is better in terms of transitions on the list. And take into account that the arithmetic operations on integers usually begin with a calculation on the rightmost digits.
It is possible to decide on the internal structure of the class alone, but the interface of the class must contain only the methods available in the BigDidit class
Write to the BigDidit class
3 constructors:
1. A constructor 1 - An empty builder that initializes the list to number 0,
That is, a list that contains one member whose number is 0
2. A constructor 2 - that receives as a parameter a long type and stores it in a list format.
3. A constructor 3 - Copy builder
Methods:
Because the class represents integers and so we can easily perform various arithmetic operations on the numbers.
Therefore the following methods must be defined
1. public String toString ()
The method should return as a string the number represented in the list.
The string contains the number in its usual form, that is, the most significant digit will appear first, followed by the rest of the digits.
The method must be implemented at time O (n) when n is the number of digits in the number. Do not create new lists, arrays or make the list a number. This method must be practiced in recursion.
2. public int compareTo (BigDigit other)
A method of comparing two large numbers. If the number on which the method is applied is smaller than the number obtained as a parameter, the value -1 will be returned. If the number on which the method is applied is greater than the number obtained as a parameter, the value 1 will be returned,
If even numbers are returned the value 0.
3. public BigDigit addBigDigit (BigDigit other)
The method of connecting two large numbers. The connection returns a new BigDigit object
4. public BigDigit addLong (long num)
The method of connecting a large number object and another long number. The connection returns a new BigDigit object
5. public BigDigit subtractBigDigit (BigDigit other)
The subtraction method of two large numbers. The subtraction is of the larger number of the two less the smaller number. Returns as a result a new BigDigit object. Check which is greater than the two numbers (so that the result is not a negative number)
6. public BigDigit multBigDigit (BigDigit other)
The multiplication method of two large numbers. The multiplication returns as a result a new BigDigit object.
In addition, subtraction and multiplication methods. The methods will not change the object on which they were applied nor the object passed as a parameter, but will create a new object that will contain the solution of the exercise
All actions must be as effective as possible in terms of transitions on the list (if it is possible to manage with only one passage on the list this must be done)
In all the methods in the exercise it is forbidden to turn the number represented by the list into a real number, perform the actions on it and then turn it back into a list - why ????
(write me answer)
Arrays must not be used !!!
No methods can be added to the above interface, but only private methods can be added
Please write me an API documentation.
And what are the time complications and the place complications of each method !!!
And the most effective methods should be written.
Class Node:
public class Node
{
private int _value;
private Node _next;
/**
* Constructor for objects of class Node
*/
public Node(int val){
_value = val;
_next = null;
}
public Node(int val, Node next){
_value = val;
_next = next;
}
public int getValue(){
return _value;
}
public void setValue(int v){
_value = v;
}
public Node getNext(){
return _next;
}
public void setNext(Node node){
_next = node;
}
}// end of class Node
Class List:
public class List
{
private Node _head;
/**
* Constructor for objects of class List
*/
public List(){
_head = null;
}
public List(Node node){
_head = node;
}
public boolean empty(){
return _head == null;
}
public Node nextElement(Node node){
return node.getNext();
}
public int getValueOfNode(Node node){
return node.getValue();
}
}
Tester:
public class Tester {
public static void main(String[] args) {
System.out.println("***** b1 = new BigDidit() = ");
BigDidit b1 = new BigDidit();
System.out.println(b1);
System.out.println(" ***** b2 = new BigDidit(1234567895432L) = ");
BigDidit b2 = new BigDidit(1234567895432L);
System.out.println(b2);
System.out.println(" ****** b3 = new BigDidit(b2) = ");
BigDidit b3 = new BigDidit(b2);
System.out.println(b3);
System.out.println(" ****** b4 = b3.addLong(123456789L) = ");
BigDidit b4 = b3.addLong(123456789L);
System.out.println(b4);
System.out.println(" ****** b5 = b3.addBigDidit(b4) = ");
BigDidit b5 = b3.addBigDidit(b4);
System.out.println(b5);
System.out.println(" ****** b6 = b4.subtractBigDidit(b2) = ");
BigDidit b6 = b4.subtractBigDidit(b2);
System.out.println(b6);
System.out.println(" ****** comp = b2.compareTo(b3) = ");
int comp = b2.compareTo(b3);
System.out.println(comp);
System.out.println(" ****** b7 = b2.multBigDidit(b4) = ");
BigDidit b7 = b2.multBigDidit(b4);
System.out.println(b7);
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started