A mono digit is a type of natural number made of the same repeated digits. Examples: 11, 222, 3333, etc. A class called Mono is defined which will check whether a given number is a mono digit number or not using recursive technique. The details of the class are given below:
Class name: Mono
Data members:
num: to store an integer number
d: to store the last digit as an integer
Member functions/Methods:
Mono(): default constructor to initialize num to 0
void getNum(): to accept the number in ‘num’
boolean isMono(int n): check if the number is a mono digit number or not and returns a boolean value accordingly
void show(): calls the isMono(int) method to check for mono digit number and display appropriate message accordingly
Define the class Mono giving details of the constructor, methods void getNum(), boolean isMono(int) and void show(). Also write the main() method to create an object and call the methods accordingly to enable the task.
import java.util.Scanner;
class Mono{
int num;
int d;
public Mono(){
num = 0;
d = 0;
}
public void getNum(){
Scanner in = new Scanner(System.in);
System.out.print("Enter the number: ");
num = Integer.parseInt(in.nextLine());
d = num % 10;
}
public boolean isMono(int n){
if(n < 10){
if(n == d)
return true;
return false;
}
int digit = n % 10;
if(digit != d)
return false;
return isMono(n / 10);
}
public void show(){
if(isMono(num))
System.out.println(num + " is a mono digit number!");
else
System.out.println(num + " is not a mono digit number.");
}
public static void main(String[] args){
Mono obj = new Mono();
obj.getNum();
obj.show();
}
}