CPS109 C01 Ferworn
CPS109 C01 Ferworn
Class Notes 1
Alexander Ferworn
Table of Contents
1 How this course works ..................................................................................................................... 2
2 What is computer science? ............................................................................................................. 2
2.1 Computer Science Misconceptions ........................................................................................ 3
2.1.1 Computer Science is not the study of computers ........................................................ 3
2.1.2 Computer Science is not the study of the programming of computers ..................... 4
2.1.3 Computer Science is not the study of the uses and applications of computers and
software 5
2.1.4 Computer Science is not the study of problem solving via mechanisms ................. 5
2.2 Computer Science is the Study of Algorithms ...................................................................... 6
2.2.1 An example: The Walking Problem ................................................................................ 7
2.3 Pseudocode ............................................................................................................................... 8
3 Computer Science Concept: Abstraction ...................................................................................... 9
4 Computer Science Concept: Conceptual Models and Modeling ............................................... 9
5 What is a Computer? ...................................................................................................................... 10
5.1 Potential Definitions ................................................................................................................ 10
5.2 Digital vs. Analog .................................................................................................................... 10
5.3 Acting like a Computer ........................................................................................................... 10
5.4 The Von Neuman Architecture (the modern computer) .................................................... 11
5.4.1 The Von Neuman Architecture Hardware Components ........................................... 12
5.4.2 Input/Output peripherals ................................................................................................ 13
5.4.3 Memory ............................................................................................................................. 13
5.4.4 Processing Unit ............................................................................................................... 13
5.5 Hooking the whole thing up ................................................................................................... 14
6 Computer Programs ....................................................................................................................... 14
6.1 Binary and Machine Codes ................................................................................................... 14
6.2 Language Interpretation ......................................................................................................... 15
6.2.1 Operations and Operands ............................................................................................. 15
6.3 Assembly Language ............................................................................................................... 16
6.3.1 Translation and Interpretation ....................................................................................... 17
6.4 High-Level languages............................................................................................................. 17
6.4.1 Compilation, linking and executing ............................................................................... 18
6.5 A program for walking ............................................................................................................ 19
7 What is Java? .................................................................................................................................. 20
7.1 History ....................................................................................................................................... 20
School of Computer Science
CPS109 Course Notes Set 1
Alexander Ferworn Updated Fall 15
You will find that this document and all the documents that you see in this course
support the lectures in this subject. I have liberally stolen bits and pieces out of a lot of
publications and have put a lot of work into it. This means that the best thing that you
can do is to print out each document, as it becomes relevant (and available from the
web site) and bring it to class with you.
I guarantee that you will see me using these documents to support any discussion in
class.
When we ask this question, consider that we don't have car science or telephone
School of Computer Science
CPS109 Course Notes Set 1
Alexander Ferworn Updated Fall 15
science, dishwasher science, etc. Perhaps we should ask whether or not computer
science is part of some other disciplines like mathematics, physics, neuro-science,
electrical engineering, linguistics, logic or philosophy. In fact, some computer scientists
consider this to be the case and consider it their goal to so thoroughly integrate the
various parts of computer science back into its parent disciplines so that it no longer
has a separate identity as a separate science. Well judging by what is happening in the
world today that is unlikely to happen.
In fact, I will go a little further to suggest that even before the term “computer” even
existed, the concept of “programmer” existed—if I can be granted some leeway as to
what the programmer was programming—and the first programmer was also a woman.
Ada Lovelace (1815-1852) was the only legitimate child of Annabella Milbanke and the
poet Lord Byron. Her mother, Lady Byron, had mathematical training (Byron called her
his 'Princess of Parallelograms') and insisted that Ada, who was tutored privately, study
mathematics too.
Ada met Charles Babbage at a party in 1833 when she was seventeen and was
entranced when Babbage demonstrated the small working section of his “Analytical
Engine” to her. In1843 she published a translation from the French of an article on the
Analytical Engine by an Italian engineer, Luigi Menabrea, to which Ada added
extensive notes of her own. The Notes included the first published description of a
stepwise sequence of operations for solving certain mathematical problems and Ada is
School of Computer Science
CPS109 Course Notes Set 1
Alexander Ferworn Updated Fall 15
Ada speculated that the Engine “might act upon other things besides numbers... the
Engine might compose elaborate and scientific pieces of music of any degree of
complexity or extent”. The idea of a machine that could manipulate symbols in
accordance with rules and that number could represent entities other than quantity
mark the fundamental transition from calculation to computation. She has been referred
to as 'prophet of the computer age'. Certainly she was the first to express the potential
for computers outside mathematics1.
Given this example, one might correctly surmise that much of the theoretical basis for
computer science was developed before computers existed, so a more inclusive
definition is needed.
1
Adapted from http://www.computerhistory.org/babbage/adalovelace/
School of Computer Science
CPS109 Course Notes Set 1
Alexander Ferworn Updated Fall 15
In the previous paragraph a number of terms were used that you may not be familiar
with. Do not worry about this for the moment as definitions for most of these will be
given later.
2.1.3 Computer Science is not the study of the uses and applications of
computers and software
Since the personal computer became widely available in the early 1980's, the pervasive
application of computers to nearly all fields has popularized the view that computer
science is concerned primarily with computer applications. It is thought by some that
computers, without their widespread general applicability to a variety of fields, would
not constitute a sufficient body of knowledge to warrant being considered a body of
scientific information. They also argue that it is sufficient to be trained in a few computer
applications software packages in order to work as a computer specialist.
Clearly, the field of computer science is much larger than just computer application
software--we do more than just “apps”!
A plan is a detailed proposal for achieving a goal. It is a means for gaining a common
understanding of how a goal will be achieved. Because humans are the target audience
many components of a plan are assumed to be understood by all the humans hearing
it, the plan tends to be descriptive of what needs to happen to achieve success and
does not necessarily prescribe how every step is to be achieved.
Using this definition one might consider some of the underlying areas of computer
science to involve the design, structure, analysis, efficiency and correctness of
algorithms, the mathematical properties of algorithms, the data they manipulate, the
languages in which they are implemented. Finally, one might study the limitations of
algorithms.
Starting from the standing position, lift one leg and lean forward, letting the leg swing
past the stationary leg until your foot contacts the floor. Push forward with the other
foot, lifting your leg and swinging it forward until it passes the first leg and the other foot
contacts the floor. Repeat these motions until it is time to stop.
Each leg is capable of creating a motive force at the hip to propel a leg forward
with sufficient force to achieve angle Θ.
Where:
M: hip pivot point at the joint,
K: knee,
H: height of the leg from M to surface being walked on,
R: raised foot distance,
L0: H-R,
L1: leg 1 with height H
L2: leg 2 with height H
Θ: Angle from vertical for leg Ln
Τ: Torque applied at M to achieve Θ
2.3 Pseudocode
Algorithms present the solution to a problem as a logical sequence of steps, usually
presented with rigor and mathematical notation. In many instances, it is possible to
translate an algorithm directly into a computer programming language. Sometimes it is
desirable to clarify an algorithm by first writing it in Pseudocode: an English-like
language which has no formal definition but might be best described as “programmer
slang”.
School of Computer Science
CPS109 Course Notes Set 1
Alexander Ferworn Updated Fall 15
The reasons for making this intermediate translation is to make it a little easier to
understand some of the notation for people who might be somewhat unfamiliar with the
formalities of algorithms and to start the design process that informally employs
programming language constructs (like decisions) and will eventually become a
computer program.
avg(xi).
f'(x).
If we examine our walking algorithm from the previous section we used abstraction
repeatedly. For example the term L was used for a leg. A human leg is very complex,
we do not know how it works exactly but when we name it L, we get a kind of power
over the concept and it can be used without further definition (unless we have to
describe an algorithm to create one).
2
Adapted from https://en.wikipedia.org/wiki/Conceptual_model_(computer_science)
School of Computer Science
CPS109 Course Notes Set 1
Alexander Ferworn Updated Fall 15
Computer scientists build conceptual models so that they can study, analyze and
predict the behavior of the systems they are studying. Wherever possible, these models
will be designed using some notation that is useful, not only for thinking and analysis,
but also as a means of implementing that model on a computer.
In our walking algorithm, you will notice that the diagram showing the stick figures is
actually a model of what the system is doing at any particular time.
5 What is a Computer?
Now that we have the computer science mumbo-jumbo out of the way we still have a
problem. What is a computer? Well we could use a dictionary to help us.
3
A big word indicating the functional components of a computer and how they interact.
School of Computer Science
CPS109 Course Notes Set 1
Alexander Ferworn Updated Fall 15
everyday device and a simplified problem. The device is the common numeric
handheld calculator and the problem is to add a set of numbers (3) together.
4,
7, and
9.
The concept of "add a set of numbers together" is in fact a series of discrete operations
performed in a particular order that form an algorithm. The steps are :
The data is
1 4
2 7
3 9
"If people do not believe that mathematics is simple, it is only because they do not
realize how complicated life is."
John Von Neuman
This automating of the calculation process allows it to proceed at the native speed of
the calculating device rather than the speed of the human operating the calculator. This
is extremely significant in that refinements in the computing process are left to the
engineers or "plumbers". They can concentrate on the refinement of their device and
"we" can concentrate on its application or "cooking" (Did I just equate engineers with
plumbers?)
Thus was born the modern computer that has remained more or less unchanged apart
from the process refinements in the manufacturing of integrated circuits that is the art
that keeps things hopping in the computer hardware field. Computers have become
faster, cheaper, physically smaller and logically larger at an exponential rate.
Input/Output Peripherals
Memory
Processing Unit(s)
School of Computer Science
CPS109 Course Notes Set 1
Alexander Ferworn Updated Fall 15
5.4.3 Memory
Computer memories are capable of storing data over time.
The memory of a computer is not a single entity (although in many cases you can think
of it that way). It is a collection of units dispersed in space and function with a spectrum
of speeds, capacities and costs.
Memory can only store bits because the tiny circuitry that makes up each spot in
memory only knows how to be on or off (1 or 0) (Actually on or off or +5V or 0V).
Primary memory refers to memory that is typically on-board the circuit board that is
your CPU board. Secondary memory is everything else.
If you forget all that, you can think of memory as a long narrow sheet of paper. You can
only write one thing on each line of the paper and then you must go to the next line.
The same is true for reading it. So, to get from any one line to any other you just need
to add or subtract an integer value from where you are to where you want to go....you
can jump around in memory.
All these devices share data across a bunch of wires called a bus. The width of a bus
is the amount of bits that it can carry in parallel at once. So, a 16-bit bus can carry 16
bits at once.
6 Computer Programs
A program is an algorithm described to a computer. How this happens is a very long
story but no matter how you tell the story, you must remember 2 things: Computers are,
exceedingly fast
surprisingly stupid
Of course, binary digits (bits) can only be on or off, so some clever people came up
with the notion of grouping several bits together. Now you can encode more than two
things in this way (Why is it that you can only encode 2 things with 1 bit?).
Suppose you had two bits grouped together. You could represent 4 things. For example
we could encode how well you are doing in this course.
Mark Meaning
11 Excellent work
10 Good work
01 Poor work
00 Oh no
School of Computer Science
CPS109 Course Notes Set 1
Alexander Ferworn Updated Fall 15
The standard is to group 8 bits into something called a byte. 8 bits give you the
possibility to represent 256 things (0 to 255).
To confuse things a bit more, each byte can represent either a number (data) or an
instruction. Forget this for the moment and we will assume each byte is a number
representing an instruction. Instructions represented in this way are called byte codes.
If you make the bytes represent words you can tell a computer about your algorithm.
Here is an example dictionary of bytes/words (this is not a real machine language just
an example).
Example Operations
School of Computer Science
CPS109 Course Notes Set 1
Alexander Ferworn Updated Fall 15
Example Operands
00000000 DOOR
00000001 ROOM
00000010 SHEETS
00000011 BED
00000100 BROTHER
00000101 TOPOFBED
00000110 HALL
00000111 BOTTOMOFBED
We could tell the computer our algorithm for dragging us to bed (see above)
00000000 00000000 00000001
00000101 00000011
00000011 00000010
00000010 00000010 00000111
00000110 00000010
00000101 00000100
00000011 00000100
00000101 00000101
00000110 00000100
00000011 00000010
00000101 00000101
00000110 00000010
00000101 00000110
improvement was assembly language that shortened all the commands into assembly
codes and grouped some of the related stuff together.
High-level languages hide some of the obscurity of the machine. In Machine Code and
Assembly Language the programmer must specify everything. This is a problem
because many times the same code is written again and again which could lead to
errors as things are mistyped or later modified but only in some places. In addition
machine code and assembly languages are specific to individual CPUs. If you move
from one type of CPU to another the codes can be quite different.
School of Computer Science
CPS109 Course Notes Set 1
Alexander Ferworn Updated Fall 15
Higher level languages (like Java) avoid this problem by making each command group
together a whole bunch of lower-level machine codes--you don’t have to keep repeating
these codes yourself--avoiding introducing more errors.
import brothers.*;
Public class movebrother
{
public static void main(String[] args)
brother = new Brother;
brother.move(“bed”);
brother.cover;
}
There are of course even higher-level languages--the higher you go, the more stuff is
done for you. The repetitious stuff now happens in the background.
It must translate the high-level language program into machine code routines, so that it
can understand it. This is call compiling the program. Each one of those high-level
language commands is replaced with the lower level code it represents. This is great
because the compiler will not allow you to enter a program which is syntactically
incorrect. A syntax error is one in which you break the language rules and is the only
type of error picked up by a compiler
It must get the code resources that the program needs from libraries of common code.
These functions are not actually part of the high-level language but may be written in it.
For example, in C there is no command to sort a list of numbers but you could write a
program to do it. If you wanted to reuse this program you could store it in a library from
which other programs could use it. This is called linking or link editing. You need to
link because the language is only so big and you might need to call sub-programs which
are stored in libraries of commonly used code which you link to. Linking allows you
detect error like--the darn thing isn’t in the library. This is a linking error.
School of Computer Science
CPS109 Course Notes Set 1
Alexander Ferworn Updated Fall 15
Finally the computer is able to execute the translated and linked program--called
execution or go! Note: at this stage the machine code is being executed--not the high
level language. An error at this point is called an execution error and normally has
something to do with the logic of your program--meaning that although what you have
written makes sense syntactically and links fine you still cannot solve the problem
because what you are saying in the program makes no sense semantically. An error
may also mean there is some sort of physical problem. For example if your program is
designed to print something on a printer and you run it without an actual printer attached
to the computer you will receive an execution or run time error.
This site implements a form of machine learning where a genetic algorithm learns how
to walk (kind of drunkenly).
School of Computer Science
CPS109 Course Notes Set 1
Alexander Ferworn Updated Fall 15
7 What is Java?
Java is the high-level programming language we use in this course to teach you some
of the basic concepts of computer science. It shares many of the characteristics of
other high-level programming languages but it also has some interesting distinctions
that we will discuss as we proceed.
7.1 History
The Java programming language and environment is designed to solve a number of
problems in modern programming practice. Java started as a part of a larger project to
develop advanced software for consumer electronics. These devices are small, reliable,
portable, distributed, real-time embedded systems. When developers at Sun
Microsystems started the project they intended to use C++ (another high-level
language), but encountered a number of problems. Initially these were just compiler
technology problems, but as time passed more problems emerged that were best
solved by changing the language being used.
Why, you might wonder, use the intermediate Java bytecode at all? Why not just
distribute the original Java program and let each person compile it into the machine
language of whatever computer they want to run it on? There are many reasons. First
of all, a compiler has to understand Java, a complex high-level language. The compiler
is itself a complex program. A Java bytecode interpreter, on the other hand, is a fairly
small, simple program. This makes it easy to write a bytecode interpreter for a new type
of computer; once that is done, that computer can run any compiled Java program. It
would be much harder to write a Java compiler for the same computer.
Furthermore, Java programs are meant to be downloaded over a network. This leads to
obvious security concerns: you don't want to download and run a program that will
damage your computer or your files. The bytecode interpreter acts as a buffer between
you and the program you download. You are really running the interpreter, which runs
the downloaded program indirectly. The interpreter can protect you from potentially
dangerous actions on the part of that program.
I should note that there is no necessary connection between Java and Java bytecode.
A program written in Java could certainly be compiled into the machine language of a
real computer. And programs written in other languages could be compiled into Java
bytecode. However, it is the combination of Java and Java bytecode that is platform-
independent, secure, and network-compatible while allowing you to program in a
modern high-level object-oriented language.
School of Computer Science
CPS109 Course Notes Set 1
Alexander Ferworn Updated Fall 15
8 Before you can use Java you need to install the JDK
This section is for all those people who own their own computer that does not have
Java installed on it already you will have to install it. Oracle Corporation took over from
Sun Microsystems when they bought the company and now maintains the download
site where you can download the appropriate version for your computer. You will need
to install something called the “Java Development Kit” (JDK). As the JDK tends to be
updated relatively frequently, this site moves around a bit. The easiest way to find it is
to Google it with the search string: “Java Development Kit download” and a screen
something like this will show up.
All Java programs consist of one or more class definitions (wait until we talk about
object orientation). In this course, we will refer to the primary class definition for a Java
application as the controlling class.
A stand-alone Java application requires a method (Hey, there is that object stuff again)
named main in its controlling class. An Applet does not require a main method nor is
one allowed.
The first thing you need to do is somehow create a file with your program on the
computer you are using. If you are using an IDE like blueJ just double click on its icon
to get started.
A window should open on your desktop that looks very much like this:
In this example, I will call our project “Hello”. Projects in BlueJ provide a convenient place to store
related Java code.
School of Computer Science
CPS109 Course Notes Set 1
Alexander Ferworn Updated Fall 15
In order for us to write our program we need to create something called a “Class”. Press the “New
Class” button on the window and call your new Class “Hello”. You will end up with a with a window that
looks like this:
Double click on the Hello box. This will let you start editing your Java program. You will notice there is
an awful lot of Java already in the file. BlueJ tries to be your friend by populating a file with common
Java stuff. At the moment, it is just confusing so delete everything in your Hello file and replace it with
the Java program below.
Once you have pasted the program inside, close the window.
Compiled Java programs are stored in "bytecode" form in a file with an extension of
.class where the name of the file is the same as the name of the controlling class in the
program. In our example above the controlling class (and the only class) is the Hello
class.
In order to generate the “bytecode” you must “compile” your application. This is done by
pressing the “compile” button. If everything goes well (and it rarely does) you will end
up with a window that looks like this:
When a Java application is started, the Java interpreter finds and invokes the main
method in the class whose name matches the name of the class (in this case, Hello).
To run our Hello program, right click on the Hello box and invoke main() as shown
below,
School of Computer Science
CPS109 Course Notes Set 1
Alexander Ferworn Updated Fall 15
is a comment. Comments are a way of describing to yourself and others what the
program does. Sometimes the comment only describes what a small part of the
program does. This is a really useful facility in most programming languages as it
makes it much easier to fixthe code if something goes wrong later on.
/*
Java/C/C++ style multi-line comment
*/
and
The lines,
are a required part of the template. Every program and every applet—every piece of
Java code—you write is part of a class. The first line says that we’re writing a class
named Hello that is a public class, which means that it can be used by anyone.
Every standalone Java program requires a main() method. This is where the Java
interpreter begins running a Java program. The second line of the example declares
this main() method. It says that the method is public, that it has no return value (i.e., its
return value is non-existent or “void”), and that it is passed an array of strings as its
argument. The name of the array is args. The line also says that main() is a static
method. This is all a bunch of mumbo-jumbo right now but as you learn Java you will
see what they mean.
Every standalone Java program you will ever write will contain a line that looks exactly
like this one. (actually, you can change “args” but people don’t).
The line,
System.out.println(“Hello Java!”);
is the line that does something we can notice. The System.out.println() method sends a
line of output to “standard output”, which is usually the screen.
The lined up { and } represent “blocks” of code. A block of code forms the body of
things like classes and methods. Method blocks are always defined within class blocks.
As you can see here, we can “nest” blocks within one another.
Remember, an applet must use the resources of its viewer to run while a Java
application has all the resources it needs to run internally (stand-alone).
The first thing you need to do is the same as what you needed to do to create an
application.
In order to generate the “bytecode” you must compile your applet. Again, press the compile
button in the window. If it compiles without syntax errors, close the window. To run the applet,
right click on the Helloapp box and select “Run Applet”.
This will give you several options on where to run the applet, we will select the applet viewer
built into BlueJ.
School of Computer Science
CPS109 Course Notes Set 1
Alexander Ferworn Updated Fall 15
To do graphics we need access to graphical methods. They live in awt which we also import.
We invoke the drawString method which is part of our object g to put the text on the screen.
g.drawString("Hello Java!",25,50);