CS139 October 11, 2004.

Slides:



Advertisements
Similar presentations
Flow of Control Usually the order of statement execution through a method is linear: one after another flow of control: the order statements are executed.
Advertisements

1 Classes, Encapsulation, Methods and Constructors (Continued) Class definitions Instance data Encapsulation and Java modifiers Method declaration and.
CS-1010 Dr. Mark L. Hornick 1 Selection Statements and conditional expressions.
1 Control Structures (and user input). 2 Flow of Control The order statements are executed is called flow of control By default, statements in a method.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
Flow of Control (1) : Logic Clark Savage Turner, J.D., Ph.D. Some lecture slides have been adapted from those developed.
Conditions What if?. Flow of Control The order of statement execution is called the flow of control Unless specified otherwise, the order of statement.
COMP 14 Introduction to Programming Miguel A. Otaduy May 18, 2004.
Aalborg Media Lab 23-Jun-15 Software Design Lecture 6 “Conditionals and Loops”
ECE122 L7: Conditional Statements February 20, 2007 ECE 122 Engineering Problem Solving with Java Lecture 7 Conditional Statements.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Logical Operators and Conditional statements
Boolean Expressions and If Flow of Control / Conditional Statements The if Statement Logical Operators The else Clause Block statements Nested if statements.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 30, 2005.
COMP 110 Introduction to Programming Mr. Joshua Stough September 19, 2007.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved2/33 Conditionals and Loops Now we will examine programming statements.
Chapter 3: Program Statements
Programming in Java (COP 2250) Lecture 11 Chengyong Yang Fall, 2005.
Chapter 3: Program Statements
Chapter 5: Conditionals and loops. 2 Conditionals and Loops Now we will examine programming statements that allow us to: make decisions repeat processing.
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Copyright © 2012 Pearson Education, Inc. Lab 8: IF statement …. Conditionals and Loops.
1 Chapter 3 Selections. 2 Outline 1. Flow of Control 2. Conditional Statements 3. The if Statement 4. The if-else Statement 5. The Conditional operator.
Introduction to Java Java Translation Program Structure
1 Interactive Applications (CLI) and Math Interactive Applications Command Line Interfaces The Math Class Flow of Control / Conditional Statements The.
5-1 Chapter 5: Conditionals and Loops Topics: –Boolean expressions –Conditional statements –Increment and Decrement Operators (Chapter 2.4) –Repetition.
Chapter 5 Conditionals and Loops 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights.
Control Flow. Data Conversion Promotion happens automatically when operators in expressions convert their operands For example, if sum is a float and.
Flow of Control Unless indicated otherwise, the order of statement execution through a method is linear: one after the other in the order they are written.
ICT Introduction to Programming Chapter 4 – Control Structures I.
Control statements Mostafa Abdallah
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
Design A software design specifies how a program will accomplish its requirements A design includes one or more algorithms to accomplish its goal.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
if ( condition ) statement; if is a Java reserved word The condition must be a boolean expression. It must evaluate to either true or false.
Chapter VII: Arrays.
COMP 14 Introduction to Programming
Operator Precedence Operators Precedence Parentheses () unary
Boolean Expressions and If
Boolean Expressions & the ‘if’ Statement
The order in which statements are executed is called the flow of control. Most of the time, a running program starts at the first programming statement,
Lecture 14 Writing Classes part 2 Richard Gesick.
Inheritance November 10, 2006 ComS 207: Programming I (in Java)
Conditionals & Boolean Expressions
Inheritance April 7, 2006 ComS 207: Programming I (in Java)
Conditionals & Boolean Expressions
Chapter 3: Program Statements
Classes, Encapsulation, Methods and Constructors (Continued)
Outline Boolean Expressions The if Statement Comparing Data
Chapter 4 Writing Classes.
Chapter 3: Program Statements
Logic of an if statement
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
CSC 1051 – Data Structures and Algorithms I
Comparing Data & the ‘switch’ Statement
Outline Software Development Activities
Comparing Data & the ‘switch’ Statement
CprE 185: Intro to Problem Solving (using C)
Conditionals and Loops
Boolean Expressions & the ‘if’ Statement
CprE 185: Intro to Problem Solving (using C)
3.0 - Design A software design specifies how a program will accomplish its requirements A design includes one or more algorithms to accomplish its goal.
Presentation transcript:

CS139 October 11, 2004

Review of TestNamesV1 Within the class, call method using its name. If a procedure (void return type), simply call the procedure as a statement. Pass in appropriate values in the argument list. Ex: displayName( name1 ) ; If a function (returns a value), call or invoke the function by using an assignment statement or by invoking the function where you want to use the value. System.out.println( name1.firstMiddleLast() ) ; © 2004 Pearson Addison-Wesley. All rights reserved

Method Overloading (Chapter 6 ) Method overloading is the process of giving a single method name multiple definitions If a method is overloaded, the method name is not sufficient to determine which method is being called The signature of each overloaded method must be unique The signature includes the number, type, and order of the parameters © 2004 Pearson Addison-Wesley. All rights reserved

Method Overloading The compiler determines which method is being invoked by analyzing the parameters float tryMe(int x) { int out; out = x + .375; return out; } result = tryMe(25, 4.32) Invocation float tryMe(int x, double y) { int out; out = x * y; return out; } © 2004 Pearson Addison-Wesley. All rights reserved

Method Overloading The println method is overloaded (see page 860): println (String s) println (int i) println (double d) and so on... The following lines invoke different versions of the println method: System.out.println ("The total is:"); System.out.println (total); © 2004 Pearson Addison-Wesley. All rights reserved

Overloading Methods The return type of the method is not part of the signature That is, overloaded methods cannot differ only by their return type Constructors can be overloaded Overloaded constructors provide multiple ways to initialize a new object (See next slide) © 2004 Pearson Addison-Wesley. All rights reserved

Constructor Overloading – An Example public class Name { private String fName , mName, lName ; // Name parts public Name( String first, String middle, String last) { fName = first ; mName = middle ; lName = last ; } public Name( String first, String last) { fName = first ; mName = “” ; lName = last ; } . . . . . } © 2004 Pearson Addison-Wesley. All rights reserved

Constructors A class with no constructor methods uses a “default” constructor. Default constructor builds the data areas, but does not explicitly initialize instance variables. © 2004 Pearson Addison-Wesley. All rights reserved

Conditionals and Loops (Ch. 5) Now we will examine programming statements that allow us to: make decisions repeat processing steps in a loop © 2004 Pearson Addison-Wesley. All rights reserved

Flow of Control Unless specified otherwise, the order of statement execution through a method is linear: one statement after another in sequence Some programming statements allow us to: decide whether or not to execute a particular statement execute a statement over and over, repetitively These decisions are based on boolean expressions (or conditions) that evaluate to true or false The order of statement execution is called the flow of control © 2004 Pearson Addison-Wesley. All rights reserved

Conditional Statements - 1 A conditional statement lets us choose which statement will be executed next Therefore they are sometimes called selection statements Conditional statements give us the power to make basic decisions The Java conditional statements are the: if statement if-else statement switch statement © 2004 Pearson Addison-Wesley. All rights reserved

Conditional Statements - 2 Let us simulate decisions in the “real world”. If it is raining, then I will take my umbrella to work, otherwise, I will leave it at home. The “take my umbrella” action is conditioned on the result of the decision, if it is raining, which asks the question “is it raining?”. When building selections, always think about both pieces – what do I do if this decision is true, what do I do if this decision is false. © 2004 Pearson Addison-Wesley. All rights reserved

The if Statement The if statement has the following syntax: The condition must be a boolean expression. It must evaluate to either true or false. if is a Java reserved word if ( condition ) statement; If the condition is true, the statement is executed. If it is false, the statement is skipped. © 2004 Pearson Addison-Wesley. All rights reserved

Logic of an if statement condition evaluated false statement true © 2004 Pearson Addison-Wesley. All rights reserved

Boolean expression An arithmetic expression’s evaluation results in some numeric result. A Boolean expression’s evaluation results in either a true or a false result. See Schaum’s for lots of Boolean expression examples. © 2004 Pearson Addison-Wesley. All rights reserved

Boolean Expressions A condition often uses one of Java's equality operators or relational operators, which all return boolean results: == equal to != not equal to < less than > greater than <= less than or equal to >= greater than or equal to Note the difference between the equality operator (==) and the assignment operator (=) © 2004 Pearson Addison-Wesley. All rights reserved

The if Statement - 1 An example of an if statement: delta = 0 ; if (sum > MAX) delta = sum - MAX; System.out.println ("The sum is " + sum); First the condition is evaluated -- the value of sum is either greater than the value of MAX, or it is not If the condition is true, the assignment statement is executed -- if it isn’t, it is skipped. Either way, the call to println is executed next See Age.java (page 208) © 2004 Pearson Addison-Wesley. All rights reserved

The if Statement - 2 An if statement may conditionally execute a block of statements, not just one statement. if (sum > MAX) { delta = sum - MAX; sum = MAX ; } System.out.println ("The sum is " + sum); © 2004 Pearson Addison-Wesley. All rights reserved

Indentation The statement controlled by the if statement is indented to indicate that relationship The use of a consistent indentation style makes a program easier to read and understand Although it makes no difference to the compiler, proper indentation is crucial "Always code as if the person who ends up maintaining your code will be a violent psychopath who knows where you live." -- Martin Golding © 2004 Pearson Addison-Wesley. All rights reserved

The if-else Statement An else clause can be added to an if statement to make an if-else statement if ( condition ) statement1; else statement2; If the condition is true, statement1 is executed; if the condition is false, statement2 is executed One or the other will be executed, but not both See Wages.java (page 211) © 2004 Pearson Addison-Wesley. All rights reserved

Lab Tomorrow We will practice some if/else statements using prior labs. Make sure you have the Circle class and the Names class available for lab. There will also be a practice exercise available to let you practice evaluating boolean expressions. This should be done Tuesday night and is due in class on Wednesday. © 2004 Pearson Addison-Wesley. All rights reserved