-
-
Save rumaisaabdulhai/9bd5c253ec65adfd77c7eebd02912fdd to your computer and use it in GitHub Desktop.
Week 3 Classwork Solutions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* This class contains the solutions to | |
* the Week 3 classwork exercises. | |
*/ | |
public class WeekThreeCW { | |
public static void main(String[] args) { | |
/* Problem 1: Using Relational Operators in Conditional Statements | |
------------------------------------------------------------------ | |
Write a program that does the following: | |
- Create a variable called temperature that stores any number. | |
- If the temperature is greater than 30, then print “Let’s go swim” | |
- If the temperature is less than 30, then print “The pool is too cold” | |
- Hint: you will need to use a relational operator and a conditional statement | |
*/ | |
System.out.println("\nClasswork Problem 1:\n"); // to separate Problems | |
double temp1 = 35; | |
double temp2 = 29.9; | |
double temperature = temp1; // replace with temp2 to see what happens | |
// if the temp is greater than 30 | |
if (temperature > 30) { // Using the ">" (greater than) relational operator | |
System.out.println("\tLet's go swim"); | |
} | |
// anything else - the temp is less than 30 | |
else { | |
System.out.println("\tThe pool is too cold"); | |
} | |
/* Problem 2: Using Logical Operators in Conditional Statements | |
--------------------------------------------------------------- | |
Write a program that does the following: | |
- If I am 18 or older OR if I am at least 17 with a 100 as my grade | |
print “I can go to college” | |
- If neither of those is met, print “I can’t go to college” | |
- Hint: Use two int variables, one for age and one for grade! | |
*/ | |
System.out.println("\nClasswork Problem 2:\n"); // to separate Problems | |
int a1 = 20; // examples for age | |
int a2 = 17; | |
double g1 = 85.4; // examples for grade | |
double g2 = 100; | |
int age = a1; // replace with a2 to see what happens | |
double grade = g1; // replace with g2 to see what happens | |
// if age is at least 18 OR if age is at least 17 AND grade equals 100 | |
if (age >= 18 || (age >= 17 && grade == 100)) { // Using the && (and) logical operator | |
System.out.println("\tI can go to college"); | |
} | |
// anything else - student's age is less than 17 OR | |
// the student's age is 17 with a grade that is not 100 | |
else { | |
System.out.println("\tI can't go to college"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment