Lecture 2
Web page correction: a108xxx is wrong.
It should read h108xxx.
Readings
Methods say what an object does
Why Accessing methods?
to maintain encapsulation
Setter methods
setter methods or mutator methods just set the value of a field
Getter methods
getter methods or accessor methods return the value of a field
Getter methods: getX, getY
Setter methods: setX, setY, addToX
class Point {
private double x;
private double y;
public String toString() {
return "(" + x + "," + y + ")";}
public void setX(double value) { x = value;}
public void setY(double value) { y = value;}
public void addToX() { x = x + 1;}
public void addToX(int increment) {
x = x + increment;
}
public double getX() { return x;}
public double getY() { return y;}
}
class TestPoint {
public static void main (String[] args) {
Point origin = new Point();
origin.setX(0.0);
origin.setY(0.0);
origin.addToX(); // method polymorphism
origin.addToX(2.0);
System.out.println(
"The x coordinate is " + origin.getX());
}
}
type identifier;
int count; // defines type, allocates memory
Assignment Statements
identifier = expression;
count = 0; // assigns value count = count + 1;
System.out.println(String);
System.out.print(String); // no newline
Reading from Standard Input
DataInputStream stdin = new DataInputStream(System.in); String line; line = stdin.readLine();
Constructors create new instances of a class, that is objects.
Constructors are special
methods that have the same name as their class and no return type.
For example,
class Point {
private double x;
private double y;
Point(double xvalue, double yvalue) {
x = xvalue;
y = yvalue;
}
...
}
Constructors are used along with the ``new'' keyword to produce an object
or instance of the class.
Point origin = new Point(0, 0); Point point = new Point(1, 1);
Java Program Structure
White Space
Identifiers
Reserved Words
Some identifiers, called reserved words, have
specific meanings in Java and cannot be used in other ways
| abstract | default | goto | operator | synchronized |
| boolean | do | if | outer | this |
| break | double | implements | package | throw |
| byte | else | import | private | throws |
| byvalue | extends | inner | protected | transient |
| case | false | instanceof | public | true |
| cast | final | int | rest | try |
| catch | finally | interface | return | var |
| char | float | long | short | void |
| class | for | native | static | volatile |
| const | future | new | super | while |
| continue | generic | null | switch |
Literals
A literal is an explicit data value used in a
program there are seven kinds of literals
The Java API
The Java
Application Programmer Interface
String Concatenation and Addition
Programming Languages
Java Translation and Execution
Java source ,-------> bytecode
| / / \
| / / \
V / V V
Java Compiler interpreter bytecode compiler
|
|
V
Machine code
Syntax and Semantics
Errors
Software Engineering
Software Components
input ----> component -------> output
list of numbers --> compute average --> average
input --. ____ input --. _____
`->| | `->| |
input ---->| |--> input ---->| |--> output
.-->|____| |_____|
input--'
Object-Oriented Programming in Java
Defining classes isn't easy
Diogenes Laertius. Circa 200 A. D.
Plato having defined man to be a two-legged animal without feathers,
Diogenes plucked a cock and brought it into the Academy, and said, ``This
is Plato's man.'' On which account this addition was made to the definition,-''With
broad at nails.''
Inheritance, Classes and Objects
Bicycle --> My Touring bicycle
--> Joan's Mountain bicycle
--> Chiapucci's Racing bicycle
--> Steve Robert's Recumbent bicycle
Daily Modes of Transportation
<-- Walking
<-- Bicycle
<-- Roller Blades
<-- Public Transit
<-- Car
<-- Truck
Java Applets
Java source code -> Java compiler
|
|
V
Java bytecode
|
remote computer
|
|
|
V
Web browser contains
Java interpreter