Unit 3 Blog Booleans
if statements are conditionals that if the conditional is true, then the code inside the if statement is ran.
if(true) {
System.out.print("true");
}
if else statements are like if statements, just with another block of code that executes if the if conditional is not met
if(false) {
System.out.print("true");
}
else {
System.out.print("false");
}
else if statements are just adding more if conditionals, so if those are met, they execute
String name = "samuel";
if(name=="Samuel") {
System.out.print("SAMUEL");
}
else if(name=="samuel") {
System.out.print("samuel");
}
else {
System.out.print("neither");
}
switch statements are like else if statements, just replaced with case instead
int num = 2;
switch (num) {
case 1:
System.out.print("1");
break;
case 2:
System.out.print("2");
break;
default:
System.out.print("neither");
}
many booleans can be used in if else statements to make the conditional
if (2==2){
System.out.println("true");
}
if (2!=3){
System.out.println("true");
}
if (2<3){
System.out.println("true");
}
if (2<=2){
System.out.println("true");
}
- De morgan's law uses not (!) and shows what happens when using multiple operators with this
- the complement of the union of two sets A and B is equal to the intersection of the complement of the sets A and B
- !(a && b) is equivalent to !a || !b
- !(a || b) is equivalent to !a && !b
if (!(true && false) == ((!true)||(!false))) {
System.out.print("these statements are equivalent");
}
else {
System.out.print("not the same");
}
- this includes using the &&, ||, and ! operators,
public class TestNum
{
public static void main(String[] args)
{
int score = 10;
if (score < 0 || score > 100)
{
System.out.println("Score has an illegal value.");
}
if (score >= 0 && score <= 100)
{
System.out.println("Score is in the range 0-100");
}
if (score != 10) {
System.out.println("pick a different score");
}
}
}
TestNum.main(null)
- define boolean function by choosing value for each possible value of arguments, use truth table to do so
a)
public static int numberOfLeapYears(int year1, int year2) {
int cnt = 0;
for (int y=year1; y<=year2; y++) {
if (isLeapYear(y)) {
cnt++;
}
}
return cnt;
}
b)
public static int dayOfWeek(int month, int day, int year) {
int firstDay = firstDayOfYear(year);
int doy = dayOfYear(month, day, year);
return (firstDay + doy - 1) % 7;
}
2
public class main {
public static void main(String[] Strings) {
double a = 1;
double b = 3;
double c = 2;
double result = b * b - 4.0 * a * c;
if (result > 0.0) {
double r1 = (-b + Math.pow(result, 0.5)) / (2.0 * a);
double r2 = (-b - Math.pow(result, 0.5)) / (2.0 * a);
System.out.println("The roots are " + r1 + " and " + r2);
} else if (result == 0.0) {
double r1 = -b / (2.0 * a);
System.out.println("The root is " + r1);
} else {
System.out.println("The equation has no real roots.");
}
}
}
main.main(null)
4
public class main {
public static void main(String[] Strings) {
double a = 1;
if (a > 0.0) {
System.out.println("positive number");
} else {
System.out.println("negative number");
}
}
}
main.main(null)
6
public class main {
public static void main(String[] Strings) {
double a = 55.1321;
double b = 55.132;
a = Math.round(a * 1000);
b = Math.round(b * 1000);
if (a == b) {
System.out.println("same");
} else {
System.out.println("different");
}
}
}
main.main(null)
8
public class main {
public static void main(String[] Strings) {
String a = "a";
boolean vowels = a.equals("a") || a.equals("e") || a.equals("i") || a.equals("o") || a.equals("u");
if (a.length() > 1)
{
System.out.println("not one letter");
}
else if (vowels)
{
System.out.println("vowel");
}
else
{
System.out.println("consonant");
}
}
}
main.main(null)
10
public class main {
public static void main(String[] args) {
int i;
for (i=1;i<=10;i++) {
System.out.println (i);
}
}
}
main.main(null)
12
public class main {
public static void main(String[] args) {
int a = 3;
int b = 4;
int c = 1;
int d = 7;
int e = 3;
double sum = a + b + c + d + e;
double avg = sum/5;
System.out.println(sum);
System.out.println(avg);
}
}
main.main(null)
14
public class main {
public static void main(String[] args) {
int a = 4;
int b = 3;
int i;
for(i = 0; i <= b; i++) {
int c = a * i;
System.out.println(a + " * " + i + " = " + c);
c = 0;
}
}
}
main.main(null)
16
public class main {
public static void main(String[] args) {
int a;
int b;
int r = 5;
for(a=1;a<=r;a++) {
for(b=1;b<=a;b++)
System.out.print(b);
System.out.println("");
}
}
}
main.main(null);
18
public class main {
public static void main(String[] args) {
int a,b,c=1;
int n = 4;
for(a=1;a<=n;a++)
{
for(b=1;b<=a;b++)
System.out.print(c++);
System.out.println(" ");
}
}
}
main.main(null);
20
public class main {
public static void main(String[] args) {
int a,b,c=1;
int n = 5;
for(a=1;a<=n;a++)
{
for(b=1;b<=a;b++)
System.out.print(" " + c++);
System.out.println(" ");
}
}
}
main.main(null);
String[] names = new String[] {"samuel", "kian", "samuel", "bob"};
for(int i = 0; i < names.length; i++ ) {
for(int j = 0; j < names.length; j++) {
if(names[i]==names[j]) {
System.out.println(names[j]);
}
}
}