INTERMEDIATE PROGRAMMING USING JAVA CS-0401 INTERMEDIATE PROGRAMMING USING JAVA Prof. Dr. Paulo Brasko Ferreira Fall 2016 © 2016 www.paulobrasko.com – All Rights Reserved
Precedence and Associativity © 2016 www.paulobrasko.com – All Rights Reserved
Precedence and Associativity 2 + 3 * 4 = ? a) 20 2 + 3 - 4 = ? b) 14 2 + 3 * 4 = ? 2 + 3 - 4 = ? © 2016 www.paulobrasko.com – All Rights Reserved
Precedence and Associativity Precedence indicates the order in which operators are applied in an expression Associativity indicates the order in which operands are accessed given operators of the same precedence © 2016 www.paulobrasko.com – All Rights Reserved
© 2016 www.paulobrasko.com – All Rights Reserved
© 2016 www.paulobrasko.com – All Rights Reserved
More Operators See example: ex3.java Java has a number of convenience operators Allow us to do operations with less typing Ex: X = X + 1; is equivalent to X++; Y = Y – 5 ; is equivalent to Y –= 5; Z *=2; Z /= 365; Note: prefix and postfix versions of the unary operators X++; ++X; See example: ex3.java When used as shown above, the operators have the same effect. However, when used as part of another expression there is a difference: The postfix operator is performed AFTER the previous value is used in the expression The prefix operator is performed BEFORE the value is used in the expression So for example: X = 5; Y = X++; // Now Y is 5 and X is 6 Y = ++X; // Now Y is 6 and X is 6 © 2016 www.paulobrasko.com – All Rights Reserved
Ways of entering data into your program © 2016 www.paulobrasko.com – All Rights Reserved
Store data via Identifiers and Variables Get data in Your program Control Structures Get data out Statements and Expressions © 2016 www.paulobrasko.com – All Rights Reserved
Let’s play Family Feud Game © 2016 www.paulobrasko.com – All Rights Reserved
What are the most common ways of entering data into a program? © 2016 www.paulobrasko.com – All Rights Reserved
Hard-coded input © 2016 www.paulobrasko.com – All Rights Reserved
Hard-coded inputs Good or bad? Math.PI import java.lang.*; public class AddNumbers { public static void main(String[] args) { double a = 3; double b = 4; double c; c = a + b; System.out.println(c); } } Good or bad? Math.PI © 2016 www.paulobrasko.com – All Rights Reserved
Input Data Using Dialog Boxes © 2016 www.paulobrasko.com – All Rights Reserved
Dialog Boxes A dialog box is a small graphical window that displays a message to the user or requests input. A variety of dialog boxes can be displayed using the JOptionPane class. The JOptionPane class provides methods to display each type of dialog box. © 2016 www.paulobrasko.com – All Rights Reserved
The JOptionPane Class © 2016 www.paulobrasko.com – All Rights Reserved
Input Dialogs String name; name = JOptionPane.showInputDialog("Enter your name."); The argument passed to the method is the message to display. If the user clicks on the OK button, name references the string entered by the user. If the user clicks on the Cancel button, name references null. © 2016 www.paulobrasko.com – All Rights Reserved
Converting a String to a Number The JOptionPane’s showInputDialog method always returns the user's input as a String Numbers are not Strings! Is there a problem in this case? Names are Strings, so we are OK © 2016 www.paulobrasko.com – All Rights Reserved
You can chat and interact with this person This is John! You can chat and interact with this person This is John’s picture! You cannot chat with it! Even though they look the same, they are two different things! You cannot interact with John’s picture as you would with the actual John! © 2016 www.paulobrasko.com – All Rights Reserved
Converting a String to a Number Similarly, a String representing a number is NOT the same as a number! You cannot do math operations on a “picture” of a number You MUST convert the String representation to an actual number! © 2016 www.paulobrasko.com – All Rights Reserved
The Parse Methods // Store 1 in bVar. byte bVar = Byte.parseByte("1"); // Store 2599 in iVar. int iVar = Integer.parseInt("2599"); // Store 10 in sVar. short sVar = Short.parseShort("10"); // Store 15908 in lVar. long lVar = Long.parseLong("15908"); // Store 12.3 in fVar. float fVar = Float.parseFloat("12.3"); // Store 7945.6 in dVar. double dVar = Double.parseDouble("7945.6"); © 2016 www.paulobrasko.com – All Rights Reserved
The main(String[] args) © 2016 www.paulobrasko.com – All Rights Reserved
The main(String[] args) public static void main(String[] args) { double a = Double.parseDouble(args[0]); double b = Double.parseDouble(args[1]); double c = a + b; System.out.println("Value of c: " + c); } What does “args” stand for? But what arguments??? © 2016 www.paulobrasko.com – All Rights Reserved
The main(String[] args) Arguments from the command line © 2016 www.paulobrasko.com – All Rights Reserved
How to specify Args in NetBeans © 2016 www.paulobrasko.com – All Rights Reserved
Getting data from a text file © 2016 www.paulobrasko.com – All Rights Reserved
Getting data from a text file Your program must process this data This data is saved in a text file There are 1000 lines to process It is inefficient to type them in the command line! © 2016 www.paulobrasko.com – All Rights Reserved
Getting data from a text file BufferedReader reader = new BufferedReader(new FileReader("/path/to/file.txt")); String line = null; while ((line = reader.readLine()) != null) { // ... } © 2016 www.paulobrasko.com – All Rights Reserved
Getting Data From Terminal Window © 2016 www.paulobrasko.com – All Rights Reserved
The Scanner Class To read input from the keyboard we can use the Scanner class. The Scanner class is defined in java.util, so we will use the following statement at the top of our programs: import java.util.Scanner; © 2016 www.paulobrasko.com – All Rights Reserved
The Scanner Class Scanner objects work with System.in To create a Scanner object: Scanner keyboard = new Scanner(System.in); See example: ex4.java. © 2016 www.paulobrasko.com – All Rights Reserved
Control Structures © 2016 www.paulobrasko.com – All Rights Reserved
Store data via Identifiers and Variables Get data in Your program Control Structures Get data out Statements and Expressions © 2016 www.paulobrasko.com – All Rights Reserved
Control Statements One of the most important types of statements in programming is the control statement Allows 2 very important types of execution Conditional execution Statements may or may not execute Iterative execution Statements may execute more than one time © 2016 www.paulobrasko.com – All Rights Reserved
Control Statements Linear Execution Conditional Execution Iterative Execution © 2016 www.paulobrasko.com – All Rights Reserved
Conditional Execution The IF statement The SWITCH statement © 2016 www.paulobrasko.com – All Rights Reserved
What Boolean expressions could provide “true” or “false” values? The if Statement if (condition is true) execute next statement What Boolean expressions could provide “true” or “false” values? © 2016 www.paulobrasko.com – All Rights Reserved
if Statements and Boolean Expressions double x = 10, y = 5, z = 10; if (x > y) System.out.println("X is greater than Y"); if(x == z) System.out.println("X is equal to Y"); if(x != y) { System.out.println("X is not equal to Y"); x = y; System.out.println("However, now it is."); } Example: ex5a.java © 2016 www.paulobrasko.com – All Rights Reserved
Flowcharts IF statements can be modeled as a flow chart. if (temp < 45) System.out.println(“wear coat”); Wear a coat. Yes temp < 45? No © 2016 www.paulobrasko.com – All Rights Reserved
Flowcharts A block if statement may be modeled as: Wear a coat. Yes temp < 45? Wear a hat. Wear gloves. if (coldOutside) { wearCoat(); wearHat(); wearGloves(); } Note the use of curly braces to block several statements together. © 2016 www.paulobrasko.com – All Rights Reserved
The IF-ELSE statement © 2016 www.paulobrasko.com – All Rights Reserved
if-else Statements The if-else statement adds the ability to conditionally execute code when the if condition is false. if (expression) statementOrBlockIfTrue; else statementOrBlockIfFalse; © 2016 www.paulobrasko.com – All Rights Reserved
if-else Statement Flowcharts Wear a coat. Yes Is it cold outside? Wear shorts. No © 2016 www.paulobrasko.com – All Rights Reserved
Nested IF statements © 2016 www.paulobrasko.com – All Rights Reserved
Nested if Statement Flowcharts Wear a jacket Yes Is it cold outside? Wear shorts Is it snowing? Wear a parka No © 2016 www.paulobrasko.com – All Rights Reserved
Nested if Statements if (coldOutside) { if (snowing) { wearParka(); } else { wearJacket(); wearShorts(); © 2016 www.paulobrasko.com – All Rights Reserved
if-else-if Statements © 2016 www.paulobrasko.com – All Rights Reserved
if-else-if–else Statements if (expression_1) { statement; etc. } else if (expression_2) { … else { If expression_1 is true these statements are executed, and the rest of the structure is ignored. Otherwise, if expression_2 is true these statements are executed, and the rest of the structure is ignored. These statements are executed if none of the expressions above are true. © 2016 www.paulobrasko.com – All Rights Reserved
if-else-if Flowchart © 2016 www.paulobrasko.com – All Rights Reserved
Relational and Logical Operators © 2016 www.paulobrasko.com – All Rights Reserved
Relational Operators Relational operators Used to compare (i.e. relate) two primitive values Result is true or false based on values and the comparison that is asserted Ex: 6 < 10 -- true because 6 IS less than 10 7 != 7 -- false because 7 IS NOT equal to 7 Java has 6 relational operators < <= > >= == != © 2016 www.paulobrasko.com – All Rights Reserved
Logical Operators logical operators: ! && || Operate on boolean values, generating a new boolean value as a result ! && || A B true false !A false true A&&B true false A||B true false © 2016 www.paulobrasko.com – All Rights Reserved
Boolean Expressions Let’s look at some examples Example: ex5b.java int i = 10, j = 15, k = 20; double x = 10.0, y = 3.333333, z = 100.0; i < j || j < k && x <= y (i / 3) == y (x / 3) == y !(x != i) Note: Precedence in boolean expressions: relational operators ! && || Examples: True, due to the higher precedence of && over || False, due to integer division False, due to precision issues with floating point numbers True – mixed expressions are cast to the more “precise” type, so i is cast into a double Example: ex5b.java © 2016 www.paulobrasko.com – All Rights Reserved
Other ways of getting a boolean value © 2016 www.paulobrasko.com – All Rights Reserved
Other ways of getting boolean values Methods can return true and false String.equals( ) (returns true or false) We can use this return inside the IF statement if (name.equals(“John”)) { printHisProfile(); } © 2016 www.paulobrasko.com – All Rights Reserved
The switch Statement © 2016 www.paulobrasko.com – All Rights Reserved
The switch Statement switch(Expression) { case CaseExpression1: // place one or more statements here break; case CaseExpression2: // case statements may be repeated //as many times as necessary default: } © 2016 www.paulobrasko.com – All Rights Reserved
The case Statement The break statement ends the case statement. The break statement is optional. If a case does not contain a break, then program execution continues into the next case. The default section is optional and will be executed if no CaseExpression matches the SwitchExpression See Ex6.java and Ex6b.java © 2016 www.paulobrasko.com – All Rights Reserved
Any Questions? © 2016 www.paulobrasko.com – All Rights Reserved