Question

In: Computer Science

In Java We define BigNumber a number consisting of 0<N<21 elements.It can be negative in which...

In Java

We define BigNumber a number consisting of 0<N<21 elements.It can be negative in which case it will have a minus sign in front. Without modifying main method complete the methods add, subtract and reverse. Hints: class java.math.BigInteger would be very useful. For reverse method number== new StringBuffer(number).reverse().toString(); can be used however when you reverse a negative number the negative sign should stay in front. Thanks.

public class MD3 {

public static void main(String[] args)
{
BigNumber BigNumber1 = new BigNumber(args[0]);
BigNumber BigNumber2 = new BigNumber(args[1]);

BigNumber1.add(BigNumber2);
BigNumber1.display();
BigNumber1.reverse();
BigNumber1.display();

BigNumber2.subtract(BigNumber1);
BigNumber2.display();
BigNumber2.reverse();
BigNumber2.display();
}

class BigNumber {

private String number;
  
BigNumber(String str) { number = str; }

public void add(BigNumber sk) { /* ... */ }

public void subtract(BigNumber sk) { /* ... */ }
  
public void reverse() { /* ... */ }


  


  
public void display() {System.out.println(number);}
}
}

Solutions

Expert Solution

SOURCE CODE:

*Please follow the comments to better understand the code.

**Please look at the Screenshot below and use this code to copy-paste.

***The code in the below screenshot is neatly indented for better understanding.


import java.math.BigInteger;
public class MD3 {

    public static void main(String[] args) {
        BigNumber BigNumber1 = new BigNumber(args[0]);
        BigNumber BigNumber2 = new BigNumber(args[1]);

        BigNumber1.add(BigNumber2);
        BigNumber1.display();
        BigNumber1.reverse();
        BigNumber1.display();

        BigNumber2.subtract(BigNumber1);
        BigNumber2.display();
        BigNumber2.reverse();
        BigNumber2.display();
    }
}
    class BigNumber {

        private String number;

        BigNumber(String str) { number = str; }

        public void add(BigNumber sk)
        {
            // convert number to BigInteger
            BigInteger number1 = new BigInteger(number);
            // add it
            number1 = number1.add(new BigInteger(sk.number));
            // store in number variable
            number = number1.toString();
        }

        public void subtract(BigNumber sk)
        {
            // convert number to BigInteger
            BigInteger number1 = new BigInteger(number);
            // subtract it
            number1 = number1.subtract(new BigInteger(sk.number));
            // store in number variable
            number = number1.toString();
        }

        public void reverse()
        {
            number= new StringBuffer(number).reverse().toString();
        }

       public void display() {System.out.println(number);}
    }

==============

CODE:



Related Solutions

In Java In mathematics, factorial of a non-negative integer n, denoted by n! , is the...
In Java In mathematics, factorial of a non-negative integer n, denoted by n! , is the product of all positive integers less than or equal to n. For example, 5! = 5 * 4 * 3 * 2* 1 = 120 3! = 3 * 2 * 1 = 6 2! = 2 * 1 = 2 The value of 0! is 1. Write a program that asks user to enter an integer > 0; if a valid value is...
Which of the following can have a negative impact on a customer's profitability? a. Number of...
Which of the following can have a negative impact on a customer's profitability? a. Number of required sales contacts (phone calls, visits etc.). b. Special shipping instructions. c. Accounts receivable collection time. d. Purchase order amendments. e. All the above.
Let A be an n × n matrix which is not 0 but A2 = 0....
Let A be an n × n matrix which is not 0 but A2 = 0. Let I be the identity matrix. a)Show that A is not diagonalizable. b)Show that A is not invertible. c)Show that I-A is invertible and find its inverse.
Given the following Scheme definition: (define x '(define (fac n) (if (= n 0) 1 (*...
Given the following Scheme definition: (define x '(define (fac n) (if (= n 0) 1 (* n (fac (- n 1)))))) (This does not define the factorial function fac, but the variable x.) Write Scheme expressions in terms of x that would have the effect of extracting the following expressions: (fac n) 0 (- n 1) The second occurrence of fac. The last occurrence of n. E.g., the expression (car x) would extract define.
Define a PDA that accepts language L3 = { anbm | n > 0 and n...
Define a PDA that accepts language L3 = { anbm | n > 0 and n > m }. Implement that PDA in JFLAP. Must use JFLAP
In Java: The n^th Harmonic number is the sum of the reciprocals of the first n...
In Java: The n^th Harmonic number is the sum of the reciprocals of the first n natural numbers: H(n) = 1+ 1/2 + 1/3 +1/4 +... +1/n Write a recursive method and an accompanying main method to compute the n^th Harmonic number. I have tried but get a blank and would really apreciate a fully explained code.
Please write a JAVA program which reads a number n from the user and check whether...
Please write a JAVA program which reads a number n from the user and check whether n is a perfect number or not. For example, when n = 7, the print out should be 7 is not a perfect number. If the input n is 6, then the program prints out 6 = 1 * 2 * 3
In Java please What happens if you call factorial( with a negative value of n? With...
In Java please What happens if you call factorial( with a negative value of n? With a large value of say 35? Write a recursive function that takes an integer n as its argument and returns ln(n!)
Fibonacci Sequence in JAVA f(0) = 0, f(1) = 1, f(n) = f(n-1) + f(n-2) Part...
Fibonacci Sequence in JAVA f(0) = 0, f(1) = 1, f(n) = f(n-1) + f(n-2) Part of a code public class FS { public static void main(String[] args){ IntSequence seq = new FibonacciSequence(); for(int i=0; i<20; i++) { if(seq.hasNext() == false) break; System.out.print(seq.next()+" "); } System.out.println(" "); } } ///Fill in the rest of the code! Output 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 You should define...
In game 77, a 7-digit winning number consisting of the digits 0 to 9 is drawn...
In game 77, a 7-digit winning number consisting of the digits 0 to 9 is drawn one after the other (they put the numbers back in the bag before drawing another number) and their digits are arranged in the order of the draw. What is the probability that the numbers drawn in non-descending sequence, i.e. the number of a drawing is at least as large as the number of the previous drawing? Also calculate this probability for the general case...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT