CS 1063  Week 3:  An Introduction to Objects and Classes  

Use and Construct Objects

The Assignment Statement

Objectives

Assignments


Assignment Statement

The 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.  Example:  int length = 10;

The arithmetic operators

Symbol Meaning Example
* Multiplication 3 * 5 * x
/ division 5.2 / 1.5
% mod/remainder 7 % 2
+ addition 12 + x
- subtraction 35 - y

The Assignment Statement and Arithmetic Expressions

Combining Assignment and Arithmetic

Shortcut Equivalent
nickels += count;
nickels = nickels + count;
nickels -= count;
nickels = nickels - count;
nickels *= count;
nickels = nickels * count;
nickels /= count;
nickels = nickels / count;

Examples using the mod operator

Split the number 526 into the hundreds digit, tens digit and ones digit.

Order of Operators (Precedence)

The following rules are used for evaluating arithmetic expressions in Java:

The Point Class in the java.awt Package

The public interface for the Point class is given below.  Some methods are missing.

Activity 1: Using the Point Class

Setup:

Part 1:  Constructing Point objects

Construct a Point object named p1 at the origin (0,0)
Output p1
Construct a Point object named p2 at (30,60)
Output p2
Construct a Point object named p3 at (30,60)
Output p3

Part 2:  Draw a picture of p1, p2, and p3.  Show the object variable, object reference, and the object for each.

Part 3:  Use the equals method

Determine if object p1 equals object p2 using the equals method.
That is, is p1.getX() == p2.getX() and p1.getY() == p2.getY()?
System.out.println( p1.equals(p2) );
Determine if object p2 equals object p3 using the equals method.

What do you think the difference is between the equals method and the == operator?  That is should p2 == p3 produce the same result?  Try this in your program.

Part 4:  Use the move and toStringmethods

Move p1 to the coordinates (10,5)
Output p1
Output p1 using the toString method
Was there any difference in the output?  When you place the Object name in a System.out.println Java automatically uses the toString method by default.

Part 5:  Use the getLocation method using an assignment statement

Output the location for each point clearly labeled and on separate lines using the getLocation method

Part 6:  Use the getX and getY methods

Output the location for each point in the form (x,y) using the getX and getY methods.  Clearly label each point and place each point on separate lines.

A Programmer Defined Class

The public interface for the Button class

Activity 2:  Use the Button Class

Setup:

Part 1:  Constructing Button Objects and output using the tellState method

Construct a new Button object named b1
Output the Button state using the tellState method

Construct a new Button object named b2
Output the Button state using the tellState method

Part 2:  Use the pushButton and clearButton methods

Push Button b1
Output the state of b1

Clear Button b1
Output the state of b1

Part 3:  Use getState to obtain the state of a Button object

What will be returned when getState is called?  What is the datatype?
Use getState to determine the state of Button b2 and Store the state in a boolean variable buttonState

boolean buttonState = b2.getState();
Output buttonState clearly labeled

Part 4:  Use the toString method to output a Button state

Push Button b2
Output the state of b2 using the toString method   

Recognize the Public Interface of a Class and How a Class is Constructed

public class Button{

// Data

    // the state of the button
    // myState == true means the button is pushed
    // myState == false means the button is cleared (not pushed)
    private boolean myState;

// Construct and Initialize private data

   // constructor -- used when a new button is created (starts cleared)
    public Button(){
         myState = false;
    }

// Methods that define the behavior of the Button Class
 
   
    // 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;
    }



}