Day 1:
Cover syllabus, fill out forms, and answer
questions
Check out the course web site
www.cs.utsa.edu/~javalab
Use the computers in the classroom
Test out the Unix account for email
Day 2:
Recitation 1 - Hello World
Create a temporary directory on the
C:\ drive and copy that directory
What is object-oriented programming?
Introduce objects using the Button
class as an example
Day 3:
Variables and assignment statements
Primitive data types
Running a program that has a Button
Reading: Week 1
Programming a computer is one of the most difficult tasks you will undertake.
It will require creativity, intelligence, logic, and the ability to build
and use abstractions. Object-oriented programming is sometimes referred
to as a new programming paradigm. The word paradigm means a set of
theories, standards, and methods that together represent a way of organizing
knowledge; that is, a way of viewing the world. In trying to understand what
is meant by the term object-oriented programming we will examine a few real-world
situations and then look at how we could have the computer model the problem-solving
techniques employed.
is-a holds between two concepts when the first is a specialized
instance of the second. The is-a relationship defines class-subclass
(parent-child) hierarchies. For example:
A florist is-a shopkeeper.
A mouse is-an input device.
A rectangle is-a four-sided figure
A button is-a device
has-a holds when the second concept is a component of the
first but not the same thing. The has-a relationship describes data
to be maintained within a class. For example:
A car has-a engine.
A house has-a roof.
An elevator has-a button.
A rectangle has-a length and a width.
A button has-a state
Button class// Button.java: a class representing a button objectWhat does a Button object know how to do?
public class Button{
// the state of the button
// myState == true means the button is pushed
// myState == false means the button is cleared (not pushed)
private boolean myState;
// constructor -- used when a new button is created (starts cleared)
public Button(){
myState = false;
}
// accessor -- used to fetch the button's state
public boolean getState(){
return myState;
}
// used to print the button's state
public void tellState(){
System.out.println(" From tellState: I am in state " + myState);
}
// used to automatically print the button's state
public String toString(){
return " From toString: I am in state " + myState;
}
// modifier -- used to "push" the button
public void pushButton(){
myState = true;
}
// modifier -- used to "clear" the button
public void clearButton(){
myState = false;
}
}
// ButtonDemo.java: Test and demonstrate button objectsWhat does the main program ask the Button object thisButton to do?
public class ButtonDemo {
public static void main(String args[]) {
boolean thisButtonState;
Button thisButton = new Button();
// we can create any number of separate buttons
Button anotherButton = new Button();
// first work with thisButton
System.out.println("After Constructing thisButton");
thisButton.tellState();
System.out.println("Push Button");
thisButton.pushButton();
thisButton.tellState();
System.out.println("Clear Button");
thisButton.clearButton();
thisButton.tellState();
System.out.println("Call to GetState");
thisButtonState = thisButton.getState();
System.out.println("Current State is: " + thisButtonState);
thisButton.pushButton();
System.out.println("Call to toString ");
System.out.println( thisButton );
// now try out the other button: anotherButton
System.out.println("After constructing anotherButton");
anotherButton.tellState();
// continue as before
}
}
After Constructing thisButton
From tellState: I am in state false
Push Button
From tellState: I am in state true
Clear Button
From tellState: I am in state false
Call to GetState
Current State is: false
Call to toString
From toString: I am in state true
After constructing anotherButton
From tellState: I am in state false
The println method:
System.out.println("Push Button");
outputs Push Button and moves to the next line.
The println method:
System.out.print("Push Button");
outputs Push Button but does not move to the next line.
int x, y, z;To summarize:
double n;
boolean myState;
Button thisButton;
char sayYes;
Variables that represent primitive types are ready to be assigned values after they are declared. Variables that represent objects have to be instantiated before they can be accessed. To summarize, in the assignment state A = B:
x = 5;
n = 3.5;
myState = false;
thisButton = new Button(); // instantiates a Button object
int ans = 90; // declares and assigns
A must represent a location. B and put into the location represented
by A. A and B must have compatible types. int next;
int count = 5;
next = count;
Button thisButton;
Button anotherDate = new Button();
thisButton = anotherDate;
final int MAX_OCCUPANCY = 427;
final double CONVERSION_FACTOR = 9/4;
int, byte, short
, long double, float booleancharNumeric values:
Characters
'\b'), tab (`\t`) or newline ('\n'
). Booleans
Group Activity #1
Type in the Button class and test it using the ButtonDemo
class given above.