Java pubilc class OutPutWriter This is very similiar to Syst

Java.

pubilc class OutPutWriter

This is very similiar to System.out in that OutPutWriter have print, println, and printf methods.

However, when making OutPutWriter, the constructor figures out the destination of output. if the File be passed in, all output is directed to the file.

This program will write a simple output to the file myfile.txt and once the program executes, there will be a file in the directory the program was run in.

public class ChooseOutPut

ChooseOutPut.java file will have the ability to choose between output destinations where it will ask the user where they would like to write and then creates an appropriate OutPutWriter.

// Ask the user whether to print stuff to the screen or to a file.

// Create a new OutPutWriter using different constructors, System.out

// or String filename.

Lastly,

We will set up a class MonitoredWriter which performs this task:

if any output contains a specified string pattern it will have the pattern replaced by a censor string instead. The solution will have the following requirements,

*Making MonitoredWriter a child class of OutPutWriter

*MonitoredWriter will then have print and println methods

*MonitoredWriter will change what the print and println methods do: it will transform input to them.

*Transformations will be done in a special transform() method of the MonitoredWriter. This is a method that the parent class OutPutWriter doesn\'t have.

*Transformations of output will make use of String class methods which will then replace matched string patterns with the censored text

*Transformed text will be passed to the parent class print and println methods which will perform the printing.

Several constructors for MonitoredWriter will be provided so that output can be sent to the screen or to a file.

All other methods of MonitoredWriter will be inherited from OutPutWriter and will behave identically to the parent class.

please do not use any other built in functions unless mentioned!

whole source CODE

Solution

PROGRAM CODE:

OutPutWriter.java

package util;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.util.Formatter;

public class OutPutWriter {

   private FileOutputStream writer;

   private boolean isFileWriting;

   public OutPutWriter()

   {

   writer = null;

   isFileWriting = false;

   }

  

   public OutPutWriter(String filename)

   {

   isFileWriting = true;

   try {

   writer = new FileOutputStream(new File(filename));

   } catch (FileNotFoundException e) {

   // TODO Auto-generated catch block

   e.printStackTrace();

   }

  

   }

  

   public void print(String text)

   {

   if(isFileWriting)

   {

   try {

   writer.write(text.getBytes());

   writer.flush();

   } catch (IOException e) {

   // TODO Auto-generated catch block

   e.printStackTrace();

   }

  

   }

   else System.out.print(text);

   }

  

   public void println(String text)

   {

   text = \"\ \" + text + \"\ \";

   if(isFileWriting)

   {

   try {

   writer.write( text.getBytes());

   writer.flush();

   } catch (IOException e) {

   // TODO Auto-generated catch block

   e.printStackTrace();

   }

   }

   else

   System.out.println(text);

   }

  

   public void printf(String format, Object ...args)

   {

   Formatter formatter = null;

   if(isFileWriting)

   {

   formatter = new Formatter(writer);

   }

   else

   formatter = new Formatter(System.out);

   formatter.format(format, args);

   formatter.flush();

   formatter.close();

   }

}

MonitoredWriter.java

package util;

public class MonitoredWriter extends OutPutWriter{

  

   public MonitoredWriter() {

   super();

   }

  

   public MonitoredWriter(String filename)

   {

   super(filename);

   }

   public void transform(String text, String pattern, String censor)

   {

   while(text.contains(pattern))

   {

       text.replace(pattern, censor);

   }

   }

  

   public void print(String text, String pattern, String censor) {

   transform(text, pattern, censor);

   super.print(text);

   }

  

  

   public void println(String text, String pattern, String censor) {

   transform(text, pattern, censor);

   super.println(text);

   }

   }

ChooseOutPut.java

package util;

import java.util.Scanner;

public class ChooseOutPut {

   public static void main(String[] args) {

       System.out.println(\"1. Print on screen\ 2. print to file\ Enter your choice: \");

   Scanner reader = new Scanner(System.in);

   int option = Integer.parseInt(reader.nextLine());

   OutPutWriter writer;

   if(option == 1)

   {

   writer = new OutPutWriter();

   writer.print(\"Hello How are you\");

   writer.println(\"Printing on next line\");

   }

   else

   {

   System.out.println(\"Enter the filename: \");

   String filename = reader.nextLine();

   writer = new OutPutWriter(filename);

      writer.print(\"Hello How are you\");

   }

   }

  

}

OUTPUT:

1. Print on screen

2. print to file

Enter your choice:

1

Hello How are you

Printing on next line

Java. pubilc class OutPutWriter This is very similiar to System.out in that OutPutWriter have print, println, and printf methods. However, when making OutPutWri
Java. pubilc class OutPutWriter This is very similiar to System.out in that OutPutWriter have print, println, and printf methods. However, when making OutPutWri
Java. pubilc class OutPutWriter This is very similiar to System.out in that OutPutWriter have print, println, and printf methods. However, when making OutPutWri
Java. pubilc class OutPutWriter This is very similiar to System.out in that OutPutWriter have print, println, and printf methods. However, when making OutPutWri
Java. pubilc class OutPutWriter This is very similiar to System.out in that OutPutWriter have print, println, and printf methods. However, when making OutPutWri
Java. pubilc class OutPutWriter This is very similiar to System.out in that OutPutWriter have print, println, and printf methods. However, when making OutPutWri

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site