A Java program to check if a number is automorphic, display automorphic numbers within a range and generate automorphic numbers using eclipse IDE
What Will I Learn?
- More application of switch statement.
- More application of void return statement methods.
- More application of boolean return statement methods.
- More application of if statements.
- More application of both for loops.
Requirements
- An Integrated Development Environment(IDE) installed that can run Java (e.g Eclipse, IntelliJ, NetBeans)
- JDK 1.X installed on your Pc
- Java Programming Experience.
- Of course, willingness to learn
Difficulty
- Intermediate
Tutorial Contents
This application majors on creating a Java application that can check if a number is an auto morph, display auto morphic numbers in a particular range and also display a specific amount of auto morphic numbers as required from the user.
An automorphic number is a number thats it square ends with the number itself. (e.g 5 is an automorphic number, as the square of 5 = 25 (ends with 5)).
NB: a new Java class file was created called autoMorphic.java.
The codes are explained below
Line 5 - 16
Scanner sc = new Scanner(System.in);
int number;
boolean isAutoMorphic;
System.out.println("WELCOME TO AUTOMORPHIC NUMBER APP V1.0");
System.out.println();
System.out.println("ENTER \n1 to check if a number is autoMorphic, \n2 to display a range of "
+ "automorphic numbers from 1 to N and \n3 to display automorphic numbers in a given range"
+ "\n4 to display N numbers of auto morphic numbers");
int choice = sc.nextInt();
Line 5 - Declaration and initialzation of a scanner object called sc.
Line 7 - 8 : a declaration of an integer variable and a boolean variable, number and isAutoMorphic respectively which will be used to accept the number entered by the user and isAutoMorphic is used to indicate if a number is automorphic or not.
Line 10 - 14: Prints instructions on what keys are allowed in the application and what the keys can do.
Line 16: Accepts the user's choice from the console.
Line 18 - 49
switch (choice) {
case 1:
System.out.println("Enter number : ");
number = sc.nextInt();
isAutoMorphic = checkAutoMorphicNumber(number);
print("Is "+number+" auto morphic ? "+isAutoMorphic);
break;
case 2:
System.out.println("Enter End Number : ");
number = sc.nextInt();
if (number < 0 || number == 1) {
print("End number cannot be less than 0 or be 1");
}
displayAutomorphicNumbers(1,number);
break;
case 3:
print("Enter start Number : ");
number = sc.nextInt();
print("Enter End Number : ");
int endNumber = sc.nextInt();
displayAutomorphicNumbers(number, endNumber);
break;
case 4:
print("How many auto morphic numbers would you want to be displayed");
number = sc.nextInt();
displayAutomorphicNumbers(number);
break;
default:
print(choice+ " is a wrong entry. Valid entries - 1,2,3,4");
break;
}
sc.close();
The switch statement above is used to get the choice of the user based on the help text that was displayed.
On entering 1: The user is prompted to enter the number to be checked and the number entered is stored in the number variable which is an interger.
Next, the isAutoMorphic variable is stored with the return value from the checkAutoMorphicNumber() method which returns a boolean and lastly the print() method is used to display if the number is an automorphic number or not.
On entering 2: The user is prompted to enter the end number to which he would like an automorphic number to be displayed from 1 to N. The number is stored in the number variable and then an if statement is used to check that the number enetered is not less than 0 and is not 1 also.
The number entered is passed as an arguement to the displayAutomorphicNumber() method.
On entering 3: The user is prompted to enter the start number and also the end number which indicares a range.
This start and end number is stored in the number and endNumber variables respectively and then passed to the displayAutomorphicNumbers() method.
On entering 4: The user is prompted to enter how many automorphic numbers he would like to be displayed which the input is stored in the number variable and then passed as an argument to the displayAutomorphicNumbers() method.
And lastly the default case is used to tell the user than a wrong entry has been entered and also show the valid entries.
And the last line closes the Scanner object sc.
displayAutomorphicNumbers(int startNumber,int endNumber)
private static void displayAutomorphicNumbers(int startNumber,int endNumber) {
// TODO Auto-generated method stub
int counter = 0;
for(int i= startNumber; i <= endNumber; i++) {
if (checkAutoMorphicNumber(i)) {
System.out.println(i+" is auto Morphic");
counter++;
}
}
print("There are "+counter+" auto Morphic numbers between "+startNumber+" and "+endNumber);
}
This method takes in two integers, the startNumber and the endNumber which is used to know the boundaries of what the start number and end number is in determining an auto morphic number.
an integer variable named counter is intitialized to the value 0 which will be used to count the number of auto morphic numbers.
Next, a for loop is used to loop through the numbers between the range of numbers starting from the start number to the end number inclusive.
Then an if statment is use to check if the number currenctly being considered in the loop is an auto morphic number by sending that number as an argument to the checkAutoMorphicNumber() method after which is the return value is true, the number is being displayed in the next line and the counter variable is increased by one.
The print statement in the method is used to display how many auto morphic numbers exist between the start and end number range.
displayAutomorphicNumbers(int range)
private static void displayAutomorphicNumbers(int range) {
// TODO Auto-generated method stub
int counter = 0;
for(int i= 1; i <= 100000000; i++) {
if (checkAutoMorphicNumber(i)) {
System.out.println(i+" is auto Morphic");
counter++;
}
if (counter == range) {
break;
}
}
}
The method accepts one arguememnt which is used to indicate how many auto morphic numbers the user wants displayed.
Next an integer variable named counter is initialized to the value of 0 with the intension of being responsible for us to know how many auto morphic numbers has been gotten.
Then, a for loop is used to loop through from 1 to 100000000 inorder to check for an auto morphic number within that range.
an if statement is used to check if a number is an automorphic number using the checkAuutoMorphicNumber() which is the number is automorphic, the number is displayed along with the string " is auto morphic".
On the next line, the counter variable is increased by 1 inroder to keep records of the gotten automorphic numbers.
Outside of the first if statement, another if statment is used to check if counter (being the number of already gotten automorphic numbers) is the same as range, which is the number of automorphic numbers the user will like the program to generate and if true, the break statement breaks outside of the loop and code execution goes to the next line.
checkAutoMorphicNumber(int numberToCheck) method (Line 57 - 101)
private static boolean checkAutoMorphicNumber(int numberToCheck) {
// TODO Auto-generated method stub
String number = String.valueOf(numberToCheck);
String answer = String.valueOf((int)Math.pow(numberToCheck,2));
boolean yes = false;
switch (number.length()) {
case 1:
if (answer.substring(answer.length() - 1).equals(number)) {
yes = true;
}
break;
case 2:
if (answer.substring(answer.length() - 2).equals(number)) {
yes = true;
}
break;
case 3:
if (answer.substring(answer.length() - 3).equals(number)) {
yes = true;
}
break;
case 4:
if (answer.substring(answer.length() - 4).equals(number)) {
yes = true;
}
break;
case 5:
if (answer.substring(answer.length() - 5).equals(number)) {
yes = true;
}
break;
case 6:
if (answer.substring(answer.length() - 6).equals(number)) {
yes = true;
}
break;
case 7:
if (answer.substring(answer.length() - 7).equals(number)) {
yes = true;
}
break;
}
return yes;
}
This method takes in one argument which is the number to check if the number is automorphic.
Firstly, the String value of the number passed is taken and stored in the String 'number' variable and also the String value of the square of the number is stored in the answer variable.
next a boolean variable called yes is used to indicate if a number is automorphic or not.
A switch statement is then used to check the length of the number value, this is done inorder to be able to know the length of the number itself being that an automorphic number is a number that exist as the last digit/digits in its square hence the length of the number itself should be known.
Then the cases are used to get the last digits of the square using the substring() method.
e.g if the number has a length of 2, the last two digits are gotten from the square using answer.substring(answer.length() - 2).equals(number) and then the if statment checks if that substring is equals to the number itself and if true, the boolean variable yes is being set to yes and is returned as the return value.
print(String message) method (Line 52 - 55)
This method makes it easier to use the System.out.println() method by passing a string to the method which will be displayed , hence print() is equivalent to System.out.println();
private static void print(String message) {
// TODO Auto-generated method stub
System.out.println(message);
}
Below are code execution:
The complete code can be located from this GitHub repository: https://github.com/generalkolo/Javacodes
Posted on Utopian.io - Rewarding Open Source Contributors
Thank you for the contribution. It has been approved.
You can contact us on Discord.
[utopian-moderator]
Thanks @rufans!!!
Hey @edetebenezer I am @utopian-io. I have just upvoted you!
Achievements
Community-Driven Witness!
I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!
Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x
Congratulations!,@edetebenezer Your post has been upvoted by Reach Out.
Our goal is to support Nigerian minnows on Steemit. Join our discord group https://discord.gg/NWAkKfn
Proudly sponsored via SP donation from @eturnerx , @rufans & @solomon158
Upvotes Benefactor : @bleepcoin & the rest of us
Join Our Trail here: https://steemauto.com/dash.php?i=15&id=1&user=reachout
Curator On Duty: @akintunde, assisted by Richie, the Manual Bot
Founded by @akintunde
## Also,We'd like invite you to the @eoscafe discord community https://discord.gg/wdUnCdE , be part of something great