COP2800 – Computer Programming Using JAVA University of Florida Department of CISE Spring 2013 Lecture 09 – Programming with Java Datatypes and Flow Control.

Slides:



Advertisements
Similar presentations
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Advertisements

IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
Introduction to Computers and Programming Lecture 7:
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
CMT Programming Software Applications
Introduction to Computers and Programming More Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
Computer Programming 1 Repetition. Computer Programming 2 Objectives Repetition structures Study while and do loops Examine for loops A practical example.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Chapter 2 Data Types, Declarations, and Displays
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
What is RobotC?!?! Team 2425 Hydra. Overview What is RobotC What is RobotC used for What you need to program a robot How a robot program works Framework.
Programming Principles Data types and Variables. Data types Variables are nothing but reserved memory locations to store values. This means that when.
By: A.Abbasi Omid Reza Nejati Marvdasht Islamic Azad University.
Java Building Elements Lecture 2 Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University
CIS Computer Programming Logic
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
DAT602 Database Application Development Lecture 5 JAVA Review.
COP2800 – Computer Programming Using JAVA University of Florida Department of CISE Spring 2013 Lecture 13 – Having Fun with Arrays in Java Webpage:
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
JAVA Tokens. Introduction A token is an individual element in a program. More than one token can appear in a single line separated by white spaces.
Lec 6 Data types. Variable: Its data object that is defined and named by the programmer explicitly in a program. Data Types: It’s a class of Dos together.
Lecture #5 Introduction to C++
COP2800 – Computer Programming Using JAVA University of Florida Department of CISE Spring 2013 Lecture 10 – Programming with Java Datatypes Type Casting.
University of Palestine software engineering department Introduction to data structures Control Statements: Part 1 instructor: Tasneem Darwish.
Property of Jack Wilson, Cerritos College1 CIS Computer Programming Logic Programming Concepts Overview prepared by Jack Wilson Cerritos College.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 2: Variables & Data Types.
An Introduction to Java – Part 1 Dylan Boltz. What is Java?  An object-oriented programming language  Developed and released by Sun in 1995  Designed.
Java development environment and Review of Java. Eclipse TM Intergrated Development Environment (IDE) Running Eclipse: Warning: Never check the “Use this.
Introduction to Java Java Translation Program Structure
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
COP2800 – Computer Programming Using JAVA University of Florida Department of CISE Spring 2013 Lecture 11 – How to Get Started on Assignment #2 Webpage:
Java Programming, Second Edition Chapter Two Using Data Within a Program.
COMP Primitive and Class Types Yi Hong May 14, 2015.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
 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.
COP2800 – Computer Programming Using JAVA University of Florida Department of CISE Spring 2013 Lecture 20 – Inheritance and Polymorphism in Java Webpage:
CS-1010 Dr. Mark L. Hornick 1 Selection and Iteration and conditional expressions.
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
COMP Loop Statements Yi Hong May 21, 2015.
COP2800 – Computer Programming Using JAVA University of Florida Department of CISE Spring 2013 Lecture 06 – Java Datatypes Webpage:
 Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. 
COP2800 – Computer Programming Using JAVA University of Florida Department of CISE Spring 2013 Lecture 39 – Command Line Args & Recursion Webpage:
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Session 2 Operators, Decisions and Loops. Objectives Operators Casting data Decision marking structures Loops break, continue, return.
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.
CS0007: Introduction to Computer Programming The for Loop, Accumulator Variables, Seninel Values, and The Random Class.
JAVA Programming (Session 2) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS 1 Copyright: 2015 Illinois Institute of Technology_ George Koutsogiannakis.
COP2800 – Computer Programming Using JAVA University of Florida Department of CISE Spring 2013 Lecture 14 – How to Complete Assignment 2, Part III Webpage:
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
Basic Data Types อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา Chapter 4.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
COP2800 – Computer Programming Using JAVA University of Florida Department of CISE Spring 2013 Lecture 08 – Hand-On Work: Assignment 1, Part III Webpage:
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.
Topics introduced today (these topics would be covered in more detail in later classes) – Primitive Data types Variables Methods “for” loop “if-else” statement.
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
COP2800 – Computer Programming Using JAVA
Loops in Java.
Engineering Innovation Center
Chapter 2.
Outline Altering flow of control Boolean expressions
LRobot Game.
An Introduction to Java – Part I, language basics
Names of variables, functions, classes
Presentation transcript:

COP2800 – Computer Programming Using JAVA University of Florida Department of CISE Spring 2013 Lecture 09 – Programming with Java Datatypes and Flow Control Webpage:

COP2800 – Programming in JAVA Course Objectives – Basic Knowledge of Computers & Programming – Specific Knowledge of JAVA Programming – Practical Programming Projects Build Skills Today’s Class – Review of Java Datatypes – Review of Flow Control – Numerical Decision Statements – Filtering Numbers

Review: Java Program Structure HIGH-LEVEL VIEW JAVA Units: -Packages -Classes (Instances) -Methods -Instructions -Variables PICTURE CREDIT:

Review: Java Package Structure PICTURE CREDIT:

New: Java Datatypes A datatype is a way of specifying the value of a variable x determines what actions can be performed on x sometimes varies with the programming language Java variables are statically typed – datatype must be declared before the variable can be used have datatype declarations using special words Example: byte, short, int, long, float, boolean, char, (string in the Java String class)

Java Datatypes (cont’d) An int (integer) is a 32-bit signed twos-complement integer specified by int descriptor (reserved word) Example: int i = 10; A float (single-precision floating-point number) is a positive or negative decimal number specified by float descriptor (reserved word) Example: float x = ; Usage Danger: F2C(-22.9f)

Java Datatypes (cont’d) A short (integer) is a 16-bit signed twos-complement integer specified by short descriptor (reserved word) Example: short i, j; A long (integer) is a 64-bit signed twos-complement integer specified by long descriptor (reserved word) Example: long j = L; (Java compiler would convert this number to long)

Java Datatypes (cont’d) A double (double-precision floating point number) is a 64-bit IEEE 754 format decimal number specified by double descriptor (reserved word) Example: double y = 1.4e-32; Usage Danger: function_name(-22.9e-47d) A boolean (logical value) has value true or false specified by boolean descriptor (reserved word) Example: boolean x = true;

Java Datatypes (cont’d) A char (character) is a 16-bit Unicode representation of a symbol specified by char descriptor (reserved word) Example: char c = \u0B17; char d = ‘e’ A byte (integer) is an 8-bit signed twos-complement integer specified by byte descriptor (reserved word) Example: byte k = -27;

Simple Decision Control Structure If.. then Java Example in class Bike here is the method If Bike is moving Then apply the brakes void applyBrakes() { if (isMoving) { currentSpeed--; } “if”-part is stated “then” is implied

If..Then..Else Control Structure If.. then.. else Java Example in class Bike here is the method If Bike is moving Then apply the brakes Else inform user: stopped void applyBrakes() { if (isMoving) { currentSpeed--; } else { System.err.println(“Stopped!"); } } “if”-part is stated “then” is implied “else” part stated

“for-loop” Control Structure Syntax: for(initialization; Boolean_expression; update) { //Statements in body of loop } Meaning: Initialization Declare/initialize loop varbls Boolean_expression Controls loop execution = trueLoop continues to execute = falseExit from loop to next statement Update Optional: update loop varbls

“for-loop” Control Structure (cont’d) Syntax: for(initialization; Boolean_expression; update) { //Statements in body of loop } Example: for (int x = 10; x < 20; x = x+1) { sum = sum + x; } Integer “x” has a starting value of 10Loop runs as long as x < 20 Increment variable “x” by 1 at each iter- ation Body of Loop: add x to a variable “sum” (assumed to initially be zero) x = 10sum = 10 x = 11sum = 21 x = 12sum = 33 : : x = 20Exit Loop

“while-loop” Control Structure Syntax: while(Boolean_expression) { //Statements in body of loop } Meaning: Boolean_expression Controls loop execution = trueBody of loop executes = falseExit from loop to next statement

“while-loop” Control Structure (cont’d) Syntax: while(Boolean_expression) { //Statements in body of loop } Example: x = 10; while (x < 20) { sum = sum + x; x = x + 1; } Integer “x” has a starting value of 10 Loop runs as long as x < 20 Body of Loop: add x to a variable “sum” x = 10sum = 10 x = 11sum = 21 x = 12sum = 33 : : x = 20Exit Loop

“do-while” Control Structure Syntax: do { //Statements in body of loop } while(Boolean_expression); Meaning: Boolean_expression Controls loop execution = trueBody of loop executes = falseExit from loop to next statement Body of loop executes once before Boolean expression is evaluted

“do-while” Control Structure (cont’d) Syntax: do { //Statements in body of loop } while(Boolean_expression) ; Example: x = 10; do{ x = x + 1; sum = sum + x; } while (x < 20) ; Integer “x” has a starting value of 10 Loop runs as long as x < 20 Body of Loop: add x to a variable “sum” x = 11sum = 11 x = 12sum = 23 x = 13sum = 36 : : x = 20Exit Loop Body of loop executes once before Boolean expression is evaluted

Have You Won The Lotto? Task: Decide if you’ve got a winning lotto number (based on a known list) Algorithm: 1.Given a “test number” x 2.And a range of “winning numbers” [y,z] 3.Find if x is in range 4.Display “Winner ” or “Loser  ”

Have You Won The Lotto? (cont’d) Java Code Fragment: // assign datatypes and initial values to bounds int y = 10, z = 15; // test to see if number is in-range void testNumber( int x ) { if ( x <= z ) { if ( x >= y ) { System.out.println(“Winner!”); } else { System.out.println(“Loser!”); } } else { System.out.println(“Loser!”); } }

Have You Won The Lotto? (cont’d) A Better Designed Java Code Fragment: // assign datatypes and initial values to bounds int y = 10, z = 15; // test to see if number is in-range void testNumber(int x) { if ( x = y ) { System.out.println(“Winner!”); } else { System.out.println(“Loser!”); } } Boolean oper- ator means logical “and”

This Week: Java Program Design Next Class (Wednesday) More programming with flow control Introduction to “type casting” Overview of Assignment #2 Look-Ahead (Friday) How to e-Submit Assignment #1 How to Start Assignment #2, Parts I and II - Programming with Flow Control & Datatypes