INTERMEDIATE PROGRAMMING USING JAVA

Slides:



Advertisements
Similar presentations
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java: Early Objects Third Edition by Tony Gaddis Chapter.
Advertisements

CS-1010 Dr. Mark L. Hornick 1 Selection Statements and conditional expressions.
Conditions What if?. Flow of Control The order of statement execution is called the flow of control Unless specified otherwise, the order of statement.
The if Statement The code in methods executes sequentially.
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
Chapter 3: Decision Structures Starting Out with Java: From Control Structures through Objects Fifth Edition by Tony Gaddis.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
The switch Statement, DecimalFormat, and Introduction to Looping
Day 4 Objectives Constructors Wrapper Classes Operators Java Control Statements Practice the language.
JOptionPane class. 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.
CS-0401 INTERMEDIATE PROGRAMMING USING JAVA Prof. Dr. Paulo Brasko Ferreira Fall 2014.
© 2012 Pearson Education, Inc. All rights reserved. Chapter 3: Decision Structures Starting Out with Java: From Control Structures through Data Structures.
Lecture 3 Decision Structures. 4-2 Topics – The if Statement – The if - else Statement – The PayRoll class – Nested if Statements – The if - else - if.
Simple Programs from Chapter 2 Putting the Building Blocks All Together (corresponds with Chapter 2)
Chapter 3 Decision Structures Starting Out with Java: From Control Structures through Objects.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
Chapter 3 Selections Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
CS-0401 INTERMEDIATE PROGRAMMING USING JAVA Prof. Dr. Paulo Brasko Ferreira Fall 2013.
Control Stuctures Module 4 Copyright © Copyright © Slide #2 Decision Structures Chapter Objectives To understand: how to write boolean expressions.
OOP (pre) Basic Programming. Writing to Screen print will display the string to the screen, the following print statement will be appended to the previous.
Chapter 3: Decision Structures
CHAPTER 3 Decision Structures Copyright © 2016 Pearson Education, Inc., Hoboken NJ.
Java Fundamentals Part Integer Division Division can be tricky. In a Java program, what is the value of 1/2? You might think the answer is.
©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC INTRO TO COMPUTING - PROGRAMMING If Statement.
Starting Out With Java 5 (Control Structures to Objects) Chapter 3 By Tony Gaddis Copyright © 2005 Pearson Addison-Wesley. All rights reserved.
©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC 110 – INTRO TO COMPUTING - PROGRAMMING Switch Statement.
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
CHAPTER 3 Decision Structures Copyright © 2016 Pearson Education, Ltd.
Lecture 4 CS140 Dick Steflik. Reading Keyboard Input Import java.util.Scanner – A simple text scanner which can parse primitive types and strings using.
CompSci 230 S Programming Techniques
CS0007: Introduction to Computer Programming
Chapter 3: Decision Structures by Tony Gaddis and Godfrey Muganda
Objectives You should be able to describe: Interactive Keyboard Input
OBJECT ORIENTED PROGRAMMING I LECTURE 8 GEORGE KOUTSOGIANNAKIS
Chapter 3: Decision Structures
The switch Statement, and Introduction to Looping
2.5 Another Java Application: Adding Integers
Lecture 3- Decision Structures
Chapter 3 Control Statements Lecturer: Mrs Rohani Hassan
Chapter 3 Selections Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
User input We’ve seen how to use the standard output buffer
Lecture 3 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz
SELECTION STATEMENTS (1)
Message, Input, Confirm, and Specialized Dialogs
Chapter 3: Decision Structures
CMSC 202 Static Methods.
CS-0401 INTERMEDIATE PROGRAMMING USING JAVA
Control Statement Examples
CS-0401 INTERMEDIATE PROGRAMMING USING JAVA
CET 3640 – Lecture 2 Java Syntax Chapters 2, 4, 5
Chapter 3: Decision Structures
Starting Out With Java 5 (Control Structures to Objects) Chapter 3
Classes and Objects 5th Lecture
Chapter 3:Decision Structures
Message, Input, and Confirm Dialogs
Module 3 Selection Structures 2/19/2019 CSE 1321 Module 3.
The System.exit() Method
Lecture Notes – Week 2 Lecture-2
Chapter 3 Selections Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.
Classes and Objects Static Methods
Dr. Sampath Jayarathna Cal Poly Pomona
CSC 1051 – Data Structures and Algorithms I
JOptionPane class.
Module 3 Selection Structures 6/25/2019 CSE 1321 Module 3.
Chapter 2: Java Fundamentals cont’d
Presentation transcript:

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