Welcome to Code Camp
Let’s Begin…
Why? • Unlock your creativity, bring your ideas to life • Learn a new way of thinking • Work for one of these guys for big $$
• Start one of these for even bigger $$
What is a Program? - A program is a list of instructions - Not limited to computers, could be a program for how to brush your teeth: 1. 2. 3. 4. 5.
apply toothpaste to toothbrush bristles scrub all surfaces of all teeth w/ bristles spit wash mouth and toothbrush under faucet feel clean
What is a Computer Program? - A list of instructions that a computer can understand, looks like this: public class BestRapperEver { public static void main(String[] args){ String fName = “Biggie”;
String lName = “Smalls”; System.out.println(fName + “ “ + lName); }
}
• Machine language - computer’s native language - sequences of zeroes and ones (binary) - very unintuitive for humans - 0101011001…
• Assembly language - simple representation of base functions of computer - low level: each instruction is minimal - hard to quickly program anything substantial - ADD.L d0,d2
• High-level languages - Java, C, C++, Python, etc. - high level: each instruction made up of many low-level instructions - closer to English and Math - easier to understand - hypotenuse = Math.sqrt(leg1 * leg1 + leg2 * leg2);
Compilers - Translates your code into machine code - Defines the language - javac is java’s compiler
Your Java Program
javac (plus some other stuff…)
Now, let’s get down and dirty • Your first two tools will be: • 1. Variables – - place holder for values - in Java, every variable has a type, a name, and a value - type doesn’t change, value does change
• 2. Methods – - the muscle of your program - can have a return value and parameters
Variable Types
Object – The ‘superclass’ of all types not shown above - this is the essence of Java and other Object Oriented Programming (OOP) languages
Primitive Data Types
int – an integer (1, 1003002, -67, 0) double – a real number (2.7676, 1.0, .00001, -89.98) char – a single character (a, z, &, \n, 1) String – a series of characters (Hello World!, afdjkl34&8, pancake) boolean – two possible values; true or false
Variable Declarations String firstName; -Simple! -can name them anything you want (no spaces) -but should follow the ‘naming conventions:’ -name should represent what the variable will hold -first letter lowercase, new words’ first letter uppercase (like above)
Variable Definitions firstName = “Bill”; -Simple again! -String values are encompassed with “ ” -IMPORTANT: = does not mean equal in Java = is used to assign a value read = as ‘gets’ firstName gets “Bill”
Bring ‘em together String firstName = “Bill”; -declaring then defining is perfectly legal -quicker to do it all in one line if you can
Variable Madness String firstName; String lastName; firstName = “Bill”; lastName = “Clinton”; String fullName = firstName + “ ” + lastName; int prezYears = 8;
Now…
METHODS!!
The Magic of Methods • A series of instructions with a name • Abstraction for an ability or function
rideNoHands();
Method Declaration • Just like variables, methods have to be declared: Modifier
Return Type
Method Name
Parameters
public double average (double num1, double num2){ //definition goes here }
Method Definition • Define what the method will actually do • Has to follow the template of the declaration public double average (double num1, double num2){ double sum = num1 + num2; //sum the params double average = sum / 2.0; //divide the sum return average; //return the result }
A quick side note on comments… • comments are ignored by the compiler • really useful as a programmer • should be used extensively, don’t be shy
//this is a one line comment /* this is a multi-line comment */
A Very Special Method • The main method • This is where every java program starts • Looks like this: public static void main (String [] args){ //your java program starts here }
Let’s Put it Together public class MainClass { public double average (double num1, double num2){ double sum = num1 + num2; //sum the params double average = sum / 2.0; //divide the sum return average; //return the result } public static void main(String[] args){ double answer = average(2.5, 7.5); System.out.println(answer); return; }
}
Hand Simulation • Your programs will almost always have errors or bugs at first • If you are stuck, follow the flow of control (like we just did) • Do what the computer will do in your head
System.out.println(“gah”) • This is called a print line • This will be the line of java you write the most • It ‘prints’ it’s parameter to the terminal
You probably don’t understand but you will on…
Wednesday • 7:00pm-8:30pm lab • Right back here at the DSU • Bring your laptop and charger • It’s going to be fun! • This lesson will be on the site soon • Review it/look at powerpoint