CS 1063
Week 3: An Introduction to Objects and Classes
Use and
Construct Objects
The Assignment
Statement
Objectives
- Construct and use Objects
- Use assignment statements
- Use the Point class located in the java.awt
package
- Use the programmer defined class Button.java
Assignments
- Reading Week 3
- Lab1 Due
(Monday)
- Begin Lab 2
- Take Quiz 2 ( Thursday, Friday and Saturday)
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
- The assignment statement stores a value or a computational
result
evaluated from the right side of the = in a variable
located
on the left.
- In variable = expression;, the expression
can be a variable, a constant, a literal, or a combination of these
connected by appropriate operators.
- Example:
int x;
double d, d2;
char ch;
boolean found;
String name;
x = 11 % 3; // % computes the remainder of a division. x = 2
d = 3.5; // stores 3.5 in d
d2 = d + 1.5; // stores 5.0 in d2
d = d + d2; // stores 3.5 + 5.0 in d, the previous value is lost
ch = 'J'; // stores the character J in ch
found = true; // (note: NO quotes) stores true in found
name = "Alice"; // stores a reference to Alice in name
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.
- Hundreds digit: divide the number by 100
- Tens digit: mod the number by 100 and then divide the
result by 10
- 526 % 100 = 26
- 26 / 10 = 2
- Ones digit: mod the number by 10
- 526 % 100 = 26
- 26 % 10 = 6
Order of Operators (Precedence)
The following rules are used for evaluating arithmetic expressions
in Java:
- Evaluate all parenthesized expressions first, with nested
expressions evaluated "inside-out."
- Evaluate expressions according to operator precedence: evaluate
*, /, and % before +
and -.
- Evaluate operators with the same precedence left to right
(left-to-right associativity).
The Point Class in the java.awt Package
The public interface for the Point class is given
below. Some methods are missing.
-
Point()
This constructs and initializes a point at the origin (0,0) of the
coordinate space.
- Point(int x, int y)
This constructs and initializes a point at the specified (x,y)
location in the coordinate space.
Parameters:
x,y - coordinates for this point
- boolean equals(Object obj)
Determines whether or not two points are equal. That is if my x
= your x and my y = your y
Parameter:
obj - A Point Object
- Point getLocation()
Returns the location of this point in the form (x,y).
- double getX()
Returns the X coordinate of the point in double precision.
- double getY()
Returns the Y coordinate of the point in double precision.
- void move( int x, int y)
Moves this point to the specified location in the (x,y) coordinate
plane.
Parameters:
x,y - Coordinates for the new location in the plane
- String toString()
Returns a string
representation of this point and its location in the (x,y) coordinate
space.
Activity 1: Using the Point Class
Setup:
- Create a new class called PointMain by typing the following
in the Definitions pane.
import java.awt.Point;
public class PointMain {
public static void main(String[] args) {
}
}
- Save the class.
- Write, compile, and run a program to do the tasks below.
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
- public Button()
Constructs and initializes a Button object whose state is
off (false)
- boolean getState()
Returns the state of the Button: either true if the
Button is on or false if the Button is
off
- void tellState()
Outputs the state of the Button
- String toString()
Returns the state of the Button in the form of a
String
If used in a System.out.println will print
the state of the Button
- void pushButton()
This will set the state of the Button to
on(true). That is, if the Button
is off it will be turned on, and if the Button if on it will stay on.
- void clearButton()
This will set the state of the Button to off(false).
That is, if the Button is
on it will be turned off, and if the Button is off it will
stay off.
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;
} |
|
}
|