Chapter 4 Loops and Files

Ace your homework & exams now with Quizwiz!

what are the three loops types?

-while loop -do while loop - for loop

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;

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

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

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

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

1. intializaiton 2. test 3. update

Increments

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

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

File file = new File(filename); Scanner inputFile = new Scanner(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 cars. Use no variables other than k.

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

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

decrement

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

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

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

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

int amountOfCheese ; amountOfCheese ++;

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

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 amountOfCheese<= 2; \\number is the variable that limits the time the boolean expression is iterates

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

throw IOException

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


Related study sets

프랑스 필수 단어(amical-1)

View Set

Unit 5 revision, Module 46 Health Care Systems

View Set

Econ - Demand, Supply, & Equilibrium

View Set

Diversity in Criminal justice Midterm

View Set

College Physics II: Module 3 & Module 4

View Set

NUR 145 - Module A: Practice Questions

View Set

Spanish exam study guide, Spanish II - Preterite regular and irregular verbs, Spanish II- Verbs, Los verbos reflexivos - presente, Infinitive expressions, Preterite Clue Words, Senderos 2 Comparisons, Spanish Indirect Object Pronouns, Spanish Indirec...

View Set