Chapter 4: Checkpoints

Ace your homework & exams now with Quizwiz!

for (int value = -5; value < 5; value++) System.out.println(value);

-5 -4 -3 -2 -1 0 1 2 3 4

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");

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();

What will the following program segments display? a) for (int count = 0; count < 6; count++) System.out.println(count + count);

0 2 4 6 8 1

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

c) x = 2; System.out.println(--x);

1

b) x = 2; System.out.println(x++);

2

int x; for (x = 5; x <= 14; x += 3) System.out.println(x); System.out.println(x);

5 8 11 14 17

d) x = 8; y = x--; System.out.println(y);

8

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.

What classes do you use to read data from a file?

File and Scanner

Write code that does the following: opens a file named 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();

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

Initialization, test, and update.

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.

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 1 through 9.

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.

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.

What class do you use to write data to a file?

PrintWriter

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(); }

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;

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); }

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(); }

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.

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.

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.

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.

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?

a) count = 1

What test expression will you use?

b) count <= 50

What update expression will you use?

c) count++

Write the loop.

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

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

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

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);

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

import java.io.*;

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

throws IOException


Related study sets

Chapter6 Functions of Bone and the Skeletal System

View Set

Chapter 1: Introduction to Nursing

View Set

Pneumonoultramicroscopicsilicovolcanoconiosis

View Set

Ch 18 Performance and Breach of a Sales and Lease Contract

View Set

Chapter 18- Social Media Marketing

View Set

Chapter 27: Disorders of Cardiac Function, and Heart Failure and Circulatory Shock

View Set