Programming workshop
goals of this workshop
1. Be able to (somewhat) code or at least be able to program your way out of a wet paper bag.
2. Understand the fundamental structures of coding.
3. Understand the concept of object-oriented programming or OOP and how to use it effectively.
4. Learn the basics of the given API or whatever we're using this year.
5. Have some fun. Woo.
Variables
- byte (number, 1 byte)
- short (number, 2 bytes)
- int (number, 4 bytes)
- long (number, 8 bytes)
- float (float number, 4 bytes)
- double (float number, 8 bytes)
- char (a character, 2 bytes)
- boolean (true or false, 1 byte)
Variables
| int x; | Defines a variable. |
|---|---|
| int x = 1; | Sets the variable to a value. |
| final int x = 1; | Sets the variable to a value that cannot be changed. |
| static int x = 1; | Value is shared between all usages. |
| public int x = 1; | Value is accessible form all classes. |
| private int x = 1; | Value is only accessible from within its own class. |
methods/functions
Javascript
function name(parameter1, parameter2, parameter3){
//code to be executed
}public class Main {
public static void doSomething() {
// Do something here
}
}Java
public class Main {
public static int doSomething(parameter1) {
//Do something here with parameter1
//and return something in the int format
}
}Conditionals
if (/*check a condition*/) {
//dosomething
} else if (/*condition*/) {
//dosomethingelse
} else {
//dosomethingelse
}
Conditionals
int a = 4;
int b = 5;
result = a < b // true
result = a > b // false
result = a <= 4 // a smaller or equal to 4 - true
result = b >= 6 // b bigger or equal to 6 - false
result = a == b // a equal to b - false
result = a != b // a is not equal to b - true
result = a > b || a < b // Logical or - true
result = 3 < a && a < 6 // Logical and - true
result = !result // Logical not - false
Loops
For Loops
for (int i = 0; i < 3; i++) {
//do something
}while (/*condition*/) {
//do something
}do {
//do something
} while(/*condition*/);
While Loops
Loops
Break
int i;
for (i = 0; i < 5; i++) {
if (i >= 2) {
break;
}
System.out.println("Yuhu");
}
System.out.println(i);
// Output:
// Yuhu
// Yuhu
// 2Loops
Break and Continue
int i;
for (i = 0; i < 5; i++) {
if (i >= 3) {
break;
}
System.out.println("Yuhu");
if (i >= 1) {
continue;
}
System.out.println("Tata");
}
System.out.println(i);
// Output: Yuhu, Tata, Yuhu, Yuhu, 3
Fizzbuzz
The "Fizz-Buzz test" is an interview question designed to help filter out the 99.5% of programming job candidates who can't seem to program their way out of a wet paper bag.
Challenge
Write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”.
Fizzbuzz
Solution
public class FizzBuzz {
public static void main(String[] args) {
for (int i = 1; i <= 100; i++) {
if (i % 15 == 0) {
System.out.println("FizzBuzz")
} else if (i % 3 == 0) {
System.out.println("Fizz");
} else if (i % 5 == 0) {
System.out.println("Buzz");
} else {
System.out.println(String.valueOf(i));
}
}
}
}Oops, we forgot about oop.
Object-oriented programminG
Also known as OOP, hence the previous slide's pun.
- In OOP, a program consists of interacting objects
- To write a program like this, you have to define what objects know, how they're created, and how they interact with other objects
OBJECTS
ACCEPT the pun.
- Objects are described in terms of attributes and behaviors
- Attributes describe objects, and behaviors are what objects can do
CLASSES
Accept it.
- In order to use an object, you must define it in a class.
- A class can be thought of as a mold/blueprint that a computer uses to create objects.
- Ex: When building a house, a construction crew uses a certain blueprint to make the house. However, clients might want their houses to be different. These different houses represent objects of the original blueprint.
methods
- To instruct an object to execute a task, a message is sent to the object.
- This message is known as a method.
EXAMPLE CLASS
public class Main
{
private final int a = 10;
private final int b = 42;
public int add(int anInt, int anotherInt)
{
return anInt + anotherInt;
}
public int multiply(int anInt, int anotherInt)
{
return anInt * anotherInt;
}
public static void main(String[] args)
{
System.out.println(add(a, b)); // should return 52
System.out.println(multiply(a, b)); // should return 420
}
}
Programming Workshop 2015
By Richard Huang
Programming Workshop 2015
Use space bar to navigate.
- 518