Lecture 3 Notes

advertisement
BIT 115
Lecture 3
Page 1 / 5
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 3
Announcements
<Go through slides>
Mental Tools For Long Term Success
All the programs we’ve worked with so far have been pretty simple
More complex programs are larger.
We want a way to manage the complexity of larger programs
Today we'll examine a couple ways of organizing stuff so it's easier to understand
You'll still have to work hard to understand what's going on, it's just that this will make it
easier.
Overview Of The Big Idea:
 More complicated programs are 'composed' out of the same small parts that smaller programs
are, there's just more commands.
We want to take a bunch of commands, ("move around the wall"), and instead of typing it in
EVERY SINGLE TIME we want to do it, we'll group them up into a new command, and use that
new command.
We're also going to look at putting these new commands inside of a new type of robot.
Much like the Robot class has a turnLeft command, we can create a new class,
RobotThatCanTurnRight, which has a built-in turnRight command.
This will
Save you typing
Making it possible for you to give the RobotThatCanTurnRight class to someone else, and they
can use turnRight without knowing how it works.
Kinda like someone gave you the Robot that can turnLeft.
<< DEMO: RobotThatCanTurnRight::turnRight >>
Walk through the code, note the 'jump to method, @ end, jump back' thingee
KEY IDEA: When we've call a command, we "jump" from wherever we were (main, usually) to the
command
Then trace through there JUST LIKE MAIN: top to bottom
Then we jump BACK to whomever called us.
How to figure out what the program will do:
Find the public class in the file, find it's main method.
Start there – go through, line by line.
When you get to one of these new commands, remember where you are
Then jump to the new command.
BIT 115
LECTURE 3
Page 1 / 5
BIT 115
Lecture 3
Page 2 / 5
Inside the new command, go line-by-line through the command
Once you're done, jump back to wherever you jumped from (you did remember it, right?)
You'll do this in more detail when you do code tracing.
The main objective for the ICEs is to get you up-and-running quickly, to make sure you've seen the topic,
and to make sure you're not completely lost. Outside of class, you should first think the thing through, try
writing up some code, mentally trace through it, then type it in & debug it.
ICE: Trace the code
Syntax For Creating a New Robot, With A New Command
For each new type of Robot that you want to create:
In THE SAME FILE, you should paste the following code
class MyRobotNameGoesHere extends Robot
{
MyRobotNameGoesHere(City theCity, int StreetNum,
int AveNum, Direction Dir,
int NumBackpackItems)
{
super(theCity, StreetNum, AveNum, Dir, NumBackpackItems);
}
}
public class NameOfFileHere extends Object
{
public static void main(String [] args)
{
/* The code that you're used to seeing goes here */
}
}
Important Points:
1. MyRobotNameGoesHere shows up twice in the above code – if you change the name of the robot
type, you MUST change this in both places.
2. This class, since it's being put in the same file as the public class NameOfFileHere, must NOT BE
PUBLIC!!!!
(
class MyRobotNameGoesHere vs. public class NameOfFileHere)
Within this new type of Robot, every time you want to add a new command, write:
public void nameOfNewCommand()
{
}
the new command should go inside the new class, but not inside any other commands, like so:
class MyRobotNameGoesHere extends Robot
{
BIT 115
LECTURE 3
Page 2 / 5
BIT 115
Lecture 3
Page 3 / 5
MyRobotNameGoesHere(City theCity, int StreetNum,
int AveNum, Direction Dir,
int NumBackpackItems)
{
super(theCity, StreetNum, AveNum, Dir, NumBackpackItems);
}
public void nameOfNewCommand()
{
}
public void nameOfAnotherNewCommand()
{
}
}
Notice that you can add as many new commands as you want, so long as each one gets a unique name
You then add any commands that you want inside that new command
(See Lecture 3 Demo 1 on the website)
public void turnRight()
{
this.turnLeft();
this.turnLeft();
this.turnLeft();
}
Notice that in main, we use
robotName.turnLeft();
here, we use
this.turnLeft();
Lastly, you can use the new commands in main:
class MyRobotNameGoesHere extends Robot
{
MyRobotNameGoesHere(City theCity, int StreetNum,
int AveNum, Direction Dir,
int NumBackpackItems)
{
super(theCity, StreetNum, AveNum, Dir, NumBackpackItems);
}
public void nameOfNewCommand()
{
}
public void nameOfAnotherNewCommand()
{
}
}
BIT 115
LECTURE 3
Page 3 / 5
BIT 115
Lecture 3
Page 4 / 5
public class NameOfFileHere extends Object
{
public static void main(String [] args)
{
City seattle = new City();
MyRobotNameGoesHere bob = new MyRobotNameGoesHere( seattle,
12, Direction.NORTH, 0);
// other set-up code left out
// Since bob is a type of Robot, we can tell Bob to do
// anything a normal Robot could:
bob.move();
bob.turnLeft();
// Since bob is also a MyRobotNameGoesHere, we can also
// tell it do any of the new commands that we created, above
bob.nameOfNewCommand();
bob.nameOfAnotherNewCommand();
}
}
ICE: Download ICE_03_01_CompileErrors.java, and find all the errors that you can.
General Note:
Most of your programs will have 2 classes:
Your 'main' class, which matches the name of the file.
It has a main method, and is where the program starts
It's responsible for giving commands to all the other parts of your program
Your new type of robot, which will have some new, spiffy functionality that ordinary robots don't
Finally, y'all get to write code on your own:
ICE: Write code according to the flowchart
Conditional (If) statements
What if we want the robot to pick up a Thing, but we don't know if it's in the intersection with the robot or
not?
There might be none. pickThing  robot breaks
There might be one thing  pickThing works fine
For what we've seen so far, we can actually figure out what the program will do at compile time
However, this might not always be possible
video games use randomization to provide a different experience each time
The user may give us different input each time.
We need a way of having our program do something different, depending on exactly what's already
happened.
Further, this pattern occurs very frequently – so frequently that Java has a concise way to express it:
if(this.besideThing())
BIT 115
LECTURE 3
Page 4 / 5
BIT 115
Lecture 3
{
Page 5 / 5
this.pickThing();
}
this.nextCommand();
It reads kinda like in English – if this robot is beside a thing, then pick it up, then go to the next command
If this robot isn't beside a thing, you simply go directly to the next command AFTER the closing
curly brace.
Tracing the code:
Write down the line with the IF on it, and note whether it's true or not
if it is true
Write down the first line inside the If statement (this.pickThing())
Then use the next line to write down the robot's state, like normal
if it's not true
Write down the line AFTER the curly brace
Then use the next line to write down the robot's state, like normal
Syntax Points:
1. if is case-sensitive
2. You NEED to have the ( and )
3. You (kinda) NEED to have the { and }
(well, actually you don't, but for now just pretend like you do – it'll make your life easier)
4. You can put white-space pretty much wherever you want.
5. Anything you can use for a while's test, can be used in an if's test
Comparison:
You can use <, >, <=, >=, or == (equals), or != (not equals) to compare numbers
There are various services that you can use to ask the robot questions.
such as: how many things do you have in your backpack?
joe.countThingsInBackpack()
We can combine the two, and ask a question about the robot
if(joe.countThingsInBackpack() < 3)
{
// there are strictly less than 3 Things
// in Joe's backpack
...
}
ICE: Find compiler + intent errors.
<Go over the flowchart bit>
ICE: Some code using an if statement.
BIT 115
LECTURE 3
Page 5 / 5
Download