6 Week Coding Camp

Download as pdf or txt
Download as pdf or txt
You are on page 1of 27

6 WEEK

CODING CAMP
What we will be going
over in the course

Unit 1: Primitive Types


Unit 2: Using Objects
Unit 3: Boolean Expressions and If
statements
Unit 4: Iteration
Those looking to take AP Those who are brand new to
Computer Science in the fall coding and looking to learn

Who this course is for


All levels welcome!
Unit 1: Primitive Types
Background
We will be using java for this course
Most of our coding will be done using replit where we
can compile and practice coding without having to setup
an environment
For our 2D game we will be downloading a program
called Processing
This makes it easy for us to setup the graphics and we
can focus strictly on java and the logic.
Editors
To compile java source into a java class file you use a
compiler
To program most programmers use what’s called an
Integrated Development Environment (IDE) which has
a built in compiler
For this course we will be using Replit which is a free
online development environment
Some other editors you may see being used are
VSCode which is very popular
IntelliJ
Netbeans
Eclipse
Pair Programming
Pair programming is a software development
technique in which two programmers work together
at one workstation
Pros
Fewer mistakes and bugs
Increased code quality
Better knowledge sharing
Why care about
programming?
Phones, computers, video games,
streaming services all technology have
one thing in common. Someone had to
program them
Why Java?
Java is one of many programming languages used. It is very popular and used
everywhere. It is a great language for those learning how to code due to it’s ease of use
Printing
2 different ways to print in java
Print and println
Syntax
For this course we will be using Replit
which is a free online development
environment
Special keywords are lower case such as
public, class, static
Semicolons must end the line in java.
Similar to a period in the English language
Syntax Errors
These are things that the compiler complains
about when the proper syntax for the
language isn’t followed. Examples include
missing semicolon or missing closing
brackets.
Errors are called bugs and removing these
bugs is referred to as debugging
Term was derived around 1947 and came
about when A computer scientist named
Grace Hopper documented a real bug
that flew into his computer and caused
issues.
Spot the error
System.out.println(“Hello World);
System.out.print(“Ace AP Computer Science”)
System.out.println(“Hi everyone’);
Comments
Java allows you to leave comments which are like notes to those who will be reading
your code. When the compiler sees these comments it ignores them meaning they
have no effect on your proram
Single and multi line
Single comments //
Multi Line /* */
QUIZ TIME
Variables and data
types
What exactly is a variable?
A variable is a name associated with a
memory location in the computer, where
you can store values that can change.
Data Types
2 types of variables in java
Primitive variables that hold primitive types
Reference or object variables which hold a reference to an object of a class

Primitive types list


int (need to know) – whole numbers
double (need to know) – represents non-integer numbers i.e 7.2
boolean (need to know) – represents 2 values, true or false
float – represents non-integer numbers but half the size of a double, not used as much

Reference types list


String - is a common object type used in Java. Sequence of characters wrapped in
double quotes.
What data type to represent average grade for class?
Data type to represent how many students in a class?
Data type to hold the name of a person?
Data type to let someone know whether a person is telling the truth or
not?
Declaring variable
In java you must declare a variable by first specifying it’s type. Followed by a space and
the name.

When it is declared you can also give it a value by using the = sign. That’s optional

int score = 1;

To print can use string concatenation to add to another string inside

double price = 19.99;


System.out.println(”Final price is " + price);

The keyword final can be placed in front of the type to make it constant, meaning it can’t
change.

final doublePI=3.14
Naming variables
When choosing name for variable there are certain
rules that must be followed
Should start with alphabetic character and can
contain numbers, letters and underscores.
Cannot contain spaces
Cannot use reserved keywords such as static
Should be descriptive name
Should use java convention which is lowercase letter
and uppercase each word (camel case) i.e myVariable
Other conventions are snake case used in some other
languages i.e my_variable
QUIZ TIME
Input with Java
Variables can also be given input from stdin or standard input. This is taking input directly
from the keyboard
This is done using the Scanner class or Console class. Typically will see this done with the
scanner class
Expressions and Assignment
Statements
Assignment statements can initialize or change values stored in variables using the
assignment operator (= sign)
Adding 1 to a variable (x = x + 1). Essentially says to take the value of the original value
add 1 and set the value equal to expression
Operators
The same operators in math are available here. Addition
(+), Subtraction (-), Division (/). The multiplication
operator uses (*). For exponents typically (^) is used but
in Java this does not mean taking the exponent but
instead xor which we’ll cover later.
Arithmetic expressions will evaluate to type int or
double. If the expression only consists of ints then the
final value will be an int. If used with a double it will
evaluate to a double
This is because it defaults to the most precise data type
which is a double.
Operators take precedence just like PEMDAS in math
and the same rules apply i.e dividing by 0 is not allowed
and will throw an error.
Using these expressions you can create compound
expressions, mixing variables and numbers to do
powerful things.
Modulo Operator
The (%) symbol is referred to as the modulus
operator. When dividing by a number you
sometimes get the remainder if you think back to
when you first learned division. 10 % 3 yields that
remainder portion so the answer would be 1.
Although it’s referred to as the modulo operator it is
actually used as the remainder. Modulo uses
Euclidian division and so a true modulo would get
different values for negative numbers.
However, for positive numbers remainder and
modulo yield the same values.
Compound assignment
operators
These are shortcuts that do math in one step.
Can be used with all of the previous operators discussed before
X+=1 essentially is equivalent to x = x + 1. This can be done with the (-), the (*), the (/), or the
(%)
Since incrementing and decrementing by 1 is so common there is a special shortcut for that. X++
or ++x and x– or –x. This only works for these operations and only allows you to change the
value by 1.
These are called postfix and prefix. The difference is where the value gets updated. Only postfix
will be covered
Casting
Type casting or casting allows for converting between data types
The format looks like (int) or (double) which is placed before the
expression. For example (double) 1 / 3 will evaluate to a double
Ints are stored in 4 bytes so they range from -2^31 -> 2^31 – 1.
Stored in values in java Integer.MIN_VALUE and
Integer.MAX_VALUE. (All caps indicates constants)
When casting from doubles to ints the values get truncated because
ints are not as precise. So 1.8 casted to an int would be 1. If an
expression evaluates to an int value outside the range it could also
cause an overflow issue so be careful

You might also like