Unit 3 Flashcards
What are the operators == and != used for?
determining whether something is equal or inequal (and whether object names/reference variables would refer to the same thing or not via boolean
(ex. code lines
Object n1 = new Object()
and
Object bro = n1
makes bro and n1 refer to the same thing and thus would be ==) )
list the relational operators (numerical):
< Less Than
> Greater Than
<= Less than or equal to
> = Greater than or equal to
== equals
!= not equals
write them like you would say them “less than or equal to” means less than comes first, then equal to always comes after
what would !(c==d) be equal to?
what about !(a>c)
or
!(a && b)?
c!=d and a<=b
!a || !b
what are the cases in which demorgans law would ! a variable?
only with boolean expressions
!(a && b) —–> !a || !b
How would you see if a number is even or odd using a remainder and a relational operator?
number % 2 == 0
true for even, false for not. similar thing for seeing if something is uneven (if you want a true value)
number % 2 > 0
Where is reaminder often used? PLACEHOLDER
Use it to check for odd or even numbers. If num % 2 != 0 is true, num is odd and if num % 2 == 0 is true then num is even.
You can also use remainder to check if any number is evenly divisible by any other: If num1 % num2 == 0 is true then num1 is evenly divisible by num2.
Use it to get the last digit from an integer number: num % 10 gives us the rightmost digit of num.
Use it to get the number of minutes left when you convert a total number of minutes to hours and minutes:
int totalMinutes = 345;
int hours = totalMinutes / 60; // Number of whole hours, i.e. 5
int minutes = totalMinutes % 60; // Number of minutes left over, i.e. 45
Use it whenever you have limit in the value, and you need to wrap around to zero if the value goes over the limit: the value of num % limit will always be in the range from 0 (inclusive) to limit (exclusive) as long as num and limit are both positive.
PLACEHOLDER
What must the keyword if always be followed by in an if statement
a boolean within a parenthesis (boolean expression)
then Do statement;
or
{
Do statement
}
NEVER PUT THE SEMICOLON IN AN IF STATEMENT WITHIN THE same LINE
DO NOT DO
If (expression);
What is a “dangling else” set of code assigned to?
attachedto the closest unassigned if statement (indentation means nada zilch zero) BUT you can have curly braces to separate the ifs and assign the else to a specific, further if statement like so:
if (boolean expression)
{
if (boolean expression)
Do this statement;
}
else // belongs to first if
Do that statement;
must align squiggly braces with header of if expression, everything inside a tab forward, and if its an if… else… else should be aligned with if
true/false
true, though technically a personal style and not NECESSARY, do it for comp sci A you bum
when do you not need squiggly braces for a conditional?
when you have only one line of code, just having indenting is fine (but use squigglies because this might get messy)
define short-circuit evaluation
an order of programming which avoids error, as if a parameter is not met from left to right at the first expression, the program will not continue
Why do you need a “public static” for MOST practice problem methods?
so that the method is accessible/public to different classes, and static means it belongs to all instances of a class/is part of the class
in the code segment
String s1 = new String(“Hello”);
String s2 = new String(“Bye”);
how would you set an alias name for s2, s3?
String s3 = s2;
how do == and .equals() differ when used on strings?
== determines whether or not the names refer to the same OBJECT, whereas .equals() determines whether the VALUE is the same (same vs. equivalent) (cannot use keyword new in the second reference variable)
why compare objects to null?
to see if they exist, and thus not to cause an error when moving further into the conditional (because an object that doesn’t exist cant be compared to anything)