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

JAVA 47. Write two Java statements, the first which creates an output stream con

ID: 3596722 • Letter: J

Question

JAVA

47. Write two Java statements, the first which creates an output stream connected to a binary file named statistics.dat, and a second which writes the string "ABC" onto that stream. 48. Write a recursive method which takes as its input a single positive number of type int, and prints on the console the digits of the number in reverse order. For example, if the input to the method is an int containing the decimal number 123, then the method will display 321 on the console. Identify your base / stopping case. Do not use any methods from the string or Math classes - use arithmetic operators instead. 50. Write an iterative method which does the same thing as the recursive method in the previous problem. Again, do not use any methods from the String or Math classes

Explanation / Answer

==============================================================

--------------
47 : Answer
--------------

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

public class WriteToFile {

   public static void main(String[] args) {

       try {
          
           //output stream connected to binary file
           FileWriter fileOutputStream = new FileWriter("statistics.dat");
          
           //Create buffer writer to write data to file
           BufferedWriter bufferWriter = new BufferedWriter(fileOutputStream);
           bufferWriter.write("ABC");
          
           //Close stream
           bufferWriter.close();
           fileOutputStream.close();
       } catch (IOException e) {
           e.printStackTrace();
       }
   }

}
==============================================================

--------------
48 : Answer
--------------

public class RecursiveNumbersPrint {

   //Recursive method to print number in reverse order
   public static void printNumber(int number) {
      
       if(number!=0) {
           System.out.print(number%10);
           number = number/10;
           //Calling recursively
           printNumber(number);
       }
   }
  
   public static void main(String[] args) {
      
       printNumber(123);
   }
}

==============================================================

--------------
49 : Answer
--------------

public class IterareNumbersPrint {

   //Iterate method to print number in reverse order
   public static void printNumber(int number) {
      
       while(number!=0) {
           System.out.print(number%10);
           number = number/10;
       }
   }
  
   public static void main(String[] args) {
      
       printNumber(123);
   }
}

==============================================================