Lecture 4 Lecture notes and files
Here I will try to post the most important ideas from each lecture. Not
all ideas will be posted here. I'll also try to include useful sample
files and examples.
Week 1 1/22-1/25
Wednesday
-
Your lecturer is named Tim, and he wants you to enjoy this course,
learn a lot, and have fun in lecture.
-
This can be a very hard class. You need to know the rules and be
familiar with the website.
-
Programs are strings simple commands that are put together to make neat
thing happen
-
Some commands can do many things depending on the input, or parameters, they
take.
Friday
-
I wasn't kidding about pop quizzes--they will come very frequently.
-
I take your suggestions on how to run this course seriously.
-
Complicated programs are made of many interacting parts called objects.
-
Each object has it's own list of abilities, which are called methods.
-
Various methods have different types of inputs (parameters) and outputs
(parameters)
The drawing that only takes a few turtle commands to create is
here
Week 2 1/28-2/1
An outline of
Chapter
2 by Aneesh is here
Monday
-
Computer Science is the art, theory, and practice of programming.
-
High level code is for humans, low-level for machines. A compiler
converts the code for us.
-
Understanding errors is important. There are lots and knowing them
helps
-
To declare a variable, you need a valid type and a valid name
-
To assign a value, you need a valid variable and a value of the correct type
Wednesday
-
Each method has a list of parameters (inputs). Each
has it's own type.
-
Methods must be called with the correct number and types
of parameters (in the right order) to compile
-
Methods has a certain type of return value.
-
Constructors also take parameters
-
You can hold reasonably sized integers in int. Java
doubles are only approximate.
Friday
-
Be sure to understand compiler errors--what causes them,
etc.
-
The Java API is great gives you all you need to know to
understand a class.
-
Testing code is important--otherwise your code will
probably not work
-
Code is grouped into packages. Code in other
packages is accessed through import statements.
The compiler challenge (corrected) is found
here.
The answers are
here.
Week 3 2/4-2/8
Aneesh's outline of
Chapter
3 is found here
Monday
-
Each object of a given class stores its information in instance fields
-
Inside a class declaration we put instance fields, methods and constructors
-
The syntax for an instance field includes an access type, a type, and a name
-
The syntax for methods includes an access type, a return type, a name, a
parameter list, and the code to execute
-
The syntax for constructors includes an access type, the name (the same as
the class name), a list of parameters and the code to execute
-
The syntax for using things and for declaring things is completely different
The class we designed today is here:
Team.java
Here is another class
Human.java
and the associated
javadoc
The general syntax for a class is
here
from Rebecca Hasti
An annotated example class is found
here
from Will
Wednesday
-
Local variables are for holding intermediate values
-
Instance fields hold the data for each Object
-
Parameters are inputs to functions
-
Primitive variables (int, double, float, char, short, byte, boolean, long)
hold the actual value
-
Object variables hold a reference to the object
The object practice with the Team class is
here
Another explanation of the three types of variables is
here
from Will
Another explanation of primitive vs. reference is found
here
Constructing classes, declaring and
writing methods, declaring instance fields
Friday
-
Choose the right type of number for your job. You may need to cast
when you might lose precision.
-
Operators take some values of a certain type and returns a value of a
certain type
-
Expressions can be chained together as long as we want
-
Code tracing is an important skill you should be able to do yourself.
Here is a code tracing
challenge
and the
solution
from Andrew.
Here's another
challenge
and another
solution
from Andrew.
Week 4 2/11-2/15
Monday
-
final means that the value can't be changed once it is set
-
static means that the variable or method belongs to the class.
-
You may call a static method on a class.
-
Naming constants (often public static final) can make your code easier to
read and maintain.
Here is the
annotated
solution to the in-class Team code-tracing challenge
Wednesday
-
Scanners are a good way to take input
-
Scanners are in the java.util package and have useful methods--look them up
if you need them
-
There are lots of great String methods--look them up if you need them
-
If there is a static variable, all of the instances share that instance
-
++, +=, -= and -- are increment and decrement functions. Use them as
statements, not expressions
Friday
-
static methods shouldn't be called on instances
-
Booleans are either true or false
-
>,<,<=,>=,||,&&,!,==,!= all return booleans
-
booleans are really valuable types
Week 5 2/18-2/22
Monday
-
Truth tables are a nice way to represent boolean functions
-
Boolean expressions can sometimes be simplified by laws of logic
-
methods can return booleans
-
== checks only if two Objects are precisely the same in memory
-
Use .equals() to compare Strings. Same with many other Objects
-
.equals() often needs to be written to be useful
Wednesday
-
if statements require booleans as conditions
-
if statements let us execute code only on a given condition.
-
If we wish to execute something only if the if statement does not execute,
we need an else
-
You don't need to use braces for ifs and elses, but do it anyway.
-
else ifs are just a combination of standard if-else syntax
Friday
-
The one point questions require you to know stuff
-
Assignments require a variable to exist, but accessible (static? private?),
be non-final, match the type of the expression and the expression must
compile
-
Method calls require the object to exist, be initialized, have the method
exist, the method be accessible, have the correct number order and types of
parameters, and have the parameters compile.
-
We still have more to review
Week 6
2/25-2/29
Monday
-
You can review almost all of CS302 in one go
-
One simple class can show many concepts
-
The instantiable class on the exam is not so bad if
you figure out the data members
Here is
Bus.java
and
BusApp.java
as well which we covered in class.
Wednesday
-
Just as you can get overflow if numbers get to big, you can get underflow if
numbers get too close to 0.
-
UML lets programmers visualize concepts
-
Methods can call themselves. If you know what you are doing you can
use this to do things repeatedly
-
Objects can hold Objects of the same type as data members. If you know
what you are doing, you can hold data conveniently.
-
Stack frames are diagrams that can help you track memory precisely.
We worked with the recursive
Node
class.
Friday
-
More on the Node class, which is not how we will do iteration or data
-
again, else-ifs are used for checking multiple conditions. They are a
combination of previous syntax
-
if-else statements can be nested
Week 7
3/3-3/7
Monday
-
Instead of using else-if statements, we can use switch statements on
primitive values
-
Use default to catch remaining cases in a switch
-
If you don't include break statements, the code will continue past labels,
which you usually don't want.
-
Doing things repeatedly is a fundamental operation of computers.
-
The while loop takes a boolean as a condition and keeps executing as long as
the boolean evaluates to true
-
The while loop has similar syntax to an if statement
-
You don't need braces, but add them anyway
Wednesday
-
When you wish to run a loop at least once, you may want a do-while loop
-
When you want to run a loop for a fixed number of times (a count controlled
loop) use a for loop
-
You can nest loops just as you can nest if statements
Here is the (fixed)
LoopExamples
class with the poorly named methods. Here is the
explanation
of the methods.
Here's a
summary
of java syntax for loops, if-else statements and switch statement
Here's
the
chart that shows the right structure to use to get it to run a certain
number of times.
Friday
-
Debuggers are extremely powerful tools for finding errors
-
Using print statements can also be quite useful for finding errors
-
A loop that runs until it sees a special signal is called a sentinal
controlled loops
-
Following code through loops can be tricky and it requires some patience
-
Infinite loops, off-by-one errors are common. Some errors can be
avoided by always including braces
Week 8: 3/10-3/14
Monday
-
Arrays are lists used to hold multiple related values
-
Arrays can't change size. They are object and must be created with new
and a specific size.
-
You access elements of arrays by passing in an index. You can access
and change elements
-
You can access all elements of an array with a for loop
-
You can make random numbers with Math.random() or the
Random
class
Here's the
Tabloid
headline generator we discussed
Here's a list of
generated
headlines
Wednesday
-
If a field and a parameter share the same name, just writing the name gives
the parameter. Local varaibles also beat fields.
-
The extension does not affect the type of the file, but it should probably
match.
-
Arrays must be declared and initialized like other variables.
-
Arrays must be created with int sizes.
-
Putting things in arrays you must match types.
-
The compiler checks types, not values. Bad indexes are runtime errors.
Here's the array error
practice
Here's the array error
answers
Friday
-
You can specify the exact values of an array if you create it in a special
way
-
All objects can be passed to println even though there is not hundreds of
methods in the PrintStream class because all objects also have type Object.
-
Since each object have type Object, it shares all the methods from the
Object class--toString(), equals(), etc.
-
ArrayLists (WHICH YOU CAN'T USE IN THIS COURSE) hold many useful array
functions and can hold any Object.
-
We are not trying to teach you the easiest way to code everything. We
are trying to teach you to think like a programmer and to understand what
you are writing.
-
If you want to put primitives (which aren't Objects) in an ArrayList or
similar class (or in some other way treat them as Objects, you need a
wrapper class, like Integer, Double, and Float. They are very simple
classes.
Week 9: 3/24-3/28
Monday
Common array algorithms, array practice
ArrayList (CAN'T USE THESE FOR EXAMS)
Wednesday
Growing and shrinking arrays
Friday
2D arrays and higher
Week 10: 3/31-4/4
Monday
Designing classes, software life cycle, organizing classes, cohesion and
coupling
Wednesday
More static, more scope, regular expressions
Friday
accessors, mutators and side effects. Pass by value vs reference
Last updated 3-12-2008