Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

PLEASE WRITE THE CODE AS JAVA As you know, in Java integers have a range that ha

ID: 3887912 • Letter: P

Question

PLEASE WRITE THE CODE AS JAVA

As you know, in Java integers have a range that has a minimum value of 2.147.483.648 and a maximum value of 2.147.483.647, It is a pretty large range, but it is possible that one can encounter even larger numbers. In this lab, our aim is to write a computer program that is able to do arithmetic operations with even larger numbers. In this assignment, you are asked to implement a class called "Calculator It will contain a main method. It will contain a class called "BigNumber which stores a list of numbers (Java files can have more than one class, but only a single public class) The purpose of this exercise is: · To remember the programmings skills .To remember how to use arrays To remember how to use loops. Example: Take the number 56897531 Array of integers digits will be represented as BigNumber Data Declarations: intll digits: The array digits" is used to store a list of integers. Every integer is a digit of the BigNumber and it should be declared as private. int Sign: denotes the sign of the Number. A 0 denotes a positive number and a 1 denotes a negative number. Methods: Constructor 1: (BigNumber(String number)) First constructor Constructor 2: (BigNumber(intll digits)) Second constructor should take an array of integers as argument and assign it to digits variable. (When doing arithmetic operations, you would like to create a BigNumber from an array list, skipping keyboard input that is provided by constructor 1) int getLength: returns the number of elements in the Array List. Naturally corresponds to the number of digits the large number has. String toString): returns the number stored on Array List as well as the sign as human readable string to be used in print statements. int compareTo(BigNumber other): compares the big number the method is invoked from to the argument big number other. If they are equal, the method returns 0, if other big number is larger. it returns -1. otherwise it retuns 1 BigNumber add(BigNumber second): Adds the second BigNumber to the one method is called from. Returns the result as another BigNumber (Hint: use the second constructor to quickly create a BigNumber via an integer array). BigNumber subtract(BigNumber second): Subtracts two BigNumbers. Returns the result as another BigNumber Calculator Methods: main : The main method will contain a loop. Within this loop the user wil interact with the program. The program will ask the user to enter a command until the user enters special command Q, which will terminate the program. Following every command, the user will input two BigNumbers on different lines. Possible commands are FindMin: Find the minimum of the numbers and print it. FindMax: Find the maximum of numbers and print it. . Add: Find the sum of numbers and print it. .Sub: Find the result of X-Y and print it .Q: quits the program. Any other command will be responded with "Unknown Command, please enter a correct command

Explanation / Answer

// Program is not complete but can help you a lot

package chegg;

public class Calculator {

public static void main(String args[]){

}

}

class BigNumber{

private int [] digits;

int sign;

public BigNumber(String number){

}

public BigNumber(int [] digits){

this.digits = digits;

}

public int length(){

return digits.length;

}

public String toString(){

StringBuilder sb = new StringBuilder("");

sb.append(sign);

for(int number : digits){

sb.append(number);

}

return sb.toString();

}

public int compareTo(BigNumber number){

int len1 = this.digits.length;

int len2 = number.digits.length;

if(len1<len2){

return -1;

}

if(len1 > len2){

return 1;

}

for(int i = 0 ; i<len1; i++){

if(this.digits[i] < number.digits[i]){

return -1;

}

else if(this.digits[i] > number.digits[i]){

return 1;

}

}

return 0;

}

public BigNumber add(BigNumber number){

int newNumberLen = 0;

int len1 = number.digits.length;

// As after adding two number the new number can be max to 1 digit more to the bigger number

int len2 = this.digits.length;

if(len1 > len2){

newNumberLen = len1 +1;

}else{

newNumberLen = len2 +1;

}

int newNumber[] = new int[newNumberLen];

int carry = 0;

int i = len1-1;

int j = len2 - 1;

int k = newNumberLen - 1;

for(i= len1-1, j = len2 - 1;i>=0 && j>=0;i--,j--){

int dig = this.digits[i] + number.digits[j] + carry;

carry = 0;

if(dig > 9){

carry = dig%10;

dig = dig/10;

}

newNumber[k] = dig;

k--;

}

if(carry>0){

newNumber[k] = carry;

}

return new BigNumber(newNumber);

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote