1 Interactive Applications (CLI) and Math Interactive Applications Command Line Interfaces The Math Class Flow of Control / Conditional Statements The.

Slides:



Advertisements
Similar presentations
Flow of Control Usually the order of statement execution through a method is linear: one after another flow of control: the order statements are executed.
Advertisements

1 Control Structures (and user input). 2 Flow of Control The order statements are executed is called flow of control By default, statements in a method.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
Flow of Control (1) : Logic Clark Savage Turner, J.D., Ph.D. Some lecture slides have been adapted from those developed.
Conditions What if?. Flow of Control The order of statement execution is called the flow of control Unless specified otherwise, the order of statement.
Chapter 3 Using Classes and Objects. Creating Objects A variable holds either a primitive type or a reference to an object A class name can be used as.
Aalborg Media Lab 23-Jun-15 Software Design Lecture 6 “Conditionals and Loops”
ECE122 L7: Conditional Statements February 20, 2007 ECE 122 Engineering Problem Solving with Java Lecture 7 Conditional Statements.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Relational Operators Control structures Decisions using “if” statements  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
1 Interactive Applications (CLI) and Math Interactive Applications Command Line Interfaces The Math Class Example: Solving Quadratic Equations Example:
Boolean Expressions and If Flow of Control / Conditional Statements The if Statement Logical Operators The else Clause Block statements Nested if statements.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
Chapter 5 Conditionals and Loops. © 2004 Pearson Addison-Wesley. All rights reserved2/33 Conditionals and Loops Now we will examine programming statements.
1 Interactive Applications (CLI) Interactive Applications Command Line Interfaces Project 1: Calculating BMI Example: Factoring the Solution Reading for.
CSC 1051 M.A. Papalaskari, Villanova University Repetition CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing.
Flow of Control Java Programming Mrs. C. Furman January 5, 2009.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Chapter 3: Program Statements
Outline Questions / Review Predefined Objects Variables Primitive Data Arithmetic Expressions Interactive Programs Decision Making Assignments.
Programming in Java (COP 2250) Lecture 11 Chengyong Yang Fall, 2005.
Topics Chapter 2: –Data conversion Chapter 3 –Object creation and object references –The String class and its methods –The Java standard class library.
Chapter 5: Conditionals and loops. 2 Conditionals and Loops Now we will examine programming statements that allow us to: make decisions repeat processing.
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Copyright © 2012 Pearson Education, Inc. Lab 8: IF statement …. Conditionals and Loops.
1 Chapter 3 Selections. 2 Outline 1. Flow of Control 2. Conditional Statements 3. The if Statement 4. The if-else Statement 5. The Conditional operator.
Introduction to Java Java Translation Program Structure
5-1 Chapter 5: Conditionals and Loops Topics: –Boolean expressions –Conditional statements –Increment and Decrement Operators (Chapter 2.4) –Repetition.
Control Flow. Data Conversion Promotion happens automatically when operators in expressions convert their operands For example, if sum is a float and.
Flow of Control Unless indicated otherwise, the order of statement execution through a method is linear: one after the other in the order they are written.
1 Basic Java Constructs and Data Types – Nuts and Bolts Looking into Specific Differences and Enhancements in Java compared to C.
CSE 1201 Object Oriented Programming Using Classes and Objects.
Control statements Mostafa Abdallah
1 Program Development  The creation of software involves four basic activities: establishing the requirements creating a design implementing the code.
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Outline Creating Objects The String Class The Random and Math Classes Formatting Output Enumerated Types Wrapper Classes Components and Containers Images.
Using Classes and Objects We can create more interesting programs using predefined classes and related objects Chapter 3 focuses on: object creation and.
Design A software design specifies how a program will accomplish its requirements A design includes one or more algorithms to accomplish its goal.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
if ( condition ) statement; if is a Java reserved word The condition must be a boolean expression. It must evaluate to either true or false.
Chapter 5 The if Statement
Operator Precedence Operators Precedence Parentheses () unary
Boolean Expressions and If
Boolean Expressions & the ‘if’ Statement
The order in which statements are executed is called the flow of control. Most of the time, a running program starts at the first programming statement,
SELECTION STATEMENTS (1)
Conditionals & Boolean Expressions
Chapter 3: Program Statements
Outline Boolean Expressions The if Statement Comparing Data
1) C program development 2) Selection structure
CS139 October 11, 2004.
Logic of an if statement
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
CSC 1051 – Data Structures and Algorithms I
Outline Software Development Activities
CprE 185: Intro to Problem Solving (using C)
Conditionals and Loops
Boolean Expressions & the ‘if’ Statement
Repetition CSC 1051 – Data Structures and Algorithms I Course website:
CprE 185: Intro to Problem Solving (using C)
3.0 - Design A software design specifies how a program will accomplish its requirements A design includes one or more algorithms to accomplish its goal.
Presentation transcript:

1 Interactive Applications (CLI) and Math Interactive Applications Command Line Interfaces The Math Class Flow of Control / Conditional Statements The if Statement Reading for this class: L&L, 3.5, 5.1, 5.2

2 Interactive Applications (CLI) An interactive program with a command line interface contains a sequence of steps to: –Prompt the user to enter input data –Read and save the user’s responses –Process the data after all input(s) are received We can prompt the user: System.out.println(“prompt text”); We can read and format user responses: type variable = scan.nextType();

3 Interactive Applications (CLI) Similar to Quadratic.java (Page 129) int a, b, c;// integer coefficients Scanner scan = new Scanner(System.in); System.out.println(“Enter coefficient A”); a = scan.nextInt(); System.out.println(“Enter coefficient B”); b = scan.nextInt(); System.out.println(“Enter coefficient C”); c = scan.nextInt(); // we have the data to solve the equation // ax-squared + bx + c = 0 for it’s roots

4 We have the input values, now what? To solve the quadratic equation, we need to program in Java the formulas learned in high school algebra: discriminant = b squared – 4ac root1 = (-b + squareroot of discriminant)/2a root2 = (-b - squareroot of discriminant)/2a How do we program those equations? We need to use the Math Class Library, Expression Evaluation, and Assignment

5 The Math Class The Math class is part of the java.lang package The Math class contains methods that perform various mathematical functions These include: –absolute value –square root –exponentiation –trigonometric functions

6 The Math Class The methods of the Math class are static methods (also called class methods) Static methods can be invoked through the class name – no object of the Math class is needed value = Math.cos(90) + Math.sqrt(delta); Similar to Quadratic.java (page 129)Quadratic.java discriminant = Math.pow(b, 2) – 4.0 * a * c; root1 = (-1.0 * b + Math.sqrt(discriminant))/(2.0 * a); root2 = (-1.0 * b – Math.sqrt(discriminant))/(2.0 * a); Note: We can’t program the + in the formula on page 130 in Java. We need to calculate each root separately

7 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

8 Conditional Statements 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

9 The if Statement The if statement has the following syntax: if ( condition ) statement; if is a Java reserved word The condition must be a boolean expression. It must evaluate to either true or false. If the condition is true, the statement is executed. If it is false, the statement is skipped.

10 Conditions/Boolean Expressions A condition is often obtained using an equality operator and/or relational operator which create boolean expressions that 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 ( = )

11 The if Statement An example of an if statement: 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)Age.java if (sum > MAX) delta = sum - MAX; System.out.println ("The sum is " + sum);

12 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 to human readers "Always code as if the person who ends up maintaining your code will be a violent psychopath who knows where you live." -- Martin Golding