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: 3812188 • 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:

Output:

Enter an integer and press <ENTER>
231123
the original number is 231123
the number reversed 3 2 1 1 3 2
the even digits are 2 2
the odd digits are 3 1 1 3
y/Y to continue, any else to exit
y
Enter an integer and press <ENTER>
a
Invalid entry, try again
number
Invalid entry, try again
165615
the original number is 165615
the number reversed 5 1 6 5 6 1
the even digits are 6 6
the odd digits are 1 5 1 5
y/Y to continue, any else to exit
No

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


import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author Surya
*/
public class number {
  
  
    static void number_reverse(int n) throws IOException
    {//reverse // output reverse digits to screen and txt file
        FileWriter fw = null;
        BufferedWriter bw = null;
        PrintWriter out = null;
        fw = new FileWriter("C:/Users/Surya/Desktop/outDataFile.txt", true);
        bw = new BufferedWriter(fw);
        out = new PrintWriter(bw);
        int r;
        while(n>0)
        {
            r=n%10;
            System.out.print(r+" ");
          
            out.append((char)(r+48));
            out.append(' ');
            n=n/10;
        }
        out.println();
        out.close();
        System.out.println();
    }
    static void evendigits(int n) throws IOException
    {
        int s=0,r;
        while(n>0)
        {
            r=n%10;
            s=s*10+r;//reversing number
            n=n/10;
        }
        FileWriter fw = null;
        BufferedWriter bw = null;
        PrintWriter out = null;
        fw = new FileWriter("C:/Users/Surya/Desktop/outDataFile.txt", true);
        bw = new BufferedWriter(fw);
        out = new PrintWriter(bw);
        n=s;
      
        while(n>0)
        {
            r=n%10;
            if(r%2==0)
            {System.out.print(r+" ");
          
            out.append((char)(r+48));
            out.append(' ');
            }
            n=n/10;
        }
        out.println();
        out.close();
        System.out.println();
      
      
    }
  
    static void odddigits(int n) throws IOException
    {
    int s=0,r;
        while(n>0)
        {
            r=n%10;
            s=s*10+r;//reversing number
            n=n/10;
        }
        FileWriter fw = null;
        BufferedWriter bw = null;
        PrintWriter out = null;
        fw = new FileWriter("C:/Users/Surya/Desktop/outDataFile.txt", true);
        bw = new BufferedWriter(fw);
        out = new PrintWriter(bw);
        n=s;
      
        while(n>0)
        {
            r=n%10;
            if(r%2!=0)
            {System.out.print(r+" ");
          
            out.append((char)(r+48));
            out.append(' ');
            }
            n=n/10;
        }
        out.println();
        out.close();
        System.out.println();
  
    }
    static boolean validate(int n)
    {
        if(n<=0)return false;//if correct nummber is not entered
        return true;//if correct number eneterd
    }
  
    public static void main(String argv[]) throws IOException
    {
      
        char c;//variable declaration
        Scanner sc = new Scanner(System.in);
        int n;//variable to read number
        while(true)
        {
      
            System.out.println("Enter an integer (greater than zero)and press <ENTER>");
            n = sc.nextInt();
            if(validate(n))
            {
                //printing output
                System.out.println("The original number is: "+n);
                System.out.print("The number reversed :");
                number_reverse(n);
                System.out.print("The even digits are :");
                evendigits(n);
                System.out.print("the odd digits are :");
                odddigits(n);
                System.out.print("y/Y to continue, any else to exit");
                c=sc.next().charAt(0);
                if(c!='y'&& c!='Y')//breaking loop
                    break;
            }
            else
            {
                System.out.println("Invalid entry, try again");
            }
      
      
        }
  
  
    }
  
}

ouput:

run:
Enter an integer (greater than zero)and press <ENTER>
123
The original number is: 123
The number reversed :3 2 1
The even digits are :2
the odd digits are :1 3
y/Y to continue, any else to exitn
BUILD SUCCESSFUL (total time: 7 seconds)

txt file output:


3 2 1
2
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