How to resolve the algorithm Middle three digits step by step in the Java programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Middle three digits step by step in the Java programming language

Table of Contents

Problem Statement

Write a function/procedure/subroutine that is called with an integer value and returns the middle three digits of the integer if possible or a clear indication of an error if this is not possible. Note: The order of the middle digits should be preserved. Your function should be tested with the following values; the first line should return valid answers, those of the second line should return clear indications of an error: Show your output on this page.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Middle three digits step by step in the Java programming language

The provided Java code defines a class MiddleThreeDigits with a main method and a generic method middleThreeDigits. It aims to extract the middle three digits of a given number or string. Here's a detailed explanation:

Main Method:

  • The main method begins by initializing two arrays:
    • passing: Contains long values with an odd number of digits greater than or equal to 3.
    • failing: Contains integers with an invalid number of digits or values that cause an exception.
  • It iterates through the passing array and invokes the middleThreeDigits method on each long value, printing the results.
  • Similarly, it iterates through the failing array and again calls middleThreeDigits on each integer, printing the results.

middleThreeDigits Method:

  • This method is generic, meaning it can operate on any type T.
  • It converts the input n to a String s using String.valueOf(n).
  • If the first character of s is a negative sign ('-' ), it removes it using s.substring(1).
  • It calculates the length of s and checks the following conditions:
    • If len is less than 3 or even, it returns the string "Need odd and >= 3 digits" because the method expects an odd number of digits greater than or equal to 3.
    • Otherwise, it calculates the middle position mid as half of len.
    • It returns a substring of s from mid - 1 to mid + 2, which extracts the middle three characters.

Example Usage:

  • For valid inputs in the passing array, the method extracts the middle three digits as expected.
  • For invalid inputs in the failing array, the method returns an error message "Need odd and >= 3 digits" or throws an exception depending on the input type.

Note: The code assumes that n is a valid number or string. It does not handle cases where n is not a valid numeric representation or an empty string.

Source code in the java programming language

public class MiddleThreeDigits {

    public static void main(String[] args) {
        final long[] passing = {123, 12345, 1234567, 987654321, 10001, -10001,
            -123, -100, 100, -12345, Long.MIN_VALUE, Long.MAX_VALUE};

        final int[] failing = {1, 2, -1, -10, 2002, -2002, 0, Integer.MIN_VALUE,
            Integer.MAX_VALUE};

        for (long n : passing)
            System.out.printf("middleThreeDigits(%s): %s\n", n, middleThreeDigits(n));

        for (int n : failing)
            System.out.printf("middleThreeDigits(%s): %s\n", n, middleThreeDigits(n));
    }

    public static <T> String middleThreeDigits(T n) {
        String s = String.valueOf(n);
        if (s.charAt(0) == '-')
            s = s.substring(1);
        int len = s.length();
        if (len < 3 || len % 2 == 0)
            return "Need odd and >= 3 digits";
        int mid = len / 2;
        return s.substring(mid - 1, mid + 2);
    }
}


  

You may also check:How to resolve the algorithm Camel case and snake case step by step in the C++ programming language
You may also check:How to resolve the algorithm Delegates step by step in the F# programming language
You may also check:How to resolve the algorithm Anagrams step by step in the Haskell programming language
You may also check:How to resolve the algorithm Hello world/Standard error step by step in the PL/I programming language
You may also check:How to resolve the algorithm Formal power series step by step in the Phix programming language