Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Exception Handling - Java Implement the following program with exception handling. ----------------------------MyNumber.java------------------------------------------------------ import java.util.Arrays; public class MyNumber { int digits[] = new int[30]; //30 digit

Exception Handling - Java

Implement the following program with exception handling.

----------------------------MyNumber.java------------------------------------------------------

import java.util.Arrays;

public class MyNumber { int digits[] = new int[30]; //30 digit integers int size; public void parse(String str) //Function used to transfer the string into a integers { size = str.length(); for(int i = 0; i < size; i++) { digits[i] = str.charAt(i) - '0'; } } public String toString() { String s = " "; for(int i = 0; i < size; i++) { s+=digits[i]; } return s; } public static MyNumber add(MyNumber a, MyNumber b) //Used to add the two integers { if(a.isGreaterThan(b)) { return addMath(a,b); } return addMath(b,a); } public static MyNumber addMath(MyNumber a, MyNumber b)//Used to find and store the sum of the two integers { MyNumber addition = new MyNumber(); int num1[] = a.digits; int num2[] = b.digits; int sum[] = new int[a.size + 1]; int i = a.size - 1, j = b.size - 1,k = a.size - 1; int carry = 0, s = 0; while( j>= 0) //Finds the sum of the element of each array { s = num1[i] + num2[j] + carry; sum[k] = (s % 10); carry = s / 10; //Finds the carry for the next sum of the following two numbers k--; i--; j--; } while (i >= 0) { //Adds the sum of the first array integers s = num1[i] + carry; sum[k] = (s % 10); carry = s / 10; i--; k--; } if(carry == 1) //Shift each digit to the right and then add the carry { for(int p = 1; p<=k;p++) { num1[p] = sum[p-1]; } num1[0] = carry; addition.digits = num1; addition.size = a.size + 1; } else { addition.digits = sum; addition.size = sum.length; } return addition; //storing of the sum } public static MyNumber subtract(MyNumber a, MyNumber b) //Used to subtract the two integers { if(a.isGreaterThan(b)) { return subtractMath(a,b); } else { return subtractMath(b,a); } } public static MyNumber subtractMath(MyNumber a, MyNumber b) //Mathematical function to find and store the subtraction of the two integers { MyNumber subtraction = new MyNumber(); int num1[] = a.digits; int num2[] = b.digits; int diff[] = new int[a.size]; int i = a.size - 1, j = b.size - 1, k = a.size - 1; int borrow = 0; while(j>=0) //Previous digits were subtracted using the borrow { if((num1[i] - borrow) < num2[j]) { diff[k] = num1[i] + (10 - borrow) - num2[j]; borrow = 1; } else { diff[k] = (num1[i] - borrow) - num2[j]; borrow = 0; } i--; j--; k--; } while(i>=0) //IF the second number is smaller { if(num1[i] == 0 && borrow != 0) { diff[k] = 9; } else { diff[k] = num1[i] - borrow; borrow = 0; } i--; k--; } subtraction.digits = diff; subtraction.size = diff.length; return subtraction; //Sends the subtraction total } public boolean isEqualTo(MyNumber b) //Used to compare the two integers if it is equal returns true, if not, false { if(Arrays.toString(this.digits).compareTo(Arrays.toString(b.digits)) == 0) return true; else return false; } public boolean isNotEqualTo(MyNumber b) //Used to compare the two integers if they are not equal, returns either true or false { return !isEqualTo(b); } public boolean isGreaterThan(MyNumber b) //Used to compare the two integers, if greater than returns true, if not returns false { if(this.size > b.size) { return true; } if(this.size < b.size) { return false; } if(Arrays.toString(this.digits).compareTo(Arrays.toString(b.digits)) > 0) { return true; } return false; } public boolean isLessThan(MyNumber b) //Used to compare integers if A is less than B, returns true, if not, returns false { return !isGreaterThan(b); }

}

--------------------------------------MyNumberTest.java-------------------------------------------------

public class MyNumberTest {

/** * @param args the command line arguments */ public static void main(String[] args) { String str1 = "834546578437123456787867876356"; //String 1 String str2 = "657483456787689876787653645676"; //String 2 MyNumber num1 = new MyNumber(); MyNumber num2 = new MyNumber(); num1.parse(str1); //Parse Method num2.parse(str2); //Parse Method System.out.println("Number 1 = "); //Printing of first number System.out.println(num1); System.out.println("Number 2 = "); //Printing of second number System.out.println(num2); System.out.println("Number 1 is Equal to Number 2 = " + num1.isEqualTo(num2)); //Printing of Equal to method System.out.println("Number 1 is Not Equal to Number 2 = "+ num1.isNotEqualTo(num2)); //PRinting of not Equal to method System.out.println("Number 1 is Greater than Number 2 = "+ num1.isGreaterThan(num2)); //Prining of Greater than methof System.out.println("Number 1 is Less than Number 2 = " + num1.isLessThan(num2)); //Printing of less than method System.out.println("Addition Results = "); //Could not get the addition to work with the amount of integers, kept getting errors System.out.println("Subtraction Results = "); //Subtraction results System.out.println(MyNumber.subtract(num1, num2)); } }

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

SQL Antipatterns Avoiding The Pitfalls Of Database Programming

Authors: Bill Karwin

1st Edition

1680508989, 978-1680508987

More Books

Students also viewed these Databases questions

Question

Transcribed image text

Answered: 1 week ago