Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I have this project for my java class. here is the problem: Big Integer Subtraction and Multiplication Implement the following methods: compareTo equals mul sub

I have this project for my java class. here is the problem:

Big Integer Subtraction and Multiplication

Implement the following methods:

compareTo

equals

mul

sub

Here is the code given to me by my teacher:

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; }

Thanks for the help. Also we are not suposed to use Math.BigIng methods.

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

Step: 3

blur-text-image

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

Object Oriented Databases Prentice Hall International Series In Computer Science

Authors: John G. Hughes

1st Edition

0136298745, 978-0136298748

More Books

Students also viewed these Databases questions