Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a java program which: 1. Asks the user to enter a positive integer greater

ID: 3811143 • 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

HI, Please find my implementtion.

Please let me know in case of any issue.

import java.io.BufferedWriter;

import java.io.File;

import java.io.FileWriter;

import java.io.IOException;

import java.util.Scanner;

public class ReverseNumber {

   public static boolean validate(int n){

       if(n > 0)

           return true;

       else

           return false;

   }

   public static void reverse(int n, BufferedWriter bw) throws IOException{

       String rev = "";

       while(n > 0){

           int d = n%10;

           rev = rev + d + " ";

           n = n/10;

       }

       bw.write("the number reversed "+rev);

       bw.newLine();

       System.out.println("the number reversed "+rev);

   }

   public static void odd(int n, BufferedWriter bw) throws IOException{

       String odd = "";

       while(n > 0){

           int d = n%10;

           if(d%2 == 1)

               odd = d+" "+odd;

           n = n/10;

       }

       bw.write("the odd digits are "+odd);

       bw.newLine();

       System.out.println("the odd digits are "+odd);

   }

   public static void even(int n, BufferedWriter bw) throws IOException{

       String even = "";

       while(n > 0){

           int d = n%10;

           if(d%2 == 0)

               even = d+" "+even;

           n = n/10;

       }

       System.out.println("the even digits are "+even);

       bw.write("the even digits are "+even);

       bw.newLine();

   }

   public static void main(String[] args) throws IOException {

       Scanner sc = new Scanner(System.in);

       System.out.print("Enter a integer: ");

       int n = sc.nextInt();

       while(!validate(n)){

           System.out.print("Enter a integer: ");

           n = sc.nextInt();

       }

       // opening file

       FileWriter fr = new FileWriter(new File("outDataFile.txt"));

       BufferedWriter bw = new BufferedWriter(fr);

       System.out.println("the original number is "+n);

       bw.write("the original number is "+n);

       bw.newLine();

       reverse(n, bw);

       even(n, bw);

       odd(n, bw);

      

       bw.newLine();

       bw.close();

       fr.close();

   }

}

/*

Sample output:

Enter a integer: -5

Enter a integer: 102030

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

*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote