• Shuffle
    Toggle On
    Toggle Off
  • Alphabetize
    Toggle On
    Toggle Off
  • Front First
    Toggle On
    Toggle Off
  • Both Sides
    Toggle On
    Toggle Off
  • Read
    Toggle On
    Toggle Off
Reading...
Front

Card Range To Study

through

image

Play button

image

Play button

image

Progress

1/26

Click to flip

Use LEFT and RIGHT arrow keys to navigate between flashcards;

Use UP and DOWN arrow keys to flip the card;

H to show hint;

A reads text to speech;

26 Cards in this Set

  • Front
  • Back

how do increment and decrement operators work?


how is they're priority arranged?

if i=1


--------------


i++ = 1;


i = 2


-------------


++i = 2


i = 2


-------------


- the addition takes place before the code is executed if the ++ is before the variable. it will execute after the code has executed if ++ is after i


- decrement follow the same rules. both only add or subtract 1

show a while loop that counts to 5

int number = 1;



while (number <= 5){


System.out.println(number);


number++


}

what is an infinity loop?



how is it prevented

a loop that does not have an increment or decrement to stop the loop from runing, or a loop statement with a semicolon after it instead of a brace.. or you forget braces all together



- install an increment or decrement or check your braces

how do you use loops for input validation?

same as an if statement. if the input is valid, it will ignore/skip/breakout of the loop



- if the input is wrong, it will restart the loop and the user needs to be prompted to enter the correct value. This will be stored as the same variable name so that the loop can check for validation



team = keyboard.nextInt();


while (team < 30){


System.out.println("get more players");


team = keyboard.nextInt();


}

What is a "do while" loop?

a loop that will execute its contents at least once before terminating because it checks the boolean expression at the end of the loop



do{


enter = keyboard.nextInt()


}


while (enter < 50);


What is a for loop used for?


Does it check at the beginning of the loop or then end?


can it conduct user validation?

- executing a specified number of times


- it checks at the beginning


- no unless the validation is a number.

what does a for loop look like?



assume you want to count to 5 and:


number =1

for (number = 1, number < 5, number++){


system.out.println(number);


}

can you use user input to assign the number of times a for loop executes?

- yes



stop = keyboard.nextInt();


for (number = 1, number < stop, number++){


system.out.println(number);


}

can you use user input to assign the increment that the for loop counts each iteration?

yes



stop = keyboard.nextInt();


increment = keyboard.nextInt();



for (number = 1; number < stop; increment){


system.out.println(number);


}

can you initialize two or more variables in the for loop?



what about in the test and update sections?

- initialize = yes, use commas to separate



- test = yes but use boolean expressions like && and || to join them



- update = yea, use commas to separate

what an accumulator and how does it work?

an accumulator is a number that continues to have numbers added to it as long as the loop its contained in is executing

how do you use an accumulator in a for loop?


assume you want the total sales for the week



Whats the advantage?


whats the disadvantage?

for (int day = 1; day < 5; day++){


double sales = keyboard.nextDoube();


double totalSales += sales;


}



total sales will be added to 5 times



- advantage is the loop will terminate as soon as the user has gone thru 5 iteratios


how do you use an accumulator in a while loop?


assume you want a running total of points scored in a season



what is a sentinal value


Whats the advantage?


whats the disadvantage?

int points = keyboard.nextInt();



while (points != -1){


int totalPoints += points;


}


totalPoints will be added to each time the user enters a value other than -1. once -1 is entered the loop terminates. -1 is considered a sentinal value



- advantage is you do not need to know the number of games played. Disadvantage is the user needs to know to enter -1 to terminate

Show an example of a nested loop



assume you want to average the test scores for each student



let the user tell you how many students and the number of tests per student

double total = 0.0


int numStud = keyboard.nextDouble();


int numTest = keyboard.nextDouble();



for (int stud = 1; stud < numStud; numStud++){


for (int test = 1; test < numTest; numTest++){


double grade = keyboard.nextDouble();


double total += grade;


}


percentage = total / numTest;


}

How do you import the class that deals with saving files to the disc?

import java.io.*;



this will import the entire .io package

How do you print writing to a file?

- you must make an object from the PrintWriter class and pass a the name of the file you want to create to the constructor. user input can be substituted with keyboard.next double()



- then call the object you created and the method println the same way you'd print to a console



- object.println("writing stuff");

what must follow when you are finished writing to a file



What about reading a file?

objectYouCreated.close();



this takes the information you wrote out of RAM and writes it to the disc. If this is not done it will not be able to be read in the future



ObjectYouCreated.close();

what is a throws exception?


how is it used?


where is it used?

I think logistically it means the main method that uses methods from the .io package can throw an exception/error and this will display it on the console. Without it, it just wont run and we wont know why



- used in the method header to "rethrow" an exception as follows



public static void main(String[] args) throws IOException{



main method starts here


}


this is also to be used on objects and other methods that call this PrintWriter method

How do you add data to an existing file?



assume you want to add data to the file named stuff.txt

FileWriter boobie = new FileWriter(stuff.txt, true)



pass the name of the file you want to add to, a comma and then the word true thru to the constructor. this allows you to reopen the file and add to it

how do you save files to a specific location?



assume you want to save a file named stuff.txt



on the C drive

FileWriter boobie = new FileWriter(C:\\stuff.txt, true)



be sure to add two \\ because a single \ is the start of an escape sequence



or...



FileWriter boobie = new FileWriter(C:/stuff.txt, true)



use a single forward slash

how do you read data from a file?



show all steps, make up your own variable and object names

Scanner keyboard = new Scanner(System.in);


System.out.println("enter a file name: ")


name = keyboard.nextLine()



File myFile = new File(name)


Scanner reading1 = new Scanner(myFile);



String line1 = reading.nextLine();


What line will the nextLine() methods of the scanner class read from?

if you have not read any lines from the file yet, .nextLine() will read the first line in the file, after that it will read each line in the file from top to bottom in succession unless you pass a number through the method which will read the line of the file that the number corresponds to

how do you read all the lines in a file?

use the method .hasNext() as the boolean expression in a while loop. This method returns a value true if there is more to read or a value false if there is nothing left to read



inside the loop:


String friendName = yourObjectName.nextLine();

How can you read numbers from a file?

yourObjectName.nextDouble();



same as your use it to take a value from the user

how can you check whether or not a file exists?

String nameOfFile = keyboard.nextLine()



File objectName = new File(nameOfFile);



if (!objectName.exists()){


System.out.println("That file does not exist");


System.exit(0);


}


objectName.exists() will return a true or false

when should you use .exists() method?

anytime you are going to write a file or add data to an existing file. This method should be used with either a loop or an if statement to protect the files and the smooth flow of the program