Chapter 3 Decision Structures Starting Out with Java

  • Slides: 87
Download presentation
Chapter 3: Decision Structures Starting Out with Java: From Control Structures through Objects Fifth

Chapter 3: Decision Structures Starting Out with Java: From Control Structures through Objects Fifth Edition by Tony Gaddis

Reading Quiz

Reading Quiz

Chapter Topics Chapter 3 discusses the following main topics: – The if Statement –

Chapter Topics Chapter 3 discusses the following main topics: – The if Statement – The if-else Statement – Nested if statements – The if-else-if Statement – Logical Operators – Comparing String Objects 3 -3

Chapter Topics Chapter 3 discusses the following main topics: – More about Variable Declaration

Chapter Topics Chapter 3 discusses the following main topics: – More about Variable Declaration and Scope – The Conditional Operator – The switch Statement – The Decimal. Format Class – The printf Method 3 -4

The if Statement • The if statement decides whether a section of code executes

The if Statement • The if statement decides whether a section of code executes or not. • The if statement uses a boolean to decide whether the next statement or block of statements executes. if (boolean expression is true) execute next statement. 3 -5

Flowcharts • If statements can be modeled as a flow chart. if (cold. Outside)

Flowcharts • If statements can be modeled as a flow chart. if (cold. Outside) wear. Coat(); Is it cold outside? Yes Wear a coat. 3 -6

Flowcharts • A block if statement may be modeled as: if (cold. Outside) {

Flowcharts • A block if statement may be modeled as: if (cold. Outside) { wear. Coat(); wear. Hat(); wear. Gloves(); } Is it cold outside? Yes Wear a coat. Wear a hat. Wear gloves. Note the use of curly braces to block several statements together. 3 -7

Relational Operators • In most cases, the boolean expression, used by the if statement,

Relational Operators • In most cases, the boolean expression, used by the if statement, uses relational operators. Relational Operator Meaning > is greater than < is less than >= is greater than or equal to <= is less than or equal to == is equal to != is not equal to 3 -8

Boolean Expressions • A boolean expression is any variable or calculation that results in

Boolean Expressions • A boolean expression is any variable or calculation that results in a true or false condition. Expression Meaning x > y Is x greater than y? x < y Is x less than y? x >= y Is x greater than or equal to y? x <= y Is x less than or equal to y. x == y Is x equal to y? x != y Is x not equal to y? 3 -9

if Statements and Boolean Expressions if (x > y) System. out. println("X is greater

if Statements and Boolean Expressions if (x > y) System. out. println("X is greater than Y"); if(x == y) 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. "); } See Example on next slide 3 -10

Single Choice If public class Average. Score { public static void main(String[] args) {

Single Choice If public class Average. Score { public static void main(String[] args) { double average; // To hold the average score // Get the first test score. System. out. println("Enter your test average : "); average = keyboard. next. Int(); if (average > 95) System. out. println("That's a great score!"); } } Enter your test average: 100 That’s a great score Enter your test average: 85

Programming Style and if Statements • An if statement can span more than one

Programming Style and if Statements • An if statement can span more than one line; however, it is still one statement. if (average > 95) grade = ′A′; is functionally equivalent to if(average > 95) grade = ′A′; 3 -12

Programming Style and if Statements • Rules of thumb: – The conditionally executed statement

Programming Style and if Statements • Rules of thumb: – The conditionally executed statement should be on the line after the if condition. – The conditionally executed statement should be indented one level from the if condition. – If an if statement does not have the block curly braces, it is ended by the first semicolon encountered after the if condition. if (expression) statement; No semicolon here. Semicolon ends statement here. 3 -13

Block if Statements • Conditionally executed statements can be grouped into a block by

Block if Statements • Conditionally executed statements can be grouped into a block by using curly braces {} to enclose them. • If curly braces are used to group conditionally executed statements, the if statement is ended by the closing curly brace. if (expression) { statement 1; statement 2; Curly brace ends the statement. } 3 -14

Block if Statements • Remember that when the curly braces are not used, then

Block if Statements • Remember that when the curly braces are not used, then only the next statement after the if condition will be executed conditionally. if (expression) statement 1; statement 2; statement 3; Only this statement is conditionally executed. 3 -15

Flags • A flag is a boolean variable that monitors some condition in a

Flags • A flag is a boolean variable that monitors some condition in a program. • When a condition is true, the flag is set to true. • The flag can be tested to see if the condition has changed. if (average > 95) high. Score = true; • Later, this condition can be tested: if (high. Score) System. out. println("That′s a high score!"); 3 -16

Comparing Characters • Characters can be tested with relational operators. • Characters are stored

Comparing Characters • Characters can be tested with relational operators. • Characters are stored in memory using the Unicode character format. • Unicode is stored as a sixteen (16) bit number. • Characters are ordinal, meaning they have an order in the Unicode character set. • Since characters are ordinal, they can be compared to each other. char c = ′A′; if(c < ′Z′) System. out. println("A is less than Z"); 3 -17

Checkpoint

Checkpoint

Checkpoint if (y==20) x = 0; if (hours>40) payrate*=1. 5; if (max) fees =

Checkpoint if (y==20) x = 0; if (hours>40) payrate*=1. 5; if (max) fees = 50; if ( a < 10 ) { b = 0; c = 1; }

if-else Statements • The if-else statement adds the ability to conditionally execute code when

if-else Statements • The if-else statement adds the ability to conditionally execute code when the if condition is false. if (expression) statement. Or. Block. If. True; else statement. Or. Block. If. False; • See example on next slide 3 -20

Divide By 0 public class Division { public static void main(String[] args) { double

Divide By 0 public class Division { public static void main(String[] args) { double number 1, number 2; // Division operands double quotient; // Result of division Scanner keyboard = new Scanner(System. in); System. out. print("Enter two numbers: "); number 1 = keyboard. next. Double(); number 2 = keyboard. next. Double(); if (number 2 == 0) { System. out. println("Can't divide by zero. "); } else { quotient = number 1 / number 2; System. out. print("The quotient is " + quotient); } } }

Executing previous program Enter two numbers: 100 50 The quotient is 2. 0 Enter

Executing previous program Enter two numbers: 100 50 The quotient is 2. 0 Enter two numbers: 100 0 Can’t divide by zero.

if-else Statement Flowcharts No Wear shorts. Is it cold outside? Yes Wear a coat.

if-else Statement Flowcharts No Wear shorts. Is it cold outside? Yes Wear a coat. 3 -23

Checkpoint

Checkpoint

Checkpoint if ( x > 100) y = 20; else y = 0; if

Checkpoint if ( x > 100) y = 20; else y = 0; if (a < b = c = } else { b = c = } 10){ 0; 1; -99; 0;

Nested if Statements • If an if statement appears inside another if statement (single

Nested if Statements • If an if statement appears inside another if statement (single or block) it is called a nested if statement. • The nested if is executed only if the outer if statement results in a true condition. 3 -26

Nested if Statement Flowcharts No Yes Is it cold outside? Wear shorts. No Wear

Nested if Statement Flowcharts No Yes Is it cold outside? Wear shorts. No Wear a jacket. Is it snowing? Yes Wear a parka. 3 -27

Nested if Statements if (cold. Outside) { if (snowing) { wear. Parka(); } else

Nested if Statements if (cold. Outside) { if (snowing) { wear. Parka(); } else { wear. Jacket(); } } else { wear. Shorts(); } 3 -28

if-else Matching • Curly brace use is not required if there is only one

if-else Matching • Curly brace use is not required if there is only one statement to be conditionally executed. • However, sometimes curly braces can help make the program more readable. • Additionally, proper indentation makes it much easier to match up else statements with their corresponding if statement. 3 -29

Alignment and Nested if Statements This if and else go together. if (cold. Outside)

Alignment and Nested if Statements This if and else go together. if (cold. Outside) { if (snowing) { wear. Parka(); } else { wear. Jacket(); } } else { wear. Shorts(); } 3 -30

Checkpoint

Checkpoint

Checkpoint if ( amount 1 > 10) if (amount 2 < 100) if (amount

Checkpoint if ( amount 1 > 10) if (amount 2 < 100) if (amount 1 > amount 2) System. out. println(amount 1); else System. out. println(amount 2); if (x > 0) if (y < 20) z = 1; else z = 0;

if-else-if Statements if (expression_1) { statement; If expression_1 is true these statements are statement;

if-else-if Statements if (expression_1) { statement; If expression_1 is true these statements are statement; executed, and the rest of the structure is ignored. etc. } else if (expression_2) { statement; Otherwise, if expression_2 is true these statements are statement; executed, and the rest of the structure is ignored. etc. } Insert as many else if clauses as necessary else { statement; etc. } These statements are executed if none of the expressions above are true. 3 -33

if-else-if Statements • Nested if statements can become very complex. • The if-else-if statement

if-else-if Statements • Nested if statements can become very complex. • The if-else-if statement makes certain types of nested decision logic simpler to write. • Care must be used since else statements match up with the immediately preceding unmatched if statement. • See example on next slide 3 -34

System. out. println("Enter your test score : "); test. Score = keyboard. next. Int();

System. out. println("Enter your test score : "); test. Score = keyboard. next. Int(); Test Score if (test. Score < 60) System. out. println("Your grade is F. "); else if (test. Score < 70) System. out. println("Your grade is D. "); else if (test. Score < 80) System. out. println("Your grade is C. "); else if (test. Score < 90) System. out. println("Your grade is B. "); else if (test. Score <= 100) System. out. println("Your grade is A. "); else // Invalid score System. out. println("Invalid score. "); Enter your test score : 86 Your grade is B Enter your test score : 103 Invalid score

if-else-if Flowchart 3 -36

if-else-if Flowchart 3 -36

Checkpoint Prints:

Checkpoint Prints:

Checkpoint Prints: 1 1

Checkpoint Prints: 1 1

Logical Operators • Java provides two binary logical operators (&& and ||) that are

Logical Operators • Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. • Java also provides one unary (!) logical operator to reverse the truth of a boolean expression. 3 -39

Logical Operators Operator && || ! Meaning Effect AND Connects two boolean expressions into

Logical Operators Operator && || ! Meaning Effect AND Connects two boolean expressions into one. Both expressions must be true for the overall expression to be true. OR Connects two boolean expressions into one. One or both expressions must be true for the overall expression to be true. It is only necessary for one to be true, and it does not matter which one. NOT The ! operator reverses the truth of a boolean expression. If it is applied to an expression that is true, the operator returns false. If it is applied to an expression that is false, the operator returns true. 3 -40

The && Operator • The logical AND operator (&&) takes two operands that must

The && Operator • The logical AND operator (&&) takes two operands that must both be boolean expressions. • The resulting combined expression is true if (and only if) both operands are true. Expression 1 Expression 2 Expression 1 && Expression 2 true false false true 3 -41

The || Operator • The logical OR operator (||) takes two operands that must

The || Operator • The logical OR operator (||) takes two operands that must both be boolean expressions. • The resulting combined expression is false if (and only if) both operands are false. Expression 1 Expression 2 Expression 1 || Expression 2 true false false true 3 -42

Which loan is easier to get? // Determine whether the user qualifies for the

Which loan is easier to get? // Determine whether the user qualifies for the loan. if (salary >= 30000 && years. On. Job >= 2) { System. out. println("You qualify for the loan. "); } else { System. out. println("You do not qualify for the loan. "); } // Determine whether the user qualifies for the loan. if (salary >= 30000 || years. On. Job >= 2) { System. out. println("You qualify for the loan. "); } else { System. out. println("You do not qualify for the loan. "); }

The ! Operator • The ! operator performs a logical NOT operation. • If

The ! Operator • The ! operator performs a logical NOT operation. • If an expression is true, !expression will be false. if (!(temperature > 100)) System. out. println("Below the maximum temperature. "); • If temperature > 100 evaluates to false, then the output statement will be run. Expression 1 !Expression 1 true false true 3 -44

Short Circuiting • Logical AND and logical OR operations perform short-circuit evaluation of expressions.

Short Circuiting • Logical AND and logical OR operations perform short-circuit evaluation of expressions. • Logical AND will evaluate to false as soon as it sees that one of its operands is a false expression. • Logical OR will evaluate to true as soon as it sees that one of its operands is a true expression. 3 -45

Order of Precedence • The ! operator has a higher order of precedence than

Order of Precedence • The ! operator has a higher order of precedence than the && and || operators. • The && and || operators have a lower precedence than relational operators like < and >. • Parenthesis can be used to force the precedence to be changed. 3 -46

Order of Precedence 1 Operators Description (unary negation) ! Unary negation, logical NOT 2

Order of Precedence 1 Operators Description (unary negation) ! Unary negation, logical NOT 2 * / % 3 + - 4 < > <= >= 5 == != 6 && Logical AND 7 || Logical NOT 8 = += -= *= /= %= Multiplication, Division, Modulus Addition, Subtraction Less-than, Greater-than, Less-than or equal to, Greater-than or equal to Is equal to, Is not equal to Assignment and combined assignment operators. 3 -47

Checkpoint

Checkpoint

Checkpoint • • if ( speed >= 0 && speed <= 200 System. out.

Checkpoint • • if ( speed >= 0 && speed <= 200 System. out. println("The number is valid"); if ( speed < 0 || speed > 200 System. out. println("The number is invalid");

Comparing String Objects • In most cases, you cannot use the relational operators to

Comparing String Objects • In most cases, you cannot use the relational operators to compare two String objects (or any objects). • Reference variables contain the address of the object they represent. • Unless the references point to the same object, the relational operators will not return true. • See examples on next slides 3 -50

String. equals String name 1 = "Mark", name 2 = "Mark", name 3 =

String. equals String name 1 = "Mark", name 2 = "Mark", name 3 = "Mary"; // Compare "Mark" and "Mark" if (name 1. equals(name 2)) System. out. println(name 1 + " is the same as " + name 2); else System. out. println(name 1 + " is NOT same as " + name 2); // Compare "Mark" and "Mary" if (name 1. equals(name 3)) System. out. println(name 1 + " is the same as " + name 3); else System. out. println(name 1 + " is NOT same as " + name 3); Mark is the same as Mark is NOT same as Mary

String. compare. To String name 1 = "Mary", name 2 = "Mark"; // Compare

String. compare. To String name 1 = "Mary", name 2 = "Mark"; // Compare "Mary" and "Mark" if (name 1. compare. To(name 2) < 0) { System. out. println(name 1 + " } else if (name 1. compare. To(name 2) { System. out. println(name 1 + " } is less than " + name 2); == 0) is equal to " + name 2); > 0) is greater than " + name 2); Mary is greater than Mark

Ignoring Case in String Comparisons • In the String class the equals and compare.

Ignoring Case in String Comparisons • In the String class the equals and compare. To methods are case sensitive. • In order to compare two String objects that might have different case, use: – equals. Ignore. Case, or – compare. To. Ignore. Case • See example on next slide 3 -53

String. equals. Ignore. Case // Prompt the user to enter the secret word. System.

String. equals. Ignore. Case // Prompt the user to enter the secret word. System. out. print("Enter the secret word: "); input = keyboard. next. Line(); // Determine whether the user entered the secret word. if (input. equals. Ignore. Case("PROSPERO")) { System. out. println("Congratulations! You know the secret!"); } else { System. out. println("Sorry, that is NOT the secret word!"); } Enter the secret word: prospero Congratulations! You know the secret! Enter the secret word: PRos. PERo Congratulations! You know the secret!

Checkpoint

Checkpoint

Checkpoint if ( name. equals("Timothy") ) System. out. println("Do I know you? "); if

Checkpoint if ( name. equals("Timothy") ) System. out. println("Do I know you? "); if ( name 1. compare. To( name 2) < 0 ) System. out. println( name 1 + ", " + name 2 ); else System. out. println( name 2 + ", " + name 1 ); if ( name. equals. Ignore. Case("Timothy") ) System. out. println("Do I know you? ");

Variable Scope • In Java, a local variable does not have to be declared

Variable Scope • In Java, a local variable does not have to be declared at the beginning of the method. • The scope of a variable begins at the point it is declared and terminates at the end of the nearest enclosing braces. • When a program enters a section of code where a variable has scope, that variable has come into scope, which means the variable is visible to the program. • See example on next slide 3 -57

public class Variable. Scope { public static void main(String[] args) { Scanner keyboard =

public class Variable. Scope { public static void main(String[] args) { Scanner keyboard = new Scanner(System. in); // Get the user's first name. String first; first = keyboard. next(); Scope of keyboard Scope of first if ( first. equals("Philip")) { // Get the user's last name. String last; Scope of last = keyboard. next(); } System. out. println("Hello, " + first + " " + last); } } ERROR: last is out of scope

The switch Statement • The if-else statement allows you to make true / false

The switch Statement • The if-else statement allows you to make true / false branches. • The switch statement allows you to use an ordinal value to determine how a program will branch. • The switch statement can evaluate an integer type or character type variable and make decisions based on the value. 3 -59

The switch Statement • The switch statement takes the form: switch (Switch. Expression) {

The switch Statement • The switch statement takes the form: switch (Switch. Expression) { case Case. Expression: // place one or more statements here break; // case statements may be repeated //as many times as necessary default: // place one or more statements here } 3 -60

The switch Statement • The switch statement takes an ordinal value (byte, short, int,

The switch Statement • The switch statement takes an ordinal value (byte, short, int, long, or char) as the Switch. Expression. switch (Switch. Expression) { … } • The switch statement will evaluate the expression. • If there is an associated case statement that matches that value, program execution will be transferred to that case statement. 3 -61

The switch Statement • Each case statement will have a corresponding Case. Expression that

The switch Statement • Each case statement will have a corresponding Case. Expression that must be unique. case Case. Expression: // place one or more statements here break; • If the Switch. Expression matches the Case. Expression, the Java statements between the colon and the break statement will be executed. 3 -62

The case Statement • The break statement ends the case statement. • The break

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. – See example next slide: No. Breaks. java – See example in two slides: Pet. Food. java • The default section is optional and will be executed if no Case. Expression matches the Switch. Expression. • See example: Switch. Demo. java 3 -63

System. out. print("Enter 1, 2, or 3: "); number = keyboard. next. Int(); //

System. out. print("Enter 1, 2, or 3: "); number = keyboard. next. Int(); // Determine the number entered. switch (number) { case 1: System. out. println("You entered 1. "); case 2: System. out. println("You entered 2. "); case 3: System. out. println("You entered 3. "); default: System. out. println("That's not 1, 2, or 3!"); } Enter 1, 2, or 3: 1 You entered 2 You entered 3 That’s not 1, 2, or 3! No Breaks this shows what happens if you forget to put break after each case Enter 1, 2, or 3: 2 You entered 3 That’s not 1, 2, or 3!

System. out. print("Enter pet food grade A, B, or C "); input = keyboard.

System. out. print("Enter pet food grade A, B, or C "); input = keyboard. next. Line(); food. Grade = input. char. At(0); Pet Food // Display pricing for the selected grade. switch(food. Grade) { case 'a': case 'A': System. out. println("30 cents per lb. "); break; case 'b': case 'B': System. out. println("20 cents per lb. "); break; case 'c': case 'C': System. out. println("15 cents per lb. "); break; default: System. out. println("Invalid choice. "); } Enter grade A, B, or C A 30 cents per lb Enter grade A, B, or C C 15 cents per lb

C h e c k p o i n t

C h e c k p o i n t

C h e c k p o i n t switch ( selection){ case

C h e c k p o i n t switch ( selection){ case 'A': System. out. println("You selected A"); break; case 'B': System. out. println("You selected B"); break; // skipping cases for C and D for space reasons default: System. out. println("Not good with letters, eh? "); quit; }

The Decimal. Format Class • When printing out double and float values, the full

The Decimal. Format Class • When printing out double and float values, the full fractional value will be printed. • The Decimal. Format class can be used to format these values. • In order to use the Decimal. Format class, the following import statement must be used at the top of the program: import java. text. Decimal. Format; • See example on next slide: 3 -68

import java. text. Decimal. Format; Printing Two Digits after decimal point (a currency format)

import java. text. Decimal. Format; Printing Two Digits after decimal point (a currency format) public class Format 1 { public static void main(String[] args) { double number 1 = 0. 166666667; double number 2 = 1. 6; double number 3 = 16. 0; double number 4 = 166. 66666667; // Create a Decimal. Format object. Decimal. Format formatter = new Decimal. Format("#0. 00"); // Display the formatted variable contents. System. out. println(formatter. format(number 1)); System. out. println(formatter. format(number 2)); System. out. println(formatter. format(number 3)); System. out. println(formatter. format(number 4)); } } 0. 17 1. 60 16. 00 166. 67

END OF LECTURE!!! Please begin prelab 3 to prepare for Lab 3 For full

END OF LECTURE!!! Please begin prelab 3 to prepare for Lab 3 For full credit, prelab 3 is due Midnight Wednesday The following slides are additional material from the textbook that you may use as reference

The Conditional Operator • The conditional operator is a ternary (three operand) operator. •

The Conditional Operator • The conditional operator is a ternary (three operand) operator. • You can use the conditional operator to write a simple statement that works like an if-else statement. 3 -71

The Conditional Operator • The format of the operators is: Boolean. Expression ? Value

The Conditional Operator • The format of the operators is: Boolean. Expression ? Value 1 : Value 2 • This forms a conditional expression. • If Boolean. Expression is true, the value of the conditional expression is Value 1. • If Boolean. Expression is false, the value of the conditional expression is Value 2. 3 -72

The Conditional Operator • Example: z = x > y ? 10 : 5;

The Conditional Operator • Example: z = x > y ? 10 : 5; • This line is functionally equivalent to: if(x > y) z = 10; else z = 5; 3 -73

The Conditional Operator • Many times the conditional operator is used to supply a

The Conditional Operator • Many times the conditional operator is used to supply a value. number = x > y ? 10 : 5; • This is functionally equivalent to: if(x > y) number = 10; else number = 5; • See example: Consultant. Charges. java 3 -74

The printf Method • You can use the System. out. printf method to performatted

The printf Method • You can use the System. out. printf method to performatted console output. • The general format of the method is: System. out. printf(Format. String, Arg. List); 3 -75

The printf Method System. out. printf(Format. String, Arg. List); Format. String is a string

The printf Method System. out. printf(Format. String, Arg. List); Format. String is a string that contains text and/or special formatting specifiers. Arg. List is optional. It is a list of additional arguments that will be formatted according to the format specifiers listed in the format string. 3 -76

The printf Method • A simple example: System. out. printf("Hello Worldn"); 3 -77

The printf Method • A simple example: System. out. printf("Hello Worldn"); 3 -77

The printf Method • Another example: int hours = 40; System. out. printf("I worked

The printf Method • Another example: int hours = 40; System. out. printf("I worked %d hours. n", hours); 3 -78

The printf Method int hours = 40; System. out. printf("I worked %d hours. n",

The printf Method int hours = 40; System. out. printf("I worked %d hours. n", hours); The %d format specifier indicates that a decimal integer will be printed. The contents of the hours variable will be printed in the location of the %d format specifier. 3 -79

The printf Method • Another example: int dogs = 2, cats = 4; System.

The printf Method • Another example: int dogs = 2, cats = 4; System. out. printf("We have %d dogs and %d cats. n", dogs, cats); 3 -80

The printf Method • Another example: double gross. Pay = 874. 12; System. out.

The printf Method • Another example: double gross. Pay = 874. 12; System. out. printf("Your pay is %f. n", gross. Pay); 3 -81

The printf Method • Another example: double gross. Pay = 874. 12; System. out.

The printf Method • Another example: double gross. Pay = 874. 12; System. out. printf("Your pay is %f. n", gross. Pay); The %f format specifier indicates that a floating-point value will be printed. The contents of the gross. Pay variable will be printed in the location of the %f format specifier. 3 -82

The printf Method • Another example: double gross. Pay = 874. 12; System. out.

The printf Method • Another example: double gross. Pay = 874. 12; System. out. printf("Your pay is %. 2 f. n", gross. Pay); 3 -83

The printf Method • Another example: double gross. Pay = 874. 12; System. out.

The printf Method • Another example: double gross. Pay = 874. 12; System. out. printf("Your pay is %. 2 f. n", gross. Pay); The %. 2 f format specifier indicates that a floating-point value will be printed, rounded to two decimal places. 3 -84

The printf Method • Another example: String name = "Ringo"; System. out. printf("Your name

The printf Method • Another example: String name = "Ringo"; System. out. printf("Your name is %s. n", name); The %s format specifier indicates that a string will be printed. 3 -85

The printf Method • Specifying a field width: int number = 9; System. out.

The printf Method • Specifying a field width: int number = 9; System. out. printf("The value is %6 dn", number); The %6 d format specifier indicates the integer will appear in a field that is 6 spaces wide. 3 -86

The printf Method • Another example: double number = 9. 76891; System. out. printf("The

The printf Method • Another example: double number = 9. 76891; System. out. printf("The value is %6. 2 fn", number); The %6. 2 f format specifier indicates the number will appear in a field that is 6 spaces wide, and be rounded to 2 decimal places. 3 -87