Presentation is loading. Please wait.

Presentation is loading. Please wait.

BIT 115: Introduction To Programming

Similar presentations


Presentation on theme: "BIT 115: Introduction To Programming"— Presentation transcript:

1 BIT 115: Introduction To Programming
Professor: Dr. Baba Kofi Weusijana Pronounced Bah-bah Co-fee Way-ou-see-jah-nah Call him “Baba” or “Dr. Weusijana”

2 Lecture 6 Log into Canvas Put your name sticker on your computer
Announcements: Midterm No Quiz Today 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 Chapter 5.2 – Temporary Variables (Local Variables) Counters Queries Scope Chapter – The Counting Pattern Assignment 2 ICE06 Lots of very important details in one lecture today

3 Canvas Discussions (A Head's Up)
Mid-Term scheduled for Lecture 9  Monday, October 26th Have you used Canvas Discussions yet? You have to do 2 posts BEFORE the Midterm Ask a question or answer one ASAP Everything can’t be handled in-class, so take advantage! Potential embarrassment is costing you knowledge, so get what you came to college for and help each other! I’ve noticed that people who are asking questions more tend to be earning better grades. You don’t have to ask questions in class if you don’t feel like it, but not asking or answering questions even online is just not good for you.

4 Mid-Term (A Head's Up) Mid-Term scheduled for Lecture 9
 Monday, October 26th It will cover everything learned up through Lecture 8 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 afterwards 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

5 Mid-Term, continued… 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).

6 Temporary Memory, Variables
Appendix F.4 – Temporary Memory Chapter 5.2 – Temporary Variables (Local 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. byte short int long

7 Temporary Memory, Variables
1 byte 2 byte 4 byte 8 byte 1 byte is 8 bits 1 bit is 1 on/off memory register in your computer’s memory chips byte short int long float double

8 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 numObjects = 0, numStuff = 1; Data Types int num A variable in Java must have a certain type associated with it that 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 briefly now Reference Data Types We will discuss Reference Data Types later in the Quarter int numObjects int numStuff

9 Primitive Data Types Primitive data types are built into the Java language and are not derived from classes. There are 8 Java primitive data types: byte -128 to 127 short -32,768 to 32,767 int -2,147,483,648 to 2,147,483,647 (billion) long -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 (quintillion) float 127 digits after the decimal point double 1023 digits after the decimal point boolean True or False char Alphabetical characters stored as an ASCII numerical value (e.g., ‘A’ is 65, ‘B’ is 66)

10 Temporary Memory, Variables
public void grabThings(int numMoves) { int counter = 0; while(counter < 10) numMoves = numMoves * 2; counter = counter + 1; } int counter int numMoves // Called down in main int num = 2; karel.grabThings(num); int num

11 WHAT DOES “SIGNED” MEAN?
Primitive Data Types WHAT DOES “SIGNED” 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. The “sign” in this case means the integer values can have a ‘minus sign’ (-), and “unsigned” means ‘no minus sign’ For example: ‘signed’ short has a range of to 32767 ‘unsigned’ short has a range of 0 to 65535

12 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 -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

13 Variable Declarations
Variable Declarations take the following form: DataType VariableName; byte inches; short month; int speed; long timeStamp; float salesCommission; double distance;

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

15 Ranking Data Types The Java primitive data types are ranked, as shown here:

16

17 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

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

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

20 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. final float CAL_SALES_TAX = 0.75; Both the Java and Becker API libraries have several constants built in programmatically by default. For example: Java has math.PI (where PI = ) Becker has direction.NORTH (including EAST, SOUTH, WEST) where the direction represents specific degrees on a compass like 0, 90, 180, 270

21 Example from Robot World
lisa.grabThings(6) A B 1 public void grabThings() 2 { int numMoves = 0; 3 // move one fewer times than there are Things 4 while (numMoves < 4) 5 { this.pickThingIfPresent(); this.move(); numMoves = numMoves + 1; 8 } 9 this.pickThingIfPresent(); 10 } 1 public void grabThings(int numObjects) 2 { int numMoves = 0; 3 // move one fewer times than there are Things 4 while (numMoves < numObjects) 5 { this.pickThingIfPresent(); this.move(); numMoves = numMoves + 1; numObjects = numObjects - 1; 9 } this.pickThingIfPresent(); 11 } First Pass C numObjects 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), numObjects will be given the value six. Writing karel.grabThings(7), numObjects will be given the value seven. Writing jasmine.grabThings(8), numObjects will be given the value eight. 1 public void grabThings(6) 2 { int numMoves = 0; 3 // move one fewer times than there are Things 4 while (0 < 6) 5 { this.pickThingIfPresent(); this.move(); 1 = 0 + 1; 5 = 6 - 1; 9 } this.pickThingIfPresent(); 11 }

22 Another Way to Show Increment & Decrement
numMoves = numMoves + 1; numObjects = numObjects ˗ 1; numMoves++; numObjects ˗ ˗; SAME AS To Repeat: X = X + 1 is the same as X++ X = X - 1 is the same as X--

23 To Summarize: Temporary Variables / Local Variables

24 Declaring an Integer Variable
How else could you have written that last line?

25 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: You can also do this all on the same line by combining the declaration and the initialization, which saves keystrokes: int counter = 0;

26 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 < 10) // As long as this is true, loop { move(); counter++; // Same as counter = counter + 1; } See ICE_06_Demo_CountingLoops.java

27 EXAMPLE: Counting Things on an Intersection

28 Storing the Results of a Query

29 A Quick Word About Scope

30 Assignment 2 Part 3 - Hints
If heard that some of you are still struggling with Assignment 2 Part 3 even though it is imminently due, so I thought I'd give you some 'hints".

31 Create a class that extends Robot and a constructor
Create a class that extends Robot and a constructor. Remember if you do it all in one class (the same class that contains main, then the file name, the class name, and the constructor name all have to be the same. For example: public class A2_Part_3 extends Robot { public A2_Part_3(City theCity, int avenue, int street, Direction aDirection, int item) { super(theCity, avenue, street, aDirection, item); }

32 When you create a new instance of the robot down in main, then it too has to have this same class name ( and not Robot). For example: A2_Part_3 rob = new A2_Part_3(wallville, 1, 2, Direction.EAST, 0); Otherwise you are making a Becker Robot, no your special new kind-of/class-of Robot

33 Create a turnRight() and turnAround() method, which you will use with three other methods (which I'll talk about one at a time). Create a movetoWall() method which moves while the front is clear

34 Create a doEverything() method which will do the following:
Initialize a counter to 0 Use the counter in a while loop four times Call movetoWall() Pick up a Thing Turn around Call moveToWall() Turn left Move Increment the counter by //second part of doEverything continued on next slide

35 [doEverything continued] Next, use a while loop to count things in the backpack, and as long as the count is greater than zero, move and put a thing down. // third part of doEverything continued on next slide

36 [doEverything continued] Finally, call the returnToStart() method to go back to where the robot start [end of doEverything] The returnToStart() method will do the following: turn around Call moveToWall() Turn right

37 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);

38 Time for ICE06 Lecture 6  Counting Loops
Do each part together! Worth 10 points (because there is no quiz for today) Follow the In-Class Exercises Directions Work in the ICE06 Groups you have been assigned to Go to the People section of our Canvas course site and find the ICE06 Groups Don’t skip reading the example trace


Download ppt "BIT 115: Introduction To Programming"

Similar presentations


Ads by Google