Unit 3 Booleans
Notes and Homework
- 3.1 Boolean Expressions
- 3.2 If Statements and Control Flow
- 3.3 If-Else Statements
- 3.4 Else-If Statements
- 3.5 Compound Boolean Statements
- 3.6 Compound Boolean Expressions
- 3.7 Comparing Objects
- 2019 FRQ 1 Part A
- 2019 Part B
- Quizziz
- Even Numbered Conditional Exercises
3.1 Boolean Expressions
- Represent logical and tell whether something is true or false
- =, !, <, >, >= are different operators
3.2 If Statements and Control Flow
- Perform computation depending on whether a Boolean conditions is true or false
- If Statements: If the statement is true, it will return true; otherwise false
3.3 If-Else Statements
- Run a block of code if there is more than one alternative
3.4 Else-If Statements
- More conditions
- If the first condition is proven false, the other conditions will be evaluated
3.5 Compound Boolean Statements
- Nested if statements: if-statements within if-statement -- allows more flexibility to run the code; if outer if-statement is false, internal if-statement won't be evaluated
- Logical operators: && (and) ll(or) and !(not)
- Short circulated evaluation: the result of a compound Boolean expression can be determined just by looking at a few expressions
3.6 Compound Boolean Expressions
- De Morgan's laws: simplifies Boolean expressions
- !(a&7B) = (!a ll !b)
- !(a ll b) = (!a && !b)
3.7 Comparing Objects
- Use '==' to see if two objects are referring to aliases for the same object
- Use '.equals()' to see if the attributes of two objects are the same
- Ex: vehicle car = new Vehicle("blue," 4); vehicle car2 = new Vehicle ("blue," 4) --> Vehicle blue car = car
- Both the vehicles have the same attributes
boolean cloudy = true;
boolean rainy = false;
if (!cloudy && !rainy) {
System.out.println("Don't forget to bring a hat!");
}
public static int numberOfLeapYears(int year1, int year2)
{
int count = 0;
for (int y = year1; y <= year2; y++) {
if (isLeapYear(y))
{
count++; }
}
return count;
}
public static int dayOfWeek(int month, int day, int year)
{
int startDay = firstDayOfYear(year);
int nthDay = dayOfYear(month, day, year);
int returnDay = (startDay + nthDay - 1) % 7;
return returnDay;
}
import java.util.Scanner;
public class Problem2 {
public static void main(String[] Strings) {
Scanner input = new Scanner(System.in);
System.out.print("What is the coefficient A?: ");
double a = input.nextDouble();
System.out.print("What is the coefficient B?: ");
double b = input.nextDouble();
System.out.print("What is the coefficient C?: ");
double c = input.nextDouble();
double result = b * b - 4.0 * a * c;
if (result > 0.0) {
double r1 = (-b + (b * b - 4 * a * c)/(2*a));
double r2 = (-b - (b * b - 4 * a * c)/(2*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.");
}
}
}
Exercise2.main(null);
public class Problem4 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("number: ");
double number = input.nextDouble();
System.out.println(number);
if (number > 0) {
System.out.println("positive");
} else if (number == 0) {
System.out.println("zero");
} else {
System.out.println("neg");
}
if (number < 1) {
System.out.println("neg");
} else if (number > 1000000) {
System.out.println("too big ");
}
}
}
Problem4.main(null);
import java.util.Scanner;
public class Problem6 {
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Input number: ");
double x = in.nextDouble();
System.out.print("Input another number: ");
double y = in.nextDouble();
x = Math.round(x * 1000);
x = x / 1000;
y = Math.round(y * 1000);
y = y / 1000;
if (x == y)
{
System.out.println("They are the same up to three decimal places");
}
else
{
System.out.println("They are different");
}
}
}
Problem6.main(null);
import java.util.Scanner;
public class Exercise8 {
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Input an alphabet: ");
String input = in.next().toLowerCase();
boolean uppercase = input.charAt(0) >= 65 && input.charAt(0) <= 90;
boolean lowercase = input.charAt(0) >= 97 && input.charAt(0) <= 122;
boolean vowels = input.equals("a") || input.equals("e") || input.equals("i")
|| input.equals("o") || input.equals("u");
if (input.length() > 1)
{
System.out.println("Error. Not a single character.");
}
else if (!(uppercase || lowercase))
{
System.out.println("Error. Not a letter. Enter uppercase or lowercase letter.");
}
else if (vowels)
{
System.out.println("Input letter is Vowel");
}
else
{
System.out.println("Input letter is Consonant");
}
}
}
Exercise8.main(null);
public class Problem10 {
public static void main(String[] args) {
System.out.println("The First 10 Natural Numbers are");
for(int i = 1; i <= 10; i++)
{
System.out.println(i);
}
}
}
Problem10.main(null);
import java.util.Scanner;
public class Problem12 {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int sum = 0;
int avg = 0;
System.out.println("Input the 5 numbers: ");
for(int i=1; i<=5; i++){
int num = input.nextInt();
sum += num;
}
System.out.println("The sum of the given number is " + sum);
avg = sum/5;
System.out.println("The average of the given number is " + avg);
}
}
Problem12.main(null);
public class Problem14 {
public static void main(String[] args) {
int num = 5;
for(int i = 1; i <= 10; ++i)
{
System.out.printf("%d * %d = %d \n", num, i, num * i);
}
}
}
Problem14.main(null);
public class Problem16 {
public static void main(String[] args) {
System.out.print("rows: ");
Scanner input = new Scanner(System.in);
int num = input.nextInt();
System.out.println(num);
for (int i = 1; i <= num; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(j);
}
System.out.println();
}
}
}
Problem16.main(null);
import java.util.Scanner;
public class Exercise18 {
public static void main(String[] args)
{
int i,j,n,k=1;
System.out.print("Input number of rows : ");
Scanner in = new Scanner(System.in);
n = in.nextInt();
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
System.out.print(k++);
System.out.println("");
}
}
}
Exercise18.main(null);
import java.util.Scanner;
public class Main {
public static void main(String[] args)
{
int numberOfRows;
System.out.print("Input number of rows : ");
Scanner in = new Scanner(System.in);
numberOfRows = in.nextInt();
int number = 1;
for (int row = 1; row <= numberOfRows; row++)
{
for (int column = 1; column <= row; column++)
{
System.out.print(number + " ");
number++;
}
System.out.println();
}
}
}
Main.main(null);