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

Slides:



Advertisements
Similar presentations
Control Structures.
Advertisements

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.
Return values.
8-May-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 Control Statements Presented by: Parminder Singh BCA 5 th Sem. [ Batch] PCTE, Ludhiana 5/12/ Control Statements.
5/17/ Programming Constructs... There are several types of programming constructs in JAVA. - If-else construct or ternary operator - while - do-while.
Topic 03 Control Statements Programming II/A CMC2522 / CIM2561 Bavy Li.
1 Control Statements Lecture 6 from Chapter 5. 2 Three principles of OOP- Encapsulation Encapsulation allows changes to code to be more easily made. It.
Object-Oriented Programming MISM/MSIT Carnegie Mellon University Lecture 2: Program Control.
(c) 2003 E.S.Boese1 Conditionals Chapter 10 - Student.
16-Jun-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
Intermediate Java Sakir YUCEL MISM/MSIT Carnegie Mellon University Program Control Slides adapted from Steven Roehrig.
CSCI1402: Lecture 2 Week 6 Dr. David A. Elizondo Centre for Computational Intelligence School of Computing Office: Gateway 6.61
Chapter 3 Control Statements F Selection Statements –Using if and if...else –Nested if Statements –Using switch Statements –Conditional Operator F Repetition.
TODAY’S LECTURE Review Chapter 2 Go over exercises.
Day 4 Objectives Constructors Wrapper Classes Operators Java Control Statements Practice the language.
General Features of Java Programming Language Variables and Data Types Operators Expressions Control Flow Statements.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Control Structures A control structure is simply a pattern for controlling the flow of a program module. The three fundamental control structures of a.
Control Structures if else do while continue break switch case return for.
Object Oriented Programming I
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 Conditionals Instructor: Mainak Chaudhuri
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
An Introduction to Java – Part 1 Dylan Boltz. What is Java?  An object-oriented programming language  Developed and released by Sun in 1995  Designed.
Warm-Up: Monday, March 24 List as many commands in Java as you can remember (at least 10)
 The if statement and the switch statement are types of conditional/decision controls that allow your program.  Java also provides three different looping.
4.1 Object Operations operators Dot (. ) and new operate on objects The assignment operator ( = ) Arithmetic operators + - * / % Unary operators.
Controlling Program Flow. Data Types and Variable Declarations Controlling Program Flow.
Repetition Statements while and do while loops
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Sahar Mosleh California State University San MarcosPage 1 Program Control Statement.
Lesson 6 Selection Structures. Example program //This will convert a numeric grade into a letter grade import TerminalIO.KeyboardReader; public class.
Basics and arrays Operators:  Arithmetic: +, -, *, /, %  Relational:, >=, ==, !=  Logical: &&, ||, ! Primitive data types Byte(8), short(16), int(32),
J AVA P ROGRAMMING 2 C H 03: C ONTROL STATEMENTS if, for loop (review) switch, while, do while break, continue Fall Java Programming.
More loops while and do-while. Recall the for loop in general for (initialization; boolean_expression; update) { }
S ystem P rogrammers' A ssociation for R esearching C omputer S ystems 4. Controlling Execution SPARCS JAVA Study.
FOR LOOP WALK THROUGH public class NestedFor { public static void main(String [] args) { for (int i = 1; i
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
CONTROL STRUCTURE. 2 CHAPTER OBJECTIVES  Learn about control structures.  Examine relational and logical operators.  Explore how to form and evaluate.
Application development with Java Lecture 6 Rina Zviel-Girshin.
Creating and Using Class Methods. Definition Class Object.
A Introduction to Computing II Lecture 1: Java Review Fall Session 2000.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
CS 139 – Programming Fundamentals Lecture 08A. Relational/comparison Operators Relational Operator Meaning > is greater than < is less than >= is greater.
Loops and Logic. Making Decisions Conditional operator Switch Statement Variable scope Loops Assertions.
Methods What is a method? Main Method the main method is where a stand alone Java program normally begins execution common compile error, trying.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
Structured Programming Structured Programming is writing a program in terms of only 3 basic control structures: sequence selection repetition We have already.
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
Object-Oriented Programming and Problem Solving Dr. Ramzi Saifan Introduction and basics of Java Slides adapted from Steven Roehrig.
Control Structures- Decisions. Smart Computers Computer programs can be written to make computers seem smart Making computers smart is based on decision.
Lecture 4b Repeating With Loops
Control Structures.
Introduction to programming in java
Control Statements Lecture 6 from Chapter 5.
CiS 260: App Dev I Chapter 4: Control Structures II.
Chapter 5: Control Structures II
An Introduction to Java – Part I
CS 177 Week 3 Recitation Slides
TO COMPLETE THE FOLLOWING:
An Introduction to Java – Part I, language basics
Java so far Week 7.
class PrintOnetoTen { public static void main(String args[]) {
Control Structure Chapter 3.
PROGRAM FLOWCHART Selection Statements.
Loops CGS3416 Spring 2019 Lecture 7.
Control Structure.
More on iterations using
Control Statements:.
Presentation transcript:

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 Java’s RNGs (cont.)  java.lang.Math static double random()random in [0, 1.0)  The sequence doesn’t seem to be repeatable  Bad for debugging  Good for experimental work

Using Java’s 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