diff --git a/.gitignore b/.gitignore index d33629a..a22fcf3 100644 --- a/.gitignore +++ b/.gitignore @@ -4,8 +4,13 @@ .mtj.tmp/ # Package Files # +*.jar *.war *.ear # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml hs_err_pid* + +# IntelliJ specific files # +*.idea/ +*.iml diff --git a/week1/2-OOP/prereading.md b/week1/2-OOP/prereading.md index de733f3..85d317f 100644 --- a/week1/2-OOP/prereading.md +++ b/week1/2-OOP/prereading.md @@ -1,32 +1,80 @@ *On OOP in General* -https://www.youtube.com/watch?v=lbXsrHGhBAU +https://www.youtube.com/watch?v=lbXsrHGhBAU + +*Q&A* + +* What are the main principles of OOP programming? +* What is Polymorphism? +* What is Encapsolation? +* What is Data Abstraction? +* What is Inheritance? +* Whis is the founding principle...? + +*Time : 30 min* *OOP Trail @ Oracle* http://docs.oracle.com/javase/tutorial/java/concepts/index.html +*Time : 15 min* + *Class and Objects @ Oracle (Those pages are actually Sun's property!)* http://docs.oracle.com/javase/tutorial/java/javaOO/ http://docs.oracle.com/javase/tutorial/java/concepts/object.html +*Q&A* +* What is Class? +* What is Interface? +* What is isntace field, what is class field? +* How is the behaviour of the class defined? +* What is "this" keyword? How it is used? + +*Time : 30 min* + *More on Classes and objects* http://www.javaworld.com/article/2075202/core-java/object-oriented-language-basics-part-1.html?page=1 +*Time : 15 min* + *Constructors & new()* http://www.javaworld.com/article/2076204/core-java/understanding-constructors.html http://www.javaworld.com/article/2076614/core-java/object-initialization-in-java.html +*Q&A* +* Default init values ? +* What if I use return in constructor? +* Usage of super and this in constructors? Where they should be put in the constructor? +* Usage of try/catch in contructors? + +*Time : 45 min* + *Abstract classes vs Interfaces* http://www.programmerinterview.com/index.php/java-questions/interface-vs-abstract-class/ http://www.javaworld.com/article/2077421/learn-java/abstract-classes-vs-interfaces.html?null +*Time : 15 min* + *Override, overload, hide* http://docs.oracle.com/javase/tutorial/java/IandI/override.html +*Q&A* +* What is @Override annotation? +* Default methods in interfaces? Their scope and how are they hidden? + +*Time : 20 min* + *Subtyping & Liskov principle* https://www.youtube.com/watch?v=IL61KMYK0b8 +*Q&A* +*What is Liskov principle?* + +*Time : 10 min* *Default Methods* http://docs.oracle.com/javase/tutorial/java/IandI/defaultmethods.html +*Time : 20 min* + *Invariants* http://people.cs.aau.dk/~normark/oop-csharp/html/notes/contracts_themes-class-inv-sect.html + +*Time : 20 min* diff --git a/week2/1-CollectionsAndGenerics1/README.md b/week2/1-CollectionsAndGenerics1/README.md index 5418cce..87e2986 100644 --- a/week2/1-CollectionsAndGenerics1/README.md +++ b/week2/1-CollectionsAndGenerics1/README.md @@ -38,6 +38,7 @@ boundedQueue.offer(4); boundedQueue.offer(5); System.out.println(boundedQueue.toString()); //3,4,5 ``` +*Hint:* View the java impl and interface:) ###4.Rotate the elements of a collection### Make a void *utility* method that rotates the contents of a collection. @@ -78,6 +79,8 @@ Lets say you have a `List`, where a Student is a class, containing two Sort them by their grades first. Their grades actually are integers => 2,3,4,5,6. If two students happen to have the same grades, sort those two by their names. +*hint* : Use Comparable or Comparator + ###8.Give me the median, quick!### Make an interface `Statistics`, which has these operations: @@ -145,6 +148,8 @@ System.out.println(todo.getTimeNeeded()) //sum of the time needed for every task See Comparable and Comparator classes in Java. Check out the PriorityQueue class. http://javarevisited.blogspot.com/2011/06/comparator-and-comparable-in-java.html +Use of generic classes. + ###10.Make a utility method that converts a hashMap into a very human-readable string### Make a *utility* method that takes a `HashMap` and *returns a String*, representing the HashMap. @@ -178,6 +183,10 @@ Retain the ordering of the elements! ##Epic bonus!## ###Cryptoanalysis fun### +======= +*Hint* +LinkedHashMap + There is an old technique for encrypting messages - shuffling the letters. For instance, if we take the string `Godzilla`, one crypted version of it is `Mrezotti`. The cipher here is diff --git a/week2/1-CollectionsAndGenerics1/prereading.md b/week2/1-CollectionsAndGenerics1/prereading.md index 4ad7248..095be7f 100644 --- a/week2/1-CollectionsAndGenerics1/prereading.md +++ b/week2/1-CollectionsAndGenerics1/prereading.md @@ -1,26 +1,58 @@ *Simplest use of an ArrayList* http://www.mkyong.com/java/how-to-loop-arraylist-in-java/ +*Time : 15 min* + *Check out some of the public methods of ArrayList* See its behaviour, what it can and can't do :) http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html +*Q&A* +* How would you implement a ArrayList + +*Time : 30 min* + *Some examples of the most used collections class in Java: ArrayList* http://javarevisited.blogspot.com/2011/05/example-of-arraylist-in-java-tutorial.html +*Time : 30 min* + *Difference between Iterator and ListIterator* (if you are unfamiliar with iterators, skip this one for now : ) http://stackoverflow.com/questions/10977992/difference-between-iterator-and-listiterator +*Time : 15 min* + *Diagram of ALL of java's collections framework:* http://www.codejava.net/java-core/collections/overview-of-java-collections-framework-api-uml-diagram +*Q&A* +* What is Set +* What is Map +* What do you understand about generics + +*Time : 20 min* + ####Part 2#### *A HashMap example* http://www.tutorialspoint.com/java/java_hashmap_class.htm +*Time : 30 min* + *HashMap vs Hashtable vs TreeMap in Java* http://www.programcreek.com/2013/03/hashmap-vs-treemap-vs-hashtable-vs-linkedhashmap/ +*Q&A* +* Differences between all + +*Time : 30 min* + *Equals and hashcode in java* http://stackoverflow.com/questions/27581/overriding-equals-and-hashcode-in-java +*Q&A* +* Mutable keys in Maps + +*Time : 20 min* + +*Additional reading* +http://javarevisited.blogspot.com/2013/10/what-is-priorityqueue-data-structure-java-example-tutorial.html diff --git a/week2/2-Exceptions/prereading.md b/week2/2-Exceptions/prereading.md index 4dcf481..c8c5f2f 100644 --- a/week2/2-Exceptions/prereading.md +++ b/week2/2-Exceptions/prereading.md @@ -1,9 +1,25 @@ *Exceptions hierarch in Java* http://www.javamex.com/tutorials/exceptions/exceptions_hierarchy.shtml +*Q&A* +* What is Throwable? +* What is Error? +* What is runtime exception? +* What is checked/uncheck exeption? +* What throw does in method? +* What is throws declaration of a method? +* EPIC QUESTION : What is Thread.setDefaultUncaughtExceptionHandler()? + +*Time : 30 min* + *Try/catch/finally example* http://www.tutorialspoint.com/java/java_exceptions.htm +*Q&A* +*What is the purpose of try/catch/finnaly? Give example with resources! What are the tricky parts this scheme? + +*Time : 30 min* + *Try with resources in Java 7* http://www.mkyong.com/java/try-with-resources-example-in-jdk-7/ diff --git a/week6/1-Multithreading/Core_Java_2_week6/src/com/hackbulgaria/corejava/WaitNotifyMechanism.java b/week6/1-Multithreading/Core_Java_2_week6/src/com/hackbulgaria/corejava/WaitNotifyMechanism.java new file mode 100644 index 0000000..95eacc6 --- /dev/null +++ b/week6/1-Multithreading/Core_Java_2_week6/src/com/hackbulgaria/corejava/WaitNotifyMechanism.java @@ -0,0 +1,66 @@ +package com.hackbulgaria.corejava; + +public class WaitNotifyMechanism { + public static long startTime = System.currentTimeMillis(); + public static Integer counter = 0; + public static final Object monitor = new Object(); + private static int turn = 0; + + public static void increment() { + System.out.println("Incrementing from Thread : " + Thread.currentThread().getName() + " " + counter ); + counter++; + } + + public static void main(String[] args) throws InterruptedException { + Thread t1 = new Thread() { + public void run() { + for (int i = 0; i < 2_000_000; i++) { + synchronized (monitor) { + while (turn != 1) { + try { + monitor.wait(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + + } + increment(); + + turn = (turn + 1) % 2; + monitor.notify(); + } + + } + } + }; + Thread t2 = new Thread() { + + public void run() { + for (int i = 0; i < 2_000_000; i++) { + synchronized (monitor) { + while (turn != 0) { + try { + monitor.wait(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + + increment(); + + turn = (turn + 1) % 2; + monitor.notify(); + } + } + } + }; + t1.setName("T1"); + t2.setName("T2"); + t1.start(); + t2.start(); + t1.join(); + t2.join(); + System.out.println(counter); + System.out.println(System.currentTimeMillis() - startTime); + } +} diff --git a/week6/1-Multithreading/Core_Java_2_week6/src/week6/task2/producer_consumer/ConsumerThread.java b/week6/1-Multithreading/Core_Java_2_week6/src/week6/task2/producer_consumer/ConsumerThread.java new file mode 100644 index 0000000..bc0a660 --- /dev/null +++ b/week6/1-Multithreading/Core_Java_2_week6/src/week6/task2/producer_consumer/ConsumerThread.java @@ -0,0 +1,17 @@ +package week6.task2.producer_consumer; + + +public class ConsumerThread implements Runnable { + private MyBlockingQueue queue; + + public ConsumerThread(MyBlockingQueue queue) { + this.queue = queue; + } + + @Override + public void run() { + while (queue.size() > 0) { + queue.poll(); + } + } +} diff --git a/week6/1-Multithreading/Core_Java_2_week6/src/week6/task2/producer_consumer/ProducerThread.java b/week6/1-Multithreading/Core_Java_2_week6/src/week6/task2/producer_consumer/ProducerThread.java new file mode 100644 index 0000000..72927dc --- /dev/null +++ b/week6/1-Multithreading/Core_Java_2_week6/src/week6/task2/producer_consumer/ProducerThread.java @@ -0,0 +1,18 @@ +package week6.task2.producer_consumer; + +public class ProducerThread implements Runnable { + + private MyBlockingQueue queue; + public ProducerThread(MyBlockingQueue queue) { + this.queue = queue; + + } + + @Override + public void run() { + for (int i = 1; i < 1001; i++) { + queue.add(Thread.currentThread().getName() + ": " + i + " - " + System.nanoTime() + % 1_000_000); + } + } +} diff --git a/week6/1-Multithreading/Core_Java_2_week6/src/week6/task2/producer_consumer/ThreadRunner.java b/week6/1-Multithreading/Core_Java_2_week6/src/week6/task2/producer_consumer/ThreadRunner.java new file mode 100644 index 0000000..3810328 --- /dev/null +++ b/week6/1-Multithreading/Core_Java_2_week6/src/week6/task2/producer_consumer/ThreadRunner.java @@ -0,0 +1,38 @@ +package week6.task2.producer_consumer; + +public class ThreadRunner { + public static void main(String[] args) throws InterruptedException { + MyBlockingQueue queue = new MyBlockingQueue<>(100); + + ProducerThread p1 = new ProducerThread(queue); + ProducerThread p2 = new ProducerThread(queue); + ProducerThread p3 = new ProducerThread(queue); + + ConsumerThread c1 = new ConsumerThread(queue); + ConsumerThread c2 = new ConsumerThread(queue); + + Thread t1 = new Thread(p1); + t1.setName("producer 1"); + Thread t2 = new Thread(p2); + t2.setName("producer 2"); + Thread t3 = new Thread(p3); + t3.setName("producer 3"); + + Thread t4 = new Thread(c1); + t4.setName("consumer 1"); + Thread t5 = new Thread(c2); + t5.setName("consumer 2"); + + t1.start(); + t2.start(); + t3.start(); + t4.start(); + t5.start(); +// t1.join(); +// t2.join(); +// t3.join(); +// t4.join(); +// t5.join(); + + } +} diff --git a/week8/interviews/OrderTaxProblem/src/Customer.java b/week8/interviews/OrderTaxProblem/src/Customer.java new file mode 100644 index 0000000..ad99d57 --- /dev/null +++ b/week8/interviews/OrderTaxProblem/src/Customer.java @@ -0,0 +1,4 @@ +public class Customer +{ + public String StateCode; +} diff --git a/week8/interviews/OrderTaxProblem/src/Order.java b/week8/interviews/OrderTaxProblem/src/Order.java new file mode 100644 index 0000000..6b8b8cf --- /dev/null +++ b/week8/interviews/OrderTaxProblem/src/Order.java @@ -0,0 +1,27 @@ +import java.util.LinkedList; +import java.util.List; + +public class Order +{ + public List orderItems = new LinkedList(); + public double CalculateTotal(Customer customer) + { + double total = 0; + for (OrderItem item : orderItems){ + total += item.Cost*item.Quantity; + }; + + double tax; + if (customer.StateCode == "BUL") + tax = total * .2; + else if (customer.StateCode == "GER") + tax = total * .3; + else + tax = .1; + + total = total + tax; + return total; + } + + +} \ No newline at end of file diff --git a/week8/interviews/OrderTaxProblem/src/OrderItem.java b/week8/interviews/OrderTaxProblem/src/OrderItem.java new file mode 100644 index 0000000..0ad8ef9 --- /dev/null +++ b/week8/interviews/OrderTaxProblem/src/OrderItem.java @@ -0,0 +1,6 @@ +public class OrderItem +{ + public int Quantity; + public double Cost; +} + diff --git a/week8/interviews/OrderTaxProblem/src/Problem.java b/week8/interviews/OrderTaxProblem/src/Problem.java new file mode 100644 index 0000000..5695ac7 --- /dev/null +++ b/week8/interviews/OrderTaxProblem/src/Problem.java @@ -0,0 +1,17 @@ +import java.util.LinkedList; + +public class Problem { + public static void main(String[] args) { + Customer bgCustomer = new Customer(); + bgCustomer.StateCode="BUL"; + + OrderItem item1 = new OrderItem(); + item1.Cost = 100; + item1.Quantity = 10; + + Order order = new Order(); + order.orderItems = new LinkedList(); + order.orderItems.add(item1); + System.out.println(order.CalculateTotal(bgCustomer)); + } +}