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.
| 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. |
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
}
}if (/*check a condition*/) {
//dosomething
} else if (/*condition*/) {
//dosomethingelse
} else {
//dosomethingelse
}
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
For Loops
for (int i = 0; i < 3; i++) {
//do something
}while (/*condition*/) {
//do something
}do {
//do something
} while(/*condition*/);
While 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
// 2Break 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
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”.
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));
}
}
}
}
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
}
}