Lecture 6 Instructor: Craig Duckett. Assignment 1, 2, and A1 Revision Assignment 1 I have finished correcting and have already returned the Assignment.

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

1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
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.
Lecture 12 Instructor: Craig Duckett ARRAYS. Announcements Assignment 3 Assignment 3 Revision Assignment 4 (and Final Exam) GRADED! RETURNED! Woot! NEXT.
Instructor: Craig Duckett Assignment 1 Due Lecture 5 by MIDNIGHT – NEXT – NEXT Tuesday, October 13 th I will double dog try to.
1 Number Types  Every value in Java is either: 1.a reference to an object or 2.one of the eight primitive types  eight primitive types: a.four integer.
CSC 107 – Programming For Science. Announcements  Textbook available from library’s closed reserve.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
CPS120: Introduction to Computer Science Variables and Constants Lecture 8 - B.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Summary of what we learned yesterday Basics of C++ Format of a program Syntax of literals, keywords, symbols, variables Simple data types and arithmetic.
BIT 115: Introduction To Programming LECTURE 3 Instructor: Craig Duckett
PHY 107 – Programming For Science. Announcements  Slides, activities, & solutions always posted to D2L  Note-taking versions before class, for those.
Fundamentals of C and C++ Programming. EEL 3801 – Lotzi Bölöni Sub-Topics  Basic Program Structure  Variables - Types and Declarations  Basic Program.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs.
Lesson 3 – Primitive Data Types Objective: Students will investigate the definition and usage of objects and primitive data types in Java in order to practice.
CHAPTER 4 GC 101 Data types. DATA TYPES  For all data, assign a name (identifier) and a data type  Data type tells compiler:  How much memory to allocate.
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
Lecture 4 – Scanner & Style. Console Output The console that starts a Java application is typically known as the standard output device. The standard.
Chapter 4 Literals, Variables and Constants. #Page2 4.1 Literals Any numeric literal starting with 0x specifies that the following is a hexadecimal value.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
Java Fundamentals Part Integer Division Division can be tricky. In a Java program, what is the value of 1/2? You might think the answer is.
Lecture 5 Instructor: Craig Duckett “Ooga-Chaka Ooga-Ooga”
©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC 110 – INTRO TO COMPUTING - PROGRAMMING Operators.
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,
CSI 3125, Preliminaries, page 1 Data Type, Variables.
1 Chapter 2: Java Fundamentals cont’d Spring Lory Al Moakar.
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Instructor: Craig Duckett Assignment 1 Due Lecture 5 by MIDNIGHT – NEXT – NEXT Wednesday, January 20 th one week from today I will.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Agenda Comments Identifiers Keywords Syntax and Symentics Indentation Variables Datatype Operator.
BIT115: Introduction to Programming
 Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. 
Java-02 Basic Concepts Review concepts and examine how java handles them.
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.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
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”
CSE 110: Programming Language I Matin Saad Abdullah UB 1222.
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.
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
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
BIT115: Introduction to Programming
Java Primer 1: Types, Classes and Operators
BIT 115: Introduction To Programming
BIT115: Introduction to Programming
Variables and Primative Types
BIT115: Introduction to Programming
IDENTIFIERS CSC 111.
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
Chapter 2 Variables.
BIT115: Introduction to Programming
Expressions and Assignment
elementary programming
Summary of what we learned yesterday
Introduction to Primitives
Primitive Types and Expressions
Chapter 2: Java Fundamentals cont’d
Variables and Constants
Presentation transcript:

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) Lecture 8 Assignment 2 Due Lecture 8 by midnight Thursday, October 22 nd PLEASE NOTE: I WILL BE GOING OVER ASSIGNMENT 2 AND OFFERING HINTS FOR SUCCESSFULLY COMPLETING PART 3 IN NEXT TUESDAY’S CLASS. SEE THE LECTURE 7 POWERPOINT IF YOU WANT A HEAD START (particularly Slides 5 through 11). Lecture 10 Assignment 1 Revision due Lecture 10 by midnight Tuesday, November 3 rd (if you are wanting to “improve” your grade or if you did not submit an initial Assignment 1)

3 Assignment 1 Graded! Returned! Woot! Assignment 2 (LECTURE 8) Thursday, October 22 Assignment 1 Revision (LECTURE 10) Tuesday, November 3 Assignment 2 Revision (LECTURE 12) Tuesday, November 10 Assignment 3 (LECTURE 13) Thursday, November 12 Assignment 3 Revision (LECTURE 16) Tuesday, November 24 Assignment 4 (LECTURE 20) Tuesday, December 8 Assignment 4 Revision (LECTURE 21) Thursday, December 10 Assignment Dates (By Due Date)

Warm-Up Quiz And Now… The Warm-Up Quiz

It’s a Mish-Mash Lecture public class vs public vs class Primitive Data Types Review Constants: The final Keyword Temporary Variables (Local Variables) Counters and Counters with Loops Scope

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

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 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 ± to ± , with 7 digits of accuracy double 8 bytes 64 bits Floating-point numbers in the range of ± to ± , 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

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

To Summarize: Temporary Variables (Local Variables)

Counters Increment – Add 1 to the counter X = X + 1; // Left  Right Decrement – Subtract 1 from the counter X = X - 1; // Left  Right

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

Declaring an Integer Variable

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

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

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

EXAMPLE: Counting Things on an Intersection

Storing the Results of a Query

A Quick Word About Scope

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