Summary Introduction to programming 2 ALL exam summaries
4 views 0 purchase
Course
Introduction to programming 2 (LCX020P05)
Institution
Rijksuniversiteit Groningen (RuG)
The file contains everything important for the exam of Introduction to programming 2. The information is taken from the lecture slides as well as the books and these summaries helped me pass the exam. The file is divided into 7 parts, for each week of the block.
* OO object is a sort of active data type that combines both data and operations
* Objects know stuff (they contain data), and they can do stuff (they have operations)
* Objects interact by sending each other messages (request for an object to
perform one of its operations)
* A graphics window is actually a collection of tiny points called pixels ("picture elements")
* Every object is an instance of some class, and the class describes the properties the instance will
have
* To create a new instance of a class, we use a special operation called a constructor
* A call to a constructor is an expression that creates a brand new object, the general form is as
follows:
<class-name> (<param1> , <param2> , . . . )
* The set of messages that an object responds to are called the methods of the object (methods are
functions that live inside the object), a method is invoked using dot-notation:
,<object> . <method-name> (<param1> , <param2> , . . . )
move (dx , dy) (moves the object dx units in the x direction and dy units in the y direction)
To move the point p to the right 10 units, we could use this statement:
p . move ( 10 , 0)
* First line creates a circle with a center located at the point (100, 100) and a radius of 30
* The second line creates a GraphWin
* The third line is a request for the Circle object circ to draw itself into the GraphWin object win
circ = Circle (Point ( 100 , 100) , 30)
win = GraphWin ( )
circ . draw (win)
* It is possible for two different variables to refer to exactly the same object; changes made to the
object through one variable will also be visible to the other
## Correct way to create two circles , using clone .
LeftEye = Circle (Point (80 , 50) , 5)
leftEye . setFill ('yellow')
leftEye . setOutline ('red')
rightEye = leftEye . clone ( ) # rightEye i s an exact copy of the left
rightEye . Move (20 , 0)
## Turtle example:
import turtle
win = turtle.Screen()
win.setup(width=512, height=512)
my_turtle = turtle.Turtle()
my_turtle.shape('turtle')
my_turtle.forward(50)
* Objects are instances of classes
* Objects are:
○ A collection of related information (data)
○ A set of operations to manipulate that information (methods)
* File handlers:
file_handler = open("test_file.txt", "r")
line = file_handler.readline()
data = file_handler.read()
file_handler.close()
, Week 2 Summary: Programming 2
* Control structures allow us to alter the sequential flow of our program
* Decision structures are a type of control structure that allow a program to execute different
sequences of instructions for different cases allowing the program to select an appropriate
course of action
* Simple if-then structures are called: one-way decisions
* Two-way decisions are if-else statements
* Multi-way decisions: nesting and adding an if-then statement inside another
* A better solution is to use if-elif-else
* For implements a definite loop: you know exactly how many iterations should be performed
*While implements an indefinite loop: the loop must be performed as long as a certain condition
applies (condition is a Boolean expression, just like in if statements)
* <filevar> = open(<name>, <mode>)
‘name’ is a string with the actual file name on the disk
‘mode’ is either ‘r’ or ‘w’ (read or write)
* infile = open("document.txt", "r")
outfile = open("document.txt", "w")
* <file>.read() – returns the entire remaining contents of the file as a single (possibly large, multi-
line) string
* <file>.readline() – returns the next line of the file. This is all text up to and including the next
newline character
* <file>.readlines() – returns a list of the remaining lines in the file. Each list item is a single line
including the newline characters
* If you open an existing file for writing, you delete the file’s contents
outfile = open("mydata.out", "w")
print(<expressions>, file=outfile)
* Standard Input/Output - allow program to read-from/write-to the console (without opening a
file)
* Using standard input/output or a file depends on program requirements
* Special file-handles that exist in the sys module:
- standard output: sys.stdout
- standard error: sys.stderr
- standard input: sys.stdin
The benefits of buying summaries with Stuvia:
Guaranteed quality through customer reviews
Stuvia customers have reviewed more than 700,000 summaries. This how you know that you are buying the best documents.
Quick and easy check-out
You can quickly pay through credit card or Stuvia-credit for the summaries. There is no membership needed.
Focus on what matters
Your fellow students write the study notes themselves, which is why the documents are always reliable and up-to-date. This ensures you quickly get to the core!
Frequently asked questions
What do I get when I buy this document?
You get a PDF, available immediately after your purchase. The purchased document is accessible anytime, anywhere and indefinitely through your profile.
Satisfaction guarantee: how does it work?
Our satisfaction guarantee ensures that you always find a study document that suits you well. You fill out a form, and our customer service team takes care of the rest.
Who am I buying these notes from?
Stuvia is a marketplace, so you are not buying this document from us, but from seller enk. Stuvia facilitates payment to the seller.
Will I be stuck with a subscription?
No, you only buy these notes for $8.14. You're not tied to anything after your purchase.