Review
Programming
-
Machine code is the only language
a processor understands
-
Assembly language uses words
for commands
-
High-level languages are compiled
or interpreted
Programming methodologies
-
Structured programming
-
Object oriented programming
Java
-
object-oriented
-
Stick to "style guide" to enjoy
all benefits!
-
platform independent
-
no recompilation necessary
-
even graphics applications platform
independent
-
robust and secure
-
many compiler errors, few(er)
wrong programs
-
applets run in browser
Programming in practice
-
save often (and under a different
name before major changes!)
-
backup sensibly
Errors
-
syntax errors (compiler errors)
-
runtime errors
-
logic errors
Algorithm
-
unambiguous
-
must execute in finite time
Fundamental Data Types
Number Types
-
integer
-
floating point (double precision)
Symbolic names (Variables)
-
each variable must have a type,
e.g., int count = 0;
-
assignments, e.g., count
= count + 4;
-
type conversion
-
int_var = "12" // Error
-
double_var = int_var //
OK
-
int_var = (int) double_var
//information lost
-
constants final
double LB_IN_GRAMS = 453.6;
Arithmetic
-
brackets as usual 12.0
/ 2.0 * 5.0 is 30.0
-
integer division: double total
= 3.0 + 5 / 6 is 3.0
Strings
-
treated as objects, but you
don't need a constructor
-
String str = "hello";
-
some methods of class String
-
int l = str.length();
-
String sub = str.substring(start,pastEnd);
-
String upper = str.toUpperCase();
-
String many = "hello" +
l;
Classes
-
make Java object-oriented
-
purpose: encapsulation (black
box)
Objects
-
instances of classes (e.g. "bert"
is an instance of a button)
Elements of a class
-
instance variables: define the
state of object
-
usually private
-
any type (number type or class)
-
methods
-
usually public
-
any return type (or void)
-
two common uses of methods:
-
either modify instance variables
(return type is void)
-
or return information about
object
-
constructors: create object
-
usually public
-
no return type
-
have to have the same name as
the class
-
different constructors have
to differ in parameter types
-
don't use name of an instance
variable as parameter
-
static variables and methods
apply to entire class
Using a class
-
methods are commonly called
from outside of class
-
(method calls are like messages
sent from one class to another)
-
calling a method with
-
return type void:
redNapkin.translate(100,100);
-
return type boolean:
boolean in = redNapkin.contains(150,150);
-
calling a constructor (creating
an object)
-
redNapkin = new Rectangle(100,100);
Object References
-
redNapkin = blueNapkin;
-
copies reference (address) and
does not produce a copy of the object!
Applets
-
no main method
-
you provide a version of methods
from class Applet instead
Introduction to graphics
-
graphics context
-
colors
-
text (fonts)
Testing of computations
Use two independent methods:
-
visual
-
analytical
-
numerical
Decisions
General form
if (condition 1)
{ ...
}
else if (condition 2)
{ ...
}
else
{
}
Block statements
-
{} not necessary, if only one
command is executed
-
{} highly recommended for nested
if statements
Comparisons
-
== for double precision might
not be true due to rounding errors
-
check instead if |a-b| <
e * max(|x|,|y|)
-
== for objects, including String
checks if objects are identical i.e. same object
-
check object1.equals(object2)
assuming equals is implemented properly
Boolean expressions
-
&& the result
is only true if all conditions are true
-
|| the result is true
if at least one of the conditions is true
-
! inverts value of boolean variable
/ expression
-
De Morgan's Law:
-
! (A && B) is the same
as !A || !B
-
! (A || B) is the same as !A
&& !B