- Collapse Code
import java.util.Scanner;
public class BooleansAndConditionals {
public static void main(String[] args) {
// Creates a Scanner object that grabs
// user input of various types
Scanner scan = new Scanner (System.in);
final boolean b1;
final boolean b2;
// FILL IN CODE TO INITIALIZE boolean VARIABLES!
// Notice the variable names: _a, _b, _c, etc.
// NOT a, b, c, etc.
final int _a;
final int _b;
final int _c;
final int _d;
final int _e;
// FILL IN CODE TO INITIALIZE int VARIABLES!
// Prints user-submitted values to confirm...
System.out.println();
System.out.println("USER-SUBMITTED VALUES:\n");
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 (but NOT both!) is true
// Write an expression that is true if
// EITHER b1 or b2 (or both) is false
// Write an expression that is true if _e
// is LESS THAN OR EQUAL to _c
// Write an expression that is true if _a
// and _d are EQUAL
// Write an expression that is true if the
// sum of _a and _c is GREATER THAN _c
// Write an expression that is true if *neither* of
// the following conditions apply:
// _a and _d are EQUAL
// the sum of _a and _c is GREATER THAN _c
System.out.println();
/////////////////////////////////////////////
//
// PART 2: BASIC IF/ELSE STATEMENTS
//
// NOTE: An "else" is only necessary if an
// alternative is explicitly called for.
//
/////////////////////////////////////////////
// EXAMPLE
// if b1 is true, print out: b1 is true!
if (b1)
System.out.println("b1 is true!");
// if b2 is false, print out: b2 is false!
// if b1 and b2 are both false, print out:
// b1 and b2 are BOTH false!
// if at least one of b1 and b2 is false, print out:
// At least one, b1 or b2, is false!
// Otherwise, print out: b1 and b2 are BOTH true!
System.out.println();
/////////////////////////////////////////////
//
// PART 3: NESTED IF/ELSE STATEMENTS
// See example code below for an illustration
// of how to translate my comments into actual
// conditional code. It will give you clues
// as to how different parts should be nested.
//
/////////////////////////////////////////////
// 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 less 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
import java.util.Scanner;
public class BooleansAndConditionals {
public static void main(String[] args) {
// Creates a Scanner object that grabs
// user input of various types
Scanner scan = new Scanner (System.in);
final boolean b1;
final boolean b2;
// FILL IN CODE TO INITIALIZE boolean VARIABLES!
// Notice the variable names: _a, _b, _c, etc.
// NOT a, b, c, etc.
final int _a;
final int _b;
final int _c;
.........
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.
- Prompt user for values of the appropriate type. EXAMPLE:
Enter a value for b1 (true or false): true
Enter a value for b2 (true or false): false
Enter a value for _a (integers ONLY!): 123
Enter a value for _b (integers ONLY!): 456
Enter a value for _c (integers ONLY!): 789
Enter a value for _d (integers ONLY!): -92
Enter a value for _e (integers ONLY!): 543
- 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.
Program output examples:
- Collapse Output
b1 is false
b2 is true
_a is 1082991299
_b is -1748290592
_c is 815057372
_d is -550864138
_e is 1187816457
__________ is false
__________ is true
__________ is true
__________ is false
__________ is false
__________ is true
__________ is false
At least one, b1 or b2, is true!
_e is greater than _c
_d may be greater than _c, but it is less than or equal to _e
- Collapse Output
b1 is true
b2 is true
_a is -1959653677
_b is 1369738698
_c is -434284996
_d is -1314716466
_e is -1673714520
__________ is true
__________ is false
__________ is false
__________ is true
__________ is false
__________ is true
__________ is false
b1 is true!
b1 and b2 are BOTH true!
_e is less than or equal to _c, but we don't know its relation to _d
_a is greater than _d
- Collapse Output
b1 is false
b2 is true
_a is -538734624
_b is -773066808
_c is 567714183
_d is -136900294
_e is -1004574335
__________ is false
__________ is true
__________ is true
__________ is true
__________ is false
__________ is false
__________ is false
At least one, b1 or b2, is true!
_e is less than or equal to _c, but we don't know its relation to _d
- Collapse Output
b1 is false
b2 is false
_a is 1659516242
_b is -689964160
_c is 468601025
_d is -1988775427
_e is -1377671121
__________ is false
__________ is false
__________ is true
__________ is true
__________ is false
__________ is true
__________ is false
b2 is false!
b1 and b2 are BOTH false!
At least one, b1 or b2, is true!
_e is less than or equal to _c, but we don't know its relation to _d
_a is greater than _d
- Collapse Output
b1 is true
b2 is true
_a is 1625886004
_b is -304612964
_c is -1446488784
_d is -1424436873
_e is -1337590156
__________ is true
__________ is false
__________ is false
__________ is false
__________ is false
__________ is true
__________ is false
b1 is true!
b1 and b2 are BOTH true!
_e is greater than _c
_d is greater than _c, too!
- Collapse Output
b1 is true
b2 is true
_a is 1911850627
_b is -1233621380
_c is 2027473958
_d is -1393115422
_e is -2082000467
__________ is true
__________ is false
__________ is false
__________ is true
__________ is false
__________ is false
__________ is false
b1 is true!
b1 and b2 are BOTH true!
_e is less than or equal to _c, but we don't know its relation to _d
_a is greater than _d
Question: How did the process of editing this program 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?