Chapter 4 Loops and Files

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

While loop

1)a Boolean expression that is tested for a true or false value, 2) a statement or block of statements that is repeated as long as the Loop expression is true while (Boolean Expression) Statement;

name the three expressions that appear inside the parentheses in the for loops header

1. intializaiton 2. test 3. update

Given an integer variable timer, write a statement that uses the auto-decrement operator to decrease the value of that variable by 1.

int timer = 1; timer--;

Given an int variable k that has already been declared , use a do...while loop to print a single line consisting of 53 asterisks. Use no variables other than k.

k = 53; do{ System.out.print("*"); k--; } while (k >= 1);

continue statement

loop to end immediately. by passes the loops logic and makes the code difficult to understand and debug.

sentinel value

makes while loop stop ex) while(points != -1)

nested loops

necessary to perform a repetitive operation and that task itself must be repeated. for(int i = 0; i < 10; i++){// rows for(int j = 0; j < 10; j++)//columns System.out.print(j+" "); System.out.println("\n"+i); } output:0 1 2 3 4 5 6 7 8 9 0 0 1 2 3 4 5 6 7 8 9 1

data types with random class

nextDouble() = 0.0 -1.0 nextFloat() = 0.0 -1.0 nextInt() = -2,0000000000 - 2000000000 nextInt(int n) = 0 -n nextLong() = -900000000000 -9000000000000

do while loop

post test loop which means it will execute the loop prior to testing the condition. keeps doing do if while is true. do{ statenents; } while (condition); ex) int x =1; do System.out.println(x); while(x<0);

input validation

process to ensure that the input is valid ex) System.out.println(number < 1 || number >100) { sytem.out.println("invalid number") } }

exceptions

when something unexpected happens in a java program, an exception is thrown. public static void main(String[] args) throws IOException

Given an int variable n that has already been declared and initialized to a positive value , use a while loop to print a single line consisting of n asterisks. Use no variables other than n.

while(n > 0){ System.out.print("*"); n--; }

what are the three loops types?

-while loop -do while loop - for loop

Consider this code: int v = 20; --v; System.out.println(v++); What value is printed, what value is v left with

19 is printed, v ends up with 20

decrement

a value is decreased by one number = number - 1; number -= 1; number --;

Increments

a value is increased by one number = number + 1; number += 1; number ++;

iteration

Each repetition of a loop

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

File file = new File(filename); Scanner inputFile = new Scanner(file);

appending data to a file

FileWriter fwriter = new FileWriter("filename.txt", true); -if file doesnt exist than it will be created. -use printwriter to write info to file.

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 files existing data?

FileWriter fwriter = new FileWriter(filename, true); PrintWriter outputfile = new PrintWriter(fwriter); outputfile.println("Gisela"); outputfile.close( );

Given an int variable k that has already been declared , use a while loop to print a single line consisting of 88 asterisks. Use no variables other than k.

Int k = 88; while( k <= 1){ System.out.print("*"); k--; }

writing text to a file

PrintWriter outputFile = new PrintWriter("filename.txt"); outputFile.println("hello"); outputFile.close( ); -if the file already exist, it will be erased and replaced with a new file

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

PrintWriter outputFile = new PrintWriter("filename.txt"); outputfile.println("gisela");

generate words with random class

Random rand = new Random(); for (int count = 0; count < 10; count++) { if (rand.nextInt(2) == 0) Systm.out.println("T"); else Systm.out.println("H"); }

Write code that does the following : open a file named MyName.txt, reads the first line from the file and displays it, and then closes the file.

Scanner kb = new Scanner(System.in); System.out.println("what is name"); String filename = kb.nextLine(); File file = new File (filename); Scanner inputFile = new Scanner(file); String line = inputFile.nextLine(); System.out.println(line); inputFile.close();

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

Scanner kb = new Scanner(System.in); System.out.println("what is name"); String filename = kb.nextLine(); FileWriter file = new FileWriter(filename); PrintWriter outputfile = new PrintWriter(file); outputfile.println("gisela"); outputfile.close( );

reading line from a file with the nextline method

System,out.println("filename??"); String filename = kb.nextLine( ); File file = new File(filename); Scanner inputfile = new Scanner(file); String line = inputfile.nextLine( ); System.out.println(line);

hasnext/ detecting end of a file

System.out.println("filename???"); String filename = kb.nextLine( ); File file = new File (filename); Scanner inputFile = new Scanner(file); while (inputfile.hasNext( )) { String friendname = inputFile.nextLine(); System.out.println(friendname)); } inputFile.close(); kb.close();

postfix mode

The value is incremented/decremented after the equation is evaluated number++

prefix mode

The value is incremented/decremented before the equation is evaluated ++number

In the following program segment, which variable is the counter variable and which is the accumulator? int a, x, y = 0; for (x = 0; x < 10; x++) { cout << "Enter a number: "; cin >> a; y += a; } cout << "The sum of those numbers is " << y << endl;

The variable x is the counter variable. The variable y is the accumulator.

Why should you be careful when choosing a sentinel value?

You must be sure to choose a sentinel value that could not be mistaken as a member of the data list.

output file

a file that a program writes data to

infinite loops

boolean expression is never false which means the expression continues to be expressed.

break statement

causes a loop to terminate early. causes the loop to be difficult to understand and debug

Given an int variable n that has already been declared and initialized to a positive value , use a do...while loop to print a single line consisting of n asterisks. Use no variables other than n.

do{ System.out.print("*"); n--; } while(n>=1);

specifying the file location

drive "letter:\\filename.txt ex) PrintWriter outputFile= new PrintWriter("A:\\PriceList.txt");

input file

file that a program reads data from

You want to write a for loop that displays "love" 50 times. assume that you will use a control variable named count. write loop

for ( int x = 50; x >= 1; x--) System.out.println("love");

Write a for loop that display all of the odd numbers, 1 through 49

for ( int x =1; x<=49 ; x+= 2) { System.out.println(x); }

1 2 3 4 5 6 1 2 3 4 5 1 2 3 4 1 2 3 1 2 1 Program to print that

for (int i = 7; i > 0; i--) { for (int j = 1; j < i; j++){ System.out.print(j + " "); } System.out.println();

* --- * ----- * ------- * program this using a nested loop

for (r =0; r < 5; r++){ for(k =0; k < r; k++){ System.out.print(" "); } System.out.println("*"); }

For loop

for(Initialization; test; update) {statements; } ex) for (int x =1; x<1: x++)

checking for a FIle's existence

if (!file.exists()) or if (file.exists())

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

import java.io.*;

what is needed to be imported to use to work with files?

import java.io.*;

generate numbers with random class

import java.util.Random; Random randomNumbers= new Random(); ex) int numbers; number = randomNumbers.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

int number, sum, ask; sum = 0 ; Scanner kb = new Scanner (System.in); for(ask = 1; ask <= 7; ask++) { System.out.println("enter number"); number = kb.nextInt(); sum = number + sum; } System.out.println(sum); }

Given an integer variable strawsOnCamel, write a statement that uses the auto-increment operator to increase the value of that variable by 1.

int strawsOnCamel; strawsOnCamel++;

reading data from a file

read input from file instead of keyboard. File myFile = new File("filename.txt"); Scanner inputFile = new Scanner(myFile); inputFile.close( );

Consider this code: int s = 20; int t = s++ + --s; What are the values of s and t?

s is 20 and t is 40

What is the difference between a text file and a binary file?

text file already converted into text while binary is not

what are the two types of files

text:converted to characters) and binary(not converted yet to text)

loop control variable

the variable that controls the number of times that the loop iterates. ex) number <= 2; \\number is the variable that limits the time the boolean expression is iterates

Accumulator

the variable used to accumulate the total of the numbers, it is critical to set the accumulator to 0 at first.

pretest loop

the while loop because it tested for the boolean expression before executing the statements.

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

throw IOException

deciding which loop to use

while: pre test,Use it where you do not want the statements to execute if the condition is false in the beginning. do while: post test,Use it where you want the statements to execute at least one time. for:- Use it where there is some type of counting variable that can be evaluated.

Assume x is an int variable and read references a random object. what does the following statement do? x = rand.nextInt(9) +1;

x is going to be assigned to a random integer value from 0 to9 and then added 1.

Assume x is an int variable and read references a random object. what does the following statement do? x = rand.nextInt();

x is going to be assigned to a random integer value.

Assume x is an int variable and read references a random object. what does the following statement do? x = rand.nextInt(100);

x is going to be assigned to a random inter value from 0 to 100

Assume x is an int variable and read references a random object. what does the following statement do? x = rand.nextDouble();

x is going to be assigned to a random interger value from 0.0 t0 1.0


Set pelajaran terkait

PADI, Open Water Diver, Final Exam Review

View Set

1st 25 US States & Capitals (ABC order)

View Set