Home Unit 1 Lesson
Post
Cancel

Unit 1 Lesson

SECTION 1.1

Why does java matter?

one of the main reason that java is such an important language in the coding world is because it is a object-oriented-programming (OOP) language.

it offers a structure that is easy to solve big problems. but i think that we all know that so lets skip to the real deal.

Basics of Java

  • Block Comments- The compiler will ignore any text between /* and */

  • line comments- Same as above but // ignores one line

  • Class Declaration - identifies the name, the start and the end of the class. the class name must match the file name. (case sensitive)

  • main Method- controls all of the action in the program.

  • system.out- objects that generate output to the console

  • system.out.print- prints what ever you out inside the ()

  • system.out.println- prints whatever is one the screen and moves to the next line to print out the next action. basically hits enter after it prints out.

tip: add “<classname>.main(null)” at the end of your code to see the output in your jupyter notebook.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/* this is a
   code block */


// code: printing out hello world. 

public class greeting {
    public static void main (String [] args) {

        System.out.println("Hello, World!");
        System.out.print("Hello,");
        System.out.print(" World!"); 
    }
}

greeting.main(null)
1
2
Hello, World!
Hello, World!

What is a string literal?

  • any sequence of letters, numbers, or symbols that is put between quotes.
  • java will put out anything in the quotes, no restrictions.

Examples:

1
2
3
4
5
6
7
8
9
10
11
public class stingLiterals {
    public static void main (String [] args) {

        System.out.println("This is a string literal.");
        System.out.println("and so are these");
        System.out.println("1234567890"); 
        System.out.println("&^&*%^$&%$#^%W#*^$%&(*^)"); 
    }
}

stingLiterals.main(null)
1
2
3
4
This is a string literal.
and so are these
1234567890
&^&*%^$&%$#^%W#*^$%&(*^)

ERRORS!!!!!!

Syntax/compiler error:

  • messed up syntax
  • compiler is not happy >:(
1
2
3
4
5
6
7
8
9
10
public class syntaxError {
    public static void main (String [] args) {

        System.out.println("This is a syntax error.")
        //missing semicolon
    }
}

syntaxError.main(null)
1
2
3
|           System.out.println("This is a syntax error.")

';' expected

Logic Error

  • compiler is happy
  • messed up in the string literals
  • code works perfectly
1
2
3
4
5
6
7
8
9
public class logicError {
    public static void main (String [] args) {

        System.out.println("This is a logic error.");

    }
}

logicError.main(null)
1
This is a leogic error.

exception error:

  • a number is divided by zero.
1
2
3
4
5
6
7
8
9
10
11
12
13
public class exceptionError {
    public static void main(String[] args) {
        try {

            int result = 2 / 0;
            System.out.println("Result: " + result);
        } catch (ArithmeticException e) {  //this just makes the error more verbose
            e.printStackTrace();
        }
    }
}
exceptionError.main(null)

1
2
3
4
5
6
7
8
9
10
java.lang.ArithmeticException: / by zero
	at REPL.$JShell$20$exceptionError.main($JShell$20.java:19)
	at REPL.$JShell$21.do_it$($JShell$21.java:16)
	at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
	at java.base/java.lang.reflect.Method.invoke(Method.java:578)
	at io.github.spencerpark.ijava.execution.IJavaExecutionControl.lambda$execute$1(IJavaExecutionControl.java:95)
	at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:317)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1144)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:642)
	at java.base/java.lang.Thread.run(Thread.java:1623)

SECTION 1.2

Variable and Data Types.

Primitive Data

  • determines the size and type of data can we can worth with in a java program.
  • focus on three different types that can we can represent data.

Smallest to biggest:

Boolean, takes up 1 bit.

  • true or false

Int, take up 32 bit

  • whole number values.
  • add, subtract, multiply, etc.

Doubles, AKA Floating point numbers. 64 bit

  • same as integers

String

  • “Hello World!”

Reference Purposes

(the collegeboard person used bows as reference so will i.)

  • there can be small bow
  • a medium bow
  • a red bow
  • a large red bow

What is the difference?

  • primitive data are already in java, you don’t have to make it. Except for string, which is created by the programmer.
  • non-primitive data can be use methods to perform actions, but primitive data cannot.

Primitive Activity

MIX&MATCH

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Choices:
1. int
2. double
3. boolean
4. String



Boolean - False

String - "ryan gosling"

Int - 1738

Double - 4.26

Questions:

what type of data should be used to store

  • someones bank number and cvc?
    • int
  • someones mother’s maiden name and their name?
    • string
  • the first 16 digits of pi
    • int
  • if you want to transact one million $.
    • int

Variables

A name given to a memory location that is holding a specified type of value.

how to name a variable (omg this is so hard !)

  • may consists of letters, digits, or an underscore (case sensitive)

  • may not start with a digit
  • space are a big no no
  • may not use other characters as &,@, or #
  • may not used java reserved words

Tip!

use camel casing when naming a variables.

example:

thisIsCamelCasing

Declare variables:

The three primitive data types in Java:

  • integers (whole #): int
  • Decimal numbers (floating-point values): double
  • Boolean values (true/false): boolean

Format:

dataType varibleName;

Example

int total;

boolean outcome;

double firstFifteenPi;

what if you don’t want to change the variable’s value after given?

add final in front of the declaration:

final double PI;

final boolean WORKOUT_DECISION;

for final variables, use all caps and underscore when naming them.

Practice

Find the odd one out.

int value;

double 4eva;

boolean private;

integer sum;

double wow!;

boolean foundIt;

int numberOfStudents;

double chanceOfRain;

boolean #mortNews;

int count;

bool isFriday;

final int DOZEN; <– odd one out


1.3 Main Topics

Literal vs String Literal

Arithmetic operators

  • Additon
  • Minus
  • Multiply
  • Divide
  • Mod

Arthemtic expressions

  • Int
  • Double

Assigment Operator

  • = vs ==

Demo 1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class demo {
    public static void main(String[] args) {
        // Whats is the outputs between these two pieces of code
        // 33
        // 1 (because integers round down)
        // 3.0
        
        System.out.println("3" + "3");
        System.out.println(3 / 2);
        System.out.println(2.0 * 1.5);
        
    }
}

demo.main(null);
1
2
3
33
1
3.0

Cheat Sheet

Literal

  • The source code representation of a fixed value

String Literal

  • Something enclosed in double qoutes

Assigment Operator

  • = is an assigment operator
  • == is repesents just normal =
  • Assigment Operators moves right to left
  • X = Y = Z = 2;
  • Why is 7 = x; a non valid peice of code

PEMDAS

  • Java uses PEMDAS still but a tiny bit different

Order

  1. ()
  2. *, /, %
  3. +, -

Primative Data Types

Operation vs Result

  • The bigger data type will always be the resulting output
  • If you were to add a double and a int what would be the result?

Demo 2

1
2
3
4
5
6
7
8
9
10
11
12
13
public class demo2 {
    public static void main(String[] args) {
        // Print a math equation that goes through these steps
        // Adds 19 + 134
        // Multiplies that value by 5
        // Then takes the mod of that value by 2
        // It should print 1
        
        System.out.println( (19 + 134) * 5 % 2);
    }
}

demo2.main(null);
1
1

Odd Even

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class OddEven {

    // Without changing any varibles
    // Print only odd numbers
    //change i % num == 0 from 0 to 1 because the remainder of every odd number divided by 2 is 1

    public static void main(String[] args) {
        int num = 2;
        int i = 0;
        while (i <= 10) {
            if (i % num == 1) {
                System.out.println(i + " is odd");
            }
            i++;
        }
    }
}

OddEven.main(null);
1
2
3
4
5
1 is odd
3 is odd
5 is odd
7 is odd
9 is odd

1.4

Compound Assignment Operators

look at storing items in variables with assignment statement.

also look at compound assignment operators (+=, -= etc) to modify values stored in variables in one step

+= adds value to existing variable value

x += 7 is equivalent to x = x + 7; x -= 5 is equivalent to x = x - 5;

same for all compound assignment operators.

also learn how to trace through code (check comments of codeblock below)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class CompoundDemo {
    public static void main(String[] args) {
        int x = 6;
        x += 7; // 6 + 7 = 13
        x -= 3; // 13 - 3 = 10
        System.out.println(x);
        x *= 10; // 10 * 10
        x /= 5; // 100 / 5 = 20
        System.out.println(x);
        x %= 3; // remainder of 100/3 = 2
        System.out.println(x);
}
}
CompoundDemo.main(null);

// NOTE: going through these compound assignments with comments is called tracing,
// when we do what the compiler does, and go through the code step by step. Should be
//done on the AP test to avoid human error.
1
2
3
10
20
2

increment and decrement operator

IMPORTANT: THE USE OF INCREMENT AND DECREMENT OPERATORS IN THE PREFIX FORM (++X) AND INSIDE OTHER EXPRESSIONS (ARR[X++]) IS OUTSIDE THE SCOPE OF THE COURSE AND THE AP EXAM

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class incdecDemo {
    public static void main(String[] args) {
    int x = 1; 
    int y = 1;
    x++; // x = x + 1, x = 2;
    y--; // y = y - 1, y = 1 - 1 = 0;
    System.out.println(x);
    System.out.println(y);
}
}
incdecDemo.main(null);

// NOTE: going through these compound assignments in order is important to
// ensure you get the correct output at the end
1
2
2
0

learn how to describe code

the following code segment has comments that describe the line of code the comment is on. Look at how the code is described.

1
2
3
4
5
6
7
8
9
10
public class behaviorDemo {
    public static void main(String[] args) {
    int x = 20; // define an int variable x and set its initial value to 23
    x *= 2; // take the current value of x, multiply it by 2, assign the result back to x
    x %= 10; // take the current value of x, find x modulus 10 (remainder x divided by 10), 
    //assign result back to x
    System.out.println(x); // display the current value of x
}
}
behaviorDemo.main(null); 
1
0

practice important concepts

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public class numbers {
    public static void main(String[] args) {
        int x = 777;

        // challenge: think of 2 compound assignment operators that can be used in tandem to get the following outputs
        // example: if we wanted output of 1 with 2 compound assignment operators, using x++; and x /= 777 would work. now how would we do that for:
        
        // 100?
        x/= 7;
        x-= 11;

        // 779?
        int x2 = 777;
        x2+=2;

        System.out.println(x);
        System.out.println(x2);
}
}
numbers.main(null);
1
2
100
779
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// PRACTICE TRACING THROUGH THE FOLLOWING CODE (someone could come up and do it on a whiteboard if we have time for fun)

public class Demo {
    public static void main(String[] args) {
        int x = 758;
        x += 423; 
        x -= 137; 
        x /= 33; 
        x %= 111; 
        x *= 0.99; 
        System.out.println(x);
}
}
Demo.main(null);

1
30
1
2
3
4
5
// DESCRIBE EACH LINE OF CODE
int x = 5; //assign 5 to x
x++; //x = x + 1 x = 6
x /= 2; //x = x/2 x = 3
System.out.println(x); //print x
1
3

what you must know

compound operators perform an operation on a variable and assign the result to the variable

ex: x /= 25; would take x and divide it by 25, then assign the result to x.

if x = 100, then the result of x/= 25; would be 4.

increment and decrement operators take x and either increment it or decrement it by 1, then assign the value to x.

x++; would take x and add 1 to it, then assign the value to x.

x–; takes x and subtracts 1 from x, then assigns the value to x.

your frq will likely see if you know how to use compound operators in actual code.

mini hacks:

  1. write some java code that uses compound operators and then trace through it similarly to the examples shown in this lesson. Use conditional statements as well, don’t just execute some compound operators on an x value in order and call it a day. You might have to do tracing on the AP test so this is for your own good.

example below

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class CompoundAssignment {

    public static void main(String[] args) {
      
      int sum = 0; 
      for (int i = 0; i < 2; i++) {  // i = i + 1 each iteration, 1st iteration i = 0
        for (int j = 0; j < 2; j++) { // j = j + 1 each iteration, 1st iteration j = 0
          System.out.println("i: " + i);
          System.out.println("j: " + j);
          sum += i + j; //  sum = sum + (i + j) each iteration, 1st iteration sum = 0 + (0 + 0)
          System.out.println(sum);

        }
      }
      
      System.out.println("Sum: " + sum);
  
  } }
CompoundAssignment.main(null);

My mini hack with compound assignments and conditionals

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
public class BankAccount {
    private double balance;

    public BankAccount(double initialBalance) {
        balance = initialBalance;
    }

    public void deposit(double amount) {
        if (amount > 0) {
            balance += amount;
            System.out.println("Deposited $" + amount);
        } else {
            System.out.println("Invalid deposit amount");
        }
    }

    public void withdraw(double amount) {
        if (amount > 0 && amount <= balance) {
            balance -= amount;
            System.out.println("Withdrawn $" + amount);
        } else {
            System.out.println("Invalid withdrawal amount");
        }
    }

    public double getBalance() {
        return balance;
    }

}

public class Main {
    public static void main(String[] args) {
        BankAccount account = new BankAccount(1000.0);
        account.deposit(500.0);
        account.withdraw(200.0);

        System.out.println("Final balance: $" + account.getBalance());
    }
}

Main.main(null);
1
2
3
Deposited $500.0
Withdrawn $200.0
Final balance: $1300.0

1.5

Casting Variable

CASTING

"Change the data type of variable from one type to another."

Example

  • Int variable —-» double
  • String variable —-» Int
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/*
* Purpose: Demonstrate casting using division.
*/
public class CastingNumbers {
    public static void main(String[] args) {
        System.out.println(6 / 4); // if I just do divide 6 by 4, these are both int value, so I'm going to wing up with an int result.
        System.out.println (6.0 / 4); // Since one of those value is a double value, output will be double 
        System.out.println(6 / 4.0); // (Same Reason) I wind up with the result of 1.5

        //answer to question below. it will be 1.5 because putting (double) at the front outputs the result as a double
        System.out.println((double)6 / 4); 
    }
}

CastingNumbers.main(null);
1
2
3
4
1
1.5
1.5
1.5

Ranges of variable

int: Stored by using a finite amount (4 bytes) of memory.

Integer

Max: 2,147,483,647
Min: -2,147,483,648

Double

up to 14 - 15 digits for storage

Boolean

only 2 (true and false)

1
2
3
4
5
6
7
8
9
10
11
12
/*
* Purpose: Demonstrate Integer Range
*/
public class TooBigNumber {
    public static void main(String[] args) {
        int posInt = 2147483647; 
        int negInt = -2147483648;
        System.out.println(posInt + " "); 
        System.out.println(negInt);
    }
}
TooBigNumber.main(null);
1
2
2147483647 
-2147483648
1
2
3
4
5
6
7
8
9
10
11
12
/*
* Purpose: Demonstrate Integer.MAX_VALUE and Integer.MIN_VALUE.
*/
public class IntMinMax {
    public static void main(String[] args) {
        int posInt = Integer.MAX_VALUE; 
        int negInt = Integer.MIN_VALUE;
        System.out.println(posInt + " "); 
        System.out.println(negInt);
    }
}
IntMinMax.main(null);
1
2
2147483647 
-2147483648
1
2
3
4
5
6
7
8
9
10
11
12
/*
* Purpose: Demonstrate Integer Overflow
*/
public class TooBigNumber {
    public static void main(String[] args) {
        int posInt = Integer.MAX_VALUE; 
        int negInt = Integer.MIN_VALUE;
        System.out.println(posInt + 1); 
        System.out.println(negInt - 1);
    }
}
TooBigNumber.main(null);
1
2
-2147483648
2147483647
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import java.util.Scanner;

public class Sum_forABC {
	public static void main(String args[]) {
		int a = 70;
		int b = 63;
		int c = 82;
		
		int total = a + b + c;
        
		double avg = total / 3; //putting double creates decimal points
		
		System.out.println("a\tb\tc");
		System.out.println(a+"\t"+b+"\t"+c);
		System.out.println("total:" + total);
		System.out.printf("average: %.2f", avg);
	}
}
Sum_forABC.main(null);
1
2
3
4
a	b	c
70	63	82
total:215
average: 71.00
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class CastingTest {
    public void printNumber(int number) {
        System.out.println(number);
    }
}

class Main {
    public static void main(String args[]) {
        CastingTest ct = new CastingTest();

        int a = 70;
        long b = 70;

        ct.printNumber((int)b);
    }
}

Main.main(null);
1
70

<!DOCTYPE html>

Java Code Runner

Questions

1. what will be the output of (int) (2.5 * 3.0)

7.0
7
7.5
7.50

2. what will be the output of (double) 25 / 4

6.50
6.0
6
6.25

3. what will be the output of 6 / (double) 5

1.0
1
1.2
1.125

4. what will be the output of (int) (-8.63 - 0.5)

-9
-13.63
-9.13
-8

Essential Knowledge (2.B)

  1. The casting operators (int) and (double) can be used to created temporary value converted into a different data type.
  2. Casting a double value to an int causes the digits to the right of the decimal point to be truncated.
  3. Some programming code causes int values to be automatically cast(widened) to double values.
  4. Value of type double can be rounded to the nearest integer (int) (x + 0.5) + (int) (x - 0.5) for negative numbers.

Essential Knowledge (5.B)

  1. Integer values in Java are represented by values of type int, which are stored using a finite amount (4 bytes) of memory. Therefore, an int value must be in the range from Integer.MIN_VALUE to Integer.MAX_VALUE inclusive.
  2. If an expression would evaluate to an int value outside of the allowed range, an integer overflow occurs. This could result in an incorrect value within the allowed range.

Questions

  1. (int) (2.5 * 3.0) = 7
  2. (double) 25 / 4 = 1.25
  3. 6 / (double) 5 = 1.2

Mini Hacks

  • Complete all uncompleted code segments
  • Answer all questions in the code
  • complete the activities in the lesson blogs
  • will be scaled to 0.2 depending on the completion of your code

Main Hack

  • Use topics from 3 out of 4 of the Units
  • It has to be a 2 part style question, A and B
  • Will Be graded out of 4 points
  • Write a scoring sheet for what you have done out six points
  • Your real grade for the assignment will be based of two things
  • How creative, unique, cool your question and code is
  • Did you achieve 4/4 points for your code
  • That will be scaled to 0.8 of 1 for all your hacks

Example FRQ 0.65

Question:

Winner of the game is represented by gameWin. Player 1 score is represented by int variable x, and Player 2 score is represented by int variable y

1
2
3
4
5
6
7
public class Game {
    int x = 0;
    int y = 0;

    boolean XgameWin = false;
    boolean YgameWin = false;
}

PART A

write a method gameWinchange that checks who wins

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class Game {
    int x = 0;
    int y = 0;

    boolean XgameWin = false;
    boolean YgameWin = false;

    public void gameWinchange(boolean X, boolean Y) {
        //insert code here
        if (X == true) {
            UpdateScore(x,y);
            X = true;
        } else {
            UpdateScore(y,x);
            Y = true;
        }
    }
}

Part B

Write a method to update score for Player 1 and Player 2, The player that wins gain a point, the player that loses minus a point If a player hits 10 points, reset values

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class Game {
    
    //pretend previous method is above

    public int UpdateScore(int Wscore, int Lscore) {
        Wscore += 1;
        Lscore -= 1;

        if (Wscore == 10) {
            Wscore = 0;
            Lscore = 0;
        }
        if (Lscore == 10) {
            Wscore = 0;
            Lscore = 0;
        }
    }
}

Grading

  • 1/1 Point for correctly changing the boolean in Part A
  • 1/1 Point for using Compound Assignment Operators in Part B
  • 1/1 Point for Values resetting when one hits 10 points
  • 1/1 Point for Passing Arguments through Part A and B

HACKS

  • Part A: Write a method donutOrder to check the price of your order based on how many boxes are ordered.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class donutOrder {

    private int numBoxes = 0;
    private static final double priceOfBox = 12.99;

    public DonutOrder(int numberOfBoxes) {
        this.numBoxes = numberOfBoxes;
    }
    
    public double donutOrderPrice(){
        if (numBoxes < 0) {
            System.out.println("No boxes ordered. Please enter a non-negative number of boxes");
            return -1;
        }

        double totalPrice = numBoxes * priceOfBox;
        return totalPrice;
    }
}
  • Part B: Write a method to see whether the buyer qualifies for a discount and applies the discount
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
public class DonutOrder {

    private int numBoxes = 0;
    private static final double priceOfBox = 12.99;

    public DonutOrder(int numberOfBoxes) {
        this.numBoxes = numberOfBoxes;
    }
    
    public double donutOrderPrice(){
        if (numBoxes < 0) {
            System.out.println("No boxes ordered. Please enter a non-negative number of boxes");
            return -1;
        }

        double totalPrice = numBoxes * priceOfBox;
        return totalPrice;
    }

    public boolean qualifiesForDiscount(){

        boolean qualifiesForDiscount = false;
        double totalPrice = donutOrderPrice();

        if (numBoxes >= 5 && totalPrice >= 50.0) {
            totalPrice *= 0.55; // 45% discount = 100% - 45% = 55%
            System.out.println("Discounted Price rounded down: $" + (int)totalPrice);
            qualifiesForDiscount = true;
            
        } else {
            System.out.println("Regular Price: $" + totalPrice);
        }
        return qualifiesForDiscount;
    }
}


public class Main {
    public static void main(String[] args) {
        
        DonutOrder order1 = new DonutOrder(3);
        DonutOrder order2 = new DonutOrder(5);
       
        double totalPrice1 = order1.donutOrderPrice();
    
        System.out.println("Total price for 3 boxes: $" + totalPrice1);

        boolean qualifies1 = order2.qualifiesForDiscount();
        System.out.println("Order qualifies for discount: " + qualifies1);
    }
}

Main.main(null);
1
2
3
Total price for 5 boxes: $38.97
Discounted Price rounded down: $35
Order qualifies for discount: true

Grading

  • 1/1 Point for correctly changing the boolean in Part B
  • 1/1 Point for using doubles or int to store variables in Part A and B
  • 1/1 Point for using Compound Assignment Operators in Part B
  • 1/1 Point for using Casting (double to int) in Part B
This post is licensed under CC BY 4.0 by the author.

-

Unit 2 Hacks