Write a java program which: 1. Asks the user to enter a positive integer greater
ID: 3810688 • Letter: W
Question
Write a java program which:
1. Asks the user to enter a positive integer greater than 0
2. Validates that the entry is a positive integer
3. Outputs the digits in reverse order with a space separating the digits
4. Outputs the even digits not in reverse order with a space separating the digits (consider zero to be even)
5. Outputs the odd digits not in reverse order with a space separating the digits
6. Allows user is to repeat/continue the program as many times as he/she wants
7. Keeps a record in a txt file named outDataFile.txt with the history of all numbers entered and the associated results, in the following format:
the original number is 1023
the number reversed 3 2 0 1
the even digits are 0 2
the odd digits are 1 3
-----------------
the original number is 102030
the number reversed 0 3 0 2 0 1
the even digits are 0 2 0 0
the odd digits are 1 3
-----------------
In addition:
8. The program must have the following four void methods:
a. validate //validate user input
b. reverse // output reverse digits to screen and txt file
c. even //output even digits to screen and txt file
d. odd //output odd digits to screen and txt file
Explanation / Answer
class MyClass
{
int[] arr = new arr[100];
public static void main(String args[])
insertInput();
}
public void insertInput()
{
System.out.println("Enter a positive integer greater than 0:");
Scanner sc = new Scanner(System.in);
int input = sc.nextInt();
System.out.print("the original number is:"+input);
validate(input);
}
public void validate(int input)
{
if(input > 0)
{
reverse(input);
even(input);
odd(input);
}
else
{
System.out.println("Do you want to repeat the process..Please enter Y/N");
Scanner sc1 = new Scanner(System.in);
String str = sc1.nextLine();
if(str.equals("Y"))
insertInput();
}
}
public void reverse()
{
System.out.println("the number reversed:");
int item = input;
int i=0;
while(item != 0 && i<100)
{
int rem = item % 10;
arr[i] = rem;
System.out.print(rem+" ");
item = (item - rem)/10;
i++;
}
}
public void even(input)
{
System.out.println("the even digits are:");
for(i=arr.length;i>=0;i--)
{
if((arr[i] % 2) == 0)
System.out.print(arr[i]+" ");
}
}
public void odd(input)
{
System.out.println("the odd digits are:");
for(i=arr.length;i>=0;i--)
{
if((arr[i] % 2) != 0)
System.out.print(arr[i]+" ");
}
System.out.println("Do you want to repeat the process..Please enter Y/N");
Scanner sc1 = new Scanner(System.in);
String str = sc1.nextLine();
if(str.equals("Y"))
insertInput();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.