- Collapse Code
public class BooleansAndConditionals {
public static void main(String[] args) {
// Creates a special type of object that
// generates random values of various types
java.util.Random rGen = new java.util.Random();
final boolean b1 = rGen.nextBoolean();
final boolean b2 = rGen.nextBoolean();
// Notice the variable names: _a, _b, _c, etc.
// NOT a, b, c, etc.
final int _a = rGen.nextInt() / 3;
final int _b = rGen.nextInt() / 3;
final int _c = rGen.nextInt() / 3;
final int _d = rGen.nextInt() / 3;
final int _e = rGen.nextInt() / 3;
System.out.println("b1 is " + b1);
System.out.println("b2 is " + b2);
System.out.println("_a is " + _a);
System.out.println("_b is " + _b);
System.out.println("_c is " + _c);
System.out.println("_d is " + _d);
System.out.println("_e is " + _e);
System.out.println();
/////////////////////////////////////////////
//
// PART 1: BOOLEAN EXPRESSION PRACTICE
// Place your line of code UNDERNEATH the
// instruction comment. Follow the format I
// provide for you here, with the expression
// as part of the String literal and in parentheses
// after the plus sign for concatenation
//
/////////////////////////////////////////////
// EXAMPLE
// Write an expression that is true if
// b1 and b2 are BOTH true
System.out.println("b1 && b2 is " + (b1 && b2));
// Write an expression that is true if
// EITHER b1 OR b2 (or both) is true
// Write an expression that is true if
// b1 and b2 are BOTH false
// Write an expression that is true if _a
// is GREATER THAN _b
// Write an expression that is true if _c
// and _e are NOT EQUAL
// Write an expression that is true if _d is GREATER THAN
// OR EQUAL TO the sum of _a and _c
// Write an expression that is true if the following conditions
// BOTH apply:
// _c and _e are NOT EQUAL
// _d is GREATER THAN OR EQUAL TO the sum of _a and _c
System.out.println();
/////////////////////////////////////////////
//
// PART 2: BASIC IF/ELSE STATEMENTS
//
//
//
/////////////////////////////////////////////
// EXAMPLE
// if b1 is true, print out: b1 is true!
if (b1)
System.out.println("b1 is true!");
// if b2 is true, print out: b2 is true!
// if b1 and b2 are true, print out:
// b1 and b2 are BOTH true!
// if at least one of b1 and b2 is true, print out:
// At least one, b1 or b2, is true!
// Otherwise, print out: b1 and b2 are BOTH false!
System.out.println();
/////////////////////////////////////////////
//
// PART 3: NESTED IF/ELSE STATEMENTS
// See example code below
//
/////////////////////////////////////////////
// if _a is less than _b, print out:
// _a is less than _b
// if _a is less than _c as well, also print out:
// _a is less than _c, too!
// Otherwise, print out:
// _a may be less than _b, but it is greater than or equal to _c
//
// However, if _a is NOT less than _b, print out:
// _a is greater than or equal to _b, but we don't know its relation to c
// In addition, if _a is less than _d, print out:
// _a is less than _d
// YOUR CODE GOES HERE
/*
*
* EXAMPLE CODE
*
* if _d is greater than or equal to _e, print out:
* _d is greater than or equal to _e
* if _d is also less than or equal to _c, also print out:
* _d is also less than or equal to _c
*
* However, if _d is less than _e, print out:
* _d is less than _e
* In addition, if _d is greater than _b, also print out:
* ...but _d is greater than _b
* Otherwise, also print out:
* ...and _d is less than or equal to _b
*
if (_d >= _e) {
System.out.println("_d is greater than or equal to _e");
if (_d <= _c)
System.out.println("_d is also less than or equal to _c");
}
else {
System.out.println("_d is less than _e");
if (_d > _b)
System.out.println("...but _d is greater than _b");
else
System.out.println("...and _d is less than or equal to _b");
}
*/
}
}
- Collapse Code
+ Expand Code
public class BooleansAndConditionals {
public static void main(String[] args) {
// Creates a special type of object that
// generates random values of various types
java.util.Random rGen = new java.util.Random();
final boolean b1 = rGen.nextBoolean();
final boolean b2 = rGen.nextBoolean();
// Notice the variable names: _a, _b, _c, etc.
// NOT a, b, c, etc.
final int _a = rGen.nextInt() / 3;
final int _b = rGen.nextInt() / 3;
final int _c = rGen.nextInt() / 3;
.........
Your task, then, is to follow the directions in the comments in order to make changes to the code that will result in the desired output being printed to the screen.
- Write the println statements that print the expression itself and whether it is true or false.
- Write the simple if and if/else statements
- Write the nested if/else statements
Note that because b1 and b2 are being assigned true or false at random, while the int variables are also being assigned random values, the output will look different each time you run the program. To this end, I have provided multiple examples of how the output could look. (Important: The sample output only includes blanks because otherwise would be basically giving you the answer. Your own output should have the expressions, not the blanks!)
Program output examples:
- Collapse Output
b1 is false
b2 is false
_a is -592100251
_b is -1864093001
_c is 1353704127
_d is -1617942246
_e is -2024303118
b1 && b2 is false
________ is false
________ is true
________ is false
________ is true
________ is false
________ is false
b1 and b2 are BOTH false!
_a is greater than or equal to _b, but we don't know its relation to c
- Collapse Output
b1 is false
b2 is true
_a is 1714693865
_b is 862279340
_c is -1357093493
_d is -614830788
_e is -815112975
b1 && b2 is false
________ is true
________ is false
________ is false
________ is true
________ is false
________ is false
b2 is true!
At least one, b1 or b2, is true!
_a is greater than or equal to _b, but we don't know its relation to c
- Collapse Output
b1 is true
b2 is true
_a is -114437699
_b is 431315496
_c is -50728209
_d is 1141196985
_e is 1674642823
b1 && b2 is true
________ is true
________ is false
________ is true
________ is true
________ is true
________ is true
b1 is true!
b2 is true!
b1 and b2 are BOTH true!
At least one, b1 or b2, is true!
_a is less than _b
_a is less than _c, too!
- Collapse Output
b1 is false
b2 is false
_a is -61204695
_b is 1896749448
_c is -1397882590
_d is -5069015
_e is -912494750
b1 && b2 is false
________ is false
________ is true
________ is true
________ is true
________ is true
________ is true
b1 and b2 are BOTH false!
_a is less than _b
_a may be less than _b, but it is greater than or equal to _c
- Collapse Output
b1 is true
b2 is true
_a is 1164092340
_b is -1021865847
_c is 1116250175
_d is 320246110
_e is -1161507817
b1 && b2 is true
________ is true
________ is false
________ is false
________ is true
________ is true
________ is true
b1 is true!
b2 is true!
b1 and b2 are BOTH true!
At least one, b1 or b2, is true!
_a is greater than or equal to _b, but we don't know its relation to c
- Collapse Output
b1 is true
b2 is false
_a is -604635712
_b is 2097800331
_c is -2020228767
_d is -290257524
_e is 1522253840
b1 && b2 is false
________ is true
________ is false
________ is true
________ is true
________ is false
________ is false
b1 is true!
At least one, b1 or b2, is true!
_a is less than _b
_a may be less than _b, but it is greater than or equal to _c
In at least 150 words, answer the following questions: - How did the process of creating these programs go for you?
- What were your main challenges and how did you overcome them?
- What did you learn that may be of use as you move along in this class?