I was asked to implement an ADT that will replicate some of the capabilities of
ID: 3853560 • Letter: I
Question
I was asked to implement an ADT that will replicate some of the capabilities of Java’s BigInteger class. Belove is what I achieved so far, and i have no idea have to write the add() method, as well as the rest.
import java.math.BigInteger;
import java.util.ArrayList;
// Your class should behave as Java's BigInteger class does. The majority of
// the methods can be studied using Java's documentation.
public class MyBigInt {
private int bigInt;
long value;
String str;
public MyBigInt (){
}
public MyBigInt(long value){
this.value = value;
}
public MyBigInt(String str){
bigInt = Integer.parseInt(str);
}
public String toString() {
return String.format("%s;%s", value, str);
}
public MyBigInt add(MyBigInt other){
}
public MyBigInt subtract(MyBigInt other){
}
public MyBigInt negate(){
}
public int compareTo(MyBigInt other)
public MyBigInt max(MyBigInt other)
public MyBigInt min(MyBigInt other)
public int signum(){
}
public static MyBigInt valueOf(long value){
return new MyBigInt(value);
}
}
Explanation / Answer
Here is the completed code with output. I have written a test program also. Please rate the answer if it helped. Thank you.
MyBigInt.java
public class MyBigInt {
long value;
public MyBigInt() {
value = 0;
}
public MyBigInt(long value) {
this.value = value;
}
//initialize the object using string input.
public MyBigInt(String str) {
value = 0;
int digit;
char digits[] = str.toCharArray();
//convert the characters by multiplying their numeric value with proper power of 10
for (int power = 0, idx = digits.length - 1; idx >= 0; idx--, power++) {
digit = digits[idx] - '0';
value += digit * Math.pow(10, power);
}
}
public MyBigInt add(MyBigInt other) {
return new MyBigInt(value + other.value);
}
public MyBigInt subtract(MyBigInt other) {
return new MyBigInt(value - other.value);
}
public MyBigInt negate() {
return new MyBigInt(-value);
}
public int compareTo(MyBigInt other) {
if (value < other.value)
return -1;
else if (value > other.value)
return 1;
else
return 0;
}
public MyBigInt max(MyBigInt other) {
if (value > other.value)
return this;
else
return other;
}
public MyBigInt min(MyBigInt other) {
if (value < other.value)
return this;
else
return other;
}
public int signum() {
if (value < 0)
return -1;
else if (value == 0)
return 0;
else
return 1;
}
public static MyBigInt valueOf(long value) {
return new MyBigInt(value);
}
public String toString() {
return "" + value;
}
}
TestMyBigInt.java
public class TestMyBigInt {
public static void main(String[] args) {
MyBigInt zero = new MyBigInt();
MyBigInt hundred = new MyBigInt(100);
MyBigInt fifty = new MyBigInt("50");
System.out.println("zero = " + zero);
System.out.println("hundred = " + hundred);
System.out.println("fifty = " + fifty);
MyBigInt sum = fifty.add(hundred);
MyBigInt diff = fifty.subtract(hundred);
System.out.println("fifty.add(hundred) = "+ sum);
System.out.println("fifty.subtract(hundred) = " + diff);
System.out.println("hundred.negate() = " + hundred.negate());
System.out.println("hundred.max(fifty) = " + hundred.max(fifty));
System.out.println("hundred.min(fifty) = " + hundred.min(fifty));
System.out.println("hundred.signum() = " + hundred.signum());
System.out.println("fifty.compareTo(zero) = " + fifty.compareTo(zero));
System.out.println("fifty.compareTo(hundred) = " + fifty.compareTo(hundred));
}
}
output
zero = 0
hundred = 100
fifty = 50
fifty.add(hundred) = 150
fifty.subtract(hundred) = -50
hundred.negate() = -100
hundred.max(fifty) = 100
hundred.min(fifty) = 50
hundred.signum() = 1
fifty.compareTo(zero) = 1
fifty.compareTo(hundred) = -1
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.