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 6 Instructor: Craig Duckett

2 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). The A1 Upload Section in StudentTracker is unavailable and locked. Assignment 2 Due Lecture 9 by midnight PLEASE NOTE: I WILL BE GOING OVER ASSIGNMENT 2 AND OFFERING HINTS FOR SUCCESSFULLY COMPLETING PART 3 IN THIS WEDNESDAY’S CLASS. SEE THE LECTURE 7 POWERPOINT IF YOU WANT A HEAD START (particularly Slides 5 through 11). Assignment 1 Revision due Lecture 11 by midnight

3 Assignment Dates (By Due Date)
Assignment 1 (LECTURE 5) 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 And Now… The Warm-Up Quiz

5 It’s a Mish-Mash Lecture
1 Class File vs. Multi Class File Primitive Data Types Review Constants: The final Keyword Temporary Variables (Local Variables) Counters and Counters with Loops Scope

6 REVIEW 1 Class File vs. 2 Class File

7 In this style, since there is only one class name, then:
SINGLE FILE | STYLE 1 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(); } } 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.

8 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(); } } SINGLE FILE | STYLE 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.

9 REVIEW Primitive Data Types

10 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

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

12 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

13 Constants The final Keyword
WRITTEN IN STONE WRITTEN IN STONE

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

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

16 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 = ) Becker has direction.NORTH (including EAST, SOUTH, WEST) where the direction represents specific degrees on a compass like 0, 90, 180, 270

17 New Topic: Counters and Counting Loops
Review: if and while Statements Boolean conditions (true or false) Example code of both statements in action Counters What Good are They? How are They Used? Example code of counter in action

18 REVIEW: if and while Statements
Both the if and while statements evaluate Boolean conditions They test to determine whether a condition is true or false True, do something False, don’t do something Do it! Don’t do it!

19 CONCEPT: The if Statement
IF EXAMPLE: Taking a free right-turn at a red light “If it is clear (no vehicle or pedestrian coming), then take a free right turn.” Do This The Next Thing The Next Thing

20 The if Statement “If true, do it once …”
False

21 if Example Code WALK-THROUGH: RobotWall.java

22 CONCEPT: The while Statement (Loop)
WHILE EXAMPLE: Buying Gum Balls “While I have enough quarters, buy a gum ball; keep buying gum balls as long as I have enough quarters.” Do This The Next Thing The Next Thing

23 The while Statement “While true do it again…”
False

24 while Example Code WALK-THROUGH: RobotWall.java

25 If and While When the simplest form of an if statement asks a question and the answer is true, it executes a group of statements once and then continues with the rest of the program. If the answer to the question is false, that group of statements is not executed. When a while statement asks a question and the answer is true, it executes a group of statements (just like the if statement). However, instead of continuing down to the rest of the program, the while statement asks the question again. If the answer is still true, that same group of statements is executed again. This continues until the answer to the question is false. The if statement’s question is “Should I execute these statements at least once?” if (test condition) { // Statements of things to do } The while statement’s question is “Should I execute these statements again?” while (test condition)

26

27

28 QUESTION With the while loop, what would happen if the condition was always true?

29 ANSWER An infinite loop!

30 while Example Code WALK-THROUGH: RobotWall.java

31 QUESTION Is there a way to configure a set number of times to go through the loop, and then stop or jump out to prevent an infinite loop?

32 Yes! You can do this by setting up and using a counter!
ANSWER Yes! You can do this by setting up and using a counter! I’m a counter. pretty big deal!

33 Counters

34 What is a Counter ? A counter is a variable, usually of an integer int data type, typically initialized to start with a 0. A variable is a small section of memory that has been set aside based on the size and type of data it will contain. It is given a unique name and then an initial starter value. As the program is run this value can be changed, it can vary, hence the name variable.

35 Setting Up a Counter In the code, setting up a counter for use with a loop is a 3-Step Process: Create and initialize the counter Create the count condition “Increment” the counter by 1 Let’s take a look at what this means and how it works with our while loop program…

36 Counter Example Code WALK-THROUGH: RobotWall.java
int counter = 0; // (1) Create the counter while(counter < 5) // (2) Create the count condition { lisa.putThing(); lisa.move(); counter = counter + 1; // (3) Increment the counter by } // Read from Right to Left! WALK-THROUGH: RobotWall.java

37 Counter Increment/Decrement
int counter = 0; while(counter < 5) { lisa.move(); counter = counter + 1; } int counter = 5; while(counter > 0) { lisa.move(); counter = counter - 1; }

38 Counters Increment Add 1 to the counter X
int X = 5; // Imagine we have this variable x or Increment Add 1 to the counter X X = X + 1; // Left  Right: ADD 1 to X NOW X is 6 Decrement Subtract 1 from the counter X X = X - 1; // Left  Right: SUBTRACT 1 from X NOW X is 4

39 Another Way to Show Increment & Decrement
numMoves = numMoves + 1; numThings = numThings ˗ 1; SAME AS numMoves++; numThings ˗ ˗; So: Using a Counter X = X + 1 is the same as X++ X = X - 1 is the same as X-- X = X++ is INCORRECT ! X = X-- is INCORRECT !

40 Counters CONTINUED But why use a counter?
Because counters are great with loops!

41 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

42 Counters CONTINUED Here’s another couple of examples!
Suppose you wanted to spin a robot all the way around four times (that is, do a complete 360 circle four times. You will have to adjust your logic accordingly. If one complete circle is 4 left turns, then four complete circles would be 16 left turns. There are two ways you could do this, either by incrementing a value to the counter, or decrementing a value from the counter. First you need to set your value of the counter accordingly: int counter = 0 (used for increment, “count up”) while(counter < 16) //count up to 16 … counter = counter+1; //Same as counter++ or int counter = 16 (used for decrement. “count down”) while(counter > 0) //count down to 0 … counter = counter-1; //Same as counter-- See: increment16.java See: decrement16.java

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

44 A Quick Word About… Scope

45 A Quick Word About Scope

46 Hey! It’s time for another ICE
Lecture 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


Download ppt "BIT115: Introduction to Programming"

Similar presentations


Ads by Google