Hi there, I need help with two methods: *One that does the division between the
ID: 3545729 • Letter: H
Question
Hi there, I need help with two methods:
*One that does the division between the numbers
* & one that returns the mod
The program's goal is to do these math operations without using the BigInt class from java. Thats the reason why
I use ArrayLists to do it.
Copy the program to your java editor to follow with further instructions.
NOTE: I will fully test the code and then I will award best answer. Do not waste my time or yours. Thanks.
import java.util.*;
public class NumberOperations {
public static void main(String[] args){
ArrayList<Integer> biggerNumber = new ArrayList<Integer>();
ArrayList<Integer> smallerNumber = new ArrayList<Integer>();
ArrayList<Integer> operatedNumber = new ArrayList<Integer>();
//just make the program work with big numbers like this.
//thats the whole purpose: handeling big numbers.
String num1 = "123456789101112";
String num2 = "12345678910";
//I thought about storing the numbers backwards into the arraylist
//for a better handeling of the number, feel free to do it as you pleased.
for(int i = num1.length() -1; i>= 0; i --){
char numberChar = num1.charAt(i);
String numberString = ""+numberChar;
biggerNumber.add(Integer.parseInt(numberString));
}
System.out.println("Printing num1: "); // output: [2,1,1,1,0,1,9,8,7,6,5,4,3,2,1]
for(int i = num2.length() -1; i>= 0; i --){
char numberChar = num2.charAt(i);
String numberString = ""+numberChar;
smallerNumber.add(Integer.parseInt(numberString));
}
System.out.println("Printing num2: "); //output: [0,1,9,8,7,6,5,4,3,2,1]
/
/ also, assume that the bigger number is always first in parameters, which is num1.
// and smaller number is second in parameters, which is num2.
operatedNumber = devide(biggerNumber, smallerNumber);
System.out.println("Division of numbers: " + operatedNumber); // dont worry about the output from this
// or if the number outputs backwards
operatedNumber = mod(biggerNumber, smallerNumber);
System.out.println("Mod of numbers: " + operatedNumber); // dont worry about the output from this
// or if the number outputs backwards
}
//Division method should look like this:
public static ArrayList<Integer> devide(ArrayList<Integer> bigger, ArrayList<Integer> smaller){
ArrayList<Integer> devidedNumber = new ArrayList<Integer>();
//
//
//
return devidedNumber;
}
//I also need a method that returns the mod of two numbers.
//This is basically the same as devision() method except that it returns the mod.
public static ArrayList<Integer> mod(ArrayList<Integer> bigger, ArrayList<Integer> smaller){
ArrayList<Integer> modOfNumbers = new ArrayList<Integer>();
//
//
//
return modOfNumbers;
}
}
Explanation / Answer
What is the precise question here?
If it's just to divide those particular numbers without using the BigInteger, then you can simply convert to the 'long' type which can deal with integers up to 18 or 19 digits long.
If it's to write methods which can divide integers of arbitrary size then, if performance is not a consideration, the easiest way is to use repeated subtraction.
Unless you've been told to do so, I wouldn't use ArrayList<Integer> here which is tantamount to writing your own BigInteger class.
Cheers
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.