chapters 3-5 checkpoints

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

5.10 what will the following program display? public class Checkpoint { public static void main(String[] args) { int num1 = 99; double num2 = 1.5; System.out.println(num1 + " " + num2); myMethod(num1, num2); System.out.println(num1 + " " + num2); } public static void myMethod(int i, double d) { System.out.println(i + " " + d); i = 0; d = 0.0; System.out.println(i + " " + d); } }

99 1.5 99 1.5 0 0.0 99 1.5

5.1 what is the difference between a void method and a value-returning method?

A value-returning method returns a value back to the code that called it, and a void method does not.

5.6 what is the difference between an argument and a parameter?

An argument is a value that is passed into a method. A parameter is a special variable that holds the value being passed into the method.

3.26 explain why you cannot convert the following if-else-if statement into a switch statement. if (temp == 100) x = 0; else if (population > 1000) x = 1; else if (rate < .1) x = -1;

Because it uses greater-than and less-than operators in the comparisons

4.16 what is the difference between an input file and an output file?

Data is read from an input file, and data is written to an output file.

4.20 what classes do you use to read data from a file?

File and Scanner

4.21 write code that does the following: opens a file naemd MyName.txt, reads the first line from the file and displays it, and then closes the file.

File file = new File("MyName.txt"); Scanner inputFile = new Scanner(file); if (inputFile.hasNext()) { String str = inputFile.nextLine(); System.out.println(str); } inputFile.close();

3.16 the following truth table shows various combinations of the values true and false connected by a logical operator. complete the table by circling T or F to indicate whether the result of such a combination is true or false. logical expression result (true or false) ------------------------------------------------------------------ true && false true && true false && true false && false true || false true || true false || true false || false !true !false

Logical Expression Result (true or false) true && false false true && true true false && true false false && false false true || false true true || true true false || true true false || false false !true false !false true

5.2 is the following line of code a method header or a method call? calcTotal( );

Method call

5.3 is the following line of code a method header or a method call? public static void calcTotal( )

Method header

4.5 write an input validation loop that asks the user to enter 'Y', 'y', 'N', or 'n'.

String input; Scanner keyboard = new Scanner(System.in); System.out.print("Enter Y, y, N, or n: "); input = keyboard.nextLine(); ch = input.charAt(0); while (ch != 'Y' && ch != 'y' && ch != 'N' && ch != 'n') { System.out.println("Try again."); System.out.print("Enter Y, y, N, or n: "); input = keyboard.nextLine(); ch = input.charAt(0); }

4.6 write an input validation loop that asks the user to enter "Yes" or "No".

String str; Scanner keyboard = new Scanner(System.in); System.out.print("Enter Yes or No: "); str = keyboard.nextLine(); while ((!str.equals("Yes")) && (!str.equals("No"))) { System.out.print("Please enter Yes or No: "); str = keyboard.nextLine(); }

3.29 assume the following variable declaration exists in a program: double number = 1234567.456; write a statement that uses System.out.printf to display the value of the number variable formatted as: 1,234,567.46

System.out.printf("%,.2f", number);

3.32 assume the following variable declaration exists in a program: int number = 123456; write a statement that uses System.out.printf to display the value of the number variable in a field that is 10 spaces wide, with comma separators.

System.out.printf("%,10d", number);

3.33 assume the following variable declaration exists in a program: double number = 123456.789; write a statement that uses System.out.printf to display the value of the number variable left-justified, with comma separators, in a field that is 20 spaces wide, rounded to two decimal places.

System.out.printf("%-,20.2f", number);

3.31 assume the following variable declaration exists in a program: double number = 123.456; write a statement that uses System.out.printf to display the value of the number variable padded with leading zeros, in a field that is eight spaces wide, rounded to one decimal place. (Do not use comma separators.)

System.out.printf("%08.1f", number);

3.30 assume the following variable declaration exists in a program: double number = 123.456; write a statement that uses System.out.printf to display the value of the number variable rounded to one decimal place, in a field that is 10 spaces wide. (Do not use comma separators.)

System.out.printf("%10.1f", number);

3.34 assume the following declaration exists in a program: String name = "James"; write a statement that uses System.out.printf to display the value of name in a field that is 20 spaces wide.

System.out.printf("%20s", name);

3.17 assume the variables a = 2, b = 4, and c = 6. circle the T or F for each of the following conditions to indicate whether it is true or false. a == 4 || b > 2 6 <= c && a > 3 1 != b && c != 3 a >= -1 || a <= b ! (a > 2)

T, F, T, T, T

4.9 what will the following program segments display? a) for (int count = 0; count < 6; count++) System.out.println(count + count); b) for (int value = -5; value < 5; value++) System.out.println(value); c) int x; for (x = 5; x <= 14; x += 3) System.out.println(x);

a) 0 2 4 6 8 10 b) -5 -4 -3 -2 -1 0 1 2 3 4 c) 5 8 11 14 17

4.1 what will the following program segments display? a) x = 2; y = x++; System.out.println(y); b) x = 2; System.out.println(x++); c) x = 2; System.out.print(--x); d) x = 8; y = x--; System.out.println(y);

a) 2 b) 2 c) 1 d) 8

4.8 you want to write a for loop that displays "I love to program" 50 times. assume that you will use a control variable named count. a) what initialization expression will you use? b) what test expression will you use? c) what update expression will you use? d) write the loop.

a) count = 1 b) count <= 50 c) count++ d) for (int count = 1; count <= 50; count++) System.out.println("I love to program");

3.10 write an if-else statement that assigns 0.10 to commission unless sales is greater than or equal to 50000.0, in which case it assigns 0.2 to commission.

if (sales >= 50000.0) commission = 0.2; else commission = 0.1;

4.17 what import statement will you need in a program that performs file operations?

import java.io.*;

5.13 write the header for a method named distance. the method should return a double and have two double parameter variables: rate and time.

public static double distance(double rate, double time)

5.12 write the header for a method named days. the method should return an int and have three int parameter variables: years, months, and weeks.

public static int days(int years, int months, int weeks)

5.14 write the header for a method named lightYears. the method should return a long and have one long parameter variable: miles.

public static long lightYears(long miles)

3.24 complete the following program skeleton by writing a switch statement that displays "one" if the user has entered 1, "two" if the user has entered 2, and "three" if the user has entered 3. if a number other than 1, 2, or 3 is entered, the program should display an error message. import java.util.Scanner; public class CheckPoint { public static void main(String[] args) { int userNum; Scanner keyboard = new Scanner(System.in); System.out.print("Enter one of the numbers " + "1, 2, or 3: "); userNum = keyboard.nextInt(); // // Write the switch statement here. // } }

// Here is the switch statement. switch(userNum) { case 1 : System.out.println("One"); break; case 2 : System.out.println("Two"); break; case 3 : System.out.println("Three"); break; default: System.out.println("Error: invalid number."); }

4.10 write a for loop that displays your name 10 times.

// This is how Chloe Ashlyn would write the loop. for (int i = 1; i <= 10; i++) System.out.println("Chloe Ashlyn");

4.19 write code that does the following: opens a file named MyName.txt, writes your first name to the file, and then closes the file.

// This is how Kathryn would write the code. PrintWriter outputFile = new PrintWriter("MyName.txt"); outputFile.println("Kathryn"); outputFile.close();

5.5 write a void method that displays your full name. the method should be named myName.

// This is how Mary Catherine Jones would write it. public static void myName() { System.out.println("Mary Catherine Jones"); }

4.2 how many times will "Hello World" be printed in the following program segment? int count = 10; while (count < 1) { System.out.println("Hello World"); count++; }

0 times

3.15 the following program is used in a bookstore to determine how many discount coupons a customer gets. complete the table that appears after the program. import javax.swing.JOptionPane; public class CheckPoint { public static void main(String[] args) { int books, coupons; String input; input = JOptionPane.showInputDialog("How many books " + "are being purchased? "); books = Integer.parseInt(input); if (books < 1) coupons = 0; else if (books < 3) coupons = 1; else if (books < 5) coupons = 2; else coupons = 3; JOptionPane.showMessageDialog(null, "The number of coupons to give is " + coupons); System.exit(0); } } if the customer purchases this many coupons this many books... given. ------------------------------------------------------------------ 1 2 3 4 5 10

If the customer purchases this many coupons are this many books . . . given. 1 1 2 1 3 2 4 2 5 3 10 3

5.4 what message will the following program display if the user enters 5? what if the user enters 10? what if the user enters 100? import javax.swing.JOptionPane; public class Checkpoint { public static void main(String[] args) { String input; int number; input = JOptionPane.showInputDialog("Enter a number."); number = Integer.parseInt(input); if (number < 10) method1( ); else method2( ); System.exit(0); } public static void method1( ) { JOptionPane.showMessageDialog(null, "Able was I."); } public static void method2( ) { JOptionPane.showMessageDialog(null, "I saw Elba."); } }

If the user enters 5 the program will display Able was I. If the user enters 10 the program will display I saw Elba. If the user enters 100 the program will display I saw Elba.

4.7 name the three expressions that appear inside the parentheses in the for loop's header.

Initialization, test, and update.

4.25 assume x is an int variable, and rand references a Random object. what does the following statement do? x = rand.nextInt(100);

It assigns the variable x a random integer in the range 0 through 99.

4.26 assume x is an int variable, and rand references a Random object. what does the following statement do? x = rand.nextInt(9) + 1;

It assigns the variable x a random integer in the range 0 through 99.

4.24 assume x is an int variable, and rand references a Random object. what does the following statement do? x = rand.nextInt();

It assigns the variable x a random integer in the range −2,147,483,648 to +2,147,483,647.

4.27 assume x is a double variable, and rand references a Random object. what does the following statement do? x = rand.nextDouble( );

It assigns the variable x a random number in the range 0.0 to 1.0.

5.9 in Java, method arguments are passed by value. what does this mean?

Only a copy of an argument's value is passed into a parameter variable. A method's parameter variables are separate and distinct from the arguments that are listed inside the parentheses of a method call. If a parameter variable is changed inside a method, it has no effect on the original argument.

5.8 suppose a method named showValues accepts two int arguments. which of the following method headers is written correctly? a) public static void showValues( ) b) public static void showValues(int num1, num2) c) public static void showValues(num1, num2) d) public static void showValues(int num1, int num2)

Only d is written correctly.

4.18 what class do you use to write data to a file?

PrintWriter

4.4 write an input validation loop that asks the user to enter a number in the range of 10 through 24.

Scanner keyboard = new Scanner(System.in); System.out.print("Enter a number in the range of 10 - 24: "); number = keyboard.nextInt(); while (number < 10 || number > 24) { System.out.println("That number is not in the range."); System.out.print("Enter a number in the range of 10 - 24: "); number = keyboard.nextInt(); }

4.13 write a for loop that repeats seven times, asking the user to enter a number. the loop should also calculate the sum of the numbers entered.

Scanner keyboard = new Scanner(System.in); int number = 0, total = 0; for (int i = 1; i <= 7; i++) { System.out.print("Enter a number: "); number = keyboard.nextInt(); total += number; }

3.28 what will the following code display? int funny = 7, serious = 15; funny = serious * 2; switch (funny) { case 0 : System.out.println("That is funny."); break; case 30: System.out.println("That is serious."); break; case 32: System.out.println("That is seriously funny."); break; default: System.out.println(funny); }

That is serious.

3.27 what is wrong with the following switch statement? // This code has errors!!! switch (temp) { case temp < 0 : System.out.println("Temp is negative."); break; case temp = 0; System.out.println("Temp is zero."); break; case temp > 0 : System.out.println("Temp is positive."); break; }

The case expressions must be a literal or a final variable, which must be of the char, byte, short, int, or String types. In this code, relational expressions are used.

4.14 in the following program segment, which variable is the loop control variable (also known as the counter variable) and which is the accumulator? int a, x = 0, y = 0; while (x < 10) { a = x * 2; y += a; x++; } System.out.println("The sum is " + y);

The variable x is the loop control variable and y is the accumulator.

4.3 how many times will "I love Java programming!" be printed in the following program segment? int count = 0; while (count < 10) System.out.println("I love Java programming!");

This must be a trick question. The statement that prints "I love Java programming!" is not in the body of the loop. The only statement that is in the body of the loop is the one that prints "Hello World". Because there is no code in the loop to change the contents of the count variable, the loop will execute infinitely, printing "Hello World". So, "I love Java programming!" is never printed.

4.22 you are opening an existing file for output. how do you open the file without erasing it, and at the same time make sure that new data that is written to the file is appended to the end of the file's existing data?

You create an instance of the FileWriter class to open the file. You pass the name of the file (a string) as the constructor's first argument, and the boolean value true as the second argument. Then, when you create an instance of the PrintWriter class, you pass a reference to the FileWriter object as an argument to the PrintWriter constructor. The file will not be erased if it already exists and new data will be written to the end of the file.

4.15 why should you be careful when choosing a sentinel value?

You should be careful to choose a value that cannot be mistaken as a valid input value.

3.23 rewrite the following if-else statements as statements that use the conditional operator. a) if (x > y) z = 1; else z = 20; b) if (temp > 45) population = base * 10; else population = base * 2; c) if (hours > 40) wages *= 1.5; else wages *= 1; d) if (result >= 0) System.out.println("The result is positive."); else System.out.println("The result is negative.");

a) z = x > y ? 1 : 20; b) population = temp > 45 ? base * 10 : base * 2; c) wages = hours > 40 ? wages * 1.5 : wages * 1; d) System.out.println(result >=0 ? "The result is positive" : "The result is negative");

5.7 look at the following method header: public static void myMethod(int num) which of the following calls to the method will cause a compiler error? a) myMethod(7); b) myMethod(6.2); c) long x = 99; myMethod(x); d) short s = 2; myMethod(s);

b and c will cause a compiler error because the values being sent as arguments (a double and a long) cannot be automatically converted to an int.

5.11 look at the following method header. what type of value does the method return? public static double getValue(int a, float b, String c)

double

4.12 write a for loop that displays every fifth number, zero through 100.

for (int i = 0; i <= 100; i += 5) System.out.println(i);

4.11 write a for loop that displays all of the odd numbers, 1 through 49.

for (int i = 1; i <= 49; i += 2) System.out.println(i);

3.3 write an if statement that assigns 0.2 to commission if sales is greater than or equal to 10000.

if (sales >= 10000) commission = 0.2;

3.6 write an if statement that assigns 0 to the variable b and assigns 1 to the variable c if the variable a is less than 10.

if (a < 10) { b = 0; c = 1; }

3.11 write an if-else statement that assigns 0 to the variable b and assigns 1 to the variable c if the variable a is less than 10. otherwise, it should assign -99 to the variable b and assign 0 to the variable c.

if (a < 10) { b = 0; c = 1; } else { b = -99; c = 0; }

3.12 write nested if statements that perform the following test: if amount1 is greater than 10 and amount2 is less than 100, display the greater of the two.

if (amount1 > 10) { if (amount2 < 100) { if (amount1 > amount2) System.out.println(amount1); else System.out.println(amount2); } }

3.2 write an if statement that multiplies payRate by 1.5 if hours is greater than 40.

if (hours > 40) payRate *= 1.5;

3.4 write an if statement that sets the variable fees to 50 if the boolean variable max is true.

if (max) fees = 50;

3.7 write an if statement that displays "Goodbye" if the variable myCharacter contains the character 'D'.

if (myCharacter == 'D') System.out.println("Goodbye");

3.20 assume the variable name references a String object. write an if statement that displays "Do I know you?" if the String object contains "Timothy".

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

3.22 modify the statement you wrote in response to Checkpoint 3.20 so it performs a case-insensitive comparison.

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

3.21 assume the variables name1 and name2 reference two different String objects, containing different strings. write code that displays the strings referenced by these variables in alphabetical order.

if (name1.compareTo(name2) < 0) System.out.println(name1 + " " + name2); else System.out.println(name2 + " " + name1);

3.19 write an if statement that displays the message "The number is not valid" if the variable speed is outsid ethe range 0 through 200.

if (speed < 0 || speed > 200) System.out.println("The number is not valid");

3.18 write an if statement that displays the message "The number is valid" if the variable speed is within the range 0 through 200.

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

3.13 write code that tests the variable x to determine whether it is greater than 0. if x is greater than 0, the code should test the variable y to determine whether it is less than 20. if y is less than 20, the code should assign 1 to the variable z. if y is not less than 20, the code should assign 0 to the variable x.

if (x > 0) { if (y < 20) { z = 1; } else { z = 0; } }

3.8 write an if-else statement that assigns 20 to the variable y if the variable x is greater than 100. otherwise, it should assign 0 to the variable y.

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

3.5 write an if statement that assigns 20 to the variable y and assigns 40 to the variable z if the variable x is greater than 100.

if (x > 100) { y = 20; z = 40; }

3.9 write an if-else statement that assigns 1 to x when y is equal to 100. otherwise, it should assign 0 to x.

if (y == 100) x = 1; else x = 0;

3.1 write an if statement that assigns 0 to x when y is equal to 20.

if (y == 20) x = 0;

3.25 rewrite the following if-else-if statement as a switch statement. if (selection == 'A') System.out.println("You selected A."); else if (selection == 'B') System.out.println("You selected B."); else if (selection == 'C') System.out.println("You selected C."); else if (selection == 'D') System.out.println("You selected D."); else System.out.println("Not good with letters, eh?");

switch(selection) { case 'A' : System.out.println("You selected A."); break; case 'B' : System.out.println("You selected B."); break; case 'C' : System.out.println("You selected C."); break; case 'D' : System.out.println("You selected D."); break; default : System.out.println("Not good with letters, eh?"); }

4.23 what clause must you write in the header of a method that performs a file operation?

throws IOException


Conjuntos de estudio relacionados

Chapter 10 Learning Activity 10-4

View Set

chapter 19 Nursing Management of Pregnancy at Risk: Pregnancy-Related Complications

View Set

Chapter 63: Management of Patients with Neurologic Trauma

View Set

Foods Chapter 15 - soups and sauces chpt 18

View Set