Object-Oriented Programming Ramzi Saifan Program Control Slides adapted from Steven Roehrig.

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



Advertisements
Похожие презентации
Arrays Dr. Ramzi Saifan Slides adapted from Prof. Steven Roehrig.
Advertisements

Java Java java ISS, Wuhan University Nov., Java Java java Java Java Java ……
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.
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.
1 © Luxoft Training 2012 Inner and anonymous classes.
2005 Pearson Education, Inc. All rights reserved. 1 Object-Oriented Programming: Interface.
Initialization & Cleanup Dr. Ramzi Saifan Slides adapted from Prof. Steven Roehrig.
Unit II Constructor Cont… Destructor Default constructor.
© Luxoft Training 2013 Using Reflection API in Java.
Loops Objectives Students will: 1. Explain what a loop is and describe the three main types of loops used in programming. 1. Дать понятие циклам. И объяснить.
Operator Overloading Customised behaviour of operators Chapter: 08 Lecture: 26 & 27 Date:
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.
© Luxoft Training 2013 Java Collections API. © Luxoft Training 2013 Collections hierarchy.
Object-Oriented Programming Dr. Ramzi Saifan Slides adapted from Prof. Steven Roehrig.
© Luxoft Training 2013 Annotations. © Luxoft Training 2013 Java reflection / RTTI // given the name of a class, get a "Class" object that // has all info.
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.
Java Collections Framework (JCF) in Java Tutorial for students of universities Author: Oxana Dudnik.
Combination. In mathematics a combination is a way of selecting several things out of a larger group, where (unlike permutations) order does not matter.
1/27 Chapter 9: Template Functions And Template Classes.
Транксрипт:

Object-Oriented Programming Ramzi Saifan Program Control Slides adapted from Steven Roehrig

Today We Look At Control structures More example programs

Execution Control: if-else if (boolean_expression) statement else if(boolean_expression) statement : else if(boolean_expression) statement else statement

if-else Example public int test(int testVal, int target) { int result = 0; if (testVal > target) result = 1; else if (testVal < target) result = -1; else { System.out.println(They are equal); result = 0; } return result; }

Execution Control: return Exit a method, returning an actual value or object, or not (if the return type is void). public int test(int testVal, int target) { if (testVal > target) return 1; else if (testVal < target) return -1; else { System.out.println(They are equal); return 0; }

Three Kinds of Iteration while (boolean_expression)// evaluate first statement_or_block do statement_or_block// evaluate last while (boolean_expression) for (initialization ; boolean_expression ; step) statement_or_block Example: for (int i = 0; i < myArray.size(); i++) { myArray[i] = 0; }

public class FlowControl { public static void main (String [] rr) { int i; for(i=0;i<3;i++) System.out.println(" For loop "+i); i=0; do { System.out.println(" Do while "+i); i++; }while (i<3); i=0; while (i<3) { System.out.println(" While "+i); i++; } Output: For loop 0 For loop 1 For loop 2 Do while 0 Do while 1 Do while 2 While 0 While 1 While 2

public class BreakAndContinue { public static void main(String[] args) { for (int i = 0; i < 100; i++) { if (i == 74) break;// out of for loop if (i % 9 != 0) continue;// next iteration System.out.println(i); } Break and Continue

Selection Via switch for (int i = 0; i < 100; i++) { char c = (char) (Math.random() * 26 + a); switch(c) { case a: case e: case i: case o: case u: System.out.println(c + Vowel); break; case y: case w: System.out.println(c + Sometimes a vowel); break; default: System.out.println(c + Not a vowel); }

Using Javas RNGs (cont.) java.lang.Math static double random()random in [0, 1.0) The sequence doesnt seem to be repeatable Bad for debugging Good for experimental work

Using Javas RNGs (cont.) java.util.Random Constructors: Random() Random(long seed) Methods: nextInt()random in (-2 31, ) nextInt(int n)random in [0, n) nextFloat()random in [0, 1) setSeed(long seed) java.lang.Math static double random()random in [0, 1.0)

Java.lang.Math Public static double : exp, log, log10, pow, random, sin, cos, tan, and many others Public static int: abs, ceil, floor, round

String Concatenation: + String greeting = Hello; String s = greeting.substring(0,4); //4 is the length String s1= greeting.substring(0,3)+!; Int n = greeting.length(); Char last = greeting.charAt(4); s.equals(HELLO); //never use == to compare Strings The String class contains more than 50 methods, define any variable as String, type its name plus dot