Java chapter 3.9 - 3.10

¡Supera tus tareas y exámenes ahora con Quizwiz!

What will be printed to the console when the following code is executed? double x = 45678.259; System.out.printf("$%,.2f", x);

$45,678.26

What will be the value of x after the following code is executed? int x = 10; switch (x) { case 10: x += 15; case 12: x -= 5; break; default: x *= 3; }

20

For the System.out.printf method, if the format string has 5 format specifiers, how many additional arguments should follow the format string?

5

What two arguments does the System.out.printf method take?

FormatString ArgumentList

Which of the following will format the value 12.7883794 to display as "12.8"?

System.out.printf("%.1f", 12.7883794);

In a switch statement the case statements show the program where to start executing in the block and the break statements show the program where to stop

True

In a switch statement, each case value must be unique.

True

In a switch statement, the break keyword in a case section causes the program to leave the switch statement immediately.

True

When a switch statement executes, it compares the value of the test expression with the values that follow each of the case statements from top to bottom.

True

In a format specifier, which conversion character is used for integer numbers?

d

This section of the switch statement is branched to if none of the case expressions match the switch expression.

default

There is a __________________ correspondence between the format specifiers and the arguments that appear after the format string.

one-to-one

When a format specifier uses a precision specifier to limit the number of decimal places the floating point number is ___________.

rounded

Read page 151. Assume that an int variable named number has been defined in the code. Write a switch statement that uses System.out to print "1" if the value of number is 1 and "2" if the value of number is 2, otherwise print "default". NOTE: No need to declare or initialize number. Simply write the switch statement based on the pattern shown on page 151.

switch (number) { case 1: System.out.println("You entered 1."); break; case 2: System.out.println("You entered 2."); break; default: System.out.println("That's not 1 or 2!"); }

Which format specifier is used to print a String?

%s

All format specifiers begin with this character

%

Which format specifier would be used to display a floating point number with comma separators and two decimal places?

%,.2f

Which of the following Format Specifiers will print a floating point number with only 2 decimal places?

%.2f

Which format specifier would be used to display in integer number with 10 characters and leading zeros?

%010d

Which format specifier would print a floating point number with 7 total characters and 2 decimal places?

%12.2f

Which format specifier will display a floating point number with 20 spaces?

%20f

A bank charges a base fee of $10 per month, plus the following check fees for a commercial checking account: $.10 each for less than 20 checks $.08 each for 20-39 checks $.06 each for 40-59 checks $.04 each for 60 or more checks Write a program that asks for the number of checks written for the month. The program should then calculate and display the bank's service fees for the month.

// Needed for Scanner class import java.util.Scanner; public class BankCharges { public static void main(String[] args) { // Declaring variables int checks = 0; double fee1 = .10; double fee2 = .08; double fee3 = .06; double fee4 = .04; // Create a Scanner object for keyboard input Scanner keyboard = new Scanner(System.in); // Get one of the numbers 1 - 10 from the user System.out.println("Enter how many checks were written this month: "); checks = keyboard.nextInt(); // else-if statements to tell the user how much // money is owed if (checks < 20) System.out.printf("Your total fee is: $%,.2f", checks * fee1 ); else if (checks >= 20 && checks <= 39) System.out.printf("Your total fee is: $%,.2f", checks * fee2 ); else if (checks >= 40 && checks <= 59) System.out.printf("Your total fee is: $%,.2f", checks * fee3 ); else if (checks > 60) System.out.printf("Your total fee is: $%,.2f", checks * fee4 ); } }

3.1 Roman Numerals Use a switch statement rather than an if-else-if structure to map the input number to a Roman numeral. Only convert numbers 1 through 10. If the user enters a number greater than 10 the print a user friendly default message.

// Needed for Scanner class import java.util.Scanner; public class RomanNumerals { public static void main(String[] args) { // Declaring variables as integers int number; // Create a Scanner object for keyboard input Scanner keyboard = new Scanner(System.in); // Get one of the numbers 1 - 10 from the user System.out.println("Enter a number from 1 - 10: "); number = keyboard.nextInt(); // Determine the Roman numeral of the entered integer switch (number) { case 1: System.out.println("1 = I") ; break; case 2: System.out.println("2 = II") ; break; case 3: System.out.println("3 = III") ; break; case 4: System.out.println("4 = IV") ; break; case 5: System.out.println("5 = V") ; break; case 6: System.out.println("6 = VI") ; break; case 7: System.out.println("7 = VII") ; break; case 8: System.out.println("8 = VIII") ; break; case 9: System.out.println("9 = IX") ; break; case 10: System.out.println("10 = X") ; break; default: System.out.println("The number must be between 1-10!!"); } } }

What will be the value of discountRate after the following statements are executed? double discountRate; char custType = 'B'; switch (custType) { case 'A': discountRate = 0.08; break; case 'B': discountRate = 0.06; case 'C': discountRate = 0.04; default: discountRate = 0.0; }

0.0

Describe the basic concept of a switch statement

A switch is a multiple alternative decision structure that allows you to test the value of a variable or an expression and then use that value to determine which statement or set of statements to execute.

In a switch statement, the default section is required.

False

In a format specifier, which conversion character is used for floating point numbers?

f


Conjuntos de estudio relacionados

Chapter 21: Activation of Cytotoxic T Cells - 4 functions of cytokines

View Set

Ch. 58 - Independent Contractor Agreements - Quiz Quesitons

View Set

Essentials of Networking Modules 7, 8, 9

View Set