0
\$\begingroup\$

Exercise: Using Arraylists create a program that makes divisions between two very large numbers (numbers greater than 10 digits).

I did it (the code works), but could my code be improved?

public class Main {
 static void division(List<Integer> lista, List<Integer> listb) {
 List<Integer> listResult = new ArrayList<>();
 if (lista.size() <= 10 || listb.size() <= 10) {
 System.out.println("The numbers must be bigger than 10");
 } else {double a = 0, j = 0;
 String Stringnumber = "";
 String Stringnumber2 = "";
 for (int number : lista) {
 Stringnumber += String.valueOf(number);
 }
 for (int number : listb) {
 Stringnumber2 += String.valueOf(number);
 }
 double test = Double.parseDouble(Stringnumber2);
 double test2 = Double.parseDouble(Stringnumber);
 double dividend = 0, divisor = 0;
 if (test >= test2) {
 dividend = test;
 divisor = test2;
 } else {
 dividend = test2;
 divisor = test;
 }
 for (int i = 9; i > 0 && j != 1; i--) {
 if (i * divisor <= dividend) {
 listResult.add(i);
 a = dividend - (i * divisor);
 if (a >= divisor) {
 i = 9;
 } else {
 j = 1;
 }
 }
 }
 System.out.println("The result is:");
 for (int i = 0; i < listResult.size(); i++) {
 System.out.println(listResult.get(i));
 }
 }
 }
 public static void main(String[] args) {
 Scanner x = new Scanner(System.in);
 List<Integer> lista = new ArrayList<>();
 List<Integer> listb = new ArrayList<>();
 System.out.println("Give a number");
 for (char digit : x.nextLine().toCharArray()) {
 lista.add(digit - '0');
 }
 System.out.println("Give another number");
 for (char digit : x.nextLine().toCharArray()) {
 listb.add(digit - '0');
 }
 x.nextLine();
 division(lista, listb);
 }
}
200_success
145k22 gold badges190 silver badges478 bronze badges
asked Sep 12, 2017 at 3:22
\$\endgroup\$

3 Answers 3

1
\$\begingroup\$
for (int number : lista) {
 Stringnumber += String.valueOf(number);
}
for (int number : listb) {
 Stringnumber2 += String.valueOf(number);
}
double test = Double.parseDouble(Stringnumber2);
double test2 = Double.parseDouble(Stringnumber);

It is very confusing that the variable names with a 2 suffix first refer to the first input a and then you swap the naming so that the 2 suffix refers to the second input b.

It would be better if all names referring to input a had an a suffix and all names referring to input b had a b suffix.

answered Sep 15, 2017 at 15:44
\$\endgroup\$
2
\$\begingroup\$

At first, I thought that you were going to implement division of two integers, each represented as a list of digits. But then, you simply concatenate all of the elements of each operand to be parsed as a double. If you're going to cheat on the arithmetic using the built-in floating-point support, then what's the point of the exercise? In fact, it doesn't even care whether you pass in an ArrayList of individual digits {1, 2, 3, 4, 5} or an ArrayList of one element {12345} — either way, it all gets stringified and parsed as 12345.0d.

answered Sep 14, 2017 at 5:03
\$\endgroup\$
2
  • \$\begingroup\$ I could not think of another idea but that (the code)..did I really cheat? \$\endgroup\$ Commented Sep 14, 2017 at 15:49
  • \$\begingroup\$ @Michelle I think you are supposed to implement an algorithm like this. \$\endgroup\$ Commented Sep 15, 2017 at 15:47
2
\$\begingroup\$

Doesn't work if the answer is more than a single digit

In your division loop, you never decrease dividend, so the loop becomes an infinite loop as long as the dividend is more than 10x the value of the divisor.

You probably want to set dividend = a or something like that to avoid the infinite loop.

More bugs

Even if you fixed the infinite loop, your division would still be broken. For example, if you divided 100 by 1 (or equivalently 1000000000000 by 10000000000) your loop would produce an answer like 999999999991 instead of 100 because you didn't take into account the fact that the divisor and the dividend may be of different lengths.

Doesn't really meet the requirement

You aren't actually performing the division using Arraylists. You are using doubles instead. That sort of defeats the purpose of the exercise.

answered Sep 14, 2017 at 5:25
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.