Homework

int a = 1;
int b = 3;
int c = 1;

double d = Math.pow(b, 2) - 4 * a * c;

if (d > 0) {
    double root1 = (-b + Math.sqrt(d)) / (2 * a);
    double root2 = (-b - Math.sqrt(d)) / (2 * a);
    System.out.println("The roots are " + root1 + " and " + root2 + ".");
} else if (d == 0) {
    double root = -b / (2 * a);
    System.out.println("The only root is " + root + ".");
} else {
    System.out.println("The roots are imaginary.");
}
The roots are -0.3819660112501051 and -2.618033988749895.
double input = 25;

if (input == 0) {
    System.out.println("zero");
} else if (input > 0) {
    if (Math.abs(input) < 1) {
        System.out.println("small positive");
    } else if (Math.abs(input) > 1000000) {
        System.out.println("large positive");
    } else {
        System.out.println("positive");
    }
} else if (input < 0) {
    if (Math.abs(input) < 1) {
        System.out.println("small negative");
    } else if (Math.abs(input) > 1000000) {
        System.out.println("large negative");
    } else {
        System.out.println("negative");
    }
}
positive
import java.util.Scanner;
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");
}
Input floating-point number: Input floating-point another number: They are the same up to three decimal places
import java.util.Scanner;
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");
}
Input an alphabet: Input letter is Consonant
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");
The first 10 natural numbers are:

1
2
3
4
5
6
7
8
9
10


import java.util.Scanner;

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);
Input the 5 numbers : 
The sum of 5 no is : 20
The Average is : 4.0
import java.util.Scanner;

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);
}
Input the number(Table to be calculated): Input number of terms : 

5 X 0 = 0
5 X 1 = 5
5 X 2 = 10
5 X 3 = 15
5 X 4 = 20
5 X 5 = 25
import java.util.Scanner;

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("");
}
Input number of rows : 1
12
123
1234
12345
123456
1234567
12345678
123456789
import java.util.Scanner;

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("");
}
Input number of rows : 1
23
456
78910
1112131415
import java.util.Scanner;

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();
}
Input number of rows : 1 
2 3 
4 5 6 
7 8 9 10 
11 12 13 14 15 
16 17 18 19 20 21 

Extra Notes

Truth Tables

Truth Tables

Truth tables can be used to visualize boolean conditionals. They're useful for evaluating basic boolean expressions as well as more complicated compound boolean expressions, which use multiple kinds of boolean operators inside each other.

De Morgan's Law

De Morgan's Law is a bit more mathematical in nature, dealing with set theory, so I'll try my best to describe it here.

De Morgan's Law basically explains how certain boolean statements can be rewritten as different expressions, yet essentially be the same. More specifically, it states that "The complement of the union of two sets is the same as the intersection of their complements" or "The complement of the intersection of two sets is the same as the union of their complements".

What the heck does that mean? An easier way to think about it would be like this:

  • not (A or B) = (not A) and (not B)
  • not (A and B) = (not A) or (not B)

In a more formalalized, mathematical way:

Set Theory Stuff

But even with all that text, it's still pretty confusing, right? So it's probably best to visualize it using an actual image. This one I found online pretty much summarizes it best:

More Set Theory Stuff

The green shaded area is basically what's being covered in each equality. Both sides cover the same thing.

Finally, let's go through an example of how this would apply in code. Essentially, we'll use a few variables to make a whole bunch of complicated compounded boolean expressions that actually mean the same exact thing:

public class deMorgansLaw {

    public static void main(String[] args) {
        boolean a = false;
        boolean b = true;

        // These two statements are exactly equal according to De Morgan's law
        // In this case, the only way to yield an output of true would be to have both a and b to be false (change it to check)
        System.out.println(!(a || b));
        System.out.println(!a && !b);

        boolean c = true;
        boolean d = false;

        // These two statements are also exactly equal according to De Morgan's Law
        // In this case, the only way to yield an output of false would be to have both c and d to be true (change it to check)
        System.out.println(!(c && d));
        System.out.println(!c || !d);
        
    }
}

deMorgansLaw.main(null);
false
false
true
true

Comparison of Various Datatypes

Numbers

Numbers such as integers, doubles, and floats can be compared using operators found in basic math such as:

  • ==
  • !=
  • <
  • >
  • <=
  • >=

Strings

Strings can be compared using the String method .equals() which checks if the objects that calls and is called are equal to each other (based on the alphanumeric contents of both)

Objects

Objects can be compared with two different methods, equals() (similar to comparing Strings) and hashCode(). Without overriding any methods, equals() checks if the two objects are equal using "==" operator (which checks if the object references point to the same object) and hashCode() generates a unique ID for each object to check if two objects are the same. In a more practical sense, both equals() and hashCode() should be overriden to better match the specific class being used.

// Numbers
System.out.println(4 > 3);
System.out.println(5 != 5);

// Strings
System.out.println("Hello World".equals("Hello World"));
System.out.println("Code Code Code".equals("code code code"));

// Objects
public class Name {
    private String first;

    public Name(String first) {
        this.first = first;
    }
}

Name name1 = new Name("Bailey");
Name name2 = new Name("Bailey");

System.out.println(name1.equals(name2));
System.out.println(name1.hashCode() == name2.hashCode());
true
false
true
false
false
false