logo Practice-It logo

highLow

Language/Type: Java method basics mod return while loops
Author: Marty Stepp (on 2010/12/28)

Write a method highLow that accepts an integer as a parameter and returns whether or not the number has alternating "high" and "low" digits. 0 through 4 are "low" digits and 5 through 9 are "high" digits. Your method should return true if the number passed alternates between "high" and "low" digits, and false if not. You may assume the number passed is positive. If the number passed consists of a single digit, your method should return true.

Note: The method returns true if the number alternates starting with a "high" digit or starting with a "low" digit. What is important is that the digits alternate. For example, both highLow(9292) and highLow(2929) should return true.

Here are some example calls to the method and their resulting return values:

Call Value Returned
highLow(1918193) true
highLow(7283) true
highLow(3827) true
highLow(9388) false
highLow(895151) false
highLow(707) true
highLow(44) false
highLow(45) true
highLow(5) true

You may not use a String to solve this problem.

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.