CS 1713 Week 1: Getting Started

Objectives:

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


Administrative:

What is object-oriented programming?


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.

Step 1:

A useful way to begin an object-oriented design is to write down a description of the problem.  The nouns give us an idea what classes and data we might need and the verbs give us an idea of the member functions.  This will not always be a complete correspondence but will give you a good start.
 

Step 2:

Two relationships are important to the second stage of object-oriented design. These two relationships are known as the is-a relationship and the has-a (or part-of) relationship.

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

Chapter 2 Objects and Primitive Data

Introduction to Objects

The information we manage in a Java program is either represented as primitive data or as objects. When you write a program in an object-oriented language: typically you have a main object (which is like your main program in other languages). The main object creates (instantiates) objects and calls their methods.

Case Study I: Creating a program with a simple class:
the Button class

A button is a device that has two states: pushed (on) and cleared (off). The button will indicate if it has been pushed and will clear itself when the request is given.
// Button.java: a class representing a button object 
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;
}
}
What does a Button object know how to do?


Here is the main program:
// ButtonDemo.java: Test and demonstrate button objects 
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
}
}
What does the main program ask the Button object thisButton to do?


Here is sample output from running ButtonDemo :
     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.

Variables 

A variable is a name for a location in memory used to hold a data value. When you declare a variable, you are instructing the compiler to reserve a portion of main memory space large enough to hold a particular type of value and indicating the name by which you will refer to that location. Examples of variable declarations:
    int x, y, z;
double n;
boolean myState;
Button thisButton;
char sayYes;
To summarize: Question:What are the variables in the Button class?

The assignment statement 

An assignment statement assigns a value to a variable. When executed, the expression on the right-hand side of the assignment operator (= ) is evaluated, and the result is stored in the memory location indicated by the variable on the left-hand side. This is called right-to-left associativity. Examples of the assignment statement:
 
x = 5;
n = 3.5;
myState = false;
thisButton = new Button(); // instantiates a Button object
int ans = 90; // declares and assigns
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:


Constants


Constants are identifiers and are similar to variables except that they hold a particular value for the duration of their existence. Examples of constant declarations:
 
final int MAX_OCCUPANCY = 427;
final double CONVERSION_FACTOR = 9/4;

Data types:

The purpose of data types is that they allow the compiler to know how big a variable is and what kinds of operations are valid.

  • Primitive types:

    Numeric values:

    Literals: A literal is an explicit data value used in a program such as 5.

    Characters

    Booleans


    Group Activity #1
    Type in the Button class and test it using the ButtonDemo class given above.



    Last revision: August 19, 2002 at 10:27 am