Instance Variable

advertisement
BIT 115
BIT 115
Page 1 / 3
NOTE TO STUDENTS:
These notes are for the instructor's use. If you happen to find them to be useful,
that's great. If not, then please ignore these. Thanks!
—The Instructor
Lecture 9
Local Variables, Instance Variables
All the local variables we've looked at so far will disappear at the end of the service.
This means that they're temporary.
We want memory that won't go away between service calls
We want the ROBOT to remember something, not just get some temporary working
space
Permanent Memory Pattern:
1. One (or more) instance variables
(To Remember Things Permanently)
2. The instance variable must have a starting value
Normally, you should do this in the construtor
3. Use the memory in 2 or more commands
Either 2 separate commands, or else the same command, called twice,
so that the memory won't go away between calls.
Constructor
We've seen lots of classes that start like this:
class MysteryRobot extends Robot
{
MysteryRobot( City c, int ave, int st, int dir, int num)
{
super(c, ave, st, dir, num);
}
The question is: what's up with the 3rd line, and the stuff following it?
Vocab: This is called a constructor service, because it constructs an individual MysteryRobot.
You can put (almost) any code in there that you can normally put inside main.
You can have it print, create things, create other robots, and assign values to variables.
(Normally you'd only assign values to variables)
All this stuff MUST go after the call to super.
Remember how the parent class is also called the superclass?
That first line calls the parent class's constructor
That first line calls the superclass's constructor
(The above two lines mean exactly the same thing)
This is a service that gets called whenever you create a new MysteryRobot, like so:
MysteryRobot mary = new MysteryRobot(ForgetsVille,
0, 4,Directions.EAST, 0);
BIT 115
BIT 115
BIT 115
Page 2 / 3
Notice how the parameters that we declare are matched up against the arguments that we give it:
new
ForgetsVille 0
MysteryRobot(
int
Parameters MysteryRobot( City c
ave
Arguments
4
Directions.EAST 0)
int
st
int dir
int
num)
These, in turn, are passed to the superclass constructor (Robot's constructor)
We can add our own parameters, so long as we also add arguments when creating a MysteryRobot
new
ForgetsVille 0
4
Directions.EAST 0,
3)
MysteryRobot(
Int
int int dir,
int int
Parameters MysteryRobot( City c,
ave, st,
num, howMany)
Arguments
The code for describing the new class looks like:
class MysteryRobot extends Robot
{
MysteryRobot( City c, int ave, int st, int dir, int num, int
HowMany)
{
super(c, ave, st, dir, num);
System.out.println("Hey, HowMany is: " + HowMany);
}
The code for creating the new object is:
MysteryRobot mary = new MysteryRobot(ForgetsVille,
0, 4,Directions.EAST, 0);
Arithmetic, Comparison Operators
Arithmetic operators
Multiplication: *
Division: /
Note that when using integers, which will do integer division:
A/B will divide A by B, DROP THE REMAINDER, IF ANY, and
return what's left.
Ex:
8/2  4
8/3  2 (8 divided by 3 is 2 with a remainder of 2)
Addition: +
Subtraction: Order of operations:
*, /
If you see these, do these first
+, If you see these, do these AFTER the *, /
What’s a comparison operator?
You've just seen arithmetic operators, which evaluate to a number
BIT 115
BIT 115
BIT 115
Page 3 / 3
Comparison operators compare two things, and evaluate to true or false
Spend some time learning these well – it’s important to understand!!
Comparison Operators
Equality: =
Inequality: !=
Less than: <
Greater than: >
Less than or equal to: <=
Greater than or equal to: >=
Note that all comparison operators have lower precedence than arith. op.s
Any expression can be evaluated just like the arithmetic expressions
You can mix arithmetic & comparison operators.
ICE: Use the operators
Permanent Memory Pattern:
1. One (or more) instance variables
(To Remember Things Permanently)
2. The instance variable must have a starting value
Normally, you should do this in the construtor
3. Use the memory in 2 or more commands
Either 2 separate commands, or else the same command, called twice,
so that the memory won't go away between calls.
<Instance Variable Review>
ICE:
BIT 115
Download