Object-Oriented Programming and Problem Solving Dr. Ramzi Saifan Introduction and basics of Java Slides adapted from Steven Roehrig.

Slides:



Advertisements
Similar presentations
Operators and Expressions Operators are symbols that produce a result based on some rules. Examples: +, -, =, > and
Advertisements

ISBN Chapter 7 Expressions and Assignment Statements.
 2005 Pearson Education, Inc. All rights reserved Introduction.
Object-Oriented Programming MISM/MSIT Carnegie Mellon University Lecture 2: Program Control.
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.
Intermediate Java Sakir YUCEL MISM/MSIT Carnegie Mellon University Program Control Slides adapted from Steven Roehrig.
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
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.
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations Imperative Programming, B. Hirsbrunner,
Expressions An expression is a sequence of operands and operators that reduces to a single value expression operator operand An operator is a language-specific.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
ISBN Chapter 7 Expressions and Assignment Statements.
Chapter 4: Basic C Operators
1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral.
7-1 Chapter 7: Expressions and Assignment Statements Arithmetic Expressions –the fundamental means of specifying computations in a programming language.
Chapter 7 Expressions and Assignment Statements. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Arithmetic Expressions Arithmetic evaluation.
C H A P T E R S E V E N Expressions and Assignment Statements.
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.
Chapter 3: Data Types and Operators JavaScript - Introductory.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
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 Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
CS 11 java track: lecture 1 Administrivia need a CS cluster account cgi-bin/sysadmin/account_request.cgi need to know UNIX
CSCI 1100/1202 January 28, The switch Statement The switch statement provides another means to decide which statement to execute next The switch.
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
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.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Java Programming: From Problem Analysis to Program Design, 5e Chapter 2 Basic Elements of Java.
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Java development environment and Review of Java. Eclipse TM Intergrated Development Environment (IDE) Running Eclipse: Warning: Never check the “Use this.
Arithmetic Expressions
Controlling Program Flow. Data Types and Variable Declarations Controlling Program Flow.
Expressions and Assignment Statements
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.
Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 3, page 1 Sun Certified Java 1.4 Programmer Chapter 3 Notes Gary Lance
ISBN Chapter 7 Expressions and Assignment Statements.
3: Controlling Program Flow Using Java operators Mathematical operators Relational operators Logical operators –Primitive type: ALL (the same with C) –String:
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.
Principles of Programming Chapter 4: Basic C Operators  In this chapter, you will learn about:  Arithmetic operators  Unary operators  Binary operators.
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.
By Mr. Muhammad Pervez Akhtar
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Boolean expressions, part 1: Compare operators. Compare operators Compare operators compare 2 numerical values and return a Boolean (logical) value A.
0 Chap.2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations 2.5Arithmetic Operators 2.6Relational.
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.
Programming Principles Operators and Expressions.
 Array ◦ Single & Multi-dimensional  Java Operators ◦ Assignment ◦ Arithmetic ◦ Relational ◦ Logical ◦ Bitwise & other.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
CompSci 230 S Programming Techniques
Expressions and Assignment Statements
Chapter 7: Expressions and Assignment Statements
Chap. 2. Types, Operators, and Expressions
University of Central Florida COP 3330 Object Oriented Programming
University of Central Florida COP 3330 Object Oriented Programming
Chapter 7: Expressions and Assignment Statements
Java Programming: From Problem Analysis to Program Design, 4e
Operators and Expressions
Operators and Expressions
Introduction to Programming in Java
Expressions and Assignment Statements
OPERATORS (2) CSC 111.
All the Operators 22-Nov-18.
Chapter 2: Basic Elements of Java
All the Operators 4-Dec-18.
All the Operators 6-Apr-19.
All the Operators 13-Apr-19.
Presentation transcript:

Object-Oriented Programming and Problem Solving Dr. Ramzi Saifan Introduction and basics of Java Slides adapted from Steven Roehrig

Source Files:  All java source files must end with the.java extension. A source file must contain at most one top-level public class definition  If a public class is present, the class name should match the unextended filename.  There are three top-level elements that may appear in a file. None of these elements is required. If they are presented, then they must appear in the following order: Package declaration Import statements Class definitions

No Main Method in the Public Class public class MainClass { } class Demo1 { public static void main (String [] args) { System.out.println("Welcome to Java Programming"); }  Compilation command: javac MainClass.java  Run-time command: java MainClass  Output: java.lang.NoSuchMethodError: main (Exception in thread "main")

No Definition for a Public Class class MainClass { public static void main (String [] args) { System.out.println("Welcome to Java Programming"); } class Demo1 { }  Compilation command: javac MainClass.java  Run-time command: java MainClass  Output: Welcome to Java Programming

Input/Output  System.out.print  System.out.println  JOptionPane.showMessageDialog  System.in  JOptionPane.showInputDialog  java.util.Scanner sc = new Scanner(System.in) int x = Sc.nextInt() String str = Sc.nextLine Output Input

JDK  JDK has a huge number of packages. However, each package has a collection of classes and interfaces.  Latest Java 2 Sun Release: JDK 1.7  Interactive Java Environments: TextPad Eclipse BlueJ NetBeans

Getting the Java Documentation  Point your browser to  Bookmark the file so you can always get to it easily  Or, just bookmark the index page on Sun’s Java Web site

Java Operators  An operator takes one or more “things” and produces a resultant “thing”.  “Things” are usually primitive types, but they are sometimes objects.  The “things” operated upon are called operands.  An operator is just a function, but with a different syntax.

A Familiar Example  The assignment operator and the addition operator are used (each exactly once!). int i = 3, j = 4, k; k = i + j;

More Operator Facts  All operators produce a value.  Sometimes they produce side effects, i.e., they change the value of an operand.  Evaluation of a statement with several operators follows precedence rules. Use parentheses for readability.  (x + y) * z / 3 is different than x + y * z / 3

Java's operator precedence hierarchy:  (postfix), -- (postfix),. (dot), [], ( )  (prefix), -- (prefix), + (unary), - (unary), ~ (bitwise inversion operator), ! (boolean complement operator)  3. (type): casting  4. *, /, %  5. + (binary), - (binary)  6. >, >>> (shift operators)  7., = (ordinal operators), instance of  8. = =, != (equality comparison operators)  9. & (bitwise AND operator)  10. ^ (bitwise XOR operator)  11. | (bitwise OR operator)  12. && (AND operator)  13. || (OR operator)  14. ?: (ternary operator)  15. =, +=, -=, *=, /=, %=, >>=, >>=, &=, ^=,!= (assignments operators)

Assignment Is Tricky, Part I public class Number { public int i; } public class Assignment1 { public static void main(String[] args) { Number n1 = new Number(); Number n2 = new Number(); n1.i = 2; n2.i = 5; n1.i = n2.i; n2.i = 10;// what is n1.i? }

Assignment Is Tricky, Part II public class Assignment2 { public static void main(String[] args) { Number n1 = new Number(); Number n2 = new Number(); n1.i = 2; n2.i = 5; n1 = n2; n2.i = 10;// what is n1.i? n1.i = 20;// what is n2.i? }

A Picture Might Help n1n2 reference variablesNumber objects n2.in1.i Before assignment n1 = n2 n1n2 reference variablesNumber objects n2.in1.i After assignment n1 = n

“Aliasing” In Function Calls public class PassObject { static void f(Number m) { m.i = 15; } public static void main(String[] args) { Number n = new Number(); n.i = 14; f(n);// what is n.i now? }

Math Operators  +, -, *, /, %  Integer division truncates, i.e., 16/3 = 5  Modulus operator returns remainder on integer division, i.e., 16%3 = 1  Shorthand: x += 4; is the same as x = x + 4;  This works for the other arithmetic operators as well.

Auto Increment and Decrement  ++ increases by one, and -- decreases by one.  Two flavors of each: pre and post: int i = 1, j; j = i++;// j = 1, i = 2 j = ++i;// j = 3, i = 3 j = i--;// j = 3, i = 2 j = --i;// j = 1, i = 1

Booleans and Relational Operators  The boolean type has two possible values, true and false.  The relational operators >, >=, <, <=, == and != produce a boolean result.  >, >=, <, <= are legal for all built-in types except booleans, == and != are legal for all.

Testing for (Non-)Equivalence  The == and != operators need to be used with care with objects. public class Equivalence { public static void main(String[] args) { Integer n1 = new Integer(47); Integer n2 = new Integer(47); System.out.println(n1 == n2);// prints false System.put.println(n1 != n2);// prints true }

The equals( ) Operator  This exists for all objects (don’t need it for built-in types). Integer n1 = new Integer(47); Integer n2 = new Integer(47); System.out.println(n1.equals(n2);// prints true

The equals( ) Operator (cont.)  But exists doesn’t necessarily mean properly defined! class Number { int i; } : Number n1 = new Number(); Number n2 = new Number(); n1.i = 3; n2.i = 3; System.out.println(n1.equals(n2));// prints false

The equals( ) Operator (cont.)  The equals( ) operator is properly defined for most Java library classes.  The default behavior is to compare references, so…  When you define a class, if you’re planning to use equals( ), you need to define it (i.e., override the default behavior).

Logical Operators  These are AND (&&), OR (||), and NOT (!).  These work on booleans only; if you have old “C” habits, forget them!  Use parentheses freely to group logical expressions.  Logical expressions short-circuit; as soon as the result is known, evaluation stops.

Short-Circuiting Example public class ShortCircuit { static boolean test1(int val) {return val < 1;} static boolean test2(int val) {return val < 2;} static boolean test3(int val) {return val < 3;} public static void main(String[] args) { if (test1(0) && test2(2) && test3(2)) System.out.println(“Expression is true”); else System.out.println(“Expression is false”); }

Bitwise, Shift, Ternary Operators  Bitwise & shift operators manipulate individual bits in integral primitive types.  The ternary if-else operator looks like this: boolean-expression ? value0 : value1  The result is either value0 or value1, depending on the truth of the boolean.

The String + Operator  The + operator is “overloaded” for String objects; it means concatenation.  It reminds me of good old C++…  If an expression begins with a String, then all the following operands of + will be converted into Strings: int x = 0, y = 1, z = 2; String myString = “x, y, z ”; System.out.println(myString + x + y + z);

A Useful Figure byteshortintlong doublefloat char When multiple types are mixed in an expression, compiler will convert the operands into the higher type: double, float, long, int

Casting  A cast produces a temporary new value of a designated type.  Implicit and explicit casts: int i = 2; double d= i;// OK, since d can hold all of i float g = F; //! int j = g;// not OK, loses information int k = (int) g;// OK, compiler is reassured