Presentation is loading. Please wait.

Presentation is loading. Please wait.

BIT115: Introduction to Programming

Similar presentations


Presentation on theme: "BIT115: Introduction to Programming"— Presentation transcript:

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

2 Assignment 1 DUE! Assignment 1 Due TONIGHT
Uploaded to StudentTracker by MIDNIGHT I will double dog dare try to have Assignment 1 graded and returned by Friday, October 13th 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 and send to me!

3 Assignment Dates (By Due Date)
Assignment 1 (LECTURE 5) DUE TONIGHT Section 1: Wednesday, October 11th Section 3: Thursday, October 12th Assignment 2 (LECTURE 9) Section 1: Wednesday, October 25th Section 3: Thursday, October 26th Assignment 1 Revision (LECTURE 11) Section 1: Monday, November 6th Section 3: Thursday, November 2nd Assignment 2 Revision (LECTURE 13) Section 1: Monday, November 13th Section 3: Thursday, November 9h Assignment 3 (LECTURE 15) Section 1: Monday, November 20th Section 3: Thursday, November 16th Assignment 3 Revision (LECTURE 18) Section 1: Wednesday, November 29th Section 3: Thursday, November 30th Assignment 4 (LECTURE 21) NO REVISION AVAILABLE! Section 1: Monday, December 11th Section 3: Tuesday, December 12th The Fickle Finger of Fate

4 A Note About I've set up a proprietary account specifically for returning graded files from StudentTracker. Depending on your account (like gmail), you may getting a warning similar to this: This 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 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 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 , or you may be waiting for a long long loooong while before receiving an answer. Thanks!

5 BIT Open Labs for Fall 2017 The Learning Center (CC2-060 and CC2-080)
Brady Pascoe, Code Guru Monday: 12 Noon to 3 pm Tuesday: 11 am to 3 pm Wednesday: 12 Noon to 3 pm Thursday: 11 am to 3 pm

6 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 REMINDER: 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 perfectly okay to use them for learning 

7 Mid-Term (A Head's Up) Mid-Term scheduled for Lecture 10
 TWO (2) 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

8 Mid-Term continued… Mid-Term is scheduled for LECTURE 10
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).

9 And Now… The Quiz

10 Refresher From the Last Lecture
The if and while Statements Extending the Robot Class using a One Class File Extending the Robot Class using a Two Class File

11 if statement: if true, do once. If not true, exit the statement.
while statement: if true, do then loop. When not true, exit the statement.

12 See the Example1.java file for reference
Extending the Robot Class (1 Class Style) Steps to Remember: In a one Class file, the file name, class name, and constructor name all have to be the same If you are going to create new methods for the robot to use, then use 'extends Robot' Build a constructor that lines up the elements of your class with the super (Robot) class Add your new methods To use the new methods, create the robot object using the class name (Example1), NOT Robot See the Example1.java file for reference

13 See the Example2.java file for reference
Extending the Robot Class (2 Class Style) Steps to Remember: In a two Class file, the file name has to match the name of class that has 'main' (e.g., Example2) If you are going to create new methods for the robot to use, then use 'extends Robot' Build a constructor that lines up the elements of your class with the super (Robot) class but create a different class name than the file name (e.g, Mover) Add your new methods To use the new methods, create the robot object using the class name (Mover), NOT Robot The constructor class uses only 'class', the main class uses 'public class' Give each class its own set of opening and closing squiggles See the Example2.java file for reference

14 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)

15 RAM_Movie.wmv – RAM_Movie.srt
Memory Video RAM_Movie.wmv – RAM_Movie.srt

16 Variables and Data Types
Appendix F.4 – Temporary Memory - Chapter 5.2 – Temporary Variables (Local Variables) What is a Variable? In programming, a variable is nothing but a reserved memory location to store a value, and this value can be changed later on—although it doesn’t have to be—either through some 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. piggy = piggy = piggy = 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.

17 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

18 "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. Data is over there 12 12 Primitive Not Primitive "Referential"

19 "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 ± E+38) Decimal (±4.9E-324 to ± E+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) 1 bit ( 1 or 0) So what do these eight terms mean and how do they work? *quintillion

20 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 = ; float ±1.4E-45 to ± E+38 RANGE float num = ; short -32,768 to 32,767 RANGE short num = 32000; long -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 RANGE long num = ; Double ±4.9E-324 to ± E+308 RANGE double num = ;

21 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 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) short long

22 Primitive Data Types: Decimal & Other
float boolean double char

23 Memory Data Container Sizes
1 byte 2 byte 4 byte 8 byte byte short int long float double

24

25 Primitive Data Types byte short int long float double boolean char

26 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; Data Types int num 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 5 int numThings 10 int numStuff

27 Declaring a Data Type Sets Memory Size
byte num = 125; byte numThings = 30000; byte numStuff = ; short num = 125; short numThings = 30000; short numStuff = ; int num = 125; int numThings = 30000; int numStuff = ; long num = 125; long numThings = 30000; long numStuff = ; 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

28 NOTE: The Next 19 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") 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)

29 The Binary Numbering System
Bits and Bytes Made Easy!

30 Decimal, Binary, Octal, and Hexadecimal quick Overview
Base 10, or decimal numbering, is what humans use for the most part. We count from 0 to 9 and then add a second column, 10. So 1|0 equals the number 10 in decimal. Base 2, or binary numbering, is a single digit that represents on or off, a 1 or a 0. How do you represent a 1 in binary? Well, “1” of course. But what’s the next number? How do you display a 2? Add another column, just like in the preceding table: 10 equals the number 2 in binary. Base 8, or octal numbering, counts from 0 to 7 with a single digit and then adds a column. So how would you display the number 8? 10 equals the number 8 in octal. Base 16, or hexadecimal numbering, also known as just hex, is a numeral system made up of 16 symbols. The standard base 10 and uses ten numerical symbols: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9. Hexadecimal uses the decimal numbers and includes six extra symbols. We do not have single-character symbols that mean 10, 11, 12, 13, 14, 15, so these symbols are taken from the alphabet: A, B, C, D, E and F. So, A = 10, B = 11, C = 12, D = 13, E = 14, and F = 15.

31 Decimal numbering System
The decimal number system (also called Hindu-Arabic or Arabic number system) is a positional numeral system employing 10 as the base and requiring ten different numerals, the digits 0, 1, 2, 3, 4, 5, 6, 7, 8, 9. It also requires a dot (decimal point) to represent decimal fractions. The position just to the left of the dot is the "Units" position. Every position further to the left is 10 times larger, and every position further to the right is 10 times smaller. In this scheme, the numerals used in denoting a number take different place values depending upon position. In a base 10 system the number represents the sum of (5 × 102) + (4 × 101) + (3 × 100) + (2 × 10-1) + (1 × 10-2).

32 Decimal numbering System (Base 10)

33 Binary Numbering System (BASE 2)
Binary number system is a positional numeral system employing 2 as the base and so requires only two different symbols for its digits, 0 and 1, instead of the usual ten different symbols needed in the decimal system. The numbers from 0 to 10 are thus in binary: 0 = 0 1 = 1 2 = 10 3 = 11 4 = 100 5 = 101 The importance of the binary system to information theory and computer technology derives mainly from the compact and reliable manner in which 0s and 1s can be represented in electromechanical devices with two states—such as “On-Off”, “Open-Closed”, or “Active-Not Active”. 6 = 110 7 = 111 8 = 1000 9 = 1001 10 = 1010 Right now this might look completely foreign to you, but it will all make sense in a moment. I promise!

34 Binary Numbering System (Base 2)
In computing and networking, binary plays a crucial role because all data—everything that is installed, used, or stored in a computer, whether in memory or on a storage drive of some kind (e.g., hard drive, SSD, USB, tape) or transmitted across a network—is done using binary elements, or bits for short. Each single bit represents a single state that is either switched “On-Off”, “Open-Closed”, or “Active-Not Active”. As I said previously, these On or Off states are represented by a 1 for on and a 0 for off. ON OFF 1 Each bit represents a single memory cell among the billions that are on a stick of RAM.

35 Binary Numbering System (Base 2)
Now, a single bit (binary element) by itself is only useful if you want to represent a single state, whether On or Off, or True or False (a Boolean value in programming), however most data needs to be represented not by using just one bit but by using several bits, that is to say a collection of bits. In computing and networking, the most common collections of bits are made up of a group of eight bits called a byte in computing and an octet in networking. Depending on size of the data being used or stored, you might only need one byte—say to store a character like the letter ‘A’—or several bytes working together—say to store an integer value like 1,000,000. For purposes of discussion and examples, a byte might be represented like this:

36 Binary Numbering System (Base 2)
Now, since a single bit represents a single state of either On (1) or Off (0), the 8 bits that make up a byte can (and will) have a mix of states, where some of the bits in the byte are On and some of the bits are Off. It is by using this mix of states that unique data can either by used in memory, stored on a drive, or transmitted across a network. Let’s look at a few examples, then drill down how it actually works.

37 Binary Numbering System (Base 2)
If you look carefully, you might notice that the top three bytes representing letters in binary are identical to the bottom three bytes representing numbers. How does the computer know whether a byte is a letter or a number or something else? It knows because when the data is used or stored on a computer it is also encoded by an additional prefix series of bits representing its data type. In other words, the computer will read this prefix first and then know what type of data the byte (or bytes) represents. For the remainder of this class, we will only be looking at numbers as the data type, and how numbers are represented in a single byte by turning on and off its various 8 bits.

38 Binary Numbering System (Base 2)
Okay, let’s see how turning the various bits On or Off in a byte can represent different numbers… First, when looking at a byte we will be using the binary numbering system, and the 8 bit positions that make up the byte are always read from right to left. BIG NOTE: in computing, instead of starting with 1 and counting up to 8 for the bit positions, we start with 0 and count up to 7. This is always the case! Of course there are still eight positions, but the first position is considered ‘0’ and the eighth position is considered ‘7’. Why it is done this way will become clear in a moment.

39 Binary Numbering System (Base 2)
Now, each bit position (or ‘slot location’) in the byte represents two values, determined by whether the bit is turned On or Off. If the bit is turned ‘Off’, that value will always be a zero (0). If the bit is turned ‘On’ that value is a number and determined by its position in the byte. Again, reading from right to left, the numerical values of each bit position are shown below.

40 Binary Numbering System (Base 2)
Okay, so how did we come up with these values for each of the bit positions (slot locations)? We came up with these values because we are using the binary numbering system and the binary numbering system is Base 2. With Base 2, a 2 will always be the base. Base 2 basically means “2 to the power of” but the power of what? That number, the exponent, is determined by the number associated with the bit’s position in the byte. Remember I said that in computing we always start counting with 0 to represent the first position or value? Here’s why:

41 Binary Numbering System (Base 2)
The bit positions (or slot numbers) 0 through 7 in the byte actually represent the exponents (“2 to the power of”) used in Base 2!  So, take the byte with it’s represented bit positions below… …and include a 2 as a base to their exponent:

42 Binary Numbering System (Base 2)
Now, do the math and you get the values listed at the top of each bit position! * * Remember: in math, any number raised to the 0 power will always be a 1.

43 Binary Numbering System (Base 2)
Okay, now that we’ve got all that under our belts, how to we come up with the number values we saw in our earlier binary example? It couldn’t be easier. Based on whether the bit is turned On or Off, and the value of the bit positions, we simply ADD THEM UP!

44 Binary Numbering System (Base 2)

45 Binary Numbering System (Base 2)

46 Binary Numbering System (Base 2)

47 Binary 128 64 32 16 8 4 2 1 Byte (8 Bits) UNSIGNED 1 21 216 1 1 15 1
A bit that is "turned off" is represented by a 0. A bit that is "turned on" is represented by a 1. 128 64 32 16 8 4 2 1 Byte (8 Bits) UNSIGNED 1 21 216 1 1 15 1 127 1 255

48 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 ± E+38) Decimal (±4.9E-324 to ± E+308) True or False Unicode character value, like 'A' or '$' or '#' or '*'

49 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.

50 Signed vs. Unsigned "Two's Complement" Byte (Unsigned) 128 64 32 16 8
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. 1 5 1 +1 -128 64 32 16 8 2 1 (or ) = -5

51 An int (integer) is 4-bytes: -2,147,483,648 to 2,147,483,647 (Signed)
(Unsigned) 128 64 32 16 8 4 2 1 byte 1 32768 16384 8192 4096 2048 1024 512 256 byte 2 524288 262144 131072 65536 byte 3 byte 4

52 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). byte short int long float double boolean char

53 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 -2,147,483,648 to +2,147,483,647 long 8 bytes 64 bits -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 ± to ± , with 7 digits of accuracy double ± to ± , with 15 digits of accuracy

54 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 = ; double distance = ; boolean user = true; char ch1 = 'A'; // or unicode: char ch1 = 65;

55 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. int someNumber = 100; 1 100 someNumber

56 Floating-Point Data Types
float and double are floating-point data types. They hold decimal-point numbers such as 1.5, , 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'.

57

58 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); You could do the same thing with Things  5 With the Becker elements, the colors can only be one of these thirteen listed above DEMO: See colors.java and colorThings.java


Download ppt "BIT115: Introduction to Programming"

Similar presentations


Ads by Google