BIT115: Introduction to Programming

Slides:



Advertisements
Similar presentations
10-Jun-15 Introduction to Primitives. 2 Overview Today we will discuss: The eight primitive types, especially int and double Declaring the types of variables.
Advertisements

Variables Pepper. Variable A variable –box –holds a certain type of value –value inside the box can change Example –A = 2B+1 –Slope = change in y / change.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Introduction to a Programming Environment
11 Chapter 4 LOOPS AND FILES. 22 THE INCREMENT AND DECREMENT OPERATORS To increment a variable means to increase its value by one. To decrement a variable.
While Loops and Do Loops. Suppose you wanted to repeat the same code over and over again? System.out.println(“text”); System.out.println(“text”); System.out.println(“text”);
Lecture 10 Instructor: Craig Duckett. Assignment 2 Revision TONIGHT DUE TONIGHT Wednesday, August 5 th Assignment 3 NEXT DUE NEXT Monday, August 10 th.
Lecture 6a Instructor: Craig Duckett. Upcoming Assignments & Mid-Term Assignment 1 Revision Assignment 1 Revision is due NEXT Wednesday, July 29 th, by.
Instructor: Craig Duckett Assignment 1 Due Lecture 5 by MIDNIGHT – NEXT – NEXT Tuesday, October 13 th I will double dog try to.
Lecture 6 Instructor: Craig Duckett. Assignment 1, 2, and A1 Revision Assignment 1 I have finished correcting and have already returned the Assignment.
BIT 115: Introduction To Programming LECTURE 3 Instructor: Craig Duckett
Elements of a Java Program Bina Ramamurthy SUNY at Buffalo.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs.
Looping and Counting Lecture 3 Hartmut Kaiser
PHY-102 SAPVariables and OperatorsSlide 1 Variables and Operators In this section we will learn how about variables in Java and basic operations one can.
Lecture 5 Instructor: Craig Duckett “Ooga-Chaka Ooga-Ooga”
Variables and Constants Objectives F To understand Identifiers, Variables, and Constants.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Instructor: Craig Duckett Assignment 1 Due Lecture 5 by MIDNIGHT – NEXT – NEXT Wednesday, January 20 th one week from today I will.
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
Lecture 11 Instructor: Craig Duckett Instance Variables.
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”
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”
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
11 Making Decisions in a Program Session 2.3. Session Overview  Introduce the idea of an algorithm  Show how a program can make logical decisions based.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
Instructor: Craig Duckett Assignment 1 Due Lecture 5 by MIDNIGHT – NEXT – NEXT Monday, October 10 th I will double dog try to have.
BIT 115: Introduction To Programming L ECTURE 3 Instructor: Craig Duckett
BIT115: Introduction to Programming
Professor: Dr. Baba Kofi Weusijana Pronounced Bah-bah Co-fee Way-ou-see-jah-nah Call him “Baba” or “Dr. Weusijana”
Lecture 13 Instructor: Craig Duckett. Assignment 2 Revision DUE TONIGHT! November 9 th DUE TONIGHT! November 9 th Uploaded to StudentTracker by midnight.
Lecture 4 – Scanner & Style
Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area
Chapter 2 Variables.
Java Language Basics.
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
Elementary Programming
Introduction to Computer Science / Procedural – 67130
Lecture 5: Some more Java!
BIT 115: Introduction To Programming
BIT115: Introduction to Programming
Variables and Primative Types
Primitive Data, Variables, Loops (Maybe)
BIT115: Introduction to Programming
BIT115: Introduction to Programming
Arrays, For loop While loop Do while loop
IDENTIFIERS CSC 111.
Chapter 2 Edited by JJ Shepherd
Variables ICS2O.
Variables Numbers can be stored and retrieved while a program is running if they are given a home. The way that integers and decimal numbers are stored.
Building Java Programs
Chapter 2 Variables.
BIT115: Introduction to Programming
Variables, Types, Operations on Numbers
Variables, Types, Operations on Numbers
Building Java Programs
Introduction to Primitives
BIT115: Introduction to Programming
Introduction to Primitives
Instructor: Craig Duckett
Homework Finishing Chapter 2 of K&R. We will go through Chapter 3 very quickly. Not a lot is new. Questions?
Building Java Programs
BIT116: Scripting Lecture 6 Part 1
Winter 2019 CISC101 4/28/2019 CISC101 Reminders
Unit 3: Variables in Java
Chapter 2 Variables.
Building Java Programs
Variables and Constants
Presentation transcript:

BIT115: Introduction to Programming Lecture 6 Instructor: Craig Duckett

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

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

And Now… The Warm-Up Quiz

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

REVIEW 1 Class File vs. 2 Class File

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.

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.

REVIEW Primitive Data Types

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 ±3.410-38 to ±3.41038, with 7 digits of accuracy double ±1.710-308 to ±1.710308, with 15 digits of accuracy

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.

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

Constants The final Keyword WRITTEN IN STONE WRITTEN IN STONE

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.

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.

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

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

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!

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

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

if Example Code WALK-THROUGH: RobotWall.java

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

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

while Example Code WALK-THROUGH: RobotWall.java

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)

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

ANSWER An infinite loop!

while Example Code WALK-THROUGH: RobotWall.java

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?

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!

Counters

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.

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…

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 1 } // Read from Right to Left! WALK-THROUGH: RobotWall.java

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

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

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 !

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

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

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

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;

A Quick Word About… Scope

A Quick Word About Scope

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