CIS 265: Data Structure and Algorithms Lecture 01 Java Programming Review 2 Outline • • • • • Introduction to computers, programs, and Java Elementary programming Selection Loops Methods 3 Outline • • • • • Introduction to computers, programs, and Java Elementary programming Selection Loops Methods 4 Introduction to programs • Writing a program involves designing algorithms and translating algorithms into code. • An algorithm describes how a problem is solved in terms of the actions to be executed and the order of their execution. 5 Executing a Java Program • You can use any text editor or IDE to create and edit a Java sourcecode file. • The source file must end with the extension .java and must have exactly the same name as the public class name. • You must first install and configure JDK before compiling and running programs Creating java source file The execution process 6 Development Process The Java program development process 7 Outline • • • • • Introduction to computers, programs, and Java Elementary programming Selection Loops Methods 8 Writing Simple Programs Example: Write a program to calculate the area of a circle. 9 Input/output • Use scanner class to create input object • Use methods of a scanner objects: nextDouble() 10 Variables • They represent data of a certain type. • They are basic structures. • Examples: Numeric data types 11 Operations & Expressions • Operations: • Java expressions 12 Example • Write a program that obtains minutes and remaining seconds from an amount of time in seconds. 13 Outline • • • • • Introduction to computers, programs, and Java Elementary programming Selection Loops Methods 14 Boolean Data Type • Example: • Java provides six comparison operators: 15 if statements • One way if statement • The boolean expression is enclosed in parenthesis. 16 Block braces • The block braces can be omitted if they enclose a single statement. • Example: 17 Two Way if Statements • Example: 18 Nested if Statements • Example: • Problem: Compute Body Mass Index 19 Body mass index program 20 Logical Operators • Sometimes, whether a statement is executed is determined by a combination of several conditions. You can use logical operators to combine these conditions. • How the truth table will look like? 21 Switch Statements • The if statement usually makes selection based on a single true or false condition. Switch statement can work based on different conditions. 22 Caution • Do not forget to use a break statement when one is needed. Once a case is matched, the statements starting from the matched case are executed until a break statement or the end of the switch statement is reached. 23 Formatting Console Output 24 Conditional Expressions • Example: • Alternative way: • Example: • Find out a number is even or odd: 25 Outline • • • • • Introduction to computers, programs, and Java Elementary programming Selection Loops Methods 26 Loops • Suppose you need to print a string a hundred times. • Using a loop statement, you simply tell the computer to print a string a hundred times without having to code the print statement a hundred times, as follows: 27 The while loop • Example: 28 Examples • Write a program to receive 100 numbers and calculate their average. • Write a program to receive 100 numbers and count even and odd numbers. 29 The do-while Loop • The loop body is executed first. Then the loopcontinuation-condition is evaluated. If the evaluation is true, the loop body is executed again; if it is false, the do-while loop terminates. 30 Example 31 The For loop • It is a quite common loop: • A for loop can be used to simplify the proceeding loop: 32 Which loop to use? • The while loop and for loop are called pretest loops because the continuation condition is checked before the loop body is executed. • The do-while loop is called a posttest loop because the condition is checked after the loop body is executed. • The three forms of loop statements, while, dowhile, and for, are expressively equivalent; that is, you can write a loop in any of these three forms. 33 Outline • • • • • Introduction to computers, programs, and Java Elementary programming Selection Loops Methods 34 Why methods? • Code looks similar except that starting and ending integers are different. Wouldn’t it be nice if we could write the common code once and reuse it without rewriting it? 35 Defining a method • The syntax is as follows: 36 Calling a method • In creating a method, you define what the method is to do. To use a method, you have to call or invoke it. • If the method returns a value, a call to the method is usually treated as a value. For example: 37 Call Stacks • Each time a method is invoked, the system stores parameters and variables in an area of memory known as a stack, which stores elements in last-in, first-out fashion. 38 Passing parameters by values • When calling a method, you need to provide arguments, which must be given in the same order as their respective parameters in the method signature. • When you invoke a method with a parameter, the value of the argument is passed to the parameter. This is referred to as pass-by-value. 39 Swap two values 40 Look at stack 41 The scope of variables • The scope of a variable is the part of the program where the variable can be referenced. A variable defined inside a method is referred to as a local variable. • The scope of a local variable starts from its declaration and continues to the end of the block that contains the variable.