Programming Methodology - Stan Flashcards
What is a good comment to put in when programming
Pre condition: what you expect before a method is called
Post condition: what you expect after it is called
What is Top Down Design (eg stepwise refinement etc)
nb think Top Down rather than Bottom Up. Think about the most abstract level and break down from there
Top Down Design
Stepwise refinement is breaking steps/tasks (actions/methods) down into ‘primitives’ until we/the computer understand them
E.g. Morning list
- Get up
- Brush teeth - put toothpaste on brush/move brush on teeth
Breaking these down is referred to as decomposition. This is all top down design
Outline some good programming practices for a method - ie decomposition
Solve one problem at a time
Mathods are usually between 1 and 15 lines. A one line method could be calling another method. This is done when you want to give one method a different name - eg ‘move to wall’ method is changed ‘descent hurdle’ to help visualise the program conceptually
This is related to giving the method good names
Add comments. Add pre- and post- conditions if needed.
What is the OBOB?
The OBOB is the Off By One Bug. When a code does not finish executing due to flawed programming - ie it does everything correct until the last move (eg Karel the Robot)
What is object orientated programme (ie java programme)
An object oriented (ie java) program is a programme that is made of a set of of classes
A class is an encapsulation of behaviour and data
Classes get organised into hierarchies
Superclass \I/ Subclass (this extends the superclass and can be more advanced)
eg classes = Animals -> mammals-> primates-> humans
an instance of a class = a human called “bob”
In object oriented languages these instances are called objects. An object is an instance of a class - a particular example of a class
Classes are the template for objects
eg in Karel, the particular Karel programme (ie superKarel), the robot running around is an instance/object of the superKarel class
Objects interact with each other
What is an instance of a class?
classes = Animals -> mammals-> primates-> humans
eg an instance of a human = a man called ‘bob’
In java is an object. An object is an instance of a class - a particular example of a class
Classes are the template for objects
eg in Karel, the particular Karel programme (ie superKarel), the robot running around is an instance/object of the superKarel class
Objects interact with each other
What is acm. program Hierarchy?
Association for Computing Machinery
Applet (runs on webpage) JApplet Program I ConsoleProgram / Dialog Program / Graphics Program
You extend these kinds of programmes above (these are classes)
nb ConsoleProgram extends Applet…it is an Applet
Give an example how graphics work
Graphics:
A blank canvas loads and you put a collage of objects on the blank canvas:
Objects that exist in the graphics world:
- GLabel
- GRect
- GOval
- GLine
]^ Note that all the above are classes, or templates for objects. You create particular instances of these objects and put them up in the college on the canvas.
eg add( new GLabel( "hello world". 100, 75) ):
Give an example of how a method works using . syntax
GLabel label = new GLablel("Hello, world", 100, 75) ; label.setFont("SansSerif-36");
You create a new label object in the first line and label it
setFont is the method, and you are passing it to the label object. This sets the label’s text to the new font.
What are primitive types?
The types that are built into Swfit / Java
eg int, string, double, boolean, char
Explain how you would describe the following (eg message and receiver)
GLabel label - new GLabel("hello", 100, 150); label.setColor(Color.Red);
label.setColor(Color.Red);
label is the receiver and setColor is the message.
The name of the variable is the receiver, and the method that’s being called is the message.
“I made a method call on the object”
What if you don’t specify a receiver for a message (method)?
eg getWidth()
If you don’t specify a receive for a method, then the receiver is the encapsulating class. eg for the below example, it sends the message to the GraphicsProgram class
eg getWidth()
(returns the width of the graphics window)
What are statements that affect the order in which a program executes commands called?
What are the two types of control statements?
Statements that affect the order in which a program executes commands are called control statements.
- Conditional statements - Conditional statements specify that certain statements in
program should be executed only if a particular condition holds eg if x = 1 { - Iterative statements - Iterative statements specify that certain statements in a program
should be executed repeatedly, forming what programmers call a loop - eg ‘for’ and ‘while’
What is a ‘for’ statement?
A ‘for’ statement is used when you want to repeat a set of commands a predetermined number of times.
What is a ‘while’ statement?
A ‘while’ statement is used when you want to repeat an operation as long as some condition holds.