Exercise: Methods and BigDecimal

In this exercise, you will practice working with methods and BigDecimal numbers.

Write a console program that:
  1. Has a method bigAdd that adds two ints and produces a BigDecimal.
  2. Has a method digitSum that calculates the digit sum of a given BigDecimal, returning an int.
  3. Has a method digitsToWords that converts an int to a string consisting of the English words of its digits.
Here's how the body of your main method should look:
System.out.print("Adding 5 to Integer.MAX_VALUE: ");
BigDecimal result = bigAdd(5, Integer.MAX_VALUE);
System.out.println(result);
System.out.print("Digit sum of " + result + ": ");
int digitSum = digitSum(result);
System.out.println(digitSum);
System.out.print("Digits as words: ");
String words = digitsToWords(digitSum);
System.out.println(words);
Program output:
Adding 5 to Integer.MAX_VALUE: 2147483652
Digit sum of 2147483652: 42
Digits as words: four two

Hints 💡

Hand in instructions

  1. Make sure your program runs correctly.
  2. Hand in your program by uploading Main.java to Moodle.