Lecture: 9 Date: Jan 28, 2008 Anonymous survey 1) I read the text regularly? Yes/No 2) I have learned the most from {lectures, labs, assignments, readings}? Write all that apply. 3) I find {lectures, labs, assignments, readings} {confusing, boring, repetitious}. 4) I {know, have seen some, have not seen} Java already. 5) I feel {confident, somewhat unsure, somewhat lost} in this course. === You're given the following data: See data linked on web. Derive information from data: Half of the class: reconstruct the class diagrams from this data. Other half: reconstruct the organizational chart for this company. See class diagrams and org. chart linked on web. Design the method countAll, which counts the number of employees this employee is responsible for (which include this employee). Examples: wkC.countAll() --> 1 pete.countAll() --> 2 jack.countAll() --> 5 Q: What class / interface should we add this method header to? A: Employee interface Q: What does this obligate us to do? A: Implement it in Boss and Worker. Q: How many employees is a worker responsible for? A: Just him/herself, ie. 1. In Worker: // count the number of employees this worker is responsible for. int countAll() { return 1; } In Boss: // count the number of employees this boss is responsible for. int countAll() { /* Template ... this.peons ... LoEmp */ ... } Q: Is it obvious how to compute countAll from a LoEmp? A: No, so we should make a wish list method, like countAll for a LoEmp. Q: Where do we put this header? A: In the interface. // represent a list of employees. interface LoEmp { // sum the number of employees this list of employees is responsible // for. int countAll(); } Q: What does this obligate us to do? A: Implement countAll in MTLoEmp and ConsLoEmp. In MTLoEmp: // sum the number of employees this empty list of employees is // responsible for, ie. none. int countAll() { return 0; } In ConsLoEmp: // sum the number of employees this cons of an employee and list of // employees is responsible for. int countAll() { /* Template ... this.first ... Emp ... this.rest ... LoEmp AND ... this.first.countAll() ... int ... this.rest.countAll() ... int */ ... } Reinterpret the purpose statement for this.rest.countAll() === "sum the number of employees the rest of this list is responsible for." Q: So how can you compute the result, given the above? A: You just add the number of employees the first employee is responsible for. And we can use countAll() to compute this. // sum the number of employees this cons of an employee and list of // employees is responsible for. int countAll() { /* Template ... this.first ... Emp ... this.rest ... LoEmp AND ... this.first.countAll() ... int ... this.rest.countAll() ... int */ return this.first.countAll() + this.rest.countAll() } So we're done with countAll in LoEmp. How can we finish countAll in Boss? return 1 + this.peons.countAll(); [NB: Such a naming convention is highly offensive and writing such code in a work environment is likely to get you fired. Keep this in mind!] Next hand out: The ProfJ beginner language grammar. A question I was emailed before class: What's wrong with this? Show changeDuration (Show s) { s.duration = s.duration + 5; return s; } If you've learned Java before, you might think nothing is wrong, but this is NOT a course in Java. Q: So, what's wrong with it? A1: You're taking in a parameter s, when you should use the implicit parameter this. Show changeDuration () { this.duration = this.duration + 5; return this; } Yes, this is true. But there's a grammatical error here, too. Look at the handout and try to see why this program is not a well-formed Beginner language "phrase". A2: The definition for METHOD says TYPE METH-NAME (TYPE NAME, ...) { STATEMENT } And a STATEMENT is either if (EXPRESSION) STATEMENT else STATEMENT return STATEMENT So this.duration = this.duration + 5; return this; is not a STATEMENT, but it should be. Q: How can we change this program to do what is intended? A: I don't know, so let's make an example! Suppose for now a show only has a duration. Write an example: new Show(100).changeDuration() --> new Show(100 + 5); ^^^^^^^^^^^^^^^^^ This tells us what should go in our code. Show changeDuration () { return new Show(this.duration + 5); } But the old code /changed/ the show. This one just makes a new show. Yes-- but we haven't introduced the linguistic features to even talk about "changing" an object. Q: Did we ever talk about it in 211? A: No. Q: If you have a Posn and you want to make the X coordinate twice as big, what did you do? A: Make a new Posn that is like the given Posn with an X that is twice as big. Exactly. Will we ever learn how to "change" something? Maybe. (You can always read ahead!) What else do you notice about the grammar of ProfJ? When you read it, what does is sound like? "A Statment is one of: If blah or Return blah." etc. Sounds like a data definition! And it is! This means we can write a data definition for Java (ProfJ Beginner) programs. If we can write these data definitions, what kinds of /programs/ can we write? 1) A program to check to make sure every class that claims to implement some interface actually implements all of the methods in the interface. 2) A program that returns the string represent the Java program. 3) A program that /runs/ the Java program (like ProfJ does!). 4) A program the returns the representing the program translated into Scheme. etc, etc. This is called "meta" programming. Once you can do it, you've mastered a language and you will be able to code circles around your colleagues.