logo Practice-It logo


Language/Type: Java recursion recursive programming
Author: Eric Spishak

Write a recursive method digitsSorted that accepts an integer as a parameter and returns true if the digits of the integer are sorted and false otherwise. The digits must be sorted in non-decreasing order (i.e. increasing order with duplicate digits allowed) when read from left to right. An integer that consists of a single digit is sorted by definition. The method should be also able to handle negative numbers. Negative numbers are also considered sorted if their digits are in non-decreasing order. The following table shows several calls to your method and their expected return values:

Call Value Returned
digitsSorted(0) true
digitsSorted(2345) true
digitsSorted(-2345) true
digitsSorted(22334455) true
digitsSorted(-5) true
digitsSorted(4321) false
digitsSorted(24378) false
digitsSorted(21) false
digitsSorted(-33331) false

You are not allowed to construct any structured objects other than strings (no array, List, Scanner, etc.) and you may not use any loops to solve this problem; you must use recursion.

Type your solution here:

This is a method problem. Write a Java method as described. Do not write a complete program or class; just the method(s) above.

You must log in before you can solve this problem.

Log In

If you do not understand how to solve a problem or why your solution doesn't work, please contact your TA or instructor.
If something seems wrong with the site (errors, slow performance, incorrect problems/tests, etc.), please

Is there a problem? Contact a site administrator.