Unit+2


 * Unit 2

What is a boolean?**

An evaluation of a statement (normally containing a comparison) that returns either true or false. Primitive type that takes up one bit of space. You can have a boolean variable:

boolean b = true;

> < >= <= == !=
 * What are the 6 mathematical comparisons that can be used in a boolean statement?**

A '!' in a boolean means 'not' ex. != is 'not equals' !(boolean statement) --> reverses the statement. If the statement is true, the ! makes it false and v/v.
 * What is the meaning of "!" in a boolean**


 * Explain && and || in terms of compound booleans and give an example of their use**

&& (AND) only evaluates as true if BOTH statements are true.
 * (OR) evaluates as true if EITHER of the statements are true.

ex.) 5 > 4 && 5 > 6 //evaluates as FALSE because even though the first statement is true, the second is not, and && requires both.

5 > 4 || 5 > 6// evaluates as TRUE because at least one of the statements is true. Will be true even if the other statement is false.

Short circuit evaluation is when the computer fails to evaluate the second part of a boolean statement because the first part determines definitively whether it is true or false.
 * Define "short circuit evaluation"**

Ex) a compound boolean || statement, if the first statement is T, it WILL NOT evaluate the second statement. Classic AP question involves the 2nd half of a compound statement containing an arrithmetic error that will not occur because it isn't evaluated.

De Morgan's Law gives rules for distribution of a '!' applied to a whole compound boolean statement... !(A && B) ...the '!' is distributed throughout and the sign switches.* !A || !B
 * Define and give an example of De Morgan's Law**
 * && becomes ||; || becomes &&


 * A method that returns a boolean is a __predicate__ method. Write an example of a predicate method that takes a single string parameter and returns true if the length of the string is greater than 10.**

public boolean stringIsLong(String test) { int len = test.length; if(len > 10) return true; else return false; }

SHORTER VERSION { return test.length > 10; }

.compareTo; //returns an int. 0 if the strings are the same. Positive number if string1 precedes string2 lexicographically. Negative number if string1 comes after string2 lexicographically. Lexicographic order: space, numbers, Capital letters, lowercase letters .equalsIgnoreCase;// returns true of false .equals; //returns true or false
 * What are 3 methods that you can use to compare strings? Give an example of the use of each.**

string1.methodname(string2);

What happens if you say String1 == String2? It checks the memory space. Don't use it!!!

You can generate a random number using the Random class or Math.random ex. Random class import java.util.Random; ... Random num= new Random; ... int x = num.nextInt(int desiredNumber);*//
 * What are two ways to generate a random number? Give an example of each.**
 * the parameter uses the inputed number as the highest integer of possible numbers. ex. (2) => will give 0 or 1

ex. Math.random* num= (int) ((Math.random*10)+1);
 * Math.random will generate a double between 0.0000... to 0.99999...; to generate a whole number between 1 and 10, you must multiply Math.random by 10 then add 1, then type cast the number.

To get a number between 1 and a integer n:// Math.random * n + 1. Then type cast to an int.


 * Write a while loop that generates and prints x ^ 1 through x ^ 10.**

int num = 1; int x;

while (num <= 10) {System.out.println(Math.pow(x, num)); num++;}


 * Write a for loop that generates and prints x ^ 10 through x ^ 1 in that order.**

for(int num = 10; num >= 1; num--) { System.out.println(Math.pow(x,num)); }


 * String method indexOf**


 * int x = string1.indexOf(string2)**

If string2 is part of string1, it returns the index where the first instance of that string starts. If string2 isn't present, it returns -1.


 * See also Unit 2 Practice Problem Sheet.**