lecture 6 instructor: craig duckett. assignment 1, 2, and a1 revision assignment 1 i have finished...

25
BIT115: Introduction to Programming Lecture 6 Instructor: Craig Duckett

Upload: noah-hoover

Post on 02-Jan-2016

238 views

Category:

Documents


4 download

TRANSCRIPT

BIT115: Introduction to Programming

Lecture 6Instructor: 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)

Assignment 2 Due Lecture 8 by midnightThursday, October 22nd

PLEASE NOTE: I WILL BE GOING OVER ASSIGNMENT 2 AND OFFERING HINTS FOR SUCCESSFULLY COMPLETING PART 3 IN NEXT TUESDAY’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 midnightTuesday, November 3rd (if you are wanting to “improve” your grade or if you did not submit an initial Assignment 1)

3

• Assignment 1 Graded! Returned! Woot! • Assignment 2 (LECTURE 8)

Thursday, October 22• Assignment 1 Revision (LECTURE 10)

Tuesday, November 3• Assignment 2 Revision (LECTURE 12)

Tuesday, November 10• Assignment 3 (LECTURE 13)

Thursday, November 12• Assignment 3 Revision (LECTURE 16)

Tuesday, November 24• Assignment 4 (LECTURE 20)

Tuesday, December 8• Assignment 4 Revision (LECTURE 21)

Thursday, December 10

Assignment Dates (By Due Date)

And Now…The

Warm-UpQuiz

It’s a Mish-Mash Lecture

public class vs public vs classPrimitive Data Types ReviewConstants: The final KeywordTemporary Variables (Local Variables)Counters and Counters with LoopsScope

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 turnAround() { this.turnLeft(); this.turnLeft(); } public void move3() { this.move(); this.move(); this.move(); } public void turnRight() { this.turnAround(); 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(); } }

SIN

GLE

FIL

E |

STY

LE 1

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.

Also, since there is only the single class here, it is made public class by default.

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(); } } 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(); } }

SIN

GLE

FIL

E |

STY

LE 2

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.

Primitive Data Typesbyte 1 byte

8 bitsIntegers in the range-128 to +127

short 2 bytes16 bits

Integers in the range of-32,768 to +32,767

int 4 bytes32bits

Integers in the range of-2,147,483,648 to +2,147,483,647

long 8 bytes64 bits

Integers in the range of-9,223,372,036,854,775,808 to +9,223,372,036,854,775,807

float 4 bytes32 bits

Floating-point numbers in the range of ±3.410-38 to ±3.41038, with 7 digits of accuracy

double 8 bytes64 bits

Floating-point numbers in the range of ±1.710-308 to ±1.710308, with 15 digits of accuracy

Integer Data Typesbyte, 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 TypeInteger 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 finalMany 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 finalConstants 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

Increment – Add 1 to the counterX = X + 1; // Left Right

Decrement – Subtract 1 from the counterX = X - 1; // Left Right

Another Way to Show Increment & Decrement

numMoves = numMoves + 1;numThings = numThings ˗ 1;

numMoves++;numThings ˗ ˗;

SAMEAS

X = X - 1 is the same as X--X = X + 1 is the same as X++

So: Using a Counter

X = X++ is INCORRECTX = X-- is INCORRECT

Declaring an Integer Variable

CountersDeclare 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;

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

Example from Robot World

numThings is like a temporary variable that is automatically assigned a value just before grabThings begins to execute. The value it is assigned is the value given between the parentheses when grabThings is called.

Writing lisa.grabThings(6), numThings will be given the value six.

Writing karel.grabThings(7), numThings will be given the value seven. Writing jasmine.grabThings(8), numThings will be given the value eight.

1 public void grabThings(int numObjects)2 { int numMoves = 0;3 // move one fewer times than there are Things4 while (numMoves < numObjects)5 { this.pickThingIfPresent();6 this.move();7 numMoves = numMoves + 1;8 numObjects = numObjects - 1;9 }10 this.pickThingIfPresent();11 }

lisa.grabThings(6)

1 public void grabThings()2 { int numMoves = 0;3 // move one fewer times than there are Things4 while (numMoves < 4)5 { this.pickThingIfPresent();6 this.move();7 numMoves = numMoves + 1;8 }9 this.pickThingIfPresent();10 }

C

BA

1 public void grabThings(6)2 { int numMoves = 0;3 // move one fewer times than there are Things4 while (0 < 6)5 { this.pickThingIfPresent();6 this.move();7 1 = 0 + 1;8 5 = 6 - 1;9 }10 this.pickThingIfPresent();11 }

First Pass

EXAMPLE:Counting Things on an Intersection

Storing the Results of a Query

A Quick Word About Scope

Time for Another ICELecture 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