Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS139 October 11, 2004.

Similar presentations


Presentation on theme: "CS139 October 11, 2004."— Presentation transcript:

1 CS139 October 11, 2004

2 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

3 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

4 Method Overloading The compiler determines which method is being invoked by analyzing the parameters float tryMe(int x) { int out; out = x ; 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

5 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

6 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

7 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

8 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

9 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

10 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

11 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

12 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

13 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

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

15 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

16 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

17 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

18 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

19 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

20 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

21 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


Download ppt "CS139 October 11, 2004."

Similar presentations


Ads by Google