From bb8ed4c47fa273287b08f7173fd7c29d4311f57c Mon Sep 17 00:00:00 2001 From: HeshamTB Date: Sun, 17 Feb 2019 17:06:36 +0300 Subject: [PATCH] lab 3 update --- src/lab/four/PalinTest.java | 10 +++++ src/lab/four/Palindrome.java | 75 ++++++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 src/lab/four/PalinTest.java create mode 100644 src/lab/four/Palindrome.java diff --git a/src/lab/four/PalinTest.java b/src/lab/four/PalinTest.java new file mode 100644 index 0000000..1a9d266 --- /dev/null +++ b/src/lab/four/PalinTest.java @@ -0,0 +1,10 @@ +package lab.four; + +public class PalinTest { + + public static void main(String[] args) { + Palindrome app = new Palindrome(); + app.checkPalindrome(); + } + +} diff --git a/src/lab/four/Palindrome.java b/src/lab/four/Palindrome.java new file mode 100644 index 0000000..ef8583a --- /dev/null +++ b/src/lab/four/Palindrome.java @@ -0,0 +1,75 @@ +package lab.four; + +import java.util.Scanner; + +public class Palindrome { + + public void checkPalindrome() + { + Scanner input = new Scanner( System.in ); + + int number; // user input number + int digit1; // first digit + int digit2; // second digit + int digit3; + int digit4; // fourth digit + int digit5; // fifth digit + int digits; // number of digits in input + + number = 0; + digits = 0; + + /* Write code that inputs a five-digit number. Display an error message + if the number is not five digits. Loop until a valid input is received. */ + boolean isFive = false; + + while (!isFive) { + System.out.print("Enter a 5-digit number: "); + number = input.nextInt(); + if (number > 9999 && number < 100000) { + isFive = true; + } + else { + System.out.println("Number must be five digits"); + } + } + /* Write code that separates the digits in the five digit number. Use + division to isolate the left-most digit in the number, use a remainder + calculation to remove that digit from the number. Then repeat this + process. */ + + + digit5 = number % 10; + number = number / 10; + + digit4 = number % 10; + number = number / 10; + + digit3 = number % 10; + number = number / 10; + + digit2 = number % 10; + number = number / 10; + + digit1 = number % 10; + number = number / 10; + +// System.out.println(digit1); +// System.out.println(digit2); +// System.out.println(digit3); +// System.out.println(digit4); +// System.out.println(digit5); + + /* Write code that determines whether the first and last digits are + identical and the second and Fourth digits are identical. Output + whether or not the original string is a palindrome. */ + + if (digit1 == digit5) { + if (digit2 == digit4) { + System.out.println("The number is a palindrome"); + } + } + System.out.println("The number is not a palindrome"); + } + +}