├── src └── main │ ├── resources │ └── oca_book_cover.png │ └── java │ └── eu │ └── verdelhan │ └── ocajexam │ ├── chapter8 │ ├── ex8_1 │ │ ├── Describable.java │ │ ├── Goatherd.java │ │ ├── Goat.java │ │ ├── Tester.java │ │ └── GoatShelter.java │ └── README.md │ ├── chapter1 │ ├── ex1_3 │ │ ├── planets │ │ │ ├── Mars.java │ │ │ ├── Earth.java │ │ │ └── Venus.java │ │ ├── README.md │ │ └── GreetingsUniverse.java │ ├── README.md │ ├── ex1_2 │ │ └── README.md │ └── ex1_1 │ │ └── TestClass.java │ ├── chapter9 │ ├── ex9_5 │ │ ├── README.md │ │ └── StackOverflowErrorExample.java │ ├── README.md │ ├── ex9_3 │ │ ├── MyException.java │ │ └── Tester.java │ ├── ex9_4 │ │ └── README.md │ ├── ex9_2 │ │ └── README.md │ └── ex9_1 │ │ └── README.md │ ├── chapter2 │ ├── README.md │ ├── ex2_4 │ │ └── README.md │ ├── ex2_3 │ │ ├── FishingSession.java │ │ └── FishermanCasting.java │ ├── ex2_1 │ │ └── StringInSwitchStatement.java │ └── ex2_2 │ │ └── ArrayListIteration.java │ ├── chapter6 │ ├── README.md │ └── ex6_1 │ │ └── ArrayListAndStandardArray.java │ ├── chapter7 │ ├── README.md │ └── ex7_1 │ │ ├── Plant.java │ │ ├── Simulator.java │ │ ├── Rose.java │ │ ├── MapleTree.java │ │ └── Tulip.java │ ├── chapter5 │ └── README.md │ ├── chapter10 │ └── README.md │ ├── chapter4 │ ├── ex4_1 │ │ ├── Main.java │ │ └── Car.java │ ├── README.md │ └── ex4_2 │ │ └── ObjectWithGettersAndSetters.java │ ├── chapter3 │ ├── README.md │ ├── ex3_2 │ │ └── StringTest.java │ ├── ex3_1 │ │ ├── AssignmentOperators.java │ │ └── README.md │ ├── ex3_4 │ │ └── StringComparison.java │ └── ex3_3 │ │ └── StringBuilderTest.java │ └── appendixh │ └── README.md ├── .gitignore ├── LICENSE └── README.md /src/main/resources/oca_book_cover.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mdeverdelhan/OCA-Java-SE-7-Programmer-I/HEAD/src/main/resources/oca_book_cover.png -------------------------------------------------------------------------------- /src/main/java/eu/verdelhan/ocajexam/chapter8/ex8_1/Describable.java: -------------------------------------------------------------------------------- 1 | package eu.verdelhan.ocajexam.chapter8.ex8_1; 2 | 3 | public interface Describable { 4 | public String getDescription(); 5 | } -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .classpath 2 | .project 3 | .settings/ 4 | .idea/ 5 | nb-configuration.xml 6 | nbactions.xml 7 | *.iml 8 | .DS_Store 9 | log/ 10 | bin/ 11 | target/ 12 | 13 | *.class 14 | 15 | # Package Files # 16 | *.jar 17 | *.war 18 | *.ear -------------------------------------------------------------------------------- /src/main/java/eu/verdelhan/ocajexam/chapter1/ex1_3/planets/Mars.java: -------------------------------------------------------------------------------- 1 | package eu.verdelhan.ocajexam.chapter1.ex1_3.planets; 2 | 3 | public class Mars { 4 | 5 | public Mars() { 6 | System.out.println("Hello from Mars!"); 7 | } 8 | } -------------------------------------------------------------------------------- /src/main/java/eu/verdelhan/ocajexam/chapter1/ex1_3/planets/Earth.java: -------------------------------------------------------------------------------- 1 | package eu.verdelhan.ocajexam.chapter1.ex1_3.planets; 2 | 3 | public class Earth { 4 | 5 | public Earth() { 6 | System.out.println("Hello from Earth!"); 7 | } 8 | } -------------------------------------------------------------------------------- /src/main/java/eu/verdelhan/ocajexam/chapter1/ex1_3/planets/Venus.java: -------------------------------------------------------------------------------- 1 | package eu.verdelhan.ocajexam.chapter1.ex1_3.planets; 2 | 3 | public class Venus { 4 | 5 | public Venus() { 6 | System.out.println("Hello from Venus!"); 7 | } 8 | } -------------------------------------------------------------------------------- /src/main/java/eu/verdelhan/ocajexam/chapter9/ex9_5/README.md: -------------------------------------------------------------------------------- 1 | *Here is the solution of the exercise 9-5.* 2 | 3 | A `StackOverflowError` is thrown [when a stack overflow occurs because an application recurses too deeply](http://docs.oracle.com/javase/7/docs/api/java/lang/StackOverflowError.html). 4 | -------------------------------------------------------------------------------- /src/main/java/eu/verdelhan/ocajexam/chapter2/README.md: -------------------------------------------------------------------------------- 1 | ### Chapter 2: Programming with Java Statements 2 | 3 | #### Self Test short answers 4 | 5 | Questions | Answers 6 | ----------|-------- 7 | 1 | D 8 | 2 | B 9 | 3 | A, C 10 | 4 | C 11 | 5 | A, B, C 12 | 6 | F 13 | 7 | A 14 | 8 | A, B, C 15 | 9 | B 16 | 10 | B 17 | -------------------------------------------------------------------------------- /src/main/java/eu/verdelhan/ocajexam/chapter9/README.md: -------------------------------------------------------------------------------- 1 | ### Chapter 9: Handling Exceptions 2 | 3 | #### Self Test short answers 4 | 5 | Questions | Answers 6 | ----------|-------- 7 | 1 | C 8 | 2 | A 9 | 3 | C 10 | 4 | A, B, C, E 11 | 5 | B 12 | 6 | B 13 | 7 | A 14 | 8 | A, C 15 | 9 | A 16 | 10 | A, B, C 17 | 11 | B 18 | 12 | D 19 | -------------------------------------------------------------------------------- /src/main/java/eu/verdelhan/ocajexam/chapter9/ex9_3/MyException.java: -------------------------------------------------------------------------------- 1 | package eu.verdelhan.ocajexam.chapter9.ex9_3; 2 | 3 | /** 4 | * My exception type. 5 | */ 6 | public class MyException extends Exception { 7 | 8 | private static final long serialVersionUID = 1L; 9 | 10 | public MyException() { 11 | super(); 12 | } 13 | 14 | public MyException(String s) { 15 | super(s); 16 | } 17 | } -------------------------------------------------------------------------------- /src/main/java/eu/verdelhan/ocajexam/chapter6/README.md: -------------------------------------------------------------------------------- 1 | ### Chapter 6: Programming with Arrays 2 | 3 | #### Self Test short answers 4 | 5 | Questions | Answers 6 | ----------|-------- 7 | 1 | A, F, I, N, O 8 | 2 | D 9 | 3 | A 10 | 4 | B 11 | 5 | C 12 | 6 | F 13 | 7 | D 14 | 8 | A 15 | 9 | B, C, D 16 | 10 | C 17 | 11 | A 18 | 12 | B 19 | 13 | B 20 | 14 | C 21 | 15 | B 22 | -------------------------------------------------------------------------------- /src/main/java/eu/verdelhan/ocajexam/chapter7/README.md: -------------------------------------------------------------------------------- 1 | ### Chapter 7: Understanding Class Inheritance 2 | 3 | #### Self Test short answers 4 | 5 | Questions | Answers 6 | ----------|-------- 7 | 1 | A 8 | 2 | D 9 | 3 | B 10 | 4 | B, C 11 | 5 | B, C 12 | 6 | C 13 | 7 | C 14 | 8 | B 15 | 9 | B 16 | 10 | A 17 | 11 | B 18 | 12 | B 19 | 13 | D 20 | 14 | B 21 | 15 | A 22 | -------------------------------------------------------------------------------- /src/main/java/eu/verdelhan/ocajexam/chapter8/README.md: -------------------------------------------------------------------------------- 1 | ### Chapter 8: Understanding Polymorphism and Casts 2 | 3 | #### Self Test short answers 4 | 5 | Questions | Answers 6 | ----------|-------- 7 | 1 | B 8 | 2 | B 9 | 3 | B, D 10 | 4 | A 11 | 5 | A 12 | 6 | C 13 | 7 | A 14 | 8 | A 15 | 9 | B 16 | 10 | B 17 | 11 | A 18 | 12 | A 19 | 13 | A, D 20 | 14 | B 21 | 15 | C 22 | -------------------------------------------------------------------------------- /src/main/java/eu/verdelhan/ocajexam/chapter5/README.md: -------------------------------------------------------------------------------- 1 | ### Chapter 5: Understanding Methods and Variable Scope 2 | 3 | #### Self Test short answers 4 | 5 | Questions | Answers 6 | ----------|-------- 7 | 1 | C 8 | 2 | C, E, H 9 | 3 | B 10 | 4 | B 11 | 5 | C 12 | 6 | A 13 | 7 | E 14 | 8 | A 15 | 9 | D 16 | 10 | B 17 | 11 | D 18 | 12 | B 19 | 13 | E 20 | 14 | D 21 | 15 | A, D 22 | -------------------------------------------------------------------------------- /src/main/java/eu/verdelhan/ocajexam/chapter1/ex1_3/README.md: -------------------------------------------------------------------------------- 1 | *Here is the solution of the exercise 1-3.* 2 | 3 | #### Compilation 4 | 5 | Command-line arguments needed to compile the complete program: 6 | 7 | `javac -cp src/main/java src/main/java/eu/verdelhan/ocajexam/chapter1/ex1_3/GreetingsUniverse.java` 8 | 9 | #### Interpretation 10 | 11 | Command-line arguments needed to interpret the program: 12 | 13 | `java -cp target/classes eu.verdelhan.ocajexam.chapter1.ex1_3.GreetingsUniverse` 14 | -------------------------------------------------------------------------------- /src/main/java/eu/verdelhan/ocajexam/chapter10/README.md: -------------------------------------------------------------------------------- 1 | ### Chapter 10: Working with Classes and Their Relationships 2 | 3 | #### Self Test short answers 4 | 5 | Questions | Answers 6 | ----------|-------- 7 | 1 | A, B, D 8 | 2 | C 9 | 3 | A 10 | 4 | D 11 | 5 | C 12 | 6 | C 13 | 7 | A 14 | 8 | A 15 | 9 | C 16 | 10 | B 17 | 11 | A 18 | 12 | C 19 | 13 | A 20 | 14 | B 21 | 15 | B, D 22 | -------------------------------------------------------------------------------- /src/main/java/eu/verdelhan/ocajexam/chapter4/ex4_1/Main.java: -------------------------------------------------------------------------------- 1 | package eu.verdelhan.ocajexam.chapter4.ex4_1; 2 | 3 | public class Main { 4 | 5 | public static void main(String[] args) { 6 | 7 | // Creating the car 8 | Car yourCar = new Car(230, true); 9 | 10 | // Accessing to Car object's fields 11 | System.out.println("Is the car running? " + yourCar.isRunning()); 12 | System.out.println("Car's top speed: " + yourCar.getTopSpeed()); 13 | } 14 | } -------------------------------------------------------------------------------- /src/main/java/eu/verdelhan/ocajexam/chapter4/ex4_1/Car.java: -------------------------------------------------------------------------------- 1 | package eu.verdelhan.ocajexam.chapter4.ex4_1; 2 | 3 | public class Car { 4 | 5 | private int topSpeed; 6 | private boolean running; 7 | 8 | Car(int topSpeed, boolean running) { 9 | this.running = running; 10 | this.topSpeed = topSpeed; 11 | } 12 | 13 | public boolean isRunning() { 14 | return running; 15 | } 16 | 17 | public int getTopSpeed() { 18 | return topSpeed; 19 | } 20 | } -------------------------------------------------------------------------------- /src/main/java/eu/verdelhan/ocajexam/chapter3/README.md: -------------------------------------------------------------------------------- 1 | ### Chapter 3: Programming with Java Operators and Strings 2 | 3 | #### Self Test short answers 4 | 5 | Questions | Answers 6 | ----------|-------- 7 | 1 | D 8 | 2 | C 9 | 3 | D 10 | 4 | A 11 | 5 | B 12 | 6 | C 13 | 7 | A, C 14 | 8 | E 15 | 9 | D 16 | 10 | A, C, D 17 | 11 | C 18 | 12 | D 19 | 13 | C 20 | 14 | B, C, D 21 | 15 | C 22 | 16 | B 23 | -------------------------------------------------------------------------------- /src/main/java/eu/verdelhan/ocajexam/chapter1/README.md: -------------------------------------------------------------------------------- 1 | ### Chapter 1: Packaging, Compiling, and Interpreting Java Code 2 | 3 | #### Self Test short answers 4 | 5 | Questions | Answers 6 | ----------|-------- 7 | 1 | A, B 8 | 2 | D 9 | 3 | B 10 | 4 | A, C, D 11 | 5 | D 12 | 6 | C 13 | 7 | C 14 | 8 | C 15 | 9 | D 16 | 10 | A, B 17 | 11 | A 18 | 12 | B 19 | 13 | D 20 | 14 | A, C 21 | 15 | A, C 22 | 16 | A 23 | -------------------------------------------------------------------------------- /src/main/java/eu/verdelhan/ocajexam/chapter2/ex2_4/README.md: -------------------------------------------------------------------------------- 1 | *Here is the solution of the exercise 2-4.* 2 | 3 | #### Java Keywords 4 | 5 | * Primary keywords you may see in **conditional** statements: `else`, `if`, `case`, `default`, `switch` 6 | 7 | * Primary keywords you may see in **iteration** statements: `do`, `for`, `while` 8 | 9 | * Primary keywords you may see in **transfer of control** statements: `break`, `continue`, `return` 10 | 11 | * Primary keywords you may see in **exception handling** statements: `throw`, `finally`, `try`, `catch` -------------------------------------------------------------------------------- /src/main/java/eu/verdelhan/ocajexam/chapter3/ex3_2/StringTest.java: -------------------------------------------------------------------------------- 1 | package eu.verdelhan.ocajexam.chapter3.ex3_2; 2 | 3 | /** 4 | * Official FindBugs™ website: http://findbugs.sourceforge.net/ 5 | */ 6 | public class StringTest { 7 | 8 | public static void main(String[] args) { 9 | // Fixing the performance bug (found with FindBugs™) 10 | // String s1 = new String("String one"); 11 | String s1 = "String one"; 12 | 13 | String s2 = "String two"; 14 | String s3 = "String " + "three"; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/main/java/eu/verdelhan/ocajexam/chapter4/README.md: -------------------------------------------------------------------------------- 1 | ### Chapter 4: Working with Basic Classes and Variables 2 | 3 | #### Self Test short answers 4 | 5 | Questions | Answers 6 | ----------|-------- 7 | 1 | D 8 | 2 | C 9 | 3 | A 10 | 4 | C 11 | 5 | D 12 | 6 | A 13 | 7 | A, C, D, F 14 | 8 | B 15 | 9 | A, D 16 | 10 | A, B, C 17 | 11 | B 18 | 12 | E 19 | 13 | C, D 20 | 14 | B 21 | 15 | C 22 | 16 | C 23 | 17 | I 24 | 18 | F 25 | -------------------------------------------------------------------------------- /src/main/java/eu/verdelhan/ocajexam/chapter1/ex1_3/GreetingsUniverse.java: -------------------------------------------------------------------------------- 1 | package eu.verdelhan.ocajexam.chapter1.ex1_3; 2 | 3 | import eu.verdelhan.ocajexam.chapter1.ex1_3.planets.Earth; 4 | import eu.verdelhan.ocajexam.chapter1.ex1_3.planets.Mars; 5 | import eu.verdelhan.ocajexam.chapter1.ex1_3.planets.Venus; 6 | 7 | public class GreetingsUniverse { 8 | 9 | public static void main(String[] args) { 10 | System.out.println("Greetings, Universe!"); 11 | Earth e = new Earth(); 12 | Mars m = new Mars(); 13 | Venus v = new Venus(); 14 | } 15 | } -------------------------------------------------------------------------------- /src/main/java/eu/verdelhan/ocajexam/chapter8/ex8_1/Goatherd.java: -------------------------------------------------------------------------------- 1 | package eu.verdelhan.ocajexam.chapter8.ex8_1; 2 | 3 | public class Goatherd implements Describable { 4 | 5 | private String description; 6 | 7 | public Goatherd(String name, int age) { 8 | description = "A " + age + "-years-old goatherd named " + name; 9 | } 10 | 11 | @Override 12 | public String getDescription() { 13 | return description; 14 | } 15 | 16 | public void feedGoats() { 17 | System.out.println("The goatherd is feeding its goats."); 18 | } 19 | } -------------------------------------------------------------------------------- /src/main/java/eu/verdelhan/ocajexam/chapter2/ex2_3/FishingSession.java: -------------------------------------------------------------------------------- 1 | package eu.verdelhan.ocajexam.chapter2.ex2_3; 2 | 3 | public class FishingSession { 4 | 5 | private String session; 6 | private boolean baitAvailable; 7 | 8 | public String getSession() { 9 | return session; 10 | } 11 | 12 | public void setSession(String session) { 13 | this.session = session; 14 | } 15 | 16 | public boolean isBaitAvailable() { 17 | return baitAvailable; 18 | } 19 | 20 | public void setBaitAvailable(boolean baitAvailable) { 21 | this.baitAvailable = baitAvailable; 22 | } 23 | } -------------------------------------------------------------------------------- /src/main/java/eu/verdelhan/ocajexam/chapter8/ex8_1/Goat.java: -------------------------------------------------------------------------------- 1 | package eu.verdelhan.ocajexam.chapter8.ex8_1; 2 | 3 | public class Goat implements Describable { 4 | 5 | private String description; 6 | 7 | public Goat(String name) { 8 | description = "A goat named " + name; 9 | } 10 | 11 | @Override 12 | public String getDescription() { 13 | return description; 14 | } 15 | 16 | /* 17 | * Implement other methods for a goat 18 | */ 19 | 20 | public void eat() { 21 | System.out.println("The goat is eating grass."); 22 | } 23 | 24 | public void sleep() { 25 | System.out.println("The goat is sleeping."); 26 | } 27 | } -------------------------------------------------------------------------------- /src/main/java/eu/verdelhan/ocajexam/chapter8/ex8_1/Tester.java: -------------------------------------------------------------------------------- 1 | package eu.verdelhan.ocajexam.chapter8.ex8_1; 2 | 3 | public class Tester { 4 | 5 | public static void main(String[] args) { 6 | new Tester(); 7 | } 8 | 9 | public Tester() { 10 | Goat goat = new Goat("Bob"); 11 | GoatShelter goatShelter = new GoatShelter(4, 4, 6); 12 | Goatherd goatherd = new Goatherd("Timmy", 42); 13 | System.out.println(description(goat)); 14 | System.out.println(description(goatShelter)); 15 | System.out.println(description(goatherd)); 16 | } 17 | 18 | private String description(Describable d) { 19 | return d.getDescription(); 20 | } 21 | } -------------------------------------------------------------------------------- /src/main/java/eu/verdelhan/ocajexam/chapter9/ex9_4/README.md: -------------------------------------------------------------------------------- 1 | *Here is the solution of the exercise 9-4.* 2 | 3 | #### __Code templates__ in NetBeans IDE 4 | 5 | The `ca` template produces: 6 | 7 | catch (Exception ex) { 8 | 9 | } 10 | 11 | 12 | The `fy` template produces: 13 | 14 | finally { 15 | 16 | } 17 | 18 | 19 | The `th` template produces: 20 | 21 | throws 22 | 23 | 24 | The `tw` template produces: 25 | 26 | throw new IllegalStateException(); 27 | 28 | 29 | The `twn` template produces: 30 | 31 | throw new 32 | 33 | 34 | The `trycatch` template produces: 35 | 36 | try 37 | { 38 | 39 | } 40 | catch (Exception e) 41 | { 42 | } 43 | -------------------------------------------------------------------------------- /src/main/java/eu/verdelhan/ocajexam/chapter2/ex2_1/StringInSwitchStatement.java: -------------------------------------------------------------------------------- 1 | package eu.verdelhan.ocajexam.chapter2.ex2_1; 2 | 3 | public class StringInSwitchStatement { 4 | 5 | public static void main(String[] args) { 6 | 7 | final String myString = "test"; 8 | 9 | switch (myString) { 10 | case "42": 11 | System.out.println("Unmanaged string. But retry when you'll want answers about universe."); 12 | break; 13 | case "test": 14 | System.out.println("String found!"); 15 | break; 16 | case "muhu": 17 | default: 18 | System.out.println("Unmanaged string"); 19 | break; 20 | } 21 | } 22 | } -------------------------------------------------------------------------------- /src/main/java/eu/verdelhan/ocajexam/chapter7/ex7_1/Plant.java: -------------------------------------------------------------------------------- 1 | package eu.verdelhan.ocajexam.chapter7.ex7_1; 2 | 3 | /** 4 | * A Plant class. 5 | */ 6 | public abstract class Plant { 7 | 8 | private int age = 0; 9 | private int height = 0; 10 | 11 | public int getAge() { 12 | return age; 13 | } 14 | 15 | public void addYearToAge() { 16 | age++; 17 | } 18 | 19 | public int getHeight() { 20 | return height; 21 | } 22 | 23 | public void setHeight(int height) { 24 | this.height = height; 25 | } 26 | 27 | abstract public void doSpring(); 28 | abstract public void doSummer(); 29 | abstract public void doFall(); 30 | abstract public void doWinter(); 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/eu/verdelhan/ocajexam/chapter9/ex9_5/StackOverflowErrorExample.java: -------------------------------------------------------------------------------- 1 | package eu.verdelhan.ocajexam.chapter9.ex9_5; 2 | 3 | public class StackOverflowErrorExample { 4 | 5 | public static void main(String[] args) { 6 | try { 7 | new StackOverflowErrorExample().recursiveMethod(); 8 | } catch (Throwable t) { 9 | System.out.println("Here is the throwable object: " + t); 10 | } 11 | } 12 | 13 | /** 14 | * A recursive method. 15 | */ 16 | public void recursiveMethod() { 17 | // Creating a useful array of objects 18 | Object[] objectArray = new Object[1024]; 19 | // Calling this method recursively 20 | new StackOverflowErrorExample().recursiveMethod(); 21 | } 22 | } -------------------------------------------------------------------------------- /src/main/java/eu/verdelhan/ocajexam/chapter8/ex8_1/GoatShelter.java: -------------------------------------------------------------------------------- 1 | package eu.verdelhan.ocajexam.chapter8.ex8_1; 2 | 3 | public class GoatShelter implements Describable { 4 | 5 | private String description; 6 | private int height; 7 | private int width; 8 | private int length; 9 | 10 | public GoatShelter(int height, int width, int length) { 11 | this.height = height; 12 | this.width = width; 13 | this.length = length; 14 | description = "A goat shelter that is " + height + " high, " + length + " long and " + width + " wide"; 15 | } 16 | 17 | @Override 18 | public String getDescription() { 19 | return description; 20 | } 21 | 22 | /* 23 | * Implement other methods for a goat shelter 24 | */ 25 | 26 | public boolean isOpened() { 27 | return true; 28 | } 29 | } -------------------------------------------------------------------------------- /src/main/java/eu/verdelhan/ocajexam/chapter9/ex9_2/README.md: -------------------------------------------------------------------------------- 1 | *Here is the solution of the exercise 9-2.* 2 | 3 | #### Observations about `FileNotFoundException` 4 | 5 | * [Class specification](http://docs.oracle.com/javase/7/docs/api/java/io/FileNotFoundException.html) 6 | * [FileNotFoundException on JDocs](http://www.jdocs.com/javase/7.b12/java/io/FileNotFoundException.html) 7 | 8 | 9 | #### Observations about `NumberFormatException` 10 | 11 | * [Class specification](http://docs.oracle.com/javase/7/docs/api/java/lang/NumberFormatException.html) 12 | * [NumberFormatException on JDocs](http://www.jdocs.com/javase/7.b12/java/lang/NumberFormatException.html) 13 | 14 | #### Observations about `AssertionError` 15 | 16 | * [Class specification](http://docs.oracle.com/javase/7/docs/api/java/lang/AssertionError.html) 17 | * [AssertionError on JDocs](http://www.jdocs.com/javase/7.b12/java/lang/AssertionError.html) 18 | -------------------------------------------------------------------------------- /src/main/java/eu/verdelhan/ocajexam/chapter2/ex2_2/ArrayListIteration.java: -------------------------------------------------------------------------------- 1 | package eu.verdelhan.ocajexam.chapter2.ex2_2; 2 | 3 | import java.util.ArrayList; 4 | 5 | public class ArrayListIteration { 6 | 7 | public static void main(String[] args) { 8 | 9 | // Creating the ArrayList 10 | ArrayList fishLengthList = new ArrayList(); 11 | 12 | // Adding the floats 13 | fishLengthList.add(10.0f); 14 | fishLengthList.add(15.5f); 15 | fishLengthList.add(18.0f); 16 | fishLengthList.add(29.5f); 17 | fishLengthList.add(45.5f); 18 | 19 | // Printing out only the numbers larger than the required length 20 | final float requiredLength = 28f; 21 | for (Float length : fishLengthList) { 22 | if (length >= requiredLength) { 23 | System.out.println(length); 24 | } 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/eu/verdelhan/ocajexam/chapter7/ex7_1/Simulator.java: -------------------------------------------------------------------------------- 1 | package eu.verdelhan.ocajexam.chapter7.ex7_1; 2 | 3 | public class Simulator { 4 | 5 | public static void main(String[] args) { 6 | 7 | System.out.println("Creating a mapple tree, a tulip... and a rose."); 8 | MapleTree mapleTree = new MapleTree(); 9 | Tulip tulip = new Tulip(); 10 | Rose rose = new Rose(); 11 | 12 | System.out.println("Entering a loop to simulate 3 years"); 13 | for (int i = 0; i < 3; i++) { 14 | mapleTree.doSpring(); 15 | tulip.doSpring(); 16 | rose.doSpring(); 17 | 18 | mapleTree.doSummer(); 19 | tulip.doSummer(); 20 | rose.doSummer(); 21 | 22 | mapleTree.doFall(); 23 | tulip.doFall(); 24 | rose.doFall(); 25 | 26 | mapleTree.doWinter(); 27 | tulip.doWinter(); 28 | rose.doWinter(); 29 | } 30 | } 31 | } -------------------------------------------------------------------------------- /src/main/java/eu/verdelhan/ocajexam/chapter9/ex9_3/Tester.java: -------------------------------------------------------------------------------- 1 | package eu.verdelhan.ocajexam.chapter9.ex9_3; 2 | 3 | public class Tester { 4 | 5 | public static void main(String[] args) { 6 | 7 | Tester t = new Tester(); 8 | 9 | try { 10 | t.tryToThrowAnException("test"); 11 | } catch (MyException e) { 12 | System.out.println("Never caught."); 13 | } 14 | 15 | try { 16 | t.tryToThrowAnException("l33t"); 17 | } catch (MyException e) { 18 | System.out.println("Exception caught. Here's the message: " + e.getMessage()); 19 | } 20 | } 21 | 22 | /** 23 | * Throws an exception if the string equals "l33t". 24 | * @param aString the string 25 | * @throws MyException if the string equals "l33t" 26 | */ 27 | private void tryToThrowAnException(String aString) throws MyException { 28 | if ("l33t".equals(aString)) { 29 | throw new MyException("Hey! You found a l33t!"); 30 | } 31 | } 32 | } -------------------------------------------------------------------------------- /src/main/java/eu/verdelhan/ocajexam/chapter3/ex3_1/AssignmentOperators.java: -------------------------------------------------------------------------------- 1 | package eu.verdelhan.ocajexam.chapter3.ex3_1; 2 | 3 | public class AssignmentOperators { 4 | 5 | /** 6 | * @param args 7 | */ 8 | public static void main(String[] args) { 9 | byte a; 10 | a = 10; 11 | System.out.println(a += 3); 12 | a = 15; 13 | System.out.println(a -= 3); 14 | a = 20; 15 | System.out.println(a *= 3); 16 | a = 25; 17 | System.out.println(a /= 3); 18 | a = 30; 19 | System.out.println(a %= 3); 20 | a = 35; 21 | // Optional as outside the scope of the exam 22 | System.out.println(a &= 3); 23 | a = 40; 24 | System.out.println(a ^= 3); 25 | a = 45; 26 | System.out.println(a |= 3); 27 | a = 50; 28 | System.out.println(a <<= 3); 29 | a = 55; 30 | System.out.println(a >>= 3); 31 | a = 60; 32 | System.out.println(a >>>= 3); 33 | // End optional 34 | } 35 | } -------------------------------------------------------------------------------- /src/main/java/eu/verdelhan/ocajexam/chapter4/ex4_2/ObjectWithGettersAndSetters.java: -------------------------------------------------------------------------------- 1 | package eu.verdelhan.ocajexam.chapter4.ex4_2; 2 | 3 | public class ObjectWithGettersAndSetters { 4 | 5 | private int answer = 0; 6 | private String foo = "bar"; 7 | 8 | public int getAnswer() { 9 | return answer; 10 | } 11 | 12 | public void setAnswer(int answer) { 13 | this.answer = answer; 14 | } 15 | 16 | public String getFoo() { 17 | return foo; 18 | } 19 | 20 | public void setFoo(String foo) { 21 | this.foo = foo; 22 | } 23 | 24 | public static void main(String[] args) { 25 | 26 | ObjectWithGettersAndSetters object = new ObjectWithGettersAndSetters(); 27 | System.out.println("This is an object with getters and setters: " + object); 28 | System.out.println("\tanswer: " + object.getAnswer()); 29 | object.setAnswer(42); 30 | System.out.println("\tnew answer: " + object.getAnswer()); 31 | System.out.println("\tfoo field: " + object.getFoo()); 32 | } 33 | } -------------------------------------------------------------------------------- /src/main/java/eu/verdelhan/ocajexam/chapter3/ex3_4/StringComparison.java: -------------------------------------------------------------------------------- 1 | package eu.verdelhan.ocajexam.chapter3.ex3_4; 2 | 3 | /** 4 | * See the documentation of the compareTo(String anotherString) method of the String class: 5 | * http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#compareTo(java.lang.String) 6 | */ 7 | public class StringComparison { 8 | 9 | public static void main(String[] args) { 10 | 11 | String eggs1 = "Cackle fruit"; 12 | String eggs2 = "Cackle fruit"; 13 | System.out.println(eggs1.compareTo(eggs2)); 14 | // 0 is printed because eggs1.equals(eggs2). 15 | 16 | eggs1 = "A fruit"; 17 | eggs2 = "The fruit"; 18 | System.out.println(eggs1.compareTo(eggs2)); 19 | // A negative integer is printed because eggs1 lexicographically precedes eggs2. 20 | 21 | eggs1 = "Your fruit"; 22 | eggs2 = "My fruit"; 23 | System.out.println(eggs1.compareTo(eggs2)); 24 | // A positive integer is printed because eggs1 lexicographically follows eggs2. 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2013 - Marc de Verdelhan 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /src/main/java/eu/verdelhan/ocajexam/chapter3/ex3_1/README.md: -------------------------------------------------------------------------------- 1 | *Here is the solution of the exercise 3-1.* 2 | 3 | #### Compound Assignment Statements 4 | 5 | Assigned Value of _a_ | Compound Assignment | Refactored Statement | New Value of _a_ 6 | -----------------------|--------------------------|-----------------------------|------------------ 7 | `a = 10;` |`a += 3;` |`a = a + 3;` |13 8 | `a = 15;` |`a -= 3;` |`a = a - 3;` |12 9 | `a = 20;` |`a *= 3;` |`a = a * 3;` |60 10 | `a = 25;` |`a /= 3;` |`a = a / 3;` |8 11 | `a = 30;` |`a %= 3;` |`a = a % 3;` |0 12 | `a = 35;` |`a &= 3;` |`a = a & 3;` |3 13 | `a = 40;` |`a ^= 3;` |`a = a ^ 3;` |43 14 | `a = 45;` |a |= 3; |a = a | 3; |47 15 | `a = 50;` |`a <<= 3;` |`a = (byte) (a << 3);` |-112 16 | `a = 55;` |`a >>= 3;` |`a = a >> 3;` |6 17 | `a = 60;` |`a >>>= 3;` |`a = a >>> 3;` |7 18 | -------------------------------------------------------------------------------- /src/main/java/eu/verdelhan/ocajexam/chapter9/ex9_1/README.md: -------------------------------------------------------------------------------- 1 | *Here is the solution of the exercise 9-1.* 2 | 3 | In Java, [an assertion is a statement that enables you to test your assumptions about your program](http://docs.oracle.com/javase/1.4.2/docs/guide/lang/assert.html). 4 | 5 | Assertions (i.e. `assert` keyword) **should** be used: 6 | 7 | * to check something that should never happen (while an exception should be used to check something that might happen) 8 | * for nonpublic [precondition, postcondition and class invariant checking](http://docs.oracle.com/javase/1.4.2/docs/guide/lang/assert.html#usage-conditions) 9 | * essentially for debugging purposes 10 | 11 | Assertions **should not** be used: 12 | 13 | * to check arguments or parameters of a public method (instead it should be checked by the method itself and an `IllegalArgumentException` or an `IllegalStateException` might be thrown) 14 | * as a replacement of unit testing 15 | 16 | Assertions [are disabled by default](http://docs.oracle.com/javase/7/docs/technotes/tools/windows/java.html). 17 | 18 | External resources: 19 | * [Where to use Assertion in Java code](http://javarevisited.blogspot.com/2012/01/where-to-use-assertion-in-java-code.html) 20 | * [When to use assertion vs exception](http://stackoverflow.com/questions/1957645/when-to-use-assertion-vs-exception) 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /src/main/java/eu/verdelhan/ocajexam/chapter1/ex1_2/README.md: -------------------------------------------------------------------------------- 1 | *Here is the solution of the exercise 1-2.* 2 | 3 | Evolution of the __Java Utilities API__ (i.e. `java.util` package) in subsequent releases of the Java SE 1.4 platform. 4 | 5 | #### J2SE 1.4.2 6 | * [API specification](http://docs.oracle.com/javase/1.4.2/docs/api/) 7 | * 6 Java Utilities API subpackages: 8 | * `java.util` 9 | * `java.util.jar` 10 | * `java.util.logging` 11 | * `java.util.prefs` 12 | * `java.util.regex` 13 | * `java.util.zip` 14 | 15 | #### J2SE 5.0 16 | * [API specification](http://docs.oracle.com/javase/1.5.0/docs/api/) 17 | * 9 Java Utilities API subpackages: 18 | * `java.util` 19 | * `java.util.concurrent` 20 | * `java.util.concurrent.atomic` 21 | * `java.util.concurrent.locks` 22 | * `java.util.jar` 23 | * `java.util.logging` 24 | * `java.util.prefs` 25 | * `java.util.regex` 26 | * `java.util.zip` 27 | 28 | #### Java SE 7 29 | * [API specification](http://docs.oracle.com/javase/7/docs/api/) 30 | * 10 Java Utilities API subpackages: 31 | * `java.util` 32 | * `java.util.concurrent` 33 | * `java.util.concurrent.atomic` 34 | * `java.util.concurrent.locks` 35 | * `java.util.jar` 36 | * `java.util.logging` 37 | * `java.util.prefs` 38 | * `java.util.regex` 39 | * `java.util.spi` 40 | * `java.util.zip` 41 | 42 | -------------------------------------------------------------------------------- /src/main/java/eu/verdelhan/ocajexam/chapter7/ex7_1/Rose.java: -------------------------------------------------------------------------------- 1 | package eu.verdelhan.ocajexam.chapter7.ex7_1; 2 | 3 | public class Rose extends Plant { 4 | 5 | private static final int AMOUNT_TO_GROW_IN_ONE_GROWING_SEASON = 2; 6 | 7 | private void grow() { 8 | int currentHeight = getHeight(); 9 | setHeight(currentHeight + AMOUNT_TO_GROW_IN_ONE_GROWING_SEASON); 10 | } 11 | 12 | @Override 13 | public void doSpring() { 14 | grow(); 15 | addYearToAge(); 16 | System.out.println("Spring: The rose is starting to grow leaves and new branches"); 17 | System.out.println("\tCurrent Age: " + getAge() + " Current Height: " + getHeight()); 18 | } 19 | 20 | @Override 21 | public void doSummer() { 22 | System.out.println("Summer: The rose has stopped growing and is flowering"); 23 | System.out.println("\tCurrent Age: " + getAge() + " Current Height: " + getHeight()); 24 | } 25 | 26 | @Override 27 | public void doFall() { 28 | System.out.println("Fall: The rose begins to wilt"); 29 | System.out.println("\tCurrent Age: " + getAge() + " Current Height: " + getHeight()); 30 | } 31 | 32 | @Override 33 | public void doWinter() { 34 | System.out.println("Winter: The rose must be protected"); 35 | System.out.println("\tCurrent Age: " + getAge() + " Current Height: " + getHeight()); 36 | } 37 | } -------------------------------------------------------------------------------- /src/main/java/eu/verdelhan/ocajexam/chapter2/ex2_3/FishermanCasting.java: -------------------------------------------------------------------------------- 1 | package eu.verdelhan.ocajexam.chapter2.ex2_3; 2 | 3 | public class FishermanCasting { 4 | private static FishingSession fishingSession = new FishingSession(); 5 | 6 | private static void castForFish() { 7 | fishingSession.setBaitAvailable(false); 8 | } 9 | 10 | public static void main(String[] args) { 11 | fishingSession.setSession("active"); 12 | int piecesOfBait = 5; 13 | piecesOfBait = 0; // Fox steals the bait! 14 | 15 | // Original loop 16 | // do { 17 | // castForFish(); 18 | // /* Check to see if bait is available */ 19 | // if (fishingSession.isBaitAvailable() == false) { 20 | // /* Place a new piece of bait on the hook */ 21 | // fishingSession.setBaitAvailable(true); 22 | // piecesOfBait--; 23 | // } 24 | // } while (piecesOfBait != 0); 25 | 26 | // Refactored loop 27 | while (piecesOfBait > 0) { 28 | castForFish(); 29 | /* Check to see if bait is available */ 30 | if (!fishingSession.isBaitAvailable()) { 31 | /* Place a new piece of bait on the hook */ 32 | fishingSession.setBaitAvailable(true); 33 | piecesOfBait--; 34 | } 35 | } 36 | System.out.println("No more pieces of bait."); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/main/java/eu/verdelhan/ocajexam/chapter1/ex1_1/TestClass.java: -------------------------------------------------------------------------------- 1 | package eu.verdelhan.ocajexam.chapter1.ex1_1; 2 | 3 | //import java.io.*; 4 | //import java.text.*; 5 | //import java.util.*; 6 | //import java.util.logging.*; 7 | 8 | import java.io.File; 9 | import java.io.IOException; 10 | import java.text.DateFormat; 11 | import java.text.SimpleDateFormat; 12 | import java.util.Date; 13 | import java.util.logging.FileHandler; 14 | import java.util.logging.Level; 15 | import java.util.logging.Logger; 16 | import java.util.logging.SimpleFormatter; 17 | 18 | public class TestClass { 19 | 20 | public static void main(String[] args) throws IOException { 21 | 22 | /* Ensure directory has been created */ 23 | new File("logs").mkdir(); 24 | 25 | /* Get the date to be used in the filename */ 26 | DateFormat df = new SimpleDateFormat("yyyyMMddhhmmss"); 27 | Date now = new Date(); 28 | String date = df.format(now); 29 | 30 | /* Set up the filename in the logs directory */ 31 | String logFileName = "logs/testlog-" + date + ".txt"; 32 | 33 | /* Set up logger */ 34 | FileHandler myFileHandler = new FileHandler(logFileName); 35 | myFileHandler.setFormatter(new SimpleFormatter()); 36 | Logger ocajLogger = Logger.getLogger("OCAJ Logger"); 37 | ocajLogger.setLevel(Level.ALL); 38 | ocajLogger.addHandler(myFileHandler); 39 | 40 | /* Log Message */ 41 | ocajLogger.info("\nThis is a logged information message."); 42 | 43 | /* Close the file */ 44 | myFileHandler.close(); 45 | } 46 | } -------------------------------------------------------------------------------- /src/main/java/eu/verdelhan/ocajexam/chapter7/ex7_1/MapleTree.java: -------------------------------------------------------------------------------- 1 | package eu.verdelhan.ocajexam.chapter7.ex7_1; 2 | 3 | public class MapleTree extends Plant { 4 | 5 | private static final int AMOUNT_TO_GROW_IN_ONE_GROWING_SEASON = 2; 6 | 7 | /* 8 | * A tree grows upwards a certain number of feet a year. A tree does not die 9 | * down to ground level during the winter. 10 | */ 11 | private void grow() { 12 | int currentHeight = getHeight(); 13 | setHeight(currentHeight + AMOUNT_TO_GROW_IN_ONE_GROWING_SEASON); 14 | } 15 | 16 | @Override 17 | public void doSpring() { 18 | grow(); 19 | addYearToAge(); 20 | System.out.println("Spring: The maple tree is starting to grow leaves and new branches"); 21 | System.out.println("\tCurrent Age: " + getAge() + " Current Height: " + getHeight()); 22 | } 23 | 24 | @Override 25 | public void doSummer() { 26 | grow(); 27 | System.out.println("Summer: The maple tree is continuing to grow"); 28 | System.out.println("\tCurrent Age: " + getAge() + " Current Height: " + getHeight()); 29 | } 30 | 31 | @Override 32 | public void doFall() { 33 | System.out.println("Fall: The maple tree has stopped growing and is losing its leaves"); 34 | System.out.println("\tCurrent Age: " + getAge() + " Current Height: " + getHeight()); 35 | } 36 | 37 | @Override 38 | public void doWinter() { 39 | System.out.println("Winter: The maple tree is dormant"); 40 | System.out.println("\tCurrent Age: " + getAge() + " Current Height: " + getHeight()); 41 | } 42 | } -------------------------------------------------------------------------------- /src/main/java/eu/verdelhan/ocajexam/chapter7/ex7_1/Tulip.java: -------------------------------------------------------------------------------- 1 | package eu.verdelhan.ocajexam.chapter7.ex7_1; 2 | 3 | public class Tulip extends Plant { 4 | 5 | private static final int AMOUNT_TO_GROW_IN_ONE_GROWING_SEASON = 1; 6 | 7 | /* 8 | * A tulip grows each year to the same height. During the winter they die 9 | * down to ground level. 10 | */ 11 | private void grow() { 12 | int currentHeight = getHeight(); 13 | setHeight(currentHeight + AMOUNT_TO_GROW_IN_ONE_GROWING_SEASON); 14 | } 15 | 16 | private void dieDownForWinter() { 17 | setHeight(0); 18 | } 19 | 20 | @Override 21 | public void doSpring() { 22 | grow(); 23 | addYearToAge(); 24 | System.out.println("Spring: The tulip is starting to grow up from the ground"); 25 | System.out.println("\tCurrent Age: " + getAge() + " Current Height: " + getHeight()); 26 | } 27 | 28 | @Override 29 | public void doSummer() { 30 | System.out.println("Summer: The tulip has stopped growing and is flowering"); 31 | System.out.println("\tCurrent Age: " + getAge() + " Current Height: " + getHeight()); 32 | } 33 | 34 | @Override 35 | public void doFall() { 36 | System.out.println("Fall: The tulip begins to wilt"); 37 | System.out.println("\tCurrent Age: " + getAge() + " Current Height: " + getHeight()); 38 | } 39 | 40 | @Override 41 | public void doWinter() { 42 | dieDownForWinter(); 43 | System.out.println("Winter: The tulip is dormant undergound"); 44 | System.out.println("\tCurrent Age: " + getAge() + " Current Height: " + getHeight()); 45 | } 46 | } -------------------------------------------------------------------------------- /src/main/java/eu/verdelhan/ocajexam/chapter3/ex3_3/StringBuilderTest.java: -------------------------------------------------------------------------------- 1 | package eu.verdelhan.ocajexam.chapter3.ex3_3; 2 | 3 | /** 4 | * See the documentation of the StringBuilder class: 5 | * http://docs.oracle.com/javase/7/docs/api/java/lang/StringBuilder.html 6 | */ 7 | public class StringBuilderTest { 8 | 9 | public static void main(String[] args) { 10 | 11 | // 1st StringBuilder 12 | StringBuilder sb1 = new StringBuilder(); 13 | sb1.append("Hello World!"); 14 | System.out.println("1st string builder\n" 15 | + "\tcapacity: " + sb1.capacity() + "\n" 16 | + "\tlength: " + sb1.length() + "\n" 17 | + "\tcontent: " + sb1.toString()); 18 | 19 | // 2nd StringBuilder 20 | StringBuilder sb2 = new StringBuilder('1'); 21 | sb2.append(':'); 22 | sb2.append(" this is a foo bar"); 23 | System.out.println("2nd string builder\n" 24 | + "\tcapacity: " + sb2.capacity() + "\n" 25 | + "\tlength: " + sb2.length() + "\n" 26 | + "\tcontent: " + sb2.toString()); 27 | 28 | // 3rd StringBuilder 29 | StringBuilder sb3 = new StringBuilder(42); 30 | sb3.append(""); 31 | System.out.println("3rd string builder\n" 32 | + "\tcapacity: " + sb3.capacity() + "\n" 33 | + "\tlength: " + sb3.length() + "\n" 34 | + "\tcontent: " + sb3.toString()); 35 | 36 | // 4th StringBuilder 37 | StringBuilder sb4 = new StringBuilder("This is a test."); 38 | sb4.reverse(); 39 | System.out.println("4th string builder\n" 40 | + "\tcapacity: " + sb4.capacity() + "\n" 41 | + "\tlength: " + sb4.length() + "\n" 42 | + "\tcontent: " + sb4.toString()); 43 | } 44 | } -------------------------------------------------------------------------------- /src/main/java/eu/verdelhan/ocajexam/appendixh/README.md: -------------------------------------------------------------------------------- 1 | ### Appendix H: Practice Exam 2 | 3 | #### Practice exam short answers 4 | 5 | Questions | Answers 6 | ----------|-------- 7 | 1 | D 8 | 2 | B 9 | 3 | B 10 | 4 | D 11 | 5 | A 12 | 6 | E 13 | 7 | A, D 14 | 8 | A 15 | 9 | E 16 | 10 | B 17 | 11 | A, C, D 18 | 12 | C, D 19 | 13 | A, C 20 | 14 | C 21 | 15 | A 22 | 16 | C 23 | 17 | D 24 | 18 | C 25 | 19 | A 26 | 20 | D 27 | 21 | D 28 | 22 | A 29 | 23 | A 30 | 24 | B 31 | 25 | A 32 | 26 | A 33 | 27 | B 34 | 28 | D 35 | 29 | A 36 | 30 | C 37 | 31 | D 38 | 32 | B, C 39 | 33 | C 40 | 34 | C, D, F 41 | 35 | A 42 | 36 | C, D 43 | 37 | B 44 | 38 | B 45 | 39 | D 46 | 40 | A, C, D 47 | 41 | B 48 | 42 | A, B, C 49 | 43 | D 50 | 44 | D 51 | 45 | D 52 | 46 | D 53 | 47 | C 54 | 48 | I 55 | 49 | B 56 | 50 | C 57 | 51 | C 58 | 52 | B 59 | 53 | D 60 | 54 | A, C 61 | 55 | B 62 | 56 | B 63 | 57 | C 64 | 58 | A, B, D 65 | 59 | B, C, D, E 66 | 60 | A 67 | 61 | C 68 | 62 | B 69 | 63 | B 70 | 64 | A, D 71 | 65 | B, C 72 | 66 | B 73 | 67 | B 74 | 68 | C, D, E 75 | 69 | B 76 | 70 | A, E 77 | 71 | D 78 | 72 | B 79 | 73 | C 80 | 74 | D 81 | 75 | B 82 | 76 | C, D 83 | 77 | B 84 | 78 | A 85 | 79 | A 86 | 80 | A, D 87 | 81 | A, B 88 | 82 | A, C, D 89 | 83 | D 90 | 84 | C 91 | 85 | A 92 | 86 | A, D 93 | 87 | B 94 | 88 | A, B, D 95 | 89 | B 96 | 90 | A 97 | -------------------------------------------------------------------------------- /src/main/java/eu/verdelhan/ocajexam/chapter6/ex6_1/ArrayListAndStandardArray.java: -------------------------------------------------------------------------------- 1 | package eu.verdelhan.ocajexam.chapter6.ex6_1; 2 | 3 | import java.util.ArrayList; 4 | 5 | public class ArrayListAndStandardArray { 6 | 7 | public static void main(String[] args) { 8 | 9 | // Initializing the standard array 10 | double temperaturesArray[] = { 24, 22, 26, 27, 27, 27, 28 }; 11 | 12 | // Initializing the ArrayList 13 | ArrayList temperaturesArrayList = new ArrayList(7); 14 | temperaturesArrayList.add(24.0); 15 | temperaturesArrayList.add(22.0); 16 | temperaturesArrayList.add(26.0); 17 | temperaturesArrayList.add(27.0); 18 | temperaturesArrayList.add(27.0); 19 | temperaturesArrayList.add(27.0); 20 | temperaturesArrayList.add(28.0); 21 | 22 | // Finding the average 23 | double averageFromArray = getAverage(temperaturesArray); 24 | System.out.println("Average from a standard array: " + averageFromArray); 25 | double averageFromArrayList = getAverage(temperaturesArrayList); 26 | System.out.println("Average from a ArrayList: " + averageFromArrayList); 27 | 28 | // Same average value for the two structures? 29 | boolean sameValue = (averageFromArray == averageFromArrayList); 30 | System.out.println("Same value for standard array and ArrayList: " + sameValue); 31 | } 32 | 33 | /** 34 | * @param values the array of double values 35 | * @return the average value of a standard array of double values 36 | */ 37 | public static double getAverage(double[] values) { 38 | double average = 0; 39 | for (double v : values) { 40 | average += v; 41 | } 42 | average /= values.length; 43 | return average; 44 | } 45 | 46 | /** 47 | * @param values the ArrayList of double values 48 | * @return the average value of an ArrayList of double values 49 | */ 50 | public static double getAverage(ArrayList values) { 51 | double average = 0; 52 | for (Double v : values) { 53 | average += v; 54 | } 55 | average /= values.size(); 56 | return average; 57 | } 58 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # OCA-Java-SE-7-Programmer-I 2 | 3 | This repository contains my implementation of the exercises provided by the *OCA Java SE 7 Programmer I Study Guide*. 4 | 5 | ### About the guide 6 | 7 | ![OCA book cover](src/main/resources/oca_book_cover.png?raw=true) 8 | 9 | The ***OCA Java SE 7 Programmer I Study Guide*** (E. Finegan, R. Liguori) has been designed to help you pass the Oracle's *Java SE 7 Programmer I* exam to achieve the *OCA Java SE 7 Programmer Certification*. The book contains coding examples, tests, exercises, and more. Its purpose is to familiarize you with Java fundamentals, concepts, tools, and technologies that will be represented on the exam. 10 | 11 | ### About the exam 12 | 13 | The [OCA Java SE 7 Programmer exam](http://education.oracle.com/pls/web_prod-plq-dad/db_pages.getpage?page_id=5001&get_params=p_exam_id:1Z0-803&p_org_id=&lang=) (a.k.a. *Java SE 7 Programmer I* exam, a.k.a. *Oracle Certified Associate* exam, a.k.a. *1Z0-803* exam) covers the fundamental basics of the Java programming language and an overview of all Java programming technologies (and also a little UML). 14 | Passing this exam allows you to achieve the ***Oracle Certified Associated, Java SE 7 Programmer Certification***. Once you've achieved it, you can take the [Java SE 7 Programmer II (1Z0-804)](http://education.oracle.com/pls/web_prod-plq-dad/db_pages.getpage?page_id=5001&get_params=p_exam_id:1Z0-804&p_org_id=&lang=) to earn the *Oracle Certified Professional, Java SE 7 Certification*. 15 | 16 | More about the Oracle Certification Program: its [Wikipedia article](http://en.wikipedia.org/wiki/Oracle_Certification_Program) and its [official website](http://education.oracle.com/pls/web_prod-plq-dad/db_pages.getpage?page_id=39). 17 | 18 | ## Exercises 19 | 20 | Here are the solutions of the exercises for each chapter. 21 | 22 | * [Chapter 1: Packaging, Compiling, and Interpreting Java Code](http://github.com/mdeverdelhan/OCA-Java-SE-7-Programmer-I/tree/master/src/main/java/eu/verdelhan/ocajexam/chapter1) 23 | * [Chapter 2: Programming with Java Statements](http://github.com/mdeverdelhan/OCA-Java-SE-7-Programmer-I/tree/master/src/main/java/eu/verdelhan/ocajexam/chapter2) 24 | * [Chapter 3: Programming with Java Operators and Strings](http://github.com/mdeverdelhan/OCA-Java-SE-7-Programmer-I/tree/master/src/main/java/eu/verdelhan/ocajexam/chapter3) 25 | * [Chapter 4: Working with Basic Classes and Variables](http://github.com/mdeverdelhan/OCA-Java-SE-7-Programmer-I/tree/master/src/main/java/eu/verdelhan/ocajexam/chapter4) 26 | * [Chapter 5: Understanding Methods and Variable Scope](http://github.com/mdeverdelhan/OCA-Java-SE-7-Programmer-I/tree/master/src/main/java/eu/verdelhan/ocajexam/chapter5) 27 | * [Chapter 6: Programming with Arrays](http://github.com/mdeverdelhan/OCA-Java-SE-7-Programmer-I/tree/master/src/main/java/eu/verdelhan/ocajexam/chapter6) 28 | * [Chapter 7: Understanding Class Inheritance](http://github.com/mdeverdelhan/OCA-Java-SE-7-Programmer-I/tree/master/src/main/java/eu/verdelhan/ocajexam/chapter7) 29 | * [Chapter 8: Understanding Polymorphism and Casts](http://github.com/mdeverdelhan/OCA-Java-SE-7-Programmer-I/tree/master/src/main/java/eu/verdelhan/ocajexam/chapter8) 30 | * [Chapter 9: Handling Exceptions](http://github.com/mdeverdelhan/OCA-Java-SE-7-Programmer-I/tree/master/src/main/java/eu/verdelhan/ocajexam/chapter9) 31 | * [Chapter 10: Working with Classes and Their Relationships](http://github.com/mdeverdelhan/OCA-Java-SE-7-Programmer-I/tree/master/src/main/java/eu/verdelhan/ocajexam/chapter10) 32 | * [Appendix H: Practice Exam](http://github.com/mdeverdelhan/OCA-Java-SE-7-Programmer-I/tree/master/src/main/java/eu/verdelhan/ocajexam/appendixh) 33 | 34 | ## About 35 | 36 | #### Support 37 | 38 | Ask all questions to [my 𝕏 account](http://www.x.com/marcdeverdelhan). 39 | 40 | 41 | --------------------------------------------------------------------------------