Cornelius Eanes's Programming Portal
Assignments (Variables Pt. 2)
[27] Variables Only Hold Values

Goal: Learn a little more on what happens when declaring a variable.

Code

/// Name: Cornelius Eanes
/// Period: 5
/// Program Name: Variables Only Hold Values
/// File Name: Sequences.java
/// Date Finished: Sep 30, 2015

import java.util.Scanner;

public class Sequences {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        double price = 0, salesTax, total;

        salesTax = price * 0.0825;
        total = price + salesTax;

        System.out.println("How much is the purchase price?");
        price = input.nextDouble();

        System.out.println("Item Price:\t" + price);
        System.out.println("Sales Tax: \t" + salesTax);
        System.out.println("Total Cost:\t" + total);

    }

}

Output

Explanation

The price variable was set to 0 when the salesTax and total were calculated. Because they were not updated when the price was set again, this proves that variables only hold the values that are calculated at the time, and are never updated unless redefined by the programmer.

[28] Variable Modification Shortcuts

Goal: Learn some shortcuts to manipulating variables.

Code

/// Name: Cornelius Eanes
/// Period: 5
/// Program Name: Variable Modification Shortcuts
/// File Name: DataManipulationShortcuts.java
/// Date Finished: Sep 30, 2015

public class DataManipulationShortcuts {

    public static void main(String[] args) {

        int i = 5, j = 5, k = 5;
        System.out.println("i = " + i + "\tj = " + j + "\tk = " + k);

        i = i + 3;
        j = j - 3;
        k = k * 3;
        System.out.println("i = " + i + "\tj = " + j + "\tk = " + k);

        i = 5;
        j = 5;
        k = 5;
        System.out.println("\ni = " + i + "\tj = " + j + "\tk = " + k);

        i += 3;
        j -= 3;
        k -= 3;
        System.out.println("i = " + i + "\tj = " + j + "\tk = " + k);

        i = j = k = 5;
        System.out.println("\ni = " + i + "\tj = " + j + "\tk = " + k);

        i += 1;
        j -= 2;
        k -= 3;
        System.out.println("i = " + i + "\tj = " + j + "\tk = " + k);

        i = j = k = 5;
        System.out.println("\ni = " + i + "\tj = " + j + "\tk = " + k);

        i =+ 1;
        j =- 2;
        System.out.println("i = " + i + "\tj = " + j + "\tk = " + k);

        i = j = k = 5;
        System.out.println("\ni = " + i + "\tj = " + j + "\tk = " + k);

        i++;
        j--;
        System.out.println("i = " + i + "\tj = " + j + "\tk = " + k);

    }

}

Output

[29] Boolean Expressions

Goal: Learn what booleans are and the operations associated with them.

Code

/// Name: Cornelius Eanes
/// Period: 5
/// Program Name: Boolean Expressions
/// File Name: BooleanExpressions.java
/// Date Finished: Sep 30, 2015

import java.util.Scanner;

public class BooleanExpressions {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        boolean a, b, c, d, e, f;
        double x, y;

        System.out.print("Give me two numbers.\nFirst: ");
        x = input.nextDouble();
        System.out.print("Second number: ");
        y = input.nextDouble();

        a = x  < y;
        b = x  > y;
        c = x == y;
        d = x <= y;
        e = x >= y;
        f = x != y;

        System.out.println(x + " is less than " + y + ": " + a);
        System.out.println(x + " is greater than " + y + ": " + b);
        System.out.println(x + " is equal to " + y + ": " + c);
        System.out.println(x + " is less than / equal to " + y + ": " + d);
        System.out.println(x + " is greater than / equal to " + y + ": " + e);
        System.out.println(x + " is not equal to " + y + ": " + f);

        System.out.println();

        System.out.println((x < y) + " " + !(x >= y));
        System.out.println((x > y) + " " + !(x <= y));
        System.out.println((x == y) + " " + !(x != y));
        System.out.println((x <= y) + " " + !(x > y));
        System.out.println((x >= y) + " " + !(x < y));

    }

}

Output