BIT115: Introduction to Programming

advertisement
Lecture 6
Instructor: Craig Duckett
Assignment 1, 2, and A1 Revision
Assignment 1
I have finished correcting and have already returned the
Assignment 1 submissions. If you did not submit an Assignment 1, and want the points,
then you can submit it as the Assignment 1 Revision (see below). Make sure and submit
all the necessary files as they are each worth 20 points (two .java files, one debug table,
two trace tables). The A1 Upload Section in StudentTracker is unavailable and locked.
Assignment 2 Due Lecture 8 by midnight
Monday, February 1st
PLEASE NOTE: I WILL BE GOING OVER ASSIGNMENT 2 AND OFFERING HINTS FOR SUCCESSFULLY COMPLETING
PART 3 IN THIS WEDNESDAY’S CLASS. SEE THE LECTURE 7 POWERPOINT IF YOU WANT A HEAD START
(particularly Slides 5 through 11).
Assignment 1 Revision due Lecture 10 by midnight
Monday, February 8th
(if you are wanting to “improve” your grade or if you did not submit an
initial Assignment 1)
Assignment Dates (By Due Date)
 Assignment 1 GRADED! RETURNED! WOOT! WOOT!







A1 Upload Selection is no longer available in StudentTracker
Assignment 2 (LECTURE 8)
The Fickle Finger
Of Fate Moves!
Monday, February 1
Assignment 1 Revision (LECTURE 11)
Wednesday, February 10
Assignment 2 Revision (LECTURE 12)
Wednesday, February 17
Assignment 3 (LECTURE 13)
Monday, February 22
Assignment 3 Revision (LECTURE 16)
Wednesday, March 2
Assignment 4 (LECTURE 19)
Monday, March 14  NO REVISION
Extra Credit 01 (LECTURE 20)
Wednesday, March 16
3
Next Generation IT Club
Looking to add some
members in order to
schedule some cool paidfor events like Linuxfest
The NGIT Club meeting times are
every other Wednesday at 4-4:30 pm in CC2-161
(bi-weekly starting 1/20)
And Now…
The
Warm-Up
Quiz
It’s a Mish-Mash Lecture
 1 Class File vs. Multi Class File
 Primitive Data Types Review
 Constants: The final Keyword
 Temporary Variables (Local Variables)
 Counters and Counters with Loops
 Scope
SINGLE FILE | STYLE 1
import becker.robots.*;
public class MrRoboto extends Robot
{
public MrRoboto(City theCity, int avenue, int street, Direction aDirection)
{ super(theCity, avenue, street, aDirection);
}
public void move3()
{ this.move();
this.move();
this.move();
}
In this style, since there is only one class
name, then:
the class name MrRoboto,
the constructor name MrRoboto, and
the file name MrRoboto
must all be the same.
public void turnRight()
{ this.turnAround();
this.turnLeft();
}
Also, since there is only the single class
here, it is made public class by default.
public void turnAround()
{ this.turnLeft();
this.turnLeft();
}
public static void main(String[] args)
{ City bothell = new City();
MrRoboto lisa = new MrRoboto(bothell, 3, 2, Direction.SOUTH);
lisa.move3();
lisa.turnRight();
lisa.move3();
lisa.turnAround();
}
}
SINGLE FILE | STYLE 2
import becker.robots.*;
class MrRoboto extends Robot
{
// Construct a new MrRoboto
public MrRoboto(City theCity, int avenue, int street, Direction aDirection)
{ super(theCity, avenue, street, aDirection);
}
public void turnAround()
{ this.turnLeft();
this.turnLeft();
}
public void move3()
{ this.move();
this.move();
this.move();
}
public void turnRight()
{ this.turnAround();
this.turnLeft();
}
}
In this style, since there are two class names, then the
file name must match the public class name, in this case
the class that contains the main method,
MrRobotoMain. Also the class that holds the
constructor and the new methods only starts with class,
and the constructor starts with public. There can only
be one public class in a file, and typically the class that
holds main will start with public class. Developers often
set it up this way by convention to remind them which
class is the class that holds the main method, although
the reverse will also work as long as the file name
matches the public class class name. See the
MrRobotoReversedClass.java example.
public class MrRobotoMain extends Object
{
public static void main(String[] args)
{
City bothell = new City();
MrRoboto lisa = new MrRoboto(bothell, 3, 2, Direction.SOUTH);
lisa.move3();
lisa.turnRight();
lisa.move3();
lisa.turnAround();
}
}
Primitive Data Types
byte
1 byte
8 bits
Integers in the range
-128 to +127
short
2 bytes
16 bits
Integers in the range of
-32,768 to +32,767
int
4 bytes
32bits
Integers in the range of
-2,147,483,648 to +2,147,483,647
long
8 bytes
64 bits
Integers in the range of
-9,223,372,036,854,775,808 to +9,223,372,036,854,775,807
float
4 bytes
32 bits
Floating-point numbers in the range of
±3.410-38 to ±3.41038, with 7 digits of accuracy
double
8 bytes
64 bits
Floating-point numbers in the range of
±1.710-308 to ±1.710308, with 15 digits of accuracy
Integer Data Types
 byte, short, int, and long are all integer data types.
 They can hold whole numbers such as 5, 10, 23, 89, etc.
 Integer data types cannot hold numbers that have a
decimal point in them.
 Integers embedded into Java source code are called integer
literals.
Integer Data Type
Integer Data Type:
•
•
•
•
Integer Data Type is used to store integer value.
Integer Data Type is Primitive Data Type in Java Programming Language.
Integer Data Type have respective Wrapper Class “Integer“
Integer Data Type is able to store both unsigned and signed integer values just like in C/C++
Integer Data Type Can have 4 types of values as listed below:
•
•
•
•
byte
short
int
long
Creating Named Constants with final
 Many programs have data that does not need to be
changed.
 Littering programs with literal values can make
the program hard do read and maintain.
 Replacing literal values with constants remedies
this problem.
 Constants allow the programmer to use a name
rather than a value throughout the program.
 Constants also give a singular point for changing
those values when needed.
Creating Named Constants with final
 Constants keep the program organized and
easier to maintain.
 Constants are identifiers that can hold only a
single value.
 Constants are declared using the keyword
final
 Constants need not be initialized when declared;
however, they must be initialized before they are
used or a compiler error will be generated.
Creating Named Constants with final
 Once initialized with a value, constants cannot be changed
programmatically.
 By convention, constants are all upper case and words are separated by the
underscore ‘_’ character.
 EXAMPLES:
final int MONTHS_IN_YEAR = 12;
final float CAL_SALES_TAX = 0.79;
Both the Java and Becker API libraries have several constants
built in programmatically by default. For example:
Java has math.PI (where PI = 3.14159265)
Becker has direction.NORTH (including EAST, SOUTH,
WEST) where the direction represents specific degrees on a
compass like 0, 90, 180, 270
To Summarize:
Temporary Variables (Local Variables)
Counters
int X = 5; // Imagine we have this variable x
Increment
Add 1 to the counter X
X = X + 1; // Left  Right: ADD 1 to X NOW X is 6
or
Decrement
Subtract 1 from the counter X
X = X - 1; // Left  Right: SUBTRACT 1 from X NOW X is 4
Another Way to Show Increment & Decrement
numMoves = numMoves + 1;
numThings = numThings ˗ 1;
SAME
AS
numMoves++;
numThings ˗ ˗;
So: Using a Counter
X = X+1
X = X-1
is the same as
is the same as
X++
X--
X = X++ is INCORRECT !
X = X-- is INCORRECT !
Counters CONTINUED
 But why use a counter?
Because counter are great with loops!
Counters CONTINUED
 You can use initialize counters outside of loops and inside of
loops, which affects their scope (which we’ll talk about in a
moment), all depending on the logic of the code.
int counter = 0;
while(counter < 5) // As long as this is true, loop
{
Rex.move();
counter = counter + 1; // Same as counter++;
}
See: counterExample.java
Counters CONTINUED
 Here’s another couple of examples!
Suppose you wanted to spin a robot all the way around four times (that is, do a complete 360
circle four times.
You will have to adjust your logic accordingly. If one complete circle is 4 left turns, then four
complete circles would be 16 left turns.
There are two ways you could do this, either by incrementing a value to the counter, or
decrementing a value from the counter. First you need to set your value of the counter
accordingly:
int counter = 0 (used for increment, “count up”)
See: increment16.java
while(counter < 16)
//count up to 16
… counter = counter+1; //Same as counter++
or
int counter = 16 (used for decrement. “count down”)
while(counter > 0)
//count down to 0
… counter = counter-1; //Same as counter--
See: decrement16.java
Declaring an Integer Variable
Counters
 Declare the datatype, and give it a name:
int counter;
 Then, initialize it with a value:
counter = 0;
 So, putting it together it might look like this:
int counter;
counter = 0;
 You can also do this all on the same line by combining the
declaration and the initialization, which saves keystrokes:
int counter = 0;
EXAMPLE:
Counting Things on an Intersection
Storing the Results of a Query
A Quick Word About Scope
Hey! It’s time for another ICE
Lecture 6  Counting Loops
Follow the In-Class Exercises Directions
•
ICE_06_CL_Trace.java ( Example Solution )DEMO
•
ICE_06_Count.java
•
ICE_06_CountingLoops.java
Download