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

Hi these are question from the book. I copied them to jdk but cannot run. Can yo

ID: 3696466 • Letter: H

Question

Hi these are question from the book. I copied them to jdk but cannot run.

Can you told we what is input and output, are they predefined class like Scanner or FileInputStream? If it is, how can I import? If not, what can I do? to defined classes "input" or "output" by myself? Thank you.

1. In each of the following fragments, how many lines appear in the output?

output out = new output();

for (int i = 0; i < 10; i++)

   out.write(i);

out.writeln();

2 .In each of the following fragments, how many lines appear in the output?

output out = new output();

for (int i = 0; i < 10; i++)

{

   out.write(i);

   if (i % 2 == 0)

      out.writeln();

}

Explanation / Answer


Create own classes called P1 and P2 and write below code to check the file contents
to see how many number of lines will appear.

//P1.java
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
public class P1
{  
   public static void main(String[] args)
   {          
       //Declare PrintWriter class variable
       PrintWriter out=null;
       try
       {          
           //instantiage PrintWriter class with a file called file1.txt
           out=new PrintWriter(new FileWriter("C:\file1.txt"));
          
           //for loop that writes 0 to 9 numbers in a single line
           for (int i = 0; i < 10; i++)                      
               out.print(i);
          
           //move to next line after writing 9 numbers
           out.println();          
           out.close();
       }
       catch (IOException e)
       {
           System.out.println(e);
       }
   }
}
Sample output file1.txt
file1.txt file is found in C drive.
0123456789

--------------------------------------------------------------------------------------------------------------------------------------------------------
//P2.java
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
public class P2
{  
   public static void main(String[] args)
   {          
       //Declare PrintWriter class variable
       PrintWriter out=null;
       try
       {          
           //instantiage PrintWriter class with a file called file1.txt
           out=new PrintWriter(new FileWriter("C:\file2.txt"));
          
           for (int i = 0; i < 10; i++)
           {
               //write i-va;ue to file
               out.print(i);
               //check if i is even number
               //then write a new line
               //For 0,2,4,6,8 five lines will appear
               //Total 6 lines are appear in the code fragment
               if (i % 2 == 0)
                  out.println();
           }
          
           out.close();
       }
       catch (IOException e)
       {
           System.out.println(e);
       }
   }
}

Sample ouput file2.txt
0
12
34
56
78
9

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