Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Note: Original slides provided by www.apComputerScience.com and modified for Mr. Smith’s AP Computer Science A classwww.apComputerScience.com If you.

Similar presentations


Presentation on theme: "1 Note: Original slides provided by www.apComputerScience.com and modified for Mr. Smith’s AP Computer Science A classwww.apComputerScience.com If you."— Presentation transcript:

1 1 Note: Original slides provided by www.apComputerScience.com and modified for Mr. Smith’s AP Computer Science A classwww.apComputerScience.com If you have not already done so, take the first 10 minutes of class to read karel 3.8

2 2 Encapsulation is the bundling of data with the methods that act on the data. The mechanism for encapsulation in Java is the class definition. Even though we don’t see it, the robot keeps track of data such as the street and avenue it is on, the direction it is facing, and the number of beepers it has. The methods that change this data are defined in the same class (or an inherited class) as the data. For example, the move() method changes the street and address data stored for this robot. The turnLeft() method changes the direction data of the robot.

3 3 While creating LetterRobot, we learned that encapsulation promotes Code Reuse. By putting the code into a method, we no longer need to write that code again. In addition, from the client’s point of view, it is no longer concerned with how to draw the ‘CS’ – it’s the beneficiary of a concept called Abstraction (we can focus on WHAT, not HOW). In general, if you find yourself doing a cut-and- paste, then there is a better way to do things – i.e., use procedural abstraction and abstract out the common code, putting it into a method. How did we use abstraction as we created methods in the LetterRobot class?

4 4 Find the common code and create other methods: drawLine() and move3() might be what you choose – you might choose others depending on how you see the problem being decomposed. How did we get access to turnRight() without recoding it? Should the methods within LetterRobot have public or private visibility? –First explain public/private by showing methods in the BetterRobot class. –The answer to the question depends on whether we believe a client program would be calling methods like drawLine() and move3() –My opinion is that these methods (except drawCS()) should be private. I’ll argue like this: the name of the class is LetterRobot and its sole purpose is to draw letters. How that object gets it done is of no concern of mine (abstraction), so I don’t need (and shouldn’t be allowed) to see the other helper/auxiliary methods. Therefore, they should be private, helper-like methods for only the LetterRobot class to use.

5 5 Technique for writing modules which are concise, correct, easy to read/modify/understand –Would a general contractor just start building a house – or would he/she break up the task into foundation, frame, electrical, plumbing, etc.? Makes sense, doesn’t it. Explain why from the contractor’s view – use our cs terms we’ve been learning. Write main task first, breaking up the BIG task into smaller tasks (using methods) – then take each method one at a time and also break it up --- continue until each method is compact and singular in focus (cohesion) Look back at what we just did – do you see this re-factoring?

6 6 Write a class called DiamondPlanter using stepwise refinement. It’s like Harvester except the field is diamond shaped and the robot is putting down beepers instead of picking them up. The robot should always place 4 beepers on a diagonal. Assume the robot is facing North to begin with, has 16 beepers, and is standing on the corner just below where the bottom of the diamond is to be. –What is common to another robot we have already created? –What is the main thing that it does? Name this method and it will be the one we call in the client program. –Now, break it down and identify new methods –While writing it, identify any helper methods you’d like to use. Abstract out any common code into their own unique methods (abstraction). –We’ll take each helper method in turn and repeat this stepwise-refinement process until we have cohesion (we finally get to the primitive methods).

7 7 End Situation

8 8 So, we wrote DiamondPlanter. More than likely we extended BetterRobot so that we could use its methods (turnRight() and maybe even a turnAround() to help us plant). Let’s look at the Inheritance Hierarchy and see what methods we were able to take advantage of.

9 9 UrRobot BetterRobot LetterRobotDiamondPlanter turnRight() turnAround() Did we have to make any modifications to LetterRobot or BetterRobot classes to create DiamondPlanter?

10 10 Homework Finish reading karel chapter 3 (3.9 – 3.11) Complete HW06_Hello due Sunday by midnight (HW05_DrawCS is due tonight by midnight)


Download ppt "1 Note: Original slides provided by www.apComputerScience.com and modified for Mr. Smith’s AP Computer Science A classwww.apComputerScience.com If you."

Similar presentations


Ads by Google