1 Some Patterns of Novice Programs Author : Eugene Wallingford ; Dan Steinberg ; Robert Duvall ; Ralph Johnson Source : PLoP 2004 Advisor : Ku-Yaw Chang.

Slides:



Advertisements
Similar presentations
Lecture 5: Interfaces.
Advertisements

CS0007: Introduction to Computer Programming Introduction to Classes and Objects.
PS4: Test Driven Development Based on Test Driven Development by Example By Kent Beck.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Week 9: Methods 1.  We have written lots of code so far  It has all been inside of the main() method  What about a big program?  The main() method.
Road Map Introduction to object oriented programming. Classes
Designing Classes Chapter 3. 2 Chapter Contents Encapsulation Specifying Methods Java Interfaces Writing an Interface Implementing an Interface An Interface.
Interfaces. In this class, we will cover: What an interface is Why you would use an interface Creating an interface Using an interface Cloning an object.
CS 106 Introduction to Computer Science I 03 / 21 / 2008 Instructor: Michael Eckmann.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
Introductory case study. 2 The problem The most difficult part of any design project is understanding the task you are attempting You have been contacted.
Software Engineering Case Study Slide 1 Introductory case study.
Options for User Input Options for getting information from the user –Write event-driven code Con: requires a significant amount of new code to set-up.
Games and Simulations O-O Programming in Java The Walker School
What is RobotC?!?! Team 2425 Hydra. Overview What is RobotC What is RobotC used for What you need to program a robot How a robot program works Framework.
Programming With Java ICS201 University Of Hail1 Chapter 12 UML and Patterns.
Object Oriented Software Development
Chapter 7 Designing Classes. Class Design When we are developing a piece of software, we want to design the software We don’t want to just sit down and.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Array.
Module 7: Essentials of Object-Oriented Programming.
Computer Science II 810:062 Section 01 Session 2 - Objects and Responsibilities.
CISC6795: Spring Object-Oriented Programming: Polymorphism.
Object Oriented Programming Philosophy. Part 1 -- Basic Understanding & Encapsulation.
CMSC 202 Exceptions. Aug 7, Error Handling In the ideal world, all errors would occur when your code is compiled. That won’t happen. Errors which.
An Introduction to Java Chapter 11 Object-Oriented Application Development: Part I.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Lecture 21 Multiple Inheritance. What is Multiple Inheritance? We defined inheritance earlier in the semester as a relationship between classes. If class.
23-Oct-15 Abstract Data Types. 2 Data types A data type is characterized by: a set of values a data representation, which is common to all these values,
Programming Principles Chapter 1. Objectives Discuss the program design process. Introduce the Game of Life. Discuss object oriented design. – Information.
Objects & Dynamic Dispatch CSE 413 Autumn Plan We’ve learned a great deal about functional and object-oriented programming Now,  Look at semantics.
Today’s Agenda  Reminder: HW #1 Due next class  Quick Review  Input Space Partitioning Software Testing and Maintenance 1.
Use Cases Use Cases are employed to describe the functionality or behavior of a system. Each use case describes a different capability that the system.
Designing Classes Chapter 3. 2 Chapter Contents Encapsulation Specifying Methods Java Interfaces Writing an Interface Implementing an Interface An Interface.
Object Oriented Software Development
Design Model Lecture p6 T120B pavasario sem.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
CS0007: Introduction to Computer Programming Classes: Documentation, Method Overloading, Scope, Packages, and “Finding the Classes”
Abstract Classes and Interfaces 5-Dec-15. Abstract methods You can declare an object without defining it: Person p; Similarly, you can declare a method.
CSCI 1100/1202 April 1-3, Program Development The creation of software involves four basic activities: –establishing the requirements –creating.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Chapter 17 – Object- Oriented Design. Chapter Goals To learn about the software life cycle To learn about the software life cycle To learn how to discover.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Designing Classes CS239 – Jan 26, Key points from yesterday’s lab  Enumerated types are abstract data types that define a set of values.  They.
Salman Marvasti Sharif University of Technology Winter 2015.
1 Object-Oriented Design Now we can extend our discussion of the design of classes and objects Chapter 6 focuses on: software development activities determining.
Static?. Static Not dynamic class Widget { static int s; int d; // dynamic // or instance // variable }
M1G Introduction to Programming 2 3. Creating Classes: Room and Item.
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
CS2102: Lecture on Abstract Classes and Inheritance Kathi Fisler.
Chapter 4Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapters 4 and 5: Excerpts l Class and Method Definitions l Information.
The Last Lecture CS 5010 Program Design Paradigms "Bootcamp" Lesson © Mitchell Wand, This work is licensed under a Creative Commons Attribution-NonCommercial.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Chapter 7 Classes and Methods III: Static Methods and Variables Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition)
Fusion Design Overview Object Interaction Graph Visibility Graph Class Descriptions Inheritance Graphs Fusion: Design The overall goal of Design is to.
Functions in C++ Top Down Design with Functions. Top-down Design Big picture first broken down into smaller pieces.
Unified Modeling Language (UML)
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes : Review Java Software Solutions Foundations of Program Design Seventh Edition John.
Summary prepared by Kirk Scott
Web Design & Development Lecture 9
Chapter 3: Using Methods, Classes, and Objects
Chapter 4: Writing Classes
Array.
Defining Classes and Methods
Lecture 22 Inheritance Richard Gesick.
Defining Classes and Methods
slides created by Ethan Apter and Marty Stepp
Introduction to Computer Science and Object-Oriented Programming
Presentation transcript:

1 Some Patterns of Novice Programs Author : Eugene Wallingford ; Dan Steinberg ; Robert Duvall ; Ralph Johnson Source : PLoP 2004 Advisor : Ku-Yaw Chang Student : Shi-kun Hou ID : E

2 Outline Introduction Good start Classes from nouns Test before code Test Leading test Specifications Before You Code Class must have behavior Public methods first Method Delegation Instance varables

3 Introduction This paper describes some of the patterns for novice programmers to learn Java programs Patterns –Getting started –Writing tests –Identifying classes –Writing methods –Identifying instance variables

4 Patterns Getting started –Good start –Classes from nouns Writing tests –Test before code –Test –Leading test –Specification before code

5 Patterns Identifying classes –Class –One major responsibility –Class captures an abstraction –Class must have behavior Writing methods –Public methods first –Method –Delegation –Helper method Identifying instance variables –Instance variable

6 Good start Starting a program is difficult. –Sometime, a problem looks too difficult. You feel like have to learn about the problem area. How much is enough? –Other times, a problem looks really easy. A mess of question:file,classes,methods and if statements. A program consists of a set of objects interacting to solve a problem.

7 Good start Find the objects you need and then write classes to represent them. – identify all the objects and classes ? Identify a class or two that you think will be in your program.

8 Classes from nouns A problem description usually talks about the most important concepts in the problem. The nouns in the description name things: –concrete things : people, toasters –processes : the computation of a wage –abstract things : categories

9 Classes from nouns start with the nouns used in the problem description as candidate names for classes. –Remove duplication by eliminating synonyms –Add names that must be present. –Eliminate candidates that seem to be beyond the purpose of program.

10 Test before code Starting to write a class is difficult, too. You need to know what messages the object responds to. Write a test for the class before you write any code for the class. The test is a user of the class Use the test to specify some intended behavior of the class

11 Test You still have to write the code. That may seem like an efficient way to work! –write code for several hours without compiling and testing –then shift into a mode where you compile and debug for a while. But that efficiency is probably an illusion. The longer you write code without compiling, the more errors your code is likely to contain.

12 Test Taking small steps –Start with a single test case. –See what objects are needed to satisfy the test. –Write just enough code to make the test pass.

13 Leading test Some tests are trivial, others are very hard. How do I know which test to write next? Pick a test –helps you know when you have successfully implemented the next piece of functionality –require some new code, but not a lot of new code. –Favor easy tests over hard ones, and important operations over unimportant ones.

14 Specification Before You Code You have read the programming assignment More questions than are answered in this initial document You would like to clarify your idea of what you are being asked to do. Write a specification before you begin to write code.

15 Specification Before You Code This specification can be a more careful description of the solution enumerates classes and possible methods To specify the solution with a UML diagram

16 Class must have behavior A class must have some behavior That distinguishes it from other objects that currently exist specialized comparisons represent behavior

17 Public methods first An object exists to serve a purpose to other objects. The essential parts of any class are the methods. the methods allow an object to do the things that other objects expect it to do. A class may end up having private methods, but other objects won't know that.

18 Public methods first The public methods specify the behavior of the object. They implement the behavior that other objects will see. write the public methods of your class first. Write just enough code to implement a method that satisfies a test.

19 Method Objects do things, and objects know things. a client that uses an object will send it messages –asking it to do something –asking about something it knows. Think of the methods in your class in terms of these two kinds of responsibility: –methods that do things –methods that report what the object knows.

20 Method you may want to write a method that does both. But consider –the object that expects the method only to report something It may send the message and then be surprised that that state of the receiver has changes. –the object that expects the method only to do something. It may send the message and not realize that the value returned carries important information

21 Method design methods so that they either do something or return something If a method does something, give it a name that sounds like a command. If a method reports something, give it a name that describes its result.

22 Delegation A program is a set of objects collaborating to solve a problem. if two objects do the same thing write code that is identical or similar in two different parts of program Duplication of this sort is bad. –When implement the behavior in a different way, then you have to change it in two or more places. –Programmers will have a harder time understanding your program

23 Delegation Therefore, ask another object to do a task if it knows how. Write new code for the part of the behavior that the other object cannot do.

24 Instance variable When do I create an instance variable? Instance variables should always be private outside users won't be able to tell whether a variable exists inside a class. create an instance variable when multiple public methods need to share the same data the history of that data is important to all those methods.

25 The end

26 Examples : Test before code Examples : writing a program to model a toaster making toast. –create a new class named Toaster –A Toaster toasts certain objects of type Bread.

27 Examples : Test before code –a simple test public void testCanSetDoneness() { Toaster toaster = new Toaster(); toaster.setDoneness(Toast.MEDIUM); assertEquals( toaster.getDoneness(), Toast.MEDIUM ); } There is a Toaster class. There is a Toast class. Toast contains a static constant named MEDIUM. Toaster contains a method named setDoneness() that takes a parameter of type compatible with Toast.MEDIUM. Toaster contains a method named getDoneness() that takes no arguments but returns a value of the same type as Toast.MEDIUM Toaster has some way of remembering the degree of doneness so that the value set by setDoneness() can later be retrieved with getDoneness().

28 Examples : Test before code –you probably want to make sure that the bread reflects that level of toasting when the Toaster is done. public void testToastIsToastedToCorrectDegreeOfDoneness() { Toaster toaster = new Toaster(); Bread bread = new RyeBread(); toaster.setDoneness( Toast.MEDIUM ); assertEquals( bread.getDoneness(), Toast.NOT_TOASTED ); toaster.toast( bread ); assertEquals( bread.getDoneness(), Toast.MEDIUM ); } – It contains both Bread objects and Toast objects.

29 Examples : Test before code –What is the relationship between them? –Just toast a piece of Bread. public void testToastBread() { Toaster toaster = new Toaster(); Bread bread = new RyeBread(); assertFalse( bread.isToasted() ); toaster.toast( bread ); assertTrue( bread.isToasted() ); } a Bread class an isToasted() method to Bread a toast() method to the Toaster class that takes a Bread object as an argument

30 Examples : Test before code –The only thing we've used the Toast class for is to contain the static final values that indicate doneness –move those values to the Toaster class public void testCanSetDoneness() { Toaster toaster = new Toaster(); toaster.setDoneness( Toaster.MEDIUM ); assertEquals( toaster.getDoneness(), Toaster.MEDIUM ); } This design of the Toaster and Bread classes should feel a lot like design in real life.

31 Examples : Test create a new VirtualPet class, following trivial test case public void testVirtualPetExists(){ VirtualPet pet = new VirtualPet(); assertNotNull(pet); } –Create the corresponding production code: class VirtualPet{ } The test should now compile and pass.

32 Examples : Test –now refactor your test like this private VirtualPet pet; protected void setUp(){ pet = new VirtualPet(); } public void testVirtualPetExists(){ asserNotNull(pet); } This should also pass. –suppose you want your pet to have a name. public void testCanNamePet(){ pet.setName("Igor"); assertSame(pet.getName(), "Igor"); }

33 Examples : Test –make small changes to your VirtualPet class: class VirtualPet{ private String name ; public void setName(String name){ this.name = name; } public String getName(){ return name; } –What happens if you ask for the name of an unnamed VirtualPet?

34 Examples : Test – Perhaps you want to gave an empty String returned. Then test it like this public void testNameOfUnnamedPetIsEmptyString(){ assertSame(pet.getName(),""); } This forces you to go back and initialize the String name in VirtualPet. –Small tests, one at a time.

35 Example : Leading test Example : John Conway's game of life. –It takes place on an infinite grid with square cells. –Each cell has as neighbors the eight cells that touch it. – you can set up the board in any state you wish and start the game.

36 Example : Leading test –The state of the board at each discrete point in time is determined by the state of the board at the previous time by the following rules: A live cell stays alive only if it has two or three live neighbors, otherwise it dies. A dead cell comes to life only if it has three live neighbors, otherwise it remains dead. ○: life ╳: dies

37 Example : Leading test –A leading test to ensure that we can create a new cell and when it is created it will not be alive. public void testNewCellIsDead(){ Cell cell = new Cell(); assertTrue(!cell.isAlive()); } –The next smallest thing to check on is that we can set a cell to be alive. public void testLiveCellIsAlive(){ Cell cell = new Cell(); cell.setAlive(true); assertTrue(cell.isAlive()); }

38 Example : Leading test –A more complicated test to set up might be this: public void testLiveCellWithThreeLiveNeighborsStaysAlive(){ cell.setAlive(true); for (int i = 0; i <3; i++){ cell.incrementNumberOfLiveNeighbors(); } assertTrue(!cell.needsToChange()); }