Computer Science Chapter 4

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

How do FileWriter and PrintWriter objects relate to one another?

A FileWriter object can be used as an augment for a PrintWriter object (pg. 236): PrintWriter outputFile = new PrintWriter (writeFile);

Input File

A file that a program reads data from, named this way because the data stored in it serves as input for the program (pg. 230).

Output File

A file that a program writes data to, named this way because the program stores output in the file (pg. 230).

Text File

A file that contains data that has been encoded as text. As a result, the file may be viewed in a text editor like Notepad (pg. 230).

Binary File

A file that contains data that has not been converted to text. As a result, the file may not be viewed in a text editor like Notepad (pg. 230).

What should a good program do when asking the user for input?

A good program gives clear instructions about the kind of input that is acceptable, and not assume the user has followed those instructions (pg. 200).

Nested Loop

A loop inside another loop (pg. 221).

What is a loop?

A loop is a control structure that causes a statement or group of statements to repeat.

User Controlled Loop

A loop that allows the user to decide the number of iterations that the loop goes through (pg. 206).

Infinite Loop

A loop that continues to repeat until the program is interrupted, caused by the loop not having a way to make its BooleanExpression false (pgs. 196-197).

Conditional Loop

A loop that executes as long as particular condition exists, i.e. as long as the user's input value is invalid (pg. 207).

Count-controlled Loop

A loop that repeats a specific number of times (pg. 207).

What is the for loop?

A pretest, count controlled loop (pg. 207): for (initialization; test; update) { statement; }

nextInt(int n)

A random class method that accepts an integer argument, n, and returns a random number in the form of an int. The number will be within the range of 0 through n inclusive (pg. 250).

nextDouble()

A random class method that returns a random number in the form of a double. The number will be within the range of 0.0 to 1.0 (pg. 250).

nextFloat()

A random class method that returns a random number in the form of a float. The number will be within the range of 0.0 to 1.0 (pg. 250).

nextLong()

A random class method that returns a random number in the form of a long. The number will be within the range of -9,223,372,036,854,775,808 to 9,223,372,036854,775,808 (pg. 250).

nextInt()

A random class method that returns a random number in the form of an int. The number will be within the range of -2,147,483,648 to 2,147,483,648 (pg. 250).

Exception

A signal indicating that the program cannot continue until the unexpected event has been dealt with (pg. 233).

Buffer

A small "holding section" of memory where information is stored until it is written onto the file when the buffer is filled or the programmer manually saves the info onto the file (pg. 231).

Sentential

A variable that signals when the end of a list of values has been reached (pg. 216).

What should be done to all files when you are done with them?

All files should be closed when you are finished with them in order to save data from the buffer to the actual file: outputFile.close(); (pg. 231).

What should be done to all methods that use a PrintWriter object themselves or call a method that uses a PrintWriter object?

All methods that call a method that use a PrintWriter object themselves or call a method that uses a PrintWriter object should include "throws IOException" at the end of their header (pg. 233).

Delimiter

An item that separates other items (pg. 232).

Iteration

Each repetition of a loop (pg. 195).

Where should a control variable be declared?

If the control variable is used only in a loop, you should declare it there, because that makes its purpose clearer (pg. 211).

What happens if the file that you are opening with a PrintWriter object already exists?

If the file that you are opening with the PrintWriter object already exists, it will be erased and an empty file by the same name will be created (pg. 231).

If the programmer(s) wants to include more than one statement in the initialization and/or update expressions of the for loop header, how would they do that?

If the programmer(s) want to include more than one statement in the initialization and/or update expressions of the for loop header, they would separate the expressions with a comma (pgs. 213-214): for (x = 1, y = 1; x <= 5; x++, y++)

When are nested loops necessary?

Nested loops are necessary when a task must be repeated multiple times for each iteration of a loop (pg. 221).

How do you write data to an instance of the PrintWriter class?

Once you have created an instance of the PrintWriter class and opened the file, you can write data to the file using the print and println methods: outputFile.print("Jim"); (pg. 231).

Besides the location of the operator, what is the difference between postfix and prefix mode?

Postfix causes the increment to happen after the value of the variable is used in the expression, while prefix mode causes the increment to happen before the value of the variable is used (pg. 192).

FileWriter class

The FileWriter class is used to append or add onto a file (pg. 236): FileWriter writeFile = new FileWriter ("MyFriends.txt", true);

PrintWriter Class

The PrintWriter Class allows you to create, open, and write data to a file as an instance of the class (pg. 230).

Random Class

The Random class can be used to generate random numbers (pg. 249).

What is the random class's import statement?

The Random class's import statement is: import java.util.Random; (pg. 249).

How does the Scanner class relate to files?

The Scanner class can be used to read from a file (pg. 238): Scanner inputFile = new Scanner (myFile);

What should the accumulator variable be set to before entering a loop?

The accumulator variable should always be set to zero before entering a loop to get the correct total value from the loop (pg. 217).

Should the break and continue statements be used in loops?

The break and continue statements should not be used in loops (pg. 229).

Break Statement

The break statement causes a loop to terminate early and the program to jump to the statement following the loop (pg. 229): break;

Continue Statement

The continue statement causes the current iteration of the loop to end immediately but allows the loop to continue looping (pg. 229): continue;

Do-while Loop

The do-while loop is a posttest loop, which means its boolean expression is tested after each iteration (pg. 204): do { statement; } while (BooleanExpression);

What is unique about the do-while loop's header?

The do-while loop's header contains a semi-colon (pg. 204): while (BooleanExpression);

What will happen to the file that a FileWriter object is initialized with?

The file of a FileWriter object will create or replace the previous file with the same name (pg. 236).

How does the exists() method work with files?

The file.exists() method can be used to make sure that a file exists or does not (pgs. 246, 248).

What is the for loop ideal for?

The for loop is ideal for performing a known number of iterations (pg. 207).

How does the hasNext() method work with files?

The hasNext() method returns true if there is another line in the file left to read (inputFule.hasNext()) (pg. 241).

What is the import statement for the PrintWriter class?

The import statement for the PrintWriter class is (pg. 230): import java.io.PrintWriter;

Can the increment and decrement operators be used in postfix and prefix mode?

The increment and decrement operators can be used in postfix and prefix mode, i.e. num++ or ++num (pgs. 190-191).

How do nested loops work?

The inner loop goes through all of its iterations for each iteration of the outer loop (pg. 222).

How does the nextLine() method work with files?

The nextLine() method can be used to read each line from a file; however, after reading a line, it moves to the line after that, and will read that one when called again (pgs. 239-240).

Postfix Mode

The operator in an expression is placed after the variable, i.e. num--; (pg. 190).

Prefix Mode

The operator in an expression is placed before the variable, i.e. --num; (pg. 190).

How do the pieces of data need to formatted in a text file?

The pieces of data in a text file need to be separated. One way to do this is the println() method (pg. 232).

Input Validation

The process of inspecting data given to a program by the user and determining whether it is valid, whether its of the right type and meets the programmer(s)' requirements, such as being in a certain value range (pg. 200).

What is the programming style for the while loop?

The programming style for the while loop is if there is only one statement in the loop, it should appear on the line after the while statement and be indented one additional level. The statement may appear in a set of braces. If the loop repeats a block, each line should be inside the braces and be indented (pg. 198).

Priming Read

The reading of input operation that takes place before the loop (pg. 201).

What should the sentential be?

The sentential should be a special value that cannot be mistaken as a member of the list (pg. 219).

While Loop

The statement or block of statements is repeated as long as the expression is true, with the boolean expression tested each-time before entering the loop (pg. 193): while (BooleanExpression) statement;

What are the types of loops in Java?

The types of loops in Java are the while, do-while, and for loops (pg. 193).

Counter Variable

The variable that keeps a count of the number of iterations of a loop (pg. 208).

Accumulator

The variable used to keep the running total, the sum of numbers that accumulates with each iteration of a loop (pg. 216).

When should the while loop be used?

The while loop can be used to create input routines that repeat until acceptable data is entered or input validation, and when you do not want the loop to iterate if the condition is false from the beginning (pg. 200, 229).

Decrement a value

To decrement a value means to decrease it by one, num--; (pg. 189).

How do you determine the total number of iterations of a nested loop?

To get the total number of iterations of a nested loop, multiply the number of iterations of all the loops together (pg. 222).

Increment a value

To increment a value means to increase it by one, num++; (pg. 189).

What should you be careful of when writing a for loop?

When writing a for loop, be careful to avoid modifying the control variable in the body of the for loop (pg. 211).

How do you create an object of the PrintWriter class?

Write the statement "PrintWriter outputFile = new PrintWriter("StudentData.txt);" to create an object of the PrintWriter class (pg. 230).

How can you specify the location of the file?

You can specify the location of the file -- writing the location in as a string literal requires double back-slashes and unix/linux requires forward-slashes (pg. 237): PrintWriter outputFile = new PrinterWriter ("c:\\MyData\\Data.txt");

How do you initialize a Random object?

You initialize a Random object with this statement: Random ranNums = new Random(); (pg. 249).

When should you use the do-while loop?

You should use the do-while loop when you want to make sure the loop executes at least once (pg. 204).


Ensembles d'études connexes

Subject Pronouns #1: Write in the subject pronoun in Spanish that is related to the person/people

View Set

Exploring the New Testament Mid Term

View Set

Complex Sim - Digestion and Pancreatitis

View Set

Principles of Integrated Pest Management

View Set

Sustainable entrepreneurship rijtjes

View Set

PREP U (1340) Ch 31 Caring for Clients with Disorders of the Hematopoietic System

View Set