CPSC 233 Tutorial Xin Jan 24, 2011. Assignment 1 Due on Jan 28 at 4:00 PM Part I  Assignment Box on 2 nd floor Part II  Submitted electronically on.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Air Force Institute of Technology Electrical and Computer Engineering
Operators and Expressions Operators are symbols that produce a result based on some rules. Examples: +, -, =, > and
L5:CSC © Dr. Basheer M. Nasef Lecture #5 By Dr. Basheer M. Nasef.
Types, Variables and Operators Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2013.
James Tam Introduction To Java Programming You will learn about the process of creating Java programs and constructs for input, output, branching, looping,
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
All the Operators. Precedence An operator with higher precedence is done earlier (prededes) one with lower precedence –A higher precedence is indicated.
All the Operators. Precedence An operator with higher precedence is done earlier (prededes) one with lower precedence –A higher precedence is indicated.
James Tam Introduction To Java Programming You will study the process of creating Java programs and constructs for input, output, branching, looping,
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
CS 3850 Lecture 5 Operators. 5.1 Binary Arithmetic Operators Binary arithmetic operators operate on two operands. Register and net (wire) operands are.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Control Statements.
SE 360 Advances in Software Development Asst.Prof.Dr. Senem Kumova Metin.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Outline Variables 1.
CONTROL STATEMENTS IF-ELSE, SWITCH- CASE Introduction to Computer Science I - COMP 1005, 1405 Instructor : Behnam Hajian
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Operators Using Java operators An operator takes one or more arguments and produces a new value. All operators produce a value from their.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
Simple Data Types and Statements. Namespaces namespace MyNamespace { // …. { MyNamespace::func1() using namespace OtherNamespace; Comments: // /* xxxx.
LESSON 6 – Arithmetic Operators
Java Keywords  Reserved Words Part of the Java language Examples: public, static, void  Pre-defined Java Identifiers Defined in Java Libraries Examples:
Chapter 3 Selections Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved
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, 4e Chapter 2 Basic Elements of Java.
C++ Basics Tutorial 6 Operators. What are going to see today? Assignment operator(=) Arithmetic operators(+,-,*,/,%) Compound assignment(+=,-=,*=……..)
Session Three Review & Conditional Control Flow. Java File Hierarchy Projects Packages Classes Methods.
CompSci 100E 2.1 Java Basics - Expressions  Literals  A literal is a constant value also called a self-defining term  Possibilities: o Object: null,
CSIS 113A Lecture 3 Conditional & Switch Glenn Stevenson CSIS 113A MSJC.
C Operators. CONTENTS C OPERATORS TYPES OF OPERATOR UNARY BINARY TERNARY ARITHMATIC RELATIONAL LOGICAL.
1 Announcements B7 tutorial today in CS103 –In CSE department –Ask at entry desk for direction to CS103.
Java operatoriai sandbolts/operators.html
4.1 Object Operations operators Dot (. ) and new operate on objects The assignment operator ( = ) Arithmetic operators + - * / % Unary operators.
15-Nov-15 All the Operators. operators.ppt 2 Precedence An operator with higher precedence is done earlier (precedes) one with lower precedence A higher.
Computing with C# and the.NET Framework Chapter 2 C# Programming Basics ©2003, 2011 Art Gittleman.
1-Dec-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.
Centre for Computer Technology ICT214 Object Oriented Design and Programming Week 03 – Operators, Algorithm Design, Programming Constructs Richard Salomon.
CompSci Primitive Types  Primitive Types (base types)  Built-in data types; native to most hardware  Note: not objects (will use mostly first.
1 Lecture 5 More Programming Constructs Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Java Operator Precedence Operator Meaning Assoc. ===========================================================.
Operators & Expressions
James Tam Introduction To Java Programming You will study the process of creating Java programs and constructs for input, output, branching, looping,
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.
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.
Programming Principles Operators and Expressions.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 CMPE-013/L Operators Gabriel Hugh Elkaim Spring 2012.
Java Keywords  Reserved Words Part of the Java language Examples: public, static, void  Pre-defined Java Identifiers Defined in Java Libraries Examples:
Object-Oriented Programming and Problem Solving Dr. Ramzi Saifan Introduction and basics of Java Slides adapted from Steven Roehrig.
CompSci 230 S Programming Techniques
Lecture 3 Java Operators.
Lecture 3: Operators, Expressions and Type Conversion
Selections Java.
Lecture 2: Data Types, Variables, Operators, and Expressions
Computing with C# and the .NET Framework
Java operatoriai
Operators and Expressions
All the Operators 22-Nov-18.
All the Operators 4-Dec-18.
Expressions and Assignment
Chapter 2 Programming Basics.
All the Operators 6-Apr-19.
Chapter 3 Selections Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
All the Operators 13-Apr-19.
Problem 1 Given n, calculate 2n
Presentation transcript:

CPSC 233 Tutorial Xin Jan 24, 2011

Assignment 1 Due on Jan 28 at 4:00 PM Part I  Assignment Box on 2 nd floor Part II  Submitted electronically on UNIX Submit submit -c -a eg. submit -c 233 -a 3 README.txt List submitted files showstuff -c -a eg. showstuff -c 233 -a 3 Submit early and update

Keyboard Input Again Scanner.nextLine() will pick up any leftovers If Scanner.nextInt() was called previously, nextLine() usually pickup a carriage-return (CR) use an extra nextLine() to get rid of the CR Experiments with MyInput.java Input 20 and see the results Input 20 abc and see the results

Pick up chars String str = in.nextLine(); char c = str.charAt(0); What other methods String has? Search in Google with class String java API using documents provided by oracle.com

Switch statement char x = ‘a’; int val; switch (x) { case ‘a’: val = 0; // fall through case ‘b’: val = 1; break; case ‘c’: // fall through case ‘C’: // do the same for ‘c’ and ‘C’ val = 3; break; default: val = 4; } Fall through: Without an explicit break statement, the execution continues on the next case!!! This can be used to do the same thing for a few values.

Operator precedence An example int v = 92 int x = 100 | 25 & 36 << & 55 * ++ v; = 100 | ((25 & (36 << (2 + 12))) & (55 * (++ v))) Extremely confusing Sometimes results are dependent on the specific complier (for c/c++) Avoid by all means Clarify with parentheses until it is easy to understand by common humans

Common Java Operators / Operator Precedence Precedence level OperatorDescriptionAssociativity 1expression++ expression-- Post-increment Post-decrement Right to left 2++expression --expression + - ! ~ (type) Pre-increment Pre-decrement Unary plus Unary minus Logical negation Bitwise complement Cast Right to left

Common Java Operators / Operator Precedence Precedence level OperatorDescriptionAssociativity 3*/%*/% Multiplication Division Remainder/modulus Left to right Addition or String concatenation Subtraction Left to right 5<< >> Left bitwise shift Right bitwise shift Left to right

Common Java Operators / Operator Precedence Precedence level OperatorDescriptionAssociativity 6< <= > >= Less than Less than, equal to Greater than Greater than, equal to Left to right 7= != Equal to Not equal to Left to right 8&Bitwise ANDLeft to right 9^Bitwise exclusive ORLeft to right

Common Java Operators / Operator Precedence Precedence level OperatorDescriptionAssociativity 10|Bitwise ORLeft to right 11&&Logical ANDLeft to right 12||Logical ORLeft to right

Common Java Operators / Operator Precedence Precedence level OperatorDescriptionAssociativity 13= += -= *= /= %= &= ^= |= <<= >>= Assignment Add, assignment Subtract, assignment Multiply, assignment Division, assignment Remainder, assignment Bitwise AND, assignment Bitwise XOR, assignment Bitwise OR, assignment Left shift, assignment Right shift, assignment Right to left

x ++ vs. ++x public class IncOperators { public static void main (String [] args) { int x = 8; System.out.println("x = " + x); int returnedValue = ++ x; System.out.println("the returned value is: " + returnedValue); System.out.println("x after the operation is: " + x); }

x ++ vs. ++x public class Order2 { public static void main (String [] args) { int num1; int num2; num1 = 5; num2 = ++num1 * num1++; System.out.println("num1=" + num1); System.out.println("num2=" + num2); }

type cast Double  int float x = 5; // OK float x = 5.0;// Illegal float x = 5.f; // OK int x = 5.0; // Illegal int x = (int) 5.f; // OK double x = 5 / 2; // x = 2 double x = 5 / (double)2; // x = 2.5 char  int char c = 97; // OK int x = ‘a’; // OK char c = ‘a’ + 1; // OK

bitwise operations ~ & | ^ > ~ = & = | = ^ = << = >> = >> = 0010

Showing integers in binary public class ShowBinary { public static void main (String [] args) { showInt(3); } public static void showInt(int x) { for (int i = 0; i < 32; i ++) { if ((x & 0x ) == 0) System.out.print("0"); else System.out.print("1"); x = x << 1; }

Experiments with the program public static void main (String [] args) { int x = 3; System.out.println("x = " + x + " in binary: "); showInt(x); System.out.println(); int y = -2011; System.out.println("y = " + y + " in binary: "); showInt(y); System.out.println(); System.out.println("x AND y in binary: "); showInt(x & y); System.out.println(); }