Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The protected method greaterList (in chapter 6) of the LargeInt class assumes th

ID: 3846871 • Letter: T

Question

The protected method greaterList (in chapter 6) of the LargeInt class assumes that its arguments have no leading zeros. When this assumption is violated, strange results can occur. Consider the following run of the Large Integer Application that claims 35 - 3 is - 968:

Enter the first large integer:
35

Enter the second large integer:
003

First number: +35
Second number: +003
Sum: +038
Difference: -968

Process another pair of numbers? (Y=Yes): n

a. Why do leading zeros cause a problem?
b. Identify at least two approaches to correcting this problem.
c. Describe the benefits and limitations of each of your identified approaches.
d. Choose one of your approaches and implement the solution.
e. Share and discuss with your classmates.

Explanation / Answer

a) Leading Zeroes are the one, which comes prior to the non-zero numbers. Examples arre:

001, 0045, -0078,091 etc.

These numbers are represented as octal in the notations. Example:

if number is 060, the decimal value is 48, and oo75 is 61.

Naturally representing numbers with leading zeroes will misinterpret the results what we expect to come.

For example: 0075 - 060 is 15, but result will be 13 . because the difference of octal represntation of those numbers as said before is: 61 - 48 = 13.

Hence these creates a problem according to the expected hypothesis.

Example:

public class Sample {

   public static void main(String args[]){
      
       System.out.println("Enter numbers leading with zeroes:");
       long x = 00123;
       long y = 0060;
       long z = x-y;
       System.out.println("The difference is:"+z);
   }

}

Output:

Enter numbers leading with zeroes:
The difference is:35

Example:

       long x = 00123;
       long y = 0060;
       String xs = Long.toString(x);
       String ys = Long.toString(y);
       System.out.println(xs+" "+ys);

Output: 83 48

b) Two ways of Programming approaches we can solve this problem :

Using Wrapper classes and BigDecimal Class in java

BigDecimal class:

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.Scanner;

public class Sample {

   public static void main(String args[]){
       Scanner s = new Scanner(System.in);
       System.out.println("Enter numbers leading with zeroes:");
       String x = s.next(); // Reading Values
       String y = s.next();
       greatestList(x,y);
   }
  
   protected static void greatestList(String x,String y){
       BigDecimal bd = new BigDecimal(x); // Passing each value into BigDecimal class constructor
       long l1 = bd.longValue();
       BigDecimal bd1 = new BigDecimal(y);
       long l2 = bd1.longValue(); // Converting that value into long value instead of leading zeroes
       long diff = l1-l2; // Difference between them gives perfect value
       System.out.println("The difference between values is:"+diff);
   }
}

Output:

Enter numbers leading with zeroes:
00123
00045
The difference between values is:78

Wrapper Class Approach:

import java.util.Scanner;

public class Sample {

   public static void main(String args[]){

       Scanner s = new Scanner(System.in);
       System.out.println("Enter numbers leading with zeroes:");
       String x = s.next();
       String y = s.next();
       greatestList(x,y);
   }
  
   protected static void greatestList(String x,String y){
       
       long l1 = Long.valueOf(x); // Converting x into Long value
       long l2 = Long.valueOf(y);
       long diff = l1-l2;
       System.out.println("The difference between values before removing is:"+diff);

   }
  
}

Output:

Enter numbers leading with zeroes:
0070
0060
The difference between values before removing is:10

Beniftis and Limitations:

The above two approaches are good enough to work on leading zeroes.

BigDecimal class can take any huge number into consideration and convert into any type of Primitive

Wrapper class has some limit when compared to above BigDecimal class.

Its better to use String logic to handle leading zeroes instead of predefined classes. By treating the zeroes as characters and the number as String to do operations for the numbers with leading zeroes.