1
Table of Contents
[0] ............................................................................................................................ 2
[1] Chapter 1: Hello World!..................................................................................... 3
[2] Chapter 2: Skeleton............................................................................................ 4
[3] Chapter 3: Variable Declaration.......................................................................... 5
[4] Chapter 4: Public, Private and Final................................................................... 6
[5] Chapter 5: Casting.............................................................................................. 7
[6] Chapter 6: Debugging and Method Calling........................................................ 8
[7] Chapter 7: Importing Scanner............................................................................. 10
[8] Chapter 8: Working with Methods...................................................................... 11
[9] Chapter 9: If Statements..................................................................................... 12
[10] Chapter 10: Nested If Statements....................................................................... 13
[11] Chapter 11: While Loops.................................................................................... 14
[12] Chapter 12: For Loops and Arrays...................................................................... 15
[13] Chapter 13: Double Arrays................................................................................. 16
[14] Chapter 14: Objects............................................................................................ 17
[15] Chapter 15: Recursion........................................................................................ 19
[16] Chapter 16: ArrayList and Iterator...................................................................... 21
[17] Chapter 17: Exceptions....................................................................................... 22
2
Chapter 1: Hello World!
public class HelloWorld {
public static void main (String args[]){
System.out.println("Hello World");
}
}
3
Chapter 2: Skeleton
public class Skeleton {
// GLOBAL VARIABLES
// integer that is public to all classes
public int publicInteger;
// integer that is only accessible to this particular class
private int privateInteger;
// integer that is only accessible to this particular class but is static (explained later)
private static int staticInteger;
// the final declaration declares a constant
final static int constant;
// this format must always be followed (but args can be anything)
public static void main (String args[]){
// Statements
}
// METHODS
// ALWAYS declare a method private or public; however Java implicitly declares
static int returnFive () {
return 5;
}
// Can only be accessed from this class - does not work with the this. operator
private static double returnFour() {
return 4;
}
// Can be accessed from any class - works with the this. operator
public double returnThree() {
return 3;
}
}
4
Chapter 3: Variable Declaration
public class DeclaringVariables {
public static void main (String args[]){
// Note that short, ints and longs are similar, but shorts store the least amount of
//information, ints a little more, and longs the most
// They are all, however, stored as integers.
short myFirstShort = 1;
int myFirstInteger = 1;
long myFirstLong;
myFirstLong = 2;
// These are decimal point delcarations
double myFirstDouble = 2;
float myFirstFloat = 3;
// Booleans are true or false statements
boolean isThisTrue = true;
// Characters just store a simple character. They must be declared in single
//quotations.
// Double quotes refer to a String
char theLetterA = 'a';
}
}
5
Chapter 4: Public, Private and Final
public class VariableFun {
static double a = 1;
double b = 2;
public static double c = 3;
private double d = 4;
private static double e = 5;
final static double f = 6;
public final static double g = 7;
public static void main (String args[]){
System.out.println(a);
System.out.println(b);
System.out.println(c);
System.out.println(d);
System.out.println(e);
System.out.println(f);
System.out.println(g);
a = 10;
b = 20;
c = 30;
d = 40;
e = 50;
f = 60;
g = 70;
System.out.println("a");
System.out.println(b);
System.out.println(c);
System.out.println(d);
System.out.println(e);
System.out.println(f);
System.out.println(g);
// This program will NOT compile. Why?
// What would this program print out if we were to remove the errors?
}
}
6
Chapter 5: Casting
public class CastingFun {
public static void main (String notargs[]) {
double pi = 3.14;
int pie = (int) pi;
System.out.print(pi);
System.out.println(pie);
pie = 5;
pi = pie;
System.out.println(pi);
// Does this program compile?
// What does it print?
}
}
7
Chapter 6: Debugging and Method Calling
//This program has three errors
//We will debug it together so that you understand what is happening.
public class EvilPrimitives {
public static int one = 1;
public static int two;
public static int three = 3;
private static int four = 4;
public static int five = 5;
public static int eight = 8;
public static void main (String args[]) {
int six = 6;
five = confusion(five);
two = confusion2();
seven = 700;
Changer.changer1(six);
Changer.changer2(one);
Changer.changer3();
four = 400;
// What gets printed and what doesn't?
System.out.println(one);
System.out.println(two);
System.out.println(three);
System.out.println(four);
System.out.println(five);
System.out.println(six);
System.out.println(seven);
}
public static void confusion (int integer) {
int seven = 7;
return;
}
public static int confusion2() {
two = 2;
return 5;
}
}
8
//This program has three errors, one of which is a logical error
public class Changer {
public static int changer1 (int integer){
six = 600;
integer = 600;
EvilPrimitives.eight = 600;
}
public static int changer2 (int integer) {
EvilPrimitives.one = 100;
}
private void static changer3 () {
EvilPrimitives.three = 300;
return;
}
}
9
Chapter 7: Importing Scanner
// One error here
import java.util.Scanner;
public class ReadFromKeyboard {
public static void main (String heyguys[]) {
Scanner sc = new Scanner(System.in);
int i = sc.nextInt();
double j = sc.nextInt();
double k = sc.nextInt();
int l = sc.nextDouble();
System.out.println(i);
System.out.println(j);
System.out.println(k);
System.out.println(l);
}
}
10
Add New Comment