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

Презентация:



Advertisements
Похожие презентации
Operators and Arithmetic Operations. Operators An operator is a symbol that instructs the code to perform some operations or actions on one or more operands.
Advertisements

Operator Overloading Customised behaviour of operators Chapter: 08 Lecture: 26 & 27 Date:
Unit II Constructor Cont… Destructor Default constructor.
Object-Oriented Programming Ramzi Saifan Program Control Slides adapted from Steven Roehrig.
1/27 Chapter 9: Template Functions And Template Classes.
Inner Classes. 2 Simple Uses of Inner Classes Inner classes are classes defined within other classes The class that includes the inner class is called.
Arrays Dr. Ramzi Saifan Slides adapted from Prof. Steven Roehrig.
1/30 Chapter 8: Dynamic Binding And Abstract classes.
A class is just a collection of variables--often of different types--combined with a set of related functions. The variables in the class are referred.
11 BASIC DRESS-UP FEATURES. LESSON II : DRESS UP FEATURES 12.
Yogesh Mehla Now concept of logic building is not so complex and not so simple. We will not work on how to make logic program in.
2005 Pearson Education, Inc. All rights reserved. 1 Object-Oriented Programming: Interface.
Sequences Sequences are patterns. Each pattern or number in a sequence is called a term. The number at the start is called the first term. The term-to-term.
© Luxoft Training 2013 Annotations. © Luxoft Training 2013 Java reflection / RTTI // given the name of a class, get a "Class" object that // has all info.
© Luxoft Training 2013 Using Reflection API in Java.
1 © Luxoft Training 2012 Inner and anonymous classes.
Data Types in C. A Data Type A data type is –A set of values AND –A set of operations on those values A data type is used to –Identify the type of a variable.
ADVANCED DRESS-UP FEATURES 39. Once OK has been selected, your part will appear with the filleted area highlighted by orange lines at the boundaries.
Here are multiplication tables written in a code. The tables are not in the correct order. Find the digit, represented by each letter.
Conditional Statements. Program control statements modify the order of statement execution. Statements in a C program normally executes from top to bottom,
Транксрипт:

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 Suns 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 (dont 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 doesnt 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 youre 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