Index

Step-By-Step


For try #1, we'll code without using interfaces.

Classroom1
  • A minimal model of a classroom
  • "printOn" is our key method. Recall that we need to print "quick info" on a report, which we do here with a line of code.
#1
public class Classroom1 { private int roomNumber; public Classroom1(int aRoomNumber) { this.roomNumber = aRoomNumber; } public int getRoomNumber() { return roomNumber; }
#2
public void printOn(PrintStream report)
{ report.println("I am Classroom: " + getRoomNumber()); } }
Professor1
  • A minimal model of a professor
  • Again "printOn" is our key method.
#1
public class Professor1 { private String name; public Professor1(String aName) { this.name = aName; } public String getName() { return name; }
#2
public void printOn(PrintStream report)
{ report.println("I am Professor: " + getName()); } }
College1
Another minimal class here. Class "College" has a name, and a list of the objects.

  • The List element type is "Object"
  • Sending "printOn" to "Object" gives us a "not understood" compile error
  • We're stuck with try #1 -- 😕. For now, we'll print "not yet supported" on the report.
public class College1 {
	private String name;
	
#1
private List<Object> list; public College1(String aName) { super(); this.name = aName; this.list = new ArrayList<>(); } public void add(Object o) { this.list.add(o); } public void printFullReport(PrintStream report) { /*Compile error, "printOn not understood by Object for (Object each: this.list)
#2
each.printOn(report); //<---------- compile error here */
report.println("College: " + this.name);
#3
report.println("Apologies, printing of list is not yet supported"); report.println("Objects do not understand \"printOn\""); } }

A test class to browse is here....

Completed Code


Here are the completed classes:

Conclusion


"Try 1" failed 😕. Our use of type "Object" worked fine until we needed to satisfy the "print on report" requirement. Let's move on to "Try 2".