Centre for Computer Technology ICT214 Object Oriented Design and Programming Week 03 – Operators, Algorithm Design, Programming Constructs Richard Salomon.

Slides:



Advertisements
Similar presentations
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
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.
Conditions What if?. Flow of Control The order of statement execution is called the flow of control Unless specified otherwise, the order of statement.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Some basic I/O.
Program Elements We can now examine the core elements of programming (as implemented in Java) We focuse on: data types variable declaration and use, constants.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Logical and Relational Expressions Nested if statements.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
1 Basic control structures Overview l Relational and Logical Operations l Selection structures »if statement »switch statement l Preview:
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
Logical Operators and Conditional statements
More about Numerical Computation CS-2301, B-Term More about Numerical Computation CS-2301, System Programming for Non-Majors (Slides include materials.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Agenda Review User input Scanner Strong type checking Other flow-control structures switch break & continue Strings Arrays 2.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
CHAPTER:8 OPERATORS AND EXPRESSION IN C++ Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
General Features of Java Programming Language Variables and Data Types Operators Expressions Control Flow Statements.
Conditional Statements For computer to make decisions, must be able to test CONDITIONS IF it is raining THEN I will not go outside IF Count is not zero.
Programming in Java (COP 2250) Lecture 11 Chengyong Yang Fall, 2005.
CSCI 1100/1202 January 28, The switch Statement The switch statement provides another means to decide which statement to execute next The switch.
Java Software Solutions Lewis and Loftus Chapter 5 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. More Programming Constructs.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
1 Advanced Programming IF. 2 Control Structures Program of control –Program performs one statement then goes to next line Sequential execution –Different.
Pseudocode When designing an ALGORITHM to solve a problem, Pseudocode, can be used. –Artificial, informal language used to develop algorithms –Similar.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
CompSci 100E 2.1 Java Basics - Expressions  Literals  A literal is a constant value also called a self-defining term  Possibilities: o Object: null,
Perform Calculations and Control Flow Telerik Software Academy Learning & Development Team.
4.1 Object Operations operators Dot (. ) and new operate on objects The assignment operator ( = ) Arithmetic operators + - * / % Unary operators.
Chapter 4 Control Structures I. Chapter Objectives Learn about control structures Examine relational and logical operators Explore how to form and evaluate.
Programming Perl in UNIX Course Number : CIT 370 Week 3 Prof. Daniel Chen.
Operators in JAVA. Operator An operator is a symbol that operates on one or more arguments to produce a result. Java provides a rich set of operators.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
1 Lecture 5 More Programming Constructs Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.
Control statements Mostafa Abdallah
BOOLEAN OPERATIONS AND CONDITIONALS CHAPTER 20 1.
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
Expressions and Order of Operations Operators – There are the standard operators: add, subtract, divide, multiply – Note that * means multiply? (No times.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103 material developed.
1 Advanced Programming IF. 2 Control Structures Program of control –Program performs one statement then goes to next line Sequential execution –Different.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Session 2 Operators, Decisions and Loops. Objectives Operators Casting data Decision marking structures Loops break, continue, return.
Control Statements: Part1  if, if…else, switch 1.
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.
Programming Principles Operators and Expressions.
Lecture 3.1 Operators and Expressions Structured Programming Instructor: Prof. K. T. Tsang 1.
Object-Oriented Programming and Problem Solving Dr. Ramzi Saifan Introduction and basics of Java Slides adapted from Steven Roehrig.
University of Central Florida COP 3330 Object Oriented Programming
Programming in Java Sachin Malhotra, Chairperson, PGDM-IT, IMS Ghaziabad Saurabh Chaudhary, Dean, Academics, IMS Ghaziabad.
Selections Java.
Sachin Malhotra Saurabh Choudhary
University of Central Florida COP 3330 Object Oriented Programming
Control Statements: Part 2
Expressions and Control Flow in JavaScript
Java Programming: From Problem Analysis to Program Design, 4e
Java Programming: Guided Learning with Early Objects
CET 3640 – Lecture 2 Java Syntax Chapters 2, 4, 5
OPERATORS (2) CSC 111.
Starting JavaProgramming
Arithmetic operations, decisions and looping
More about Numerical Computation
Chapter 2: Basic Elements of Java
Java Tokens & Data types
Chapter 4: Control Structures I (Selection)
Control Structure Chapter 3.
Chap 7. Advanced Control Statements in Java
ENERGY 211 / CME 211 Lecture 5 October 1, 2008.
Control Structure.
Problem 1 Given n, calculate 2n
Presentation transcript:

Centre for Computer Technology ICT214 Object Oriented Design and Programming Week 03 – Operators, Algorithm Design, Programming Constructs Richard Salomon and Umesh Patel Centre for Information and Communication Technology Box Hill Institute of TAFE

December 24, 2015December 24, 2015December 24, 2015 Copyright Box Hill Institute Contents at a glance Operators Operators Casting Casting java.lang.System class java.lang.System class in in out out err err Control programming constructs Control programming constructs

December 24, 2015December 24, 2015December 24, 2015 Copyright Box Hill Institute Operators Operators perform manipulations on data within expressions, to obtain a result Operators perform manipulations on data within expressions, to obtain a result Operators may be: Operators may be: object, object, arithmetic, arithmetic, logical, logical, comparison and comparison and conversion or cast conversion or cast

December 24, 2015December 24, 2015December 24, 2015 Copyright Box Hill Institute Object Operators new eg Student s1 = new Student( ) ; new eg Student s1 = new Student( ) ; dot operator as in ObjectReference. member [. method() ] dot operator as in ObjectReference. member [. method() ] object operators can only access non-private members of another class object operators can only access non-private members of another class

December 24, 2015December 24, 2015December 24, 2015 Copyright Box Hill Institute Arithmetic Operators unary +, -, ++, - - unary +, -, ++, - - binary +, -, *, /, % binary +, -, *, /, % precedence BODMAS [ or BOMDAS ] precedence BODMAS [ or BOMDAS ] Beware of division results eg 7/4 = 1 but 7.0/4.0 = % 3 = 1 while % 3.2 = 1.35 Beware of division results eg 7/4 = 1 but 7.0/4.0 = % 3 = 1 while % 3.2 = 1.35 operands in modulo operations should have no sign. Result is –ve if left operand is –ve. operands in modulo operations should have no sign. Result is –ve if left operand is –ve.

December 24, 2015December 24, 2015December 24, 2015 Copyright Box Hill Institute Prefix and Postfix Increment/Decrement x++ is postfix (L to R association) x++ is postfix (L to R association) ++x is prefix (R to L association) ++x is prefix (R to L association) ++ and -- takes precedence before other operators eg int x=5, y=7, z ; z = x * ++y; //results in z = and -- takes precedence before other operators eg int x=5, y=7, z ; z = x * ++y; //results in z = 40

December 24, 2015December 24, 2015December 24, 2015 Copyright Box Hill Institute Comparison Operators Object type - instanceof Object type - instanceof Equality - == != Equality - == != Ordinal - > >= >= < <= result is boolean literal - true or false

December 24, 2015December 24, 2015December 24, 2015 Copyright Box Hill Institute Logical Operators Boolean data type values: true / false Boolean data type values: true / false (5 >= 7) && (3 != -3) F (5 >= 7) && (3 != -3) F (5 >= 7) & (3 != -3) F (5 >= 7) & (3 != -3) F (5 < 7) || (3 != -3) T (5 < 7) || (3 != -3) T (5 < 7) | (3 == -3) T (5 < 7) | (3 == -3) T (5 < 7) ^ (3 == -3) T (5 < 7) ^ (3 == -3) T with s/c without s/c and&&& or||| not! ExOr^

December 24, 2015December 24, 2015December 24, 2015 Copyright Box Hill Institute Bitwise Operators All bitwise operators work upon 32-bit numbers. All bitwise operators work upon 32-bit numbers. & | ^ ~ data & mask  data; bit test, bit clearing data | mask  data; bit set, ~data  data ; bit complement data ^ mask  data; selected bit complement & | ^ ~ data & mask  data; bit test, bit clearing data | mask  data; bit set, ~data  data ; bit complement data ^ mask  data; selected bit complement > >>> data > n ; shift data n bits right - sign extend (arith shift) data >>> n ; shift data n bits left - 0 fill (logical shift) > >>> data > n ; shift data n bits right - sign extend (arith shift) data >>> n ; shift data n bits left - 0 fill (logical shift)

December 24, 2015December 24, 2015December 24, 2015 Copyright Box Hill Institute bitwise examples b = 0x05; (dec 5 ) ( only the 8 lsb shown) b << 2 ; ( dec 20 ) b = 0x05; (dec 5 ) ( only the 8 lsb shown) b << 2 ; ( dec 20 ) int v = -32; v = 0xFFFFFFE0 ; (a) v >> 1; 1 – ( -16) (b) v >>> 1; (32,752) int v = -32; v = 0xFFFFFFE0 ; (a) v >> 1; 1 – ( -16) (b) v >>> 1; (32,752)

December 24, 2015December 24, 2015December 24, 2015 Copyright Box Hill Institute Casting Casting converts one data type into another Casting converts one data type into another Casting that widens is implicit and OK Casting that widens is implicit and OK Casting that shortens must be explicit Casting that shortens must be explicit short a, b, c; a = 5; b = 2; c = (short) (a + b); short a, b, c; a = 5; b = 2; c = (short) (a + b); NOT short a, b, c; a = 5; b = 2; c = a + b; NOT short a, b, c; a = 5; b = 2; c = a + b;

December 24, 2015December 24, 2015December 24, 2015 Copyright Box Hill Institute Character data type Uses Unicode eg char ch = ‘a’; [ \u0061 ] Uses Unicode eg char ch = ‘a’; [ \u0061 ] glyphs \n, \t, \r, \”, \\ glyphs \n, \t, \r, \”, \\ char ch = ‘a’; ch +=2; // ch = ‘c’ char ch = ‘a’; ch +=2; // ch = ‘c’ char a=‘A’, b=‘B’, c; c = a + b; //fails but c = (char) (a + b); // is OK char a=‘A’, b=‘B’, c; c = a + b; //fails but c = (char) (a + b); // is OK char c1 = “Z”; // fails char c1 = “Z”; // fails

December 24, 2015December 24, 2015December 24, 2015 Copyright Box Hill Institute java.lang.System Class java core class java core class static (class or shared) objects: static (class or shared) objects: in in out out err err

December 24, 2015December 24, 2015December 24, 2015 Copyright Box Hill Institute java.lang.System Class objectmethodsdevice System.in read() throws exception keyboard System.outprint()println()flush()monitor System.errprint()flush()monitor

December 24, 2015December 24, 2015December 24, 2015 Copyright Box Hill Institute System Class Output stream System.out.print(“ argument to print ”); or System.out.println(“ argument to print ”); Output stream System.out.print(“ argument to print ”); or System.out.println(“ argument to print ”); Input stream System.in.read( ); returns the ordinate (int) value of the pressed key Input stream System.in.read( ); returns the ordinate (int) value of the pressed key

December 24, 2015December 24, 2015December 24, 2015 Copyright Box Hill Institute Assignment operators Identical to C Identical to C =, +=, -=, *=, /=, %= =, +=, -=, *=, /=, %= e.g. x *=3; [ triples the value of x ] e.g. x *=3; [ triples the value of x ]

December 24, 2015December 24, 2015December 24, 2015 Copyright Box Hill Institute Control Structures Used only within methods of a class Used only within methods of a class Used to control the sequence of operations at runtime Used to control the sequence of operations at runtime The 3 structure types are: The 3 structure types are: sequence sequence decision aka selection or branching decision aka selection or branching repetition aka looping repetition aka looping All program algorithms may be organised as some combination of the 3 structures All program algorithms may be organised as some combination of the 3 structures Nesting is OK Nesting is OK

December 24, 2015December 24, 2015December 24, 2015 Copyright Box Hill InstituteRepetition 3 repetition constructs 3 repetition constructs for for while while do while do while for and while test before loop execution for and while test before loop execution do-while tests after loop execution do-while tests after loop execution Same as in C

December 24, 2015December 24, 2015December 24, 2015 Copyright Box Hill Institute Repetition syntax for (int j= 0; j <5; j += 2) System.out.println(“value of j is: ” + j + “”) ; for (int j= 0; j <5; j += 2) System.out.println(“value of j is: ” + j + “”) ; int j = 0; int j = 0; while ( j < 5) { System.out.println(“value of j is: ” + j + “”) ; j += 2; }

December 24, 2015December 24, 2015December 24, 2015 Copyright Box Hill Institute Repetition syntax int j = 0; do{ System.out.println(“value of j is: ” + j + “”) ; j += 2; } while ( j < 5) ;

December 24, 2015December 24, 2015December 24, 2015 Copyright Box Hill Institute break; continue; label: break - exits a statement construct break - exits a statement construct continue - returns to the control statement helps with loop control continue - returns to the control statement helps with loop control label: - defines a point in the code sometimes used with break and continue statements, which helps with nested loop control, but should be avoided. label: - defines a point in the code sometimes used with break and continue statements, which helps with nested loop control, but should be avoided.

December 24, 2015December 24, 2015December 24, 2015 Copyright Box Hill InstituteDecision if, if–else, if–else–if, switch, conditional statement if, if–else, if–else–if, switch, conditional statement if (mark > 90) result = “Distinction” ; if (mark >= 75) result = ‘C’; if (mark >= 50) result = ‘P’; else result = ‘N’; result = (mark > 50) ? ‘P’ : ‘N’ ; result = (mark > 50) ? ‘P’ : ‘N’ ;

December 24, 2015December 24, 2015December 24, 2015 Copyright Box Hill Institute Decision – switch statement switch (expr) { case val1: statements; break; case val2: statements; break; Default: statements; }

December 24, 2015December 24, 2015December 24, 2015 Copyright Box Hill Institute switch example 1 switch (month) 1 switch (month) 2 { 2 { 3 case 1: 3 case 1: 4 case 3: 4 case 3: 5 case 5: 5 case 5: 6 case 7: 6 case 7: 7 case 8: 7 case 8: 8 case 10: 8 case 10: 9 case 12: 9 case 12: 10 // months with 31 days 10 // months with 31 days 11 numDays = 31; 11 numDays = 31; 12 break; 12 break; 13 case 4: 13 case 4: 14 case 6: 14 case 6: 15 case 9: 15 case 9: 16 case 11: 16 case 11: 17 // months with 30 days 17 // months with 30 days 18 numDays = 30; 18 numDays = 30; 19 break; 19 break; 20 case 2: 20 case 2: 21 // check for leap year 21 // check for leap year 22 if ( ((year % 4 == 0) && !(year % 100 == 0)) 22 if ( ((year % 4 == 0) && !(year % 100 == 0)) 23 || (year % 400 == 0) ) 23 || (year % 400 == 0) ) 24 numDays = 29; 24 numDays = 29; 25 else 25 else 26 numDays = 28; 26 numDays = 28; 27 break; 27 break; 28 } 28 }