Skip to content

Commit 9633716

Browse files
unknownunknown
authored andcommitted
Fork and merge
2 parents 9a4176c + aad2f9a commit 9633716

File tree

58 files changed

+1645
-30
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+1645
-30
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
.mtj.tmp/
55

66
# Package Files #
7-
*.jar
87
*.war
98
*.ear
109

Binary file not shown.

week1/2-OOP/README.md

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
Make a class Car.
55
Make several(at least 4) subtypes of Car, representing the different brands of cars - Audi, BMW, Wolkswagen (of course!), etc.
66

7-
Implement the toString method, to match the type of the class. For example, an object of type Audi should return "Audi@1f52ac3";
8-
97
Later, for the Audi type you need to return its mileage, because all the German brand manufacturers require it - how you will do it?
108

119
###2###
@@ -60,33 +58,22 @@ See http://www.programmerinterview.com/index.php/java-questions/java-anonymous-c
6058
Return your anonymous implementation from a factory method!
6159

6260
###8###
63-
Make a mutable class 'Matrix' represeting a NxM (int[][]) matrix. Think good about your constructors, your data. Your goal is to make this class as convenient as possible for the person using it.
61+
Make a mutable class 'Matrix' represeting a NxM (Pixel[][]) matrix. `Pixel` is a POJO containing three float values - R, G, B. Think good about your constructors, your data. Your goal is to make this class as convenient as possible for the person using it.
6462

6563
Your class `Matrix` should have another cool thing about it => it would allow operations with every pixel.
6664
In order for this to work, your class should declare a public method `operate(MatrixOperation op)`
6765

68-
Where `MatrixOperation` is an interface, containing a method 'int withPixel(int x, int y, int[][] matrix)'.
66+
Where `MatrixOperation` is an interface, containing a method 'Pixel withPixel(int x, int y, Pixel[][] matrix)'.
6967
*Think - why are we getting x, y, and the matrix instead of just passing the pixel value?*
7068

7169
Implement the toString(), converting the whole matrix into a String in a way that it would be visible on the console.
7270

7371
The method should `operate(MatrixOperation op)` should call the `withPixel` method for every x and y in the matrix and assign the result of `withPixel` to the matrix.
7472

7573
Now that you have this, implement:
76-
-a naive binarization filter, using a simple threshold, for instance 127.
77-
-a naive gaussian blur filter, which just averages the color of this pixel by using the colors of its neighbour pixels!
78-
79-
*Bonuses* - Use a *local class* to fetch information about the matrix using the MatrixOperation interface.
80-
81-
Make an implementation of MatrixOperation, that you can use to:
82-
- get average of pixels
83-
- get most used color in the matrix
84-
- get an image histogram of the matrix
85-
86-
See http://docstore.mik.ua/orelly/java-ent/jnut/ch03_11.htm
87-
88-
Idea -- implement gausian filter?
89-
74+
- brightness reduce
75+
- grayscale
76+
- gaussian blur (optional)
9077

9178
###9###
9279
Create a friendly interactive calculator in java

week2/1-CollectionsAndGenerics1/README.md

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,6 @@ Such expression is correct if:
77
- the number of opening brackets equals exactly the number of closing brackets
88
- at no point in the string the number of closing brackets is higher than the number of opening brackets. E.g. `())(()` is not a valid one, because at index 2 there is a second closing bracket with only one opening bracket.
99

10-
TDD, of course.
11-
12-
*Hint (and also spoiler)*
13-
Is there a way you can use a *stack* in here?
14-
1510
###1.Reverse a generic collection###
1611
Declare a method, expecting a Collection and reverse it in your method. Return the same collection given, do not return a new collection!.
1712
Either use:
Lines changed: 230 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,230 @@
1+
All of these should go in a project named 'Collections1'
2+
###0.Check if an expression of brackets is correct###
3+
Such expression is a string like `"()()())))((())("` - it has multiple brackets, your job is to check whether the expression is correct.
4+
Such expression is correct if:
5+
- it starts with a `'('`
6+
- it ends with a `')'`
7+
- the number of opening brackets equals exactly the number of closing brackets
8+
- at no point in the string the number of closing brackets is higher than the number of opening brackets. E.g. `())(()` is not a valid one, because at index 2 there is a second closing bracket with only one opening bracket.
9+
10+
###1.Reverse a generic collection###
11+
Declare a method, expecting a Collection and reverse it in your method. Return the same collection given, do not return a new collection!.
12+
Either use:
13+
`static <T> void reverse (Collection<T> collection)`
14+
or make it a concrete type:
15+
`static void reverse(Collection<Integer> collection)`
16+
17+
*Do not try to use* Collections.reverse. It works only for a List, and not for collections : )
18+
19+
20+
###2.Implement an on/off collection###
21+
Implement a Collection, in which, if you add an element twice, the element gets removed from your collection.
22+
23+
*Hint and edge case spoiler:* How would you handle null objects? :)
24+
*Fun fact:*: As logical idea behind a collection is an abstraction of a... well, bunch of elements, allowing null actually makes no sense.
25+
Note that the java implementations are broken on several places, allowing nulls to creep into the collections, making all kinds of havoc later on. Don't blindly follow the java implementations, do better.
26+
27+
28+
###3.Make a bounded queue###
29+
A bounded queue is queue with a 'limit' of maximum elements.
30+
Your `BoundedQueue` class of course should implement the `Queue` interface.
31+
Example usage of your `BoundedQueue` class:
32+
```
33+
BoundedQueue<Integer> boundedQueue = new BoundedQueue<>(3);
34+
boundedQueue.offer(1);
35+
boundedQueue.offer(2);
36+
boundedQueue.offer(3);
37+
boundedQueue.offer(4);
38+
boundedQueue.offer(5);
39+
System.out.println(boundedQueue.toString()); //3,4,5
40+
```
41+
*Hint:* View the java impl and interface:)
42+
43+
###4.Rotate the elements of a collection###
44+
Make a void *utility* method that rotates the contents of a collection.
45+
46+
You are given a collection containing `[one, two, three, four, five, six, seven]`
47+
"Rotating" it once means the collection becoming `[seven, one, two, three, four, five, six]`
48+
49+
```java
50+
void rotate(Collection<Integer> collection, int rotateStep )
51+
```
52+
53+
`rotateStep` can be negative - meaning you are rotating to the left, and not to the right.
54+
55+
56+
###5.Given a list contaning some duplicate values, find the first unique element###
57+
Make a *utility* method, which returns the first unique element in a given argument of type `Collection<Integer>`.
58+
Example:
59+
60+
```java
61+
Collection<Integer> ints = Arrays.asList(1,2,3,4,5,5,4,3,1);
62+
System.out.println(yourMethod(ints)) //2;
63+
```
64+
65+
66+
###6.Given several sets, find the duplicating elements###
67+
Write a *utility* method, that takes several sets, and returns a set that contains the duplicating elements of all the other sets.
68+
69+
```
70+
A = {1,2,3,4,5}
71+
B = {4,5,6}
72+
C = {5,6,7,8}
73+
```
74+
75+
`yourMethd(A,B,C) => D{5}`
76+
77+
###7.Given a list of Students, sort them by grade###
78+
Lets say you have a `List<Student>`, where a Student is a class, containing two fields: `name` and `grade`.
79+
80+
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.
81+
82+
*hint* : Use Comparable or Comparator
83+
84+
###8.Give me the median, quick!###
85+
Make an interface `Statistics`, which has these operations:
86+
87+
```
88+
-getMean();
89+
-getMedian();
90+
-getMode();
91+
-getRange();
92+
```
93+
94+
If you don't know what Mean,Median, Mode and Range are, see http://www.purplemath.com/modules/meanmode.htm
95+
96+
Make an implementation of this interface, with an additional operation `add(int number)` (Work with integers only).
97+
I would also like to able to look through all the integers I've given you, with a `for(Integer i : isntanceOfYourClass) {... }`.
98+
99+
So, as a client of your code, I will be adding some integers(just like in a list), and would want you to give me the mean, median, mode and range.
100+
101+
*Bonus/Challenge*
102+
Every method from `Statistics` interface should complete in O(1) time.
103+
This is a little bit on the algorithm side, and you might need some interesting data structures : )
104+
Come back to this when you are done with all the tasks.
105+
106+
107+
*Hints*
108+
Solve this one *iteratively* with TDD.
109+
110+
111+
###9.Implement a class ToDoList###
112+
Imagine you have a world of stuff to do.
113+
Homeworks, courseworks, exams, even preparing for HackBulgaria!
114+
Unfortunately you do not have much time - you need to eat, you need to sleep, you need to have a good time.
115+
116+
Now, you need to sort your priorities right! Make a class `ToDoList`, which supports the following operations:
117+
`void add(Task t)`
118+
`void markFinished(Task t)`
119+
`void markCancelled(Task t)`
120+
`Task getTop()`
121+
`boolean canFinish()`
122+
`int getRemainigTime()` //calculates the time remaining after you've done all of your tasks.
123+
124+
... where `Task` is a class which represents something you have to do. What data/methods should it have? What is common for all the tasks you need to get done?
125+
A `Task` should at the very least have a priority and a time required in order to finish.
126+
You should take an integer in your constructor - the ammount of hours available for each task.
127+
128+
Example usage of your class:
129+
130+
```java
131+
ToDoList todo = new ToDoList(11); //11 hours remaining!
132+
todo.addTask(new StudyForAlgebraTask(10)); //maximum priority!
133+
todo.addTask(new LearnGeometryTask()); //default priority, smaller than 10
134+
todo.addTask(new GoOutTask(1.5f)); //default priority, smaller than 10
135+
todo.addTask(new SleepTask()); //straight 8 hours, of course!
136+
137+
if (todo.canFinish()){
138+
System.out.println("Woohoo!");
139+
} else {
140+
System.out.println("I am ...screwed :(");
141+
}
142+
143+
System.out.println(todo.top()) //StudyForAlgebraTask
144+
System.out.println(todo.getTimeNeeded()) //sum of the time needed for every task added in todo list
145+
```
146+
147+
*Hints*
148+
See Comparable and Comparator classes in Java. Check out the PriorityQueue class.
149+
http://javarevisited.blogspot.com/2011/06/comparator-and-comparable-in-java.html
150+
151+
Use of generic classes.
152+
153+
###10.Make a utility method that converts a hashMap into a very human-readable string###
154+
Make a *utility* method that takes a `HashMap<? extends Object, ? extends Object>`
155+
and *returns a String*, representing the HashMap.
156+
157+
I want you to make a utility method that does this, *do not* override `toString()` in hashMap.
158+
159+
Example:
160+
161+
```java
162+
String result = YourUtilsClass.yourFirstUtilMethod(map);
163+
System.out.println(result) // { key1:value1, key2:value2, key3:value3 }
164+
```
165+
166+
###11.Count occurences of words in text###
167+
Make a *utility* method that takes a `String text` and returns a map, which on every word in `text` maps the number of times this word has been used.
168+
169+
See Example:
170+
171+
```java
172+
Map<String, Integer> result = YourUtilsClass.yourSecondUtilMethod("Ninjas are all over the place! We are all going to die!");
173+
System.out.println(YourUtilsClass.yourFirstUtilMethod(result));
174+
```
175+
176+
Outputs:
177+
178+
```
179+
// { Ninjas:1, are:2, all:2, over:1, the:1, place!:1, We:1, going:1, to:1, die!:1 }
180+
```
181+
182+
Retain the ordering of the elements!
183+
184+
<<<<<<< HEAD
185+
*Hint*
186+
LinkedHashMap
187+
188+
###12.Cryptoanalysis fun###
189+
=======
190+
##Epic bonus!##
191+
###Cryptoanalysis fun###
192+
>>>>>>> upstream/master
193+
There is an old technique for encrypting messages - shuffling the letters. For instance, if we take the string `Godzilla`, one crypted version of it
194+
is `Mrezotti`.
195+
The cipher here is
196+
```
197+
G=m
198+
o=r
199+
d=e
200+
i=o
201+
l=t
202+
z=z
203+
```
204+
205+
Your task here is to decrypth the following text:
206+
```
207+
Ajb vnf guj luqv akjvojufq . Sk qkkj egvfs Rkhfwu Lumemu'q akhhfjvq kj vnf ohifjbojc essoxew kg Dofck Ckqve , nfsf'q Ffsjejbk Tkssfq vk qnkr vnf ohifvukuq zkujc dkz nkr ov'q bkjf Iv'q vnf qehf fxfsz quhhfs . Nk-kjf neq fxfs coxfj hf ejzvnojc kj e iwevf . Tnfz hebf hf akhifvf gks hz iweaf vnf xfsz hkhfjv I ckv ojvk vnf gosqv vfeh; vnev'q rnev vnfz veucnv hf ejb ov'ww df vnev rez ujvow I wfexf . Tnf nocnfs puewovz iwezfsq vnev akhf - ejb Dofck Ckqve oq puewovz - vnf csfevfs vnf akhifvovokj roww df ejb vnf dfvvfs ov'ww df gks vnf vfeh .Ajb gfes jkv ( ks , ewvfsjevoxfwz , bkj'v cfv vkk ftaovfb) , Tkssfq neq jk iwejq vk vuam veow ejb suj ejb wfexf noq iezanfamq dfnojb . I bkj'v nexf ejz iwejq vk hkxf erez socnv jkr . Ig rf woqvfjfb vk eww vnf suhkusq rf'b nexf 50 iwezfsq . Nkr'q jkv vnf socnv vohf vk df vnojmojc edkuv Cnfwqfe . I'h qusf vnf awud oq rksmojc vk ohiskxf vnf vfeh , duv eww vnev hevvfsq socnv jkr oq vnf Wkswb Cui .Om , vnev dov edkuv vnf 50 iwezfsq hebf hf anuamwf . Noaf kjf , Ffsjejbk .Nkr , oj ej obfew rkswb , rf rkuwb ifsneiq cfv vnfqf rksbq gskh Lumemu , rnkh rf'b womf vk dfakhf wfcfjbesz ev Cnfwqfe . Buv rf bkj'v woxf oj ej obfew rkswb , ejb ojqvfeb rf cfv vnfqf rksbq gskh Tkssfq , rnkh rf'b womf vk dfakhf e wkjc-gksckvvfj hfhksz ev Cnfwqfe . Aweq .Tnev dfojc qeob , qojaf ov'q deqoaewwz ej ohikqqodwf vk veqm vk sob kusqfwxfq kg vnf £100h gwki , ev wfeqv nf'q ckv vnf socnv evvovubf . Hussez?
208+
```
209+
210+
Once more, your task is not to make a program that *deciphers every possible text on the planet*, you just have to decipher *this text*. And by decipher, I mean extract the meaning of it - let's not care about missed dots, commas, word casing and such stuff.
211+
212+
####Instructions and hints####
213+
214+
A usually good counter-attacking technique is to simply make a histogram of the usage of letters in the *given* text and comapre it to the histogram of letters *in the language*.
215+
For example, in the english language, the letter 'e' is the most frequently used, in a total of 13% of all letters. So there is a good chance the most commonly used letter in the given text is 'e'.
216+
217+
You can see some frequencies and letter distribution facts here:
218+
http://en.wikipedia.org/wiki/Letter_frequency
219+
http://www.cryptograms.org/letter-frequencies.php
220+
221+
222+
Some hints:
223+
- Ignore one-letter words. They are likely dots or hyphens or commas, you will add them later.
224+
- Trim spaces on words after you parse them. Make sure you are not analysing empty spaces.
225+
- Watch out for letter casing! Make sure you analyze uppercase 'T' the same as downcase 't'.
226+
- Make a method `String applyCipher(Map<Character, Character> cipher)`. You will likely find a good use for it. Of course, it is not mandatory.
227+
- As this problem requires some visual examination, do *not try* to solve in TDD at first. In fact, in general, apply *TDD* only when you have something to test for.
228+
- If frequency attack turns out to be not good enough, check [the list of 5000 most used words in English](https://docs.google.com/spreadsheets/d/1LuHWIlshSqwfr3AKwvqIoXAGZrNmsPUopwfhc3DhtS4/edit#gid=0). There is a good chance some of those will be from the first words you unveil!
229+
230+

week2/2-Exceptions/README.md

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
All tasks should go into 'Exceptions1' project in your workspace.
22

3-
### Unchecked (runtime) exception example usage
3+
### 1. Unchecked (runtime) exception example usage
44
Design an unchecked Exception class "DatabaseCorruptedException", which is to be thrown whenever a corrupted database object is detected (e.g. "" for username in table `Users`).
55
Use good and concise exception message, explaining what is wrong with the object `User` object - for this excercise you can assume that the `username` field is "".
66

77
*Database Corruption is not expected to happen. People should not need to 'catch' an eventual database corruption on every interaction/transaction with a database, because this is not an expected situation and it is not OK to be fault-tolerant on such scenarios*
88

99
Make a method that throws this exception. Use it and see your message in the stack trace.
1010

11-
### Checked exception example usage
11+
### 2. Checked exception example usage
1212
Imagine you are designing an API for using services.
1313

1414
*In the real world, services break all the time. Network connections go down, firewalls go crazy, routers get re(s)tarted, etc. It is more or less expected of you to be network fault-tolerant, as networks are not perfect and they just can't be.*
@@ -22,32 +22,38 @@ After you are done with that, try and use your method.
2222
Observe how every time you use it, you are forced to think *what will happen if this service fails?*.
2323

2424

25-
### Make the 'Time' class fool-proof
25+
### 3. Make the 'Time' class fool-proof
2626
Remember you wrote a `Time` class, some weeks ago? Go back, copy your class into your project and **make it fool-proof**. Protect the poor public user of your class using exceptions. Think of some unpleasent (or edge-case) situations that may occur while using your class.
2727

2828
Hint: Check http://docs.oracle.com/javase/7/docs/api/java/lang/IllegalArgumentException.html and
2929
http://stackoverflow.com/questions/15208544/when-should-illegalargumentexception-be-thrown
3030

31-
### Do not allow them to insert null as a key!
31+
### 4. Do not allow them to insert null as a key!
3232
You know HashMap in java. They are completely happy with (null, null) key-value pairs.
3333
Let's say we are not happy with this being the default behavior. Especially for **keys**.
3434

3535
Extends `HashMap` and make it optional for your class to accept null **keys**, but by default, set this option to `false`. (Use a flag, make a constructor with the option.)
3636
Fix the `put`, `get` methods accordingly to allow or not allow null keys.
3737
What should you do in those methods if your object is set not to allow null keys? Throw your very own, good-named, custom-made exception with a good message, of course.
3838

39-
### Impement your very own immutable list
39+
### 5. Impement your very own immutable and unmodifiable list
4040
Create a class 'ImmutableList', which implements List. It's okay to extend something else.
4141
Your delcaration should be like this:
4242
`public class Immutable<E> extends ArrayList<E>`
4343

44-
But make your instances immutable, and throw exception when anyone tries to modify them.
44+
Unmodifiable collection is collection which oce crated can not accept new ellements and its ellements can not be removed.Its add and remove methods should throw exception.
45+
46+
immutable collection is when its ellements can not be changed once put in it.
47+
4548
Of course, in order for your class to be immutable, you need good constructors.
4649
Make a constructor that accepts a `Collection<? extends E> collection` in which you add all the elements from `collection` to your list, and forbid anyone to modify it by throwing an exception when anyone tries to do that.
4750

4851
Implement `Arrays.asList()` factory method functionality - declare a static method `<T> List<T> asList(T... arguments)`.
4952
Use the `@SafeVarargs` annotation on top of your method declararation to tell the compiler you won't be naughty.
5053

54+
Hint: In the `get` method return a clone of the element, so it can't be modified from the outside. Use Apache Commons for object cloning https://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/SerializationUtils.html
55+
56+
##Bonus!##
5157
### Implement a XmlMarkupBuilder class ###
5258
Make an easy to use XmlMarkupBuilder class, which creates a **valid xml**
5359

week2/2-Exceptions/week2/.classpath

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" path="src"/>
4+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
5+
<classpathentry kind="output" path="bin"/>
6+
</classpath>

week2/2-Exceptions/week2/.project

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>week2</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.eclipse.jdt.core.javanature</nature>
16+
</natures>
17+
</projectDescription>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
eclipse.preferences.version=1
2+
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
4+
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
5+
org.eclipse.jdt.core.compiler.compliance=1.8
6+
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
7+
org.eclipse.jdt.core.compiler.debug.localVariable=generate
8+
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
9+
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
10+
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
11+
org.eclipse.jdt.core.compiler.source=1.8

0 commit comments

Comments
 (0)