introAP_CS_A-chp1.md

introAP_CS_A-chp1.md

this is a java programming class. the point is not just to memorize code rather to learn how to solve problems by breaking them into steps that a computer can follow.

computers are powerful, but at their core they do things literal, that means if it doesn't compile, its almost always your fault (atleast the more industry standard compilers). they do exactly what your code tells them to do, even when that code is not what you meant. this is why programming requires careful thinking and a methodical approach. one missing symbol orone wrong word can completely screw up the result.

for example, look at this line of java code:

System.out.println("hello");

this prints:

hello

the quotation marks matter. "hello" is treated as text. without quotation marks, java thinks hello is the name of a variable (var).

System.out.println(hello);

this would cause an error unless a variable named hello had already been created.

man in white crew neck shirt smiling
me before gpt trying to fix my program

the basic structure of a java program

a simple java program looks like this:

public class Intro {
    public static void main(String[] args) {
        System.out.println("swag on em, welcome to ap cs A part 1");
    }
}

at first, this may look complicated. do not worry about understanding every word immediately. the most important idea is that java programs need structure.

public class Intro creates a class named Intro. for now, think of a class as the container that holds the program.

public static void main(String[] args) is the starting point of the program. when java runs the program, it looks for the main method.

System.out.println("welcome to ap cs a"); prints a message to the screen.

the curly braces { } group sections of code together. the semicolon ; ends many java statements. forgetting a semicolon is one of the most common beginner mistakes.

printing output

one of the first things programmers learn is how to print output.

System.out.println("ap computer science a");

this prints:

ap computer science a

there are two common print commands:

System.out.print("ap ");
System.out.print("cs ");
System.out.println("a");
System.out.println("java");

this prints:

ap cs a
java

print keeps the cursor on the same line. println prints the message and then moves to the next line.

variables

a variable is a named storage place for a value. you can think of it as a box with a label on it.

int score = 92;
double average = 87.5;
String name = "alan";
boolean passed = true;

each variable has a type.

int stores whole numbers.

double stores decimal numbers.

String stores text.

boolean stores either true or false.

variables are useful because they let us store information and use it later.

int points = 10;
points = points + 5;
System.out.println(points);

this prints:

15

the line

points = points + 5;

may look strange at first. in math, this would not make sense as an equation. in programming, it means: take the old value of points, add 5, then store the new value back into points.

the right side happens first.

expressions and arithmetic

java can do arithmetic.

System.out.println(3 + 4);
System.out.println(10 - 6);
System.out.println(5 * 2);
System.out.println(8 / 4);

the output is:

7
4
10
2

java follows the usual order of operations. multiplication and division happen before addition and subtraction.

System.out.println(2 + 3 * 4);

this prints:

14

java does 3 * 4 first, then adds 2.

parentheses can change the order.

System.out.println((2 + 3) * 4);

this prints:

20

integer division

one of the most important beginner rules in java is integer division.

System.out.println(7 / 2);

this prints:

3

many students expect 3.5, but java gives 3 because both numbers are integers. when java divides one integer by another integer, it throws away the decimal part.

to get a decimal answer, at least one number must be a decimal.

System.out.println(7.0 / 2);

this prints:

3.5

java also has the remainder operator %.

System.out.println(7 % 2);

this prints:

1

7 % 2 asks for the remainder after dividing 7 by 2. since 2 goes into 7 three times with 1 left over, the answer is 1.

tracing code

tracing means following code step by step to figure out what it does.

look at this example:

int x = 4;
int y = x + 3;
x = y * 2;
System.out.println(x);
System.out.println(y);

start with:

x = 4

then:

y = x + 3

since x is 4, y becomes 7.

then:

x = y * 2

since y is 7, x becomes 14.

so the output is:

14
7

notice that changing x does not automatically change y. once y gets its value, it keeps that value until another line changes it.

strings and numbers

a String is text. strings use quotation marks.

System.out.println("3 + 4");
System.out.println(3 + 4);

this prints:

3 + 4
7

the first line prints the text exactly as written because it is inside quotation marks.

the second line performs the calculation because it is not inside quotation marks.

java can also combine strings with values.

int score = 87;
System.out.println("your score is " + score);

this prints:

your score is 87

the + symbol can mean addition or joining text together. when a string is involved, java usually treats + as joining.

syntax errors and logic errors

programming errors usually fall into two major categories.

a syntax error happens when java cannot understand your code.

System.out.println("hello")

this has a syntax error because it is missing a semicolon.

the corrected version is:

System.out.println("hello");

a logic error happens when the code runs, but the answer is wrong.

int length = 5;
int width = 3;
int perimeter = length + width;
System.out.println(perimeter);

this code runs, but it does not correctly calculate perimeter. it only adds length and width once.

the correct formula is:

int perimeter = 2 * (length + width);
System.out.println(perimeter);

syntax errors are usually easier to notice because the program does not run. logic errors can be harder because the program runs but gives the wrong answer.

common beginner mistakes

one common mistake is forgetting semicolons.

System.out.println("hi")

another common mistake is forgetting quotation marks around text.

System.out.println(hi);

java thinks hi is a variable, not text.

another common mistake is misunderstanding integer division.

int x = 5;
int y = 2;
double result = x / y;
System.out.println(result);

this prints:

2.0

even though result is a double, the expression x / y happens first. since both x and y are integers, java does integer division and gets 2. then that value is stored as 2.0.

to get 2.5, write:

double result = x / 2.0;

or:

double result = (double) x / y;

practice reading code

try to predict what this code prints:

int a = 6;
int b = 2;
System.out.println(a / b);
System.out.println(a + b * 3);

the first line prints 3 because 6 / 2 = 3.

the second line prints 12 because multiplication happens first. b * 3 is 2 * 3, which is 6. then a + 6 is 6 + 6, which is 12.

the output is:

3
12

now try this one:

int x = 10;
x = x + 1;
x = x * 2;
System.out.println(x);

first, x is 10.

then x = x + 1, so x becomes 11.

then x = x * 2, so x becomes 22.

the output is:

22

main ideas to remember

a java program needs a class and a main method.

System.out.println prints output and moves to a new line.

variables store values.

each variable has a type, such as int, double, String, or boolean.

integer division removes the decimal part.

% gives the remainder.

strings use quotation marks.

syntax errors stop the program from running.

logic errors allow the program to run but produce the wrong result.

tracing means following the code one step at a time.

the most important skill in early programming is not typing fast. it is learning to slow down and read code carefully.