Lecture 5 Instructor: Craig Duckett “Ooga-Chaka Ooga-Ooga”

32
BIT115: Introduction to Programming Lecture 5 Instructor: Craig Duckett “Ooga-Chaka Ooga-Ooga”

Transcript of Lecture 5 Instructor: Craig Duckett “Ooga-Chaka Ooga-Ooga”

Page 1: Lecture 5 Instructor: Craig Duckett “Ooga-Chaka Ooga-Ooga”

BIT115: Introduction to Programming

Lecture 5 Instructor: Craig Duckett

“Ooga-Chaka Ooga-Ooga”

Page 2: Lecture 5 Instructor: Craig Duckett “Ooga-Chaka Ooga-Ooga”

Assignment 1

Assignment 1 Due TONIGHT

Uploaded to StudentTracker by midnight Tuesday, October 13th I will double dog dare try to have Assignment 1 graded and returned by Friday, October 16th (hopefully sooner)

PLEASE NOTE: if, for some reason, StudentTracker is not up when you go to submit your Assignments, wait a few minutes in try again. If you still cannot access StudentTracker, then please attach your zipped Assignment to an email and send to me!

Page 3: Lecture 5 Instructor: Craig Duckett “Ooga-Chaka Ooga-Ooga”

3

• Assignment 1 (LECTURE 5) Tuesday, October 13 in StudentTracker by midnight• 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)

Page 4: Lecture 5 Instructor: Craig Duckett “Ooga-Chaka Ooga-Ooga”

A Note About EmailI've set up a proprietary [email protected] account specifically for returning graded files from StudentTracker. Depending on your email account (like gmail), you may getting a warning similar to this:

This email is indeed coming from me, and can be safely ignored.

PLEASE NOTE: If you need to contact me please do not reply to the gmail message from StudentTracker, but write to me instead using my college [email protected] email account address. As I explained, this gmail account is a proprietary account I set up solely for sending assignments to and from StudentTracker and I only check it for email messages a couple of times a month.

So: if you have a question or comment about BIT115 or your assignments, and you would prefer getting a timely reply, please contact me using my Cascadia email, or you may be waiting for a long long loooong while before receiving an answer. Thanks!

Page 5: Lecture 5 Instructor: Craig Duckett “Ooga-Chaka Ooga-Ooga”

Lecture 5 AnnouncementsWhat We Will Be Going Over Today

Appendix F.4 – Temporary Memory• Variables (Brief Introduction)

• We will be going over variables in much greater detail after the Mid-Term• Data Types• How Declaring a Data Type is Stored in Memory• How Binary Numbers represent the Storing of that Data in memory

• STARTING WITH LECTURE 4, I will be posting SOLUTIONS for the ICES. Remember, the solutions will vary depending on the developer, so these are my solutions—your solutions may look a bit different. These have been provided for reference, and to help you overt a hurdle in case you get stuck. It’s okay to use them

Page 6: Lecture 5 Instructor: Craig Duckett “Ooga-Chaka Ooga-Ooga”

Mid-Term (A Head's Up)Mid-Term scheduled for Lecture 9 Thursday, October 29th TWO WEEKS FROM THIS THURSDAY

• It will cover everything learned up through Lecture 7 (“Output”)

• It is scheduled for the entire session, so you will have more than enough time to work through each of the questions.

• When you are finished, you can hand it in and you are done for the day, so feel free to go home or the nearest pub for a celebratory pint or two ;-)

• It will be done entirely with pencil-and-paper (no .java files).

• A Mid-Term Review File is available for downloading on the BIT115 web site in a box in the right-hand column

Page 7: Lecture 5 Instructor: Craig Duckett “Ooga-Chaka Ooga-Ooga”

Mid-Term, continued…Mid-Term is scheduled for LECTURE 9 (Thursday, October 29th)

The Mid-Term Exam will focus on three learning outcomes:• conceptualize the logical steps needed to accomplish a task, • apply structured programming techniques to accomplish a task, • test and debug a program

Exam Topics:• Setting up a city with walls, things, robots • Using the robots built-in services • Extending a robot to include new services • Tracing code and pinpointing where it goes wrong • Explaining the compile/run process • Selecting when to use different programming structures like loops, decisions, and services • Writing syntax for loops, decisions, local variables, and parameters

Again, the exam will be similar to the quiz format (i.e., pencil-paper, from memory, individual work).

Page 8: Lecture 5 Instructor: Craig Duckett “Ooga-Chaka Ooga-Ooga”

And Now… The Quiz

Page 9: Lecture 5 Instructor: Craig Duckett “Ooga-Chaka Ooga-Ooga”

While Loops RefreshAs we shall soon see, while loops are commonly used for counting and implementing counters inside of programs

But before we go forward to see how counting and counters work, let’s refresh our memories by doing a few In-Class Exercises so we can get a feel again how while loops work.

Please go to to programajama.com Lecture 4 and follow the In-Class Exercises Directions under While Loops (Repeating Statements). We will be doing one trace table which you can download by looking under Course Wide Information near the top of the page, then finding Blank Documents and grabbing Program Trace Table.

While(<<BooleanExpression>>){ <<statement>>}

While(<<BooleanExpression>>){ <<statement>> <<statement>> <<statement>>}

Page 10: Lecture 5 Instructor: Craig Duckett “Ooga-Chaka Ooga-Ooga”

While Loops

BIT 115: Introduction To Programming 10

This can produce an

“Infinite Loop” (Hangs/Crashes)

Only wanted to mention these here

so you will be watchful. We will

go over while loops in greater detail in

another lecture.

Page 11: Lecture 5 Instructor: Craig Duckett “Ooga-Chaka Ooga-Ooga”

BIT 115: Introduction To Programming 11

Page 12: Lecture 5 Instructor: Craig Duckett “Ooga-Chaka Ooga-Ooga”

Memory Video

Page 13: Lecture 5 Instructor: Craig Duckett “Ooga-Chaka Ooga-Ooga”

Temporary Memory, Variables

Data Types

A data type is nothing but the type of the data that will be stored in memory.

Different data types require smaller or larger amounts of storage capacity, so when you declare a data type you are telling the program upfront the size of the storage container you want to set aside in memory.

Example by Analogy: If you want to store a quart of water, then you need a container that will hold a quart. If you want to store a gallon of water, then you need a container that will hold a gallon. In both cases, you need the container before you can put the water in it. Declaring a data type is getting the container ready before you put anything in it—first the container, then what goes into it.

Appendix F.4 – Temporary Memory Chapter 5.2 – Temporary Variables (Local Variables)

byte short int long

Page 14: Lecture 5 Instructor: Craig Duckett “Ooga-Chaka Ooga-Ooga”

Temporary Memory, Variables

byte short int long

1 by

te

4 by

te

2 by

te

8 by

te

float double

Page 15: Lecture 5 Instructor: Craig Duckett “Ooga-Chaka Ooga-Ooga”

Temporary Memory, Variables

Data Types

A variable in Java must have certain type associated with it which tells us what kind of data a variable can store, and whether that storage requires a small “container” in memory or a larger “container” (i.e., number of bits making up the storage space).

Data Types in the Java Programming Language are classified into two main groups:

• Primitive Data Types• We will discuss Primitive Data Types now

• Referential Data Types• We will discuss Reference Data Types later in the Quarter

int num = 0;int numThings = 5;int numStuff = 10;

// int – gets the container ready// num – tells which container to use// 0 – integer that is put into the container

num

numThings

int

int

numStuff

int

0

5

10

Page 16: Lecture 5 Instructor: Craig Duckett “Ooga-Chaka Ooga-Ooga”

"Primitive" Data Types• Why the term "primitive"?• "Primitive" just means an actual number is actually

contained in that actual memory location.

Primitive Not Primitive"Referential"

Data isover there

1212

Page 17: Lecture 5 Instructor: Craig Duckett “Ooga-Chaka Ooga-Ooga”

"Primitive" Data Types

byte short int long float double boolean char

• Primitive data types are built into the Java language and are not derived from classes.

• There are eight (8) "primitive" data types in Java.

Integer (-128 to 127)Integer (-32,768 to 32,767)Integer (-2,147,483,648 to 2,147,483,647) billionInteger (-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807)*Decimal (±1.4E-45 to ±3.4028235E+38)Decimal (±4.9E-324 to ±1.7976931348623157E+308)True or FalseUnicode character value, like 'A' or '$' or '#' or '*'

1-byte (8 bits)2-bytes (16 bits)4-bytes (32 bits)8-bytes (64 bits)4-bytes (32 bits)8-bytes (64 bits)1 bit ( 1 or 0)2-bytes (16 bits)

*quintillion

Page 18: Lecture 5 Instructor: Craig Duckett “Ooga-Chaka Ooga-Ooga”

int num = 125;int numThings = 30000;int numStuff = 1000000;

125

30,000

1,000,000

byte -128 to 127 Short -32,768 to 32,767 Int -2,147,483,648 to 2,147,483,647 long -9 quintillion to 9 quintillion

byte num = 125;byte numThings = 30000;byte numStuff = 1000000;

short num = 125;short numThings = 30000;short numStuff = 1000000;

long num = 125;long numThings = 30000;long numStuff = 1000000;

Declaring a Data Type Sets Memory Size

Page 19: Lecture 5 Instructor: Craig Duckett “Ooga-Chaka Ooga-Ooga”

NOTE: The Next Nine (9) Slides are for "Informational Purposes Only"

(They are NOT going to be on a Test!)

Example of Exponential Notation

1.4E-45 means 1.4*10^-45 (E stands for "exponent")0.000000000000000000000000000000000000000000014This is the smallest possible positive float number

3.4E+38 means3.4*10^38340,000,000,000,000,000,000,000,000,000,000,000,000This represents the largest possible postive float number

Remember that there are also negative float numbers (or "signed" numbers)

Page 20: Lecture 5 Instructor: Craig Duckett “Ooga-Chaka Ooga-Ooga”

Binary (A Quick Look)

128 64 32 16 8 4 2 1

Byte (8 Bits)

8 7 6 5 4 3 2 1

A byte is a collection of eight memory cells called "binary elements" or "bits" for short

The content of a byte is determined for calculation from right to left. So the first bit in a back is on the far right, and the last bit in a byte is on the far left.

Going from right to left, the first column represents a 1 when turned on, the second represents a 2, the third represents a 4, the fourth represents an 8, and so on.

Page 21: Lecture 5 Instructor: Craig Duckett “Ooga-Chaka Ooga-Ooga”

Binary128 64 32 16 8 4 2 1

0 0 0 0 0 0 0 0 0

0 0 0 1 0 1 0 1

1 1 0 1 1 0 0 0

0 0 0 0 1 1 1 1

0 1 1 1 1 1 1 1

1 1 1 1 1 1 1 1

21

216

15

127

255

Byte (8 Bits) UNSIGNED

A bit that is "turned off" is represented by a 0. A bit that is "turned on" is represented by a 1.

Page 22: Lecture 5 Instructor: Craig Duckett “Ooga-Chaka Ooga-Ooga”

Primitive Data Types are "Signed"The eight (8) "primitive" data types in Java are "signed" by default. This means by default they represent a range from a negative number, using the minus "sign" - , to a positive number. So the term "signed" represents this negative to positive range.

byte short int long float double boolean char

Integer (-128 to 127)Integer (-32,768 to 32,767)Integer (-2,147,483,648 to 2,147,483,647)Integer (-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807)Decimal (±1.4E-45 to ±3.4028235E+38)Decimal (±4.9E-324 to ±1.7976931348623157E+308)True or FalseUnicode character value, like 'A' or '$' or '#' or '*'

Page 23: Lecture 5 Instructor: Craig Duckett “Ooga-Chaka Ooga-Ooga”

Primitive Data TypesWHAT DOES “SIGNED” or "UNSIGNED" MEAN?

“Signed” means it contains values equally split between positive and negative numbers, and “Unsigned” means that that same number of values can only be positive, starting a zero and getting larger

The “sign” in this case means the integer values can have a ‘minus sign’ (-), and “unsigned” means ‘no minus sign’

For example:• A ‘signed’ byte has a range of -128 to 127• An ‘unsigned’ byte has a range of 0 to 255

NOTE: Earlier versions of Java did NOT support "unsigned" integer data types; all integer data types were "signed" which meant they represent this negative to positive range. Now, even though Java 8 does allow for "unsigned" data types there is no way to declare an unsigned type up front. You can only manipulate unsigned data into being from a method.

Page 24: Lecture 5 Instructor: Craig Duckett “Ooga-Chaka Ooga-Ooga”

Signed vs. Unsigned

128 64 32 16 8 4 2 1

255

0 0 0 0 0 0 0 0

1 1 1 1 1 1 1 1

0

"Two's Complement"

Byte (Unsigned)

But because a byte is defined as an 8-bit number in the range of -128 to 127 how would you express, for example, -5? You do it using a computer trick called "Two's Complement". First you represent +5 in binary, then you invert the binary, negate the last number, then add 1.

0 0 0 0 0 1 0 1

1 1 1 1 1 0 1 0

5

-128 64 32 16 8 0 2 1

+1

-128 + 123 (or 64+32+16+8+2+1) = -5

Page 25: Lecture 5 Instructor: Craig Duckett “Ooga-Chaka Ooga-Ooga”

128 64 32 16 8 4 2 1 byte 1

32768 16384 8192 4096 2048 1024 512 256

8388608 4194304 2097152 1048576 524288 262144 131072 65536

2147483648

1073741824

536870912

268435456

134217728

67108864

33554432

16777216

An int (integer) is 4-bytes:

-2,147,483,648 to 2,147,483,647 (Signed) 4264967295 (Unsigned)

byte 2

byte 3

byte 4

Page 26: Lecture 5 Instructor: Craig Duckett “Ooga-Chaka Ooga-Ooga”

byte

short

int

long

float

double

boolean

char

Primitive Data Types So, when you explicitly declare a data type you are setting up a collection of binary elements in memory large enough to hold a number within a particular range determined by that number of binary elements (bits).

Page 27: Lecture 5 Instructor: Craig Duckett “Ooga-Chaka Ooga-Ooga”

Primitive Data Types: Rangebyte 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

Page 28: Lecture 5 Instructor: Craig Duckett “Ooga-Chaka Ooga-Ooga”

DeclarationsDeclarations with initializations (assigning an "initial" value) take

the following form:DataType DataName = DataValue;

byte inches = 32; short month = 12; int speed = 60; long timestamp = 24.00; float salesCommission = 100.00; double distance = 92960000.00; boolean user = true; char ch1 = 'A'; // or unicode: char ch1 = 65;

Page 29: Lecture 5 Instructor: Craig Duckett “Ooga-Chaka Ooga-Ooga”

Integer Data Typesbyte, short, int, and long are all integer data types.They can hold whole numbers such as 5, 10, 23, 6789,

etc. Integer data types can not hold numbers that have a

decimal point in them. Integers embedded into Java source code are called

integer literals. "Literal" is programming-speak meaning "What you type is what you get" or "the data is literally there."

For example, if in my code I declare an integer with the name someNumber and initialize with the number 100, then I am literally putting the number 100 in a memory location called someNumber.

int someNumber = 100;someNumber

100

Page 30: Lecture 5 Instructor: Craig Duckett “Ooga-Chaka Ooga-Ooga”

Floating-Point Data Typesfloat and double are floating-point data types.

They hold decimal-point numbers such as 1.5, 123.456, etc.

If you initialize a float or double with a single number, it will display that number with a decimal point followed by a zero, for example:

float num = 1; will output 1.0

The data type double is shorthand for double-precision, and does not mean you are "doubling" a number. It just means you can hold a much larger and more precise decimal-point number.

Don't get confused with the term 'double'…it does not mean 'double it'.

Page 31: Lecture 5 Instructor: Craig Duckett “Ooga-Chaka Ooga-Ooga”
Page 32: Lecture 5 Instructor: Craig Duckett “Ooga-Chaka Ooga-Ooga”

5

32

FYI (Just for Fun)If you want to give the Robot a label when the program runs, then you will have to do something like this:

mary.setLabel("Mary");

If you want to change the Robot's color, then you will have to include this at the top of your file

import java.awt.Color;

And then do something like this:

mary.setColor(Color.ORANGE);

Also, if you want to change the color of a wall, first give the wall a name, then color it according to that name:

Wall w1 = new Wall(bothell,2,1,Direction.NORTH); w1.setColor(Color.BLUE); Wall w2 = new Wall(bothell,2,2,Direction.NORTH); w2.setColor(Color.GREEN);