Lecture 5 Instructor: Craig Duckett “Ooga-Chaka Ooga-Ooga” Assignment 1 Assignment 1 Due TONIGHT Uploaded to StudentTracker by midnight Wednesday, January 20th I will double dog dare try to have Assignment 1 graded and returned by Monday, January 25th (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! Assignment Dates (By Due Date) Assignment 1 (LECTURE 5) TONIGHT Wednesday, January 20 in StudentTracker by midnight Assignment 2 (LECTURE 8) Monday, February 1 Assignment 1 Revision (LECTURE 10) Monday, February 8 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 A Note About Email I've set up a proprietary clduckett@gmail.com 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 cduckett@cascadia.edu 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! Lecture 5 Announcements What 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 Mid-Term (A Head's Up) Mid-Term scheduled for Lecture 9 Wednesday, February 3rd TWO WEEKS FROM TODAY • 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 Mid-Term, continued… Mid-Term is scheduled for LECTURE 9 (Wednesday, February 3rd) 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). And Now… The Quiz While Loops Refresh As we shall soon see, while loops are commonly used for counting and implementing counters inside of programs while(<<BooleanExpression>>) { <<statement>> } while(<<BooleanExpression>>) { <<statement>> <<statement>> <<statement>> } 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 Loops 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. BIT 115: Introduction To Programming 10 BIT 115: Introduction To Programming 11 Variables, Data Types, Memory, and Binary Elements FUN FACT! 1 GB stick of RAM has over 8.5 billion memory cells •1 KB = 1024 bytes (8,192 bits) •1 MB = 1024*1024 bytes (8,388,608 bits) •1 GB = 1024*1024*1024 bytes (8,589,934,592 bits) Memory Video http://faculty.cascadia.edu/cduckett/bit115/lecture_01/RAM_movie.wmv Variables and Data Types Appendix F.4 – Temporary Memory - Chapter 5.2 – Temporary Variables (Local Variables) What is a Variable? In programming, a value, variable is nothing but a reserved memory location to store a and this value can be changed later on—although it doesn’t have to be—either logic of the program itself or by the user(s) of the program entering input. This means that when you create a variable you need to reserve space in memory to store the variable data based on its size and type. through some piggy = 0 piggy = 1 piggy = 2 piggy = 3 Here ‘piggy’ is the named variable location in memory and then it is assigned data (which can be changed). What we have not done yet is define the size or type of data (data type) that will go into the variable space. Variables and Data Types What is a Data Type? A data type is nothing but the type and size of the data that will be stored in memory. Data might be a small whole number, a large whole number, a small decimal point number, a large decimal point number, a Unicode character, Boolean true or false, and so on. 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 and the type of data that will go into that storage container. 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. cup pint quart gallon "Primitive" Data Types • Why the term "primitive"? • "Primitive" just means an actual number is actually contained in that actual memory location by turning a set collection of memory cells in that location on or off. 12 Primitive 12 Not Primitive "Referential" "Primitive" Data Types • Primitive data types are built into the Java language and are not derived from classes. • There are eight (8) "primitive" data types in Java. 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) billion 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 False Unicode 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) • So what do these eight terms mean and how do they work? *quintillion Memory Data Container Sizes byte -128 to 127 RANGE byte num = 100; int -2,147,483,648 to 2,147,483,647 RANGE int num = 1111111111; float ±1.4E-45 to ±3.4028235E+38 RANGE float num = 12345.6789101; short long -32,768 to 32,767 RANGE -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 RANGE short num = 32000; long num = 123456789123456789; Double ±4.9E-324 to ±1.7976931348623157E+308 RANGE double num = 9.12345678987654321; Primitive Data Types: Whole Numbers 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). byte int short long Each one of these blue rectangles represents a single memory cell located in memory on the RAM memory stick called a binary element or bit. Eight bits make up a collection of bits called a byte. Depending on the type and size of data you want to use, you can explicitly tell your program how big you want your memory location to be, ranging all the way from a single memory cell (for a boolean data type) to a collection of 64 memory cells (for a long integer data type or a double decimal data type) Primitive Data Types: Decimal & Other float boolean double char 1 byte 2 byte 4 byte 8 byte Memory Data Container Sizes byte short int long float double Primitive Data Types byte short int long float double boolean char Temporary Memory, Variables // int – gets the container ready // num – tells which container to use // 0 – integer that is put into the container int num = 0; int numThings = 5; int numStuff = 10; 0 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). int num 5 int 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 • numThings 10 int We will discuss Reference Data Types later in the Quarter numStuff Declaring a Data Type Sets Memory Size byte num = 125; byte numThings = 30000; byte numStuff = 1000000; short num = 125; short numThings = 30000; short numStuff = 1000000; int num = 125; int numThings = 30000; int numStuff = 1000000; long num = 125; long numThings = 30000; long 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 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.000000000000000000000000000000000000000000014 This is the smallest possible positive float number 3.4E+38 means 3.4*10^38 340,000,000,000,000,000,000,000,000,000,000,000,000 This represents the largest possible postive float number Remember that there are also negative float numbers (or "signed" numbers) Binary: A Quick Look NOTE: None of this binary material will be on a test (either the Mid-Term or the Final). It is only offered as a gentle introduction how data is stored in memory using binary, which you will be using in some capacity if you go on to work in the computer or technical industry. 192 . 168 . 1 . 1 1 1 0 0 0 0 0 0 1 0 1 0 1 0 0 0 00000001 00000001 255 . 255 . 255 . 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 Binary (A Quick Look) Byte (8 Bits) A byte is a collection of eight memory cells called "binary elements" or "bits" for short 8 7 6 5 4 3 2 1 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. 128 64 32 16 8 4 2 1 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. Binary 128 A bit that is "turned off" is represented by a 0. A bit that is "turned on" is represented by a 1. 64 32 16 8 4 2 1 Byte (8 Bits) UNSIGNED 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 1 21 1 1 0 1 1 0 0 0 216 0 0 0 0 1 1 1 1 15 0 1 1 1 1 1 1 1 127 1 1 1 1 1 1 1 1 255 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 False Unicode character value, like 'A' or '$' or '#' or '*' Primitive Data Types WHAT 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. Signed vs. Unsigned "Two's Complement" Byte (Unsigned) 128 64 32 16 8 4 2 1 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 255 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 +1 -128 64 32 16 8 0 -128 + 123 (or 64+32+16+8+2+1) = -5 2 1 5 An int (integer) is 4-bytes: -2,147,483,648 to 2,147,483,647 (Signed) 4264967295 (Unsigned) 128 64 32 16 8 4 2 1 byte 1 32768 16384 8192 4096 2048 1024 512 256 byte 2 8388608 4194304 2097152 1048576 524288 262144 131072 65536 byte 3 2147483648 1073741824 536870912 268435456 134217728 67108864 33554432 16777216 byte 4 Primitive Data Types byte short int long float double boolean char 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). Primitive Data Types: Range 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 Declarations Declarations 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; Integer Data Types byte, 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. 128 64 32 16 8 4 2 1 int someNumber = 100; 01 1 001 00 00000000 100 00000000 00000000 someNumber Floating-Point Data Types float 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'. FYI (Just for Fun) 5 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); 40