![Page 1: 1 Some Patterns of Novice Programs Author : Eugene Wallingford ; Dan Steinberg ; Robert Duvall ; Ralph Johnson Source : PLoP 2004 Advisor : Ku-Yaw Chang](https://reader035.vdocuments.mx/reader035/viewer/2022070406/56649e035503460f94aef0bb/html5/thumbnails/1.jpg)
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 : E9406008
![Page 2: 1 Some Patterns of Novice Programs Author : Eugene Wallingford ; Dan Steinberg ; Robert Duvall ; Ralph Johnson Source : PLoP 2004 Advisor : Ku-Yaw Chang](https://reader035.vdocuments.mx/reader035/viewer/2022070406/56649e035503460f94aef0bb/html5/thumbnails/2.jpg)
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
![Page 3: 1 Some Patterns of Novice Programs Author : Eugene Wallingford ; Dan Steinberg ; Robert Duvall ; Ralph Johnson Source : PLoP 2004 Advisor : Ku-Yaw Chang](https://reader035.vdocuments.mx/reader035/viewer/2022070406/56649e035503460f94aef0bb/html5/thumbnails/3.jpg)
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
![Page 4: 1 Some Patterns of Novice Programs Author : Eugene Wallingford ; Dan Steinberg ; Robert Duvall ; Ralph Johnson Source : PLoP 2004 Advisor : Ku-Yaw Chang](https://reader035.vdocuments.mx/reader035/viewer/2022070406/56649e035503460f94aef0bb/html5/thumbnails/4.jpg)
4
Patterns Getting started
– Good start– Classes from nouns
Writing tests– Test before code– Test– Leading test– Specification before code
![Page 5: 1 Some Patterns of Novice Programs Author : Eugene Wallingford ; Dan Steinberg ; Robert Duvall ; Ralph Johnson Source : PLoP 2004 Advisor : Ku-Yaw Chang](https://reader035.vdocuments.mx/reader035/viewer/2022070406/56649e035503460f94aef0bb/html5/thumbnails/5.jpg)
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
![Page 6: 1 Some Patterns of Novice Programs Author : Eugene Wallingford ; Dan Steinberg ; Robert Duvall ; Ralph Johnson Source : PLoP 2004 Advisor : Ku-Yaw Chang](https://reader035.vdocuments.mx/reader035/viewer/2022070406/56649e035503460f94aef0bb/html5/thumbnails/6.jpg)
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.
![Page 7: 1 Some Patterns of Novice Programs Author : Eugene Wallingford ; Dan Steinberg ; Robert Duvall ; Ralph Johnson Source : PLoP 2004 Advisor : Ku-Yaw Chang](https://reader035.vdocuments.mx/reader035/viewer/2022070406/56649e035503460f94aef0bb/html5/thumbnails/7.jpg)
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.
![Page 8: 1 Some Patterns of Novice Programs Author : Eugene Wallingford ; Dan Steinberg ; Robert Duvall ; Ralph Johnson Source : PLoP 2004 Advisor : Ku-Yaw Chang](https://reader035.vdocuments.mx/reader035/viewer/2022070406/56649e035503460f94aef0bb/html5/thumbnails/8.jpg)
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
![Page 9: 1 Some Patterns of Novice Programs Author : Eugene Wallingford ; Dan Steinberg ; Robert Duvall ; Ralph Johnson Source : PLoP 2004 Advisor : Ku-Yaw Chang](https://reader035.vdocuments.mx/reader035/viewer/2022070406/56649e035503460f94aef0bb/html5/thumbnails/9.jpg)
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.
![Page 10: 1 Some Patterns of Novice Programs Author : Eugene Wallingford ; Dan Steinberg ; Robert Duvall ; Ralph Johnson Source : PLoP 2004 Advisor : Ku-Yaw Chang](https://reader035.vdocuments.mx/reader035/viewer/2022070406/56649e035503460f94aef0bb/html5/thumbnails/10.jpg)
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
![Page 11: 1 Some Patterns of Novice Programs Author : Eugene Wallingford ; Dan Steinberg ; Robert Duvall ; Ralph Johnson Source : PLoP 2004 Advisor : Ku-Yaw Chang](https://reader035.vdocuments.mx/reader035/viewer/2022070406/56649e035503460f94aef0bb/html5/thumbnails/11.jpg)
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.
![Page 12: 1 Some Patterns of Novice Programs Author : Eugene Wallingford ; Dan Steinberg ; Robert Duvall ; Ralph Johnson Source : PLoP 2004 Advisor : Ku-Yaw Chang](https://reader035.vdocuments.mx/reader035/viewer/2022070406/56649e035503460f94aef0bb/html5/thumbnails/12.jpg)
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.
![Page 13: 1 Some Patterns of Novice Programs Author : Eugene Wallingford ; Dan Steinberg ; Robert Duvall ; Ralph Johnson Source : PLoP 2004 Advisor : Ku-Yaw Chang](https://reader035.vdocuments.mx/reader035/viewer/2022070406/56649e035503460f94aef0bb/html5/thumbnails/13.jpg)
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.
![Page 14: 1 Some Patterns of Novice Programs Author : Eugene Wallingford ; Dan Steinberg ; Robert Duvall ; Ralph Johnson Source : PLoP 2004 Advisor : Ku-Yaw Chang](https://reader035.vdocuments.mx/reader035/viewer/2022070406/56649e035503460f94aef0bb/html5/thumbnails/14.jpg)
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.
![Page 15: 1 Some Patterns of Novice Programs Author : Eugene Wallingford ; Dan Steinberg ; Robert Duvall ; Ralph Johnson Source : PLoP 2004 Advisor : Ku-Yaw Chang](https://reader035.vdocuments.mx/reader035/viewer/2022070406/56649e035503460f94aef0bb/html5/thumbnails/15.jpg)
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
![Page 16: 1 Some Patterns of Novice Programs Author : Eugene Wallingford ; Dan Steinberg ; Robert Duvall ; Ralph Johnson Source : PLoP 2004 Advisor : Ku-Yaw Chang](https://reader035.vdocuments.mx/reader035/viewer/2022070406/56649e035503460f94aef0bb/html5/thumbnails/16.jpg)
16
Class must have behavior A class must have some behavior That distinguishes it from other objects
that currently exist specialized comparisons represent
behavior
![Page 17: 1 Some Patterns of Novice Programs Author : Eugene Wallingford ; Dan Steinberg ; Robert Duvall ; Ralph Johnson Source : PLoP 2004 Advisor : Ku-Yaw Chang](https://reader035.vdocuments.mx/reader035/viewer/2022070406/56649e035503460f94aef0bb/html5/thumbnails/17.jpg)
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.
![Page 18: 1 Some Patterns of Novice Programs Author : Eugene Wallingford ; Dan Steinberg ; Robert Duvall ; Ralph Johnson Source : PLoP 2004 Advisor : Ku-Yaw Chang](https://reader035.vdocuments.mx/reader035/viewer/2022070406/56649e035503460f94aef0bb/html5/thumbnails/18.jpg)
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.
![Page 19: 1 Some Patterns of Novice Programs Author : Eugene Wallingford ; Dan Steinberg ; Robert Duvall ; Ralph Johnson Source : PLoP 2004 Advisor : Ku-Yaw Chang](https://reader035.vdocuments.mx/reader035/viewer/2022070406/56649e035503460f94aef0bb/html5/thumbnails/19.jpg)
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.
![Page 20: 1 Some Patterns of Novice Programs Author : Eugene Wallingford ; Dan Steinberg ; Robert Duvall ; Ralph Johnson Source : PLoP 2004 Advisor : Ku-Yaw Chang](https://reader035.vdocuments.mx/reader035/viewer/2022070406/56649e035503460f94aef0bb/html5/thumbnails/20.jpg)
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
![Page 21: 1 Some Patterns of Novice Programs Author : Eugene Wallingford ; Dan Steinberg ; Robert Duvall ; Ralph Johnson Source : PLoP 2004 Advisor : Ku-Yaw Chang](https://reader035.vdocuments.mx/reader035/viewer/2022070406/56649e035503460f94aef0bb/html5/thumbnails/21.jpg)
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.
![Page 22: 1 Some Patterns of Novice Programs Author : Eugene Wallingford ; Dan Steinberg ; Robert Duvall ; Ralph Johnson Source : PLoP 2004 Advisor : Ku-Yaw Chang](https://reader035.vdocuments.mx/reader035/viewer/2022070406/56649e035503460f94aef0bb/html5/thumbnails/22.jpg)
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
![Page 23: 1 Some Patterns of Novice Programs Author : Eugene Wallingford ; Dan Steinberg ; Robert Duvall ; Ralph Johnson Source : PLoP 2004 Advisor : Ku-Yaw Chang](https://reader035.vdocuments.mx/reader035/viewer/2022070406/56649e035503460f94aef0bb/html5/thumbnails/23.jpg)
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.
![Page 24: 1 Some Patterns of Novice Programs Author : Eugene Wallingford ; Dan Steinberg ; Robert Duvall ; Ralph Johnson Source : PLoP 2004 Advisor : Ku-Yaw Chang](https://reader035.vdocuments.mx/reader035/viewer/2022070406/56649e035503460f94aef0bb/html5/thumbnails/24.jpg)
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.
![Page 25: 1 Some Patterns of Novice Programs Author : Eugene Wallingford ; Dan Steinberg ; Robert Duvall ; Ralph Johnson Source : PLoP 2004 Advisor : Ku-Yaw Chang](https://reader035.vdocuments.mx/reader035/viewer/2022070406/56649e035503460f94aef0bb/html5/thumbnails/25.jpg)
25
The end
![Page 26: 1 Some Patterns of Novice Programs Author : Eugene Wallingford ; Dan Steinberg ; Robert Duvall ; Ralph Johnson Source : PLoP 2004 Advisor : Ku-Yaw Chang](https://reader035.vdocuments.mx/reader035/viewer/2022070406/56649e035503460f94aef0bb/html5/thumbnails/26.jpg)
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.
![Page 27: 1 Some Patterns of Novice Programs Author : Eugene Wallingford ; Dan Steinberg ; Robert Duvall ; Ralph Johnson Source : PLoP 2004 Advisor : Ku-Yaw Chang](https://reader035.vdocuments.mx/reader035/viewer/2022070406/56649e035503460f94aef0bb/html5/thumbnails/27.jpg)
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().
![Page 28: 1 Some Patterns of Novice Programs Author : Eugene Wallingford ; Dan Steinberg ; Robert Duvall ; Ralph Johnson Source : PLoP 2004 Advisor : Ku-Yaw Chang](https://reader035.vdocuments.mx/reader035/viewer/2022070406/56649e035503460f94aef0bb/html5/thumbnails/28.jpg)
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.
![Page 29: 1 Some Patterns of Novice Programs Author : Eugene Wallingford ; Dan Steinberg ; Robert Duvall ; Ralph Johnson Source : PLoP 2004 Advisor : Ku-Yaw Chang](https://reader035.vdocuments.mx/reader035/viewer/2022070406/56649e035503460f94aef0bb/html5/thumbnails/29.jpg)
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
![Page 30: 1 Some Patterns of Novice Programs Author : Eugene Wallingford ; Dan Steinberg ; Robert Duvall ; Ralph Johnson Source : PLoP 2004 Advisor : Ku-Yaw Chang](https://reader035.vdocuments.mx/reader035/viewer/2022070406/56649e035503460f94aef0bb/html5/thumbnails/30.jpg)
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.
![Page 31: 1 Some Patterns of Novice Programs Author : Eugene Wallingford ; Dan Steinberg ; Robert Duvall ; Ralph Johnson Source : PLoP 2004 Advisor : Ku-Yaw Chang](https://reader035.vdocuments.mx/reader035/viewer/2022070406/56649e035503460f94aef0bb/html5/thumbnails/31.jpg)
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.
![Page 32: 1 Some Patterns of Novice Programs Author : Eugene Wallingford ; Dan Steinberg ; Robert Duvall ; Ralph Johnson Source : PLoP 2004 Advisor : Ku-Yaw Chang](https://reader035.vdocuments.mx/reader035/viewer/2022070406/56649e035503460f94aef0bb/html5/thumbnails/32.jpg)
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");
}
![Page 33: 1 Some Patterns of Novice Programs Author : Eugene Wallingford ; Dan Steinberg ; Robert Duvall ; Ralph Johnson Source : PLoP 2004 Advisor : Ku-Yaw Chang](https://reader035.vdocuments.mx/reader035/viewer/2022070406/56649e035503460f94aef0bb/html5/thumbnails/33.jpg)
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?
![Page 34: 1 Some Patterns of Novice Programs Author : Eugene Wallingford ; Dan Steinberg ; Robert Duvall ; Ralph Johnson Source : PLoP 2004 Advisor : Ku-Yaw Chang](https://reader035.vdocuments.mx/reader035/viewer/2022070406/56649e035503460f94aef0bb/html5/thumbnails/34.jpg)
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.
![Page 35: 1 Some Patterns of Novice Programs Author : Eugene Wallingford ; Dan Steinberg ; Robert Duvall ; Ralph Johnson Source : PLoP 2004 Advisor : Ku-Yaw Chang](https://reader035.vdocuments.mx/reader035/viewer/2022070406/56649e035503460f94aef0bb/html5/thumbnails/35.jpg)
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.
![Page 36: 1 Some Patterns of Novice Programs Author : Eugene Wallingford ; Dan Steinberg ; Robert Duvall ; Ralph Johnson Source : PLoP 2004 Advisor : Ku-Yaw Chang](https://reader035.vdocuments.mx/reader035/viewer/2022070406/56649e035503460f94aef0bb/html5/thumbnails/36.jpg)
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
![Page 37: 1 Some Patterns of Novice Programs Author : Eugene Wallingford ; Dan Steinberg ; Robert Duvall ; Ralph Johnson Source : PLoP 2004 Advisor : Ku-Yaw Chang](https://reader035.vdocuments.mx/reader035/viewer/2022070406/56649e035503460f94aef0bb/html5/thumbnails/37.jpg)
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());
}
![Page 38: 1 Some Patterns of Novice Programs Author : Eugene Wallingford ; Dan Steinberg ; Robert Duvall ; Ralph Johnson Source : PLoP 2004 Advisor : Ku-Yaw Chang](https://reader035.vdocuments.mx/reader035/viewer/2022070406/56649e035503460f94aef0bb/html5/thumbnails/38.jpg)
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());
}