Download presentation
Presentation is loading. Please wait.
1
INTERMEDIATE PROGRAMMING USING JAVA
CS-0401 INTERMEDIATE PROGRAMMING USING JAVA Prof. Dr. Paulo Brasko Ferreira Fall 2016 © – All Rights Reserved
2
Precedence and Associativity
© – All Rights Reserved
3
Precedence and Associativity
2 + 3 * 4 = ? a) 20 = ? b) 14 2 + 3 * 4 = ? = ? © – All Rights Reserved
4
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 © – All Rights Reserved
5
© 2016 www.paulobrasko.com – All Rights Reserved
6
© 2016 www.paulobrasko.com – All Rights Reserved
7
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 © – All Rights Reserved
8
Ways of entering data into your program
© – All Rights Reserved
9
Store data via Identifiers and Variables
Get data in Your program Control Structures Get data out Statements and Expressions © – All Rights Reserved
10
Let’s play Family Feud Game
© – All Rights Reserved
11
What are the most common ways of entering data into a program?
© – All Rights Reserved
12
Hard-coded input © – All Rights Reserved
13
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 © – All Rights Reserved
14
Input Data Using Dialog Boxes
© – All Rights Reserved
15
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. © – All Rights Reserved
16
The JOptionPane Class © – All Rights Reserved
17
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. © – All Rights Reserved
18
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 © – All Rights Reserved
19
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! © – All Rights Reserved
20
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! © – All Rights Reserved
21
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 in lVar. long lVar = Long.parseLong("15908"); // Store 12.3 in fVar. float fVar = Float.parseFloat("12.3"); // Store in dVar. double dVar = Double.parseDouble("7945.6"); © – All Rights Reserved
22
The main(String[] args)
© – All Rights Reserved
23
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??? © – All Rights Reserved
24
The main(String[] args)
Arguments from the command line © – All Rights Reserved
25
How to specify Args in NetBeans
© – All Rights Reserved
26
Getting data from a text file
© – All Rights Reserved
27
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! © – All Rights Reserved
28
Getting data from a text file
BufferedReader reader = new BufferedReader(new FileReader("/path/to/file.txt")); String line = null; while ((line = reader.readLine()) != null) { // ... } © – All Rights Reserved
29
Getting Data From Terminal Window
© – All Rights Reserved
30
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; © – All Rights Reserved
31
The Scanner Class Scanner objects work with System.in
To create a Scanner object: Scanner keyboard = new Scanner(System.in); See example: ex4.java. © – All Rights Reserved
32
Control Structures © – All Rights Reserved
33
Store data via Identifiers and Variables
Get data in Your program Control Structures Get data out Statements and Expressions © – All Rights Reserved
34
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 © – All Rights Reserved
35
Control Statements Linear Execution Conditional Execution
Iterative Execution © – All Rights Reserved
36
Conditional Execution
The IF statement The SWITCH statement © – All Rights Reserved
37
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? © – All Rights Reserved
38
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 © – All Rights Reserved
39
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 © – All Rights Reserved
40
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. © – All Rights Reserved
41
The IF-ELSE statement © – All Rights Reserved
42
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; © – All Rights Reserved
43
if-else Statement Flowcharts
Wear a coat. Yes Is it cold outside? Wear shorts. No © – All Rights Reserved
44
Nested IF statements © – All Rights Reserved
45
Nested if Statement Flowcharts
Wear a jacket Yes Is it cold outside? Wear shorts Is it snowing? Wear a parka No © – All Rights Reserved
46
Nested if Statements if (coldOutside) { if (snowing) { wearParka(); }
else { wearJacket(); wearShorts(); © – All Rights Reserved
47
if-else-if Statements
© – All Rights Reserved
48
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. © – All Rights Reserved
49
if-else-if Flowchart © – All Rights Reserved
50
Relational and Logical Operators
© – All Rights Reserved
51
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 < true because 6 IS less than 10 7 != false because 7 IS NOT equal to 7 Java has 6 relational operators < <= > >= == != © – All Rights Reserved
52
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 © – All Rights Reserved
53
Boolean Expressions Let’s look at some examples Example: ex5b.java
int i = 10, j = 15, k = 20; double x = 10.0, y = , 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 © – All Rights Reserved
54
Other ways of getting a boolean value
© – All Rights Reserved
55
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(); } © – All Rights Reserved
56
The switch Statement © – All Rights Reserved
57
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: } © – All Rights Reserved
58
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 © – All Rights Reserved
59
Any Questions? © – All Rights Reserved
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.