Learnings

  • De Morgan's laws: Help simplify Boolean expressions
  • !(a&&b) = (!a || !b)
  • !(a || b) = (!a && !b)
  • If-else statements allow for more conditions to be defined
  • Else-if statements allow for more conditions to be defined

De Morgan's Law

  • shows how we can remove an && statement or remove an || statement. It's the way of creating equivalent statements by switching the && or || and distributing the ! sign if you need.

Compound Boolean Expression

  • Combinations of boolean operators
  • Can be and (&&), or (||), not (!), as well as parenthesis for grouping purposes
// First demonstration
System.out.println("First Demonstration:");
boolean lights = true;
boolean fan = true;

// first way
if(!(lights && fan)){
    System.out.println("Something is off!");
}

// second way
if(!lights || !fan){
    System.out.println("Something is off!");
}

// if one of the variables is set to true
else{
    System.out.println("Everything is on!");
}



// Second demonstration
System.out.println("Second Demonstration:");
boolean sink = false;
boolean shower = false;

// first way
if(!(sink || shower)){
    System.out.println("Everything is off!");
}

// second way
if(!sink && !shower){
    System.out.println("Everything is off!");
}

else{
    System.out.println("Something is on!");
}



// third demonstration
System.out.println("Third Demonstration: ");
int num1 = 3;
int num2 = 10;

// first way
System.out.println(!(num1 == 5 && num2 != 5));
// second way
System.out.println(num1 != 5 || num2 == 5);


// Weird and confusing
System.out.println("Overly complicated for no reason:");
if((lights || !fan) || (sink && shower)){
    System.out.println("Either lights are on or fan is off, or sink and shower are on.");
}

Truth Tables

  • Used to see values of boolean expressions
  • A truth table has one column for each variable, one row for each possible combination of variable values, and a column that specifies the value of the function for that combination. 0 | 0 | 0 | 1 | 0 | 1 | ### OR X | 0 | 1 | 0 | 0 | 1 | 1 | 1 | 1 |
// FRQ PART A

public static int numberOfLeapYears(int year1, int year2){
    int numLeapYears = 0;
    for(int i = year1; i <= year2; i++){
        if(isLeapYear(i)){
            numLeapYears++;
        }
    }
    return numLeapYears;
}
// FRQ PART B

public class APCalendar{
    /** Returns true if year is a leap year and false otherwise. */
    private static boolean isLeapYear(int year)
    { /* implementation not shown */ }
    /** Returns the number of leap years between year1 and year2, inclusive.
     * Precondition: 0 <= year1 <= year2
     */
    public static int numberOfLeapYears(int year1, int year2){
        int numLeapYears = 0;
        for(int i = year1; i <= year2; i++){
            if(isLeapYear(i)){
                numLeapYears++;
            }
        }
        return numLeapYears;
    }
    /** Returns the value representing the day of the week for the first day of year,
     * where 0 denotes Sunday, 1 denotes Monday, ..., and 6 denotes Saturday.
     */
    private static int firstDayOfYear(int year)
    { /* implementation not shown */ }
    /** Returns n, where month, day, and year specify the nth day of the year.
     * Returns 1 for January 1 (month = 1, day = 1) of any year.
     * Precondition: The date represented by month, day, year is a valid date.
     */
    private static int dayOfYear(int month, int day, int year)
    { /* implementation not shown */ }
    /** Returns the value representing the day of the week for the given date
     * (month, day, year), where 0 denotes Sunday, 1 denotes Monday, ...,
     * and 6 denotes Saturday.
     * Precondition: The date represented by month, day, year is a valid date.
     */
    public static int daysOfWeek(int month, int day, int year){
        int firstDay = firstDayOfYear(year);
        int nth = dayOfYear(month, day, year);
        int returnDay = ((firstDay + nth) -1) % 7;
        return returnDay;
    }
    // There may be instance variables, constructors, and other methods not shown.
}

CONDITIONALS

//2 

import java.util.Scanner;
public class Exercise2 {

    
  public static void main(String[] Strings) {

        Scanner input = new Scanner(System.in);

            System.out.print("Input a: ");
            double a = input.nextDouble();
            System.out.print("Input b: ");
            double b = input.nextDouble();
            System.out.print("Input c: ");
            double c = input.nextDouble();

            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.");
            }

    }
}
Exercise2.main(null);
Input a: Input b: Input c: The equation has no real roots.
//4

import java.util.Scanner;
public class Exercise4 {

    
  public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        System.out.print("Input value: ");
        double input = in.nextDouble();

        if (input > 0)
        {
            if (input < 1)
            {
                System.out.println("Positive small number");
            }
            else if (input > 1000000)
            {
                System.out.println("Positive large number");
            }
            else
            {
                System.out.println("Positive number");
            }
        }
        else if (input < 0)
        {
            if (Math.abs(input) < 1)
            {
                System.out.println("Negative small number");
            }
            else if (Math.abs(input) > 1000000)
            {
                System.out.println("Negative large number");
            }
            else
            {
                System.out.println("Negative number");
            }
        }
        else
        {
            System.out.println("Zero");
        }
    }
}
Exercise4.main(null);
Input value: Positive number
//6

import java.util.Scanner;
public class Exercise6 {

    
  public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);

        System.out.print("Input floating-point number: ");
        double x = in.nextDouble();
        System.out.print("Input floating-point 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");
        }
    }
}
Exercise6.main(null);
Input floating-point number: Input floating-point another number: They are different
//8

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);
Input an alphabet: Input letter is Consonant
//10

public class Exercise10 {
    
    public static void main(String[] args)
      {     
      int i;
      System.out.println ("The first 10 natural numbers are:\n");
      for (i=1;i<=10;i++)
      {      
          System.out.println (i);
      }
  System.out.println ("\n");
  }
  }
Exercise10.main(null);
The first 10 natural numbers are:

1
2
3
4
5
6
7
8
9
10


//12

import java.util.Scanner;
public class Exercise12 {

    
  public static void main(String[] args)

{       
    int i,n=0,s=0;
	double avg;
	{
	   
        System.out.println("Input the 5 numbers : ");  
         
	}
		for (i=0;i<5;i++)
		{
		    Scanner in = new Scanner(System.in);
		    n = in.nextInt();
		    
  		s +=n;
	}
	avg=s/5;
	System.out.println("The sum of 5 no is : " +s+"\nThe Average is : " +avg);
 
}
}
Exercise12.main(null);
Input the 5 numbers : 
The sum of 5 no is : 66
The Average is : 13.0
//14

import java.util.Scanner;
public class Exercise14 {

   public static void main(String[] args)

{
   int j,n;

   System.out.print("Input the number(Table to be calculated): ");
{
   System.out.print("Input number of terms : ");
    Scanner in = new Scanner(System.in);
		    n = in.nextInt();

   System.out.println ("\n");
   for(j=0;j<=n;j++)
  
     System.out.println(n+" X "+j+" = " +n*j);
   }
}
}
Exercise14.main(null);
Input the number(Table to be calculated): Input number of terms : 

35 X 0 = 0
35 X 1 = 35
35 X 2 = 70
35 X 3 = 105
35 X 4 = 140
35 X 5 = 175
35 X 6 = 210
35 X 7 = 245
35 X 8 = 280
35 X 9 = 315
35 X 10 = 350
35 X 11 = 385
35 X 12 = 420
35 X 13 = 455
35 X 14 = 490
35 X 15 = 525
35 X 16 = 560
35 X 17 = 595
35 X 18 = 630
35 X 19 = 665
35 X 20 = 700
35 X 21 = 735
35 X 22 = 770
35 X 23 = 805
35 X 24 = 840
35 X 25 = 875
35 X 26 = 910
35 X 27 = 945
35 X 28 = 980
35 X 29 = 1015
35 X 30 = 1050
35 X 31 = 1085
35 X 32 = 1120
35 X 33 = 1155
35 X 34 = 1190
35 X 35 = 1225
//16

import java.util.Scanner;
public class Exercise16 {

   public static void main(String[] args)

{
   int i,j,n;
   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(j);

    System.out.println("");
    }
}
}
Exercise16.main(null);
Input number of rows : 1
12
//18

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);
Input number of rows : 1
23
456
78910
1112131415
161718192021
//20

import java.util.Scanner;
public class Exercise20 {
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();
    }
  }
}

Exercise20.main(null);
Input number of rows : 1 
2 3 
4 5 6 
7 8 9 10