Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hello, the languge is java. I am having trouble with this assignment: If you are able to provide a small explanation for this assignment, it

Hello, the languge is java. I am having trouble with this assignment:

If you are able to provide a small explanation for this assignment, it would be great. Thank you! Big Integer Subtraction and Multiplication Implement the following methods:

compareTo equals mul sub

We are supposed to use BIG Integer method with this MAIN code that has already been given to us.

public class Main {

public static void main(String[] args) {

BigInt a = new BigInt(args.length == 2 ? args[0] : "9999"); BigInt b = new BigInt(args.length == 2 ? args[1] : "1999");

System.out.println(a + (a.equals(b) ? " equals " : " does not equal ") + b);

System.out.println(a + (a.compareTo(b) < 0 ? " < " : (a.compareTo(b) > 0 ? " > " : " = ")) + b);

System.out.println(a + " + " + b + " = " + a.add(b)); if (a.compareTo(b) >= 0) { System.out.println(a + " - " + b + " = " + a.sub(b)); } System.out.println(a + " * " + b + " = " + a.mul(b)); if (a.compareTo(b) >= 0) { System.out.println(a + " / " + b + " = " + a.div(b)); } } }

final class BigInt implements Comparable {

public BigInt() {

n = new int[1]; }

public BigInt(String s) {

n = new int[s.length()];

for (int i = 0; i < n.length; ++i) { n[n.length - i - 1] = s.charAt(i) - '0'; }

n = trim(n); }

private BigInt(int[] n) {

this.n = new int[n.length];

for (int i = 0; i < n.length; ++i) { this.n[i] = n[i]; } }

public BigInt add(BigInt o) {

int carry = 0; int max = n.length > o.n.length ? n.length : o.n.length; int[] result = new int[max+1];

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

int top = i < n.length ? n[i] : 0; int bot = i < o.n.length ? o.n[i] : 0;

result[i] = (top + bot + carry) % 10; carry = (top + bot + carry) / 10; }

return new BigInt(trim(result)); }

public int compareTo(BigInt o) {

return 0; }

public BigInt div(BigInt o) {

return null; }

public boolean equals(Object o) {

return false; }

public BigInt mul(BigInt o) {

return null; }

public BigInt sub(BigInt o) {

return null; }

public String toString() {

String s = "";

for (int i : n) { s = i + s; }

return s; }

private int[] trim(int[] nums) {

int size = nums.length;

for (int i = nums.length - 1; i > 0; --i) { if (nums[i] != 0) { break; } --size; }

int[] res = new int[size];

for (int i = 0; i < size; ++i) { res[i] = nums[i]; }

return res; }

private int[] n; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Design Application Development And Administration

Authors: Michael V. Mannino

3rd Edition

0071107010, 978-0071107013

More Books

Students also viewed these Databases questions

Question

What are the various types of investments?

Answered: 1 week ago