Lecture 5
Review Primitives, Relations, and Logical Operators
Java has several primitive types that we can use, including int, byte, long,
float, double, boolean, etc.
Primitive types are different than objects. They don't have methods, and we
create new ones.
We can use them directly, or store them in our instance variables. We can operate
on them in the usual way with +, -, *, /, etc.
We can also examine their relations with the relational operators:
- == "equals"
- > "greater than"
- < "less than"
- >= "greater than or equals"
- <= "less than or equals"
- != "not equal"
as in "7 < 10" or "x >= y". Each of these operators will return either true
or false, and can be used in "if" statements or where any boolean values
are used.
We typically do not use these operations for objects in Java.
We can compare objects with "==", but this will only tell us if
two names refer to the same object. It will return false if
the two names point to two different objects, even if both objects
are identical in every way!
Java also has logical operators:
- && "and"
- || "inclusive or"
- ^ "exclusive or"
- ! "not"
Pong and Complex Conditionals
Relational operators and logical operators can be combined to implement
advanced ideas.
The
Unbounded Paddle version of Pong was modified so that the paddle
stayed nicely within the border of the pong court.
The
Bounded version works much nicer.
Classes
We can use classes to define our own objects. These new classes can be built
from other known classes.
We define the characteristics of our new class with instance variables.
We define Constructor methods that describe how to create new versions
of our new objects of the class.
We need to define the behavior of our new class of objects through its methods.
Class example:
Classy Basketball