Representation Exposure

Home   »   Representation Exposure

import java.io.*;

public class Main {
	public static void main(String[] args) throws IOException {
		File file = new File("test.txt");
		file.createNewFile();
		A a = new A(file);
		a.sayHello();
		a.solveEquations(0,1,2);
		System.out.close();
		a.sayWorld();
	}
}

class A {
	private PrintStream internal;

	// File objects are immutable, no worries there
	public A(File file) throws FileNotFoundException {
		internal = new PrintStream(file);
	}

	public void sayHello() {
		internal.println("Hello");
	}

	public void sayWorld() {
		internal.println("World");
	}

	// doesn't modify internal...
	public int solveEquations(int x, int y, int z) {
		System.setOut(internal);
		return x + y + z;
	}
}

Leave a Reply

Your email address will not be published. Required fields are marked *