-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWeek02BooleanConditionalsLoopsLab.java
More file actions
212 lines (168 loc) · 6.71 KB
/
Week02BooleanConditionalsLoopsLab.java
File metadata and controls
212 lines (168 loc) · 6.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
//
// Copyright (c) 2023 Promineo Tech
// Author: Promineo Tech Academic Team
// Subject: Boolean & Conditionals Lab
// Java Week 02 Lab
//
package week02;
public class Week02BooleanConditionalsLoopsLab {
public static void main(String[] args) {
//
// BOOLEANS and CONDITIONALS:
//
// 1. Variable Declaration:
// a. Create a variable named age and assign it a value of 14
int age = 14;
// 2. Print a Boolean Expression:
// a. Print the boolean expression age >= 16 to the console and note the results.
// a. Change the value of age to 18 and print again.
System.out.println(age >= 16);
age = 18;
System.out.println(age >= 16);
// 3. Can you drive?
// a. Using a conditional, print one of the following:
// i. "You can drive" if age is greater than or equal to 16
// ii. "You cannot drive" otherwise
//
// a. Change the value of age and rerun to see the result
if(age >= 16) {
System.out.println("You can drive");
} else {System.out.println("You cannot drive");}
age = 14;
if(age >= 16) {
System.out.println("You can drive");
} else {System.out.println("You cannot drive");}
// 4. Update Solution to Question 3 as follows:
// a. Add a new variable called hasLicense before the conditional.
// b. Change the boolean expression in the conditional to additionally
// include the need for hasLicense to be true.
// c. Try changing the values of age and hasLicense and note the different results.
boolean hasLicense = true;
if(age >= 16 && hasLicense == true) {
System.out.println("You can drive");
} else {System.out.println("You cannot drive");}
age = 18;
if(age >= 16 && hasLicense == true) {
System.out.println("You can drive");
} else {System.out.println("You cannot drive");}
hasLicense = false;
if(age >= 16 && hasLicense == true) {
System.out.println("You can drive");
} else {System.out.println("You cannot drive");}
// 5. Milk?
// a. Create two new variables - costOfMilk and thirstLevel
// b. Create a new conditional that prints "Milk Please" if costOfMilk is less than 2.50
// or if thirstLevel is greater than 6 and prints "No Thanks" otherwise.
// c. Change the values and note the different results.
double costOfMilk = 1;
int thirstLevel = 2;
if(costOfMilk < 2.5 || thirstLevel >6) {
System.out.println("Milk Please");}
else {System.out.println("No Thanks");
}
// 6. Cookie Distribution:
// Note: You will evenly distribute all of the cookies to the children
// and as the adult you get to keep the remaining cookies for yourself.
//
// a. Create two variables called numberOfCookies and numberOfChildren.
// b. Initialize the two variables to integer values.
// b. Use a conditional to print the following based on the following conditions:
// i. If there are 0 cookies remaining, print "Sad Face"
// ii. If there are less than 2 cookies, print "Yes!"
// iii. If there are less than 5 cookies, print "Whoohoooo!"
// iv. If there are 5 or more cookies, print "Jackpot!"
int numberOfCookies = 18;
int numberOfChildren = 6;
int remainingCookies = numberOfCookies % numberOfChildren;
if (remainingCookies == 0) {
System.out.println("There are " + remainingCookies + " cookies left over....");
System.out.println("Sad Face");}
else if (remainingCookies < 2) {
System.out.println("There are " + remainingCookies + " cookies left over....");
System.out.println("Yes");}
else if (remainingCookies < 5) {
System.out.println("There are " + remainingCookies + " cookies left over....");
System.out.println("Whoohoooo");}
else if (remainingCookies >= 5) {
System.out.println("There are " + remainingCookies + " cookies left over....");
System.out.println("Jackpot!");
}
// 7. Loyalty Member Program:
// a. Create a variable called loyaltyMemberStatus and assign the value "SILVER"
// b. Create a variable called loyaltyMemberDiscount and assign the value 0.0
// c. Using a switch, set the value of loyaltyMemberDiscount based on
// the following loyaltyMemberStatus scale:
// i. "SILVER" is 0.10
// ii. "GOLD" is 0.15
// iii. "PLATINUM" is 0.25
String loyaltyMemberStatus = "GOLD";
double loyaltyMemberDiscount = 0.0;
switch (loyaltyMemberStatus) {
case "SILVER":
loyaltyMemberDiscount = .10;
break;
case "GOLD":
loyaltyMemberDiscount = .15;
break;
case "PLATINUM":
loyaltyMemberDiscount = .25;
break;
}
System.out.println(loyaltyMemberStatus + " " + loyaltyMemberDiscount);
// 8. Using the Loyalty Member Program variables from Question 7, do the following:
// a. Create a variable called billTotal and assign a value
// b. Create a variable called adjustedTotal and assign it the billTotal minus
// the loyaltyMemberDiscount percent of the billTotal
// c. If the adjustedBillTotal is greater than $500 upgrade the
// loyaltyMemberStatus from SILVER to GOLD or from GOLD to PLATINUM
double billTotal = 600;
double adjustedBillTotal = billTotal - billTotal * loyaltyMemberDiscount;
if(adjustedBillTotal > 500) {
if(loyaltyMemberStatus == "SILVER") {
loyaltyMemberStatus = "GOLD";
System.out.println("Your new Member Status is " + loyaltyMemberStatus);
} else if(loyaltyMemberStatus == "GOLD") {
loyaltyMemberStatus = "PLATINUM";
System.out.println("Your new Member Status is " + loyaltyMemberStatus);
}
} else {System.out.println("Your Member Status has not changed");}
// 9. Login -- username & password:
// a. Create two variables, username and password
// b. Create a conditional that prints one of the following:
// i. "login successful" if the username is "Tommy123" and the password is "12345"
// ii. "access denied" otherwise
String username = "Tommy123";
String password = "12345";
if (username.equals("Tommy123") && password.equals("12345")) {
System.out.println("login successful");
} else {System.out.println("access denied");}
//
// LOOPS:
//
// 10. Write a for loop that prints each number from 0 to 9
for (int i = 0; i<10; i++) {
System.out.println(i);
}
// 11. Write a for loop that prints each number from 10 to 0 backwards
for (int i = 10; i>-1; i--) {
System.out.println(i);
}
// 12. Write a for loop that prints every other number from 0 to 100
for (int i = 0; i<101; i += 2 ) {
System.out.println(i);
}
// 13. Write a for loop that iterates from 0 to 100 and prints
// "EVEN" if the number is even and "ODD" if it's odd
for (int i = 0; i<101; i++ ) {
if(i%2 == 0) {System.out.println(i + " EVEN");
} else {System.out.println(i + " ODD");}
}
// 14. Write a while loop that starts at 100 and iterates backwards by 1 until it reaches 0
// within the loop, divide each number by 3 and print the remainder to the console.
int i = 101;
while (i > 0) {
i--;
System.out.println(i%3);
}
}
}