1 Fall 2007ACS-1903 Chapter 3 Decision Structures Variable Scope The Conditional Operator The switch Statement DecimalFormat Class printf.

Slides:



Advertisements
Similar presentations
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Advertisements

Introduction to Flowcharting
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Java Programming, 3e Concepts and Techniques Chapter 4 Decision Making and Repetition with Reusable Objects.
1 Fall 2007ACS-1903 Chapter 3 Decision Structures The if Statement The if-else Statement The if-else-if Statement Nested if Statements Logical Operators.
 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
1 Fall 2008ACS-1903 Chapter 3 Decision Structures The if Statement The if-else Statement The if-else-if Statement Nested if Statements Logical Operators.
C Programming Language 4 Developed in 1972 by Dennis Ritchie at AT&T Bell Laboratories 4 Used to rewrite the UNIX operating system 4 Widely used on UNIX.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 3 Control Statements.
Java An introduction. Example 1 public class Example1 { public static void main (String [] args) { System.out.println (“This is the first example”); int.
Chapter 3: Decision Structures Starting Out with Java: From Control Structures through Objects Fifth Edition by Tony Gaddis.
 2003 Prentice Hall, Inc. All rights reserved. 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 if Single-Selection.
The switch Statement, DecimalFormat, and Introduction to Looping
11 Chapter 3 DECISION STRUCTURES CONT’D. 22 FORMATTING FLOATING-POINT VALUES WITH THE DecimalFormat CLASS We can use the DecimalFormat class to control.
© 2012 Pearson Education, Inc. All rights reserved. Chapter 3: Decision Structures Starting Out with Java: From Control Structures through Data Structures.
Chapter 3 Decision Structures 1. Contents 1.The if Statement 2.The if-else Statement 3.The if-else-if Statement 4.Nested if Statement 5.Logical Operators.
 Pearson Education, Inc. All rights reserved Formatted Output.
Chapter 3: Data Types and Operators JavaScript - Introductory.
Starting Out with Java: From Control Structures through Objects
Chapter 3 Decision Structures Starting Out with Java: From Control Structures through Objects.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 2 Input,
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Chapter 3:Decision Structures.  3.1 The if Statement  3.2 The if-else Statement  3.3 The if-else-if Statement  3.4 Nested if Statements  3.5 Logical.
Introduction to C Programming Chapter 2 : Data Input, Processing and Output.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
3 1 Sending Data Using an Online Form CGI/Perl Programming By Diane Zak.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Control Structures: Part 1.
CS-0401 INTERMEDIATE PROGRAMMING USING JAVA Prof. Dr. Paulo Brasko Ferreira Fall 2013.
 2003 Prentice Hall, Inc. All rights reserved. 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 if Single-Selection.
Chapter 3: Decision Structures
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java: From Control Structures through Objects Third Edition.
Sections 5.1 – 5.4 © Copyright by Pearson Education, Inc. All Rights Reserved.
By Mr. Muhammad Pervez Akhtar
CSCI 1226 FALL 2015 MIDTERM #1 REVIEWS.  Types of computers:  Personal computers  Embedded systems  Servers  Hardware:  I/O devices: mice, keyboards,
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Chapter 6 - More About Problem Domain Classes1 Chapter 6 More About Problem Domain Classes.
Lecture 15: Course Review BJ Furman ME 30 16MAY2011.
 2003 Prentice Hall, Inc. All rights reserved. 1 Will not cover 4.14, Thinking About Objects: Identifying Class Attributes Chapter 4 - Control Structures.
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
1 Creating Objects  A variable holds either a primitive type or a reference to an object  A class name can be used as a type to declare an object reference.
CCSA 221 Programming in C CHAPTER 3 COMPILING AND RUNNING YOUR FIRST PROGRAM 1 ALHANOUF ALAMR.
 Very often when you write code, you want to perform different actions for different decisions. You can use conditional statements in your code to do.
1 float Data Type Data type that can hold numbers with decimal values – e.g. 3.14, 98.6 Floats can be used to represent many values: –Money (but see warning.
3 Introduction to Classes and Objects.
ECE Application Programming
Chapter 3: Decision Structures by Tony Gaddis and Godfrey Muganda
Objects and Classes.
Chapter 3: Decision Structures
Yanal Alahmad Java Workshop Yanal Alahmad
Problem Solving and Control Statements: Part 2
SELECTION STATEMENTS (1)
CS-0401 INTERMEDIATE PROGRAMMING USING JAVA
Chapter 3: Decision Structures
CS1100 Computational Engineering
Variables Numbers can be stored and retrieved while a program is running if they are given a home. The way that integers and decimal numbers are stored.
Statements and expressions - java
Chap 1 Chap 2 Chap 3 Chap 5 Surprise Me
BBIT 212/ CISY 111 Object Oriented Programming (OOP)
SELECTIONS STATEMENTS
CS360 Client/Server Programming Using Java
Chapter 4 - Control Structures: Part 1
JavaScript: Introduction to Scripting
Putting Values into a Suitable Form for Display
Presentation transcript:

1 Fall 2007ACS-1903 Chapter 3 Decision Structures Variable Scope The Conditional Operator The switch Statement DecimalFormat Class printf

2 Fall 2007ACS-1903 Variable scope Scope: a variable is only known within the block where it is declared. Local variable: a variable declared within a method. It is not known outside the method, and it can only be used from the point where it is declared to the end of the method. Aside: Java different kinds of variables are instance, static, final, local See VariableScope.javaVariableScope.java

3 Fall 2007ACS-1903 Conditional Operator Consider: If sale is less than or equal to 300 Then reward points are dollar value / 25 Otherwise reward points are 50 plus (dollar value / 25) Java: sale <= 300 ? rewardPoints = dollars / 25 : rewardPoints = 50 + dollars / 25 ; General Syntax for a Conditional Expression expression1 ? expression2 : expression3 ;

4 Fall 2007ACS-1903 Conditional Expression Java: rewardPoints = sale <= 300 ? dollars / 25 : 50 + dollars/25 ; See Checkpoint exercises on page 148 e.g. rewrite if (x > y) z = 1; else z = 20; as a conditional expression Can also appear in an assignment statement

5 Fall 2007ACS-1903 switch statement – an n-way decision structure Value of integer expression is case 1 actions =case 1 =case 2 =case n case 2 actionscase n actions … …

6 Fall 2007ACS-1903 switch As a UML activity diagram [expression=case 1 ] Action 2Action 1 [expression=case 2 ][expression=case n ] Action n … SwitchDemo.java NoBreaks.java PetFood.java Notice use of break in the cases

7 Fall 2007ACS-1903 DecimalFormat Class Used to control the way floating-point numbers are formatted Required to make the class available to the compiler: import java.text.DecimalFormat; Must instantiate a DecimalFormat object with a pattern: DecimalFormat formatter = new DecimalFormat(“#0.00”); Send this object the format() message & it returns a string System.out.println(formatter.format(number));

8 Fall 2007ACS-1903 DecimalFormat Class Formatting patterns # used to indicate leading zero is suppressed 0 used to indicate a digit position. used to specify decimal point % used to have value multiplied by 100 and obtain a percent sign, used to obtain a digits separator Format1.java Format2.java Format3.java Format4.java

9 Fall 2007ACS-1903 printf This method uses a string with embedded format specifiers and other arguments that replace the specifiers in the output System.out.printf(“Hours worked is %d, pay rate is %d, and gross is %d”, hours, ratePay, grossPay); Examples %dfor an integer value %6d… in 6 positions %ffor a floating-point value %8f… in 8 positions %.2f… with 2 decimal places %8.2f… in 8 positions with 2 decimal places %12sfor a string in 12 positions