|
Thread Rules 1. This is not a "do my homework for me" thread. If you have specific questions, ask, but don't post an assignment or homework problem and expect an exact solution. 2. No recruiting for your cockamamie projects (you won't replace facebook with 3 dudes you found on the internet and $20) 3. If you can't articulate why a language is bad, don't start slinging shit about it. Just remember that nothing is worse than making CSS IE6 compatible. 4. Use [code] tags to format code blocks. |
On December 16 2016 00:09 travis wrote: sorry, I meant when are static initialization blocks ran
so the answer is "when the class is first loaded" But... what does that actually mean? When are classes loaded? You made me type all that out for an answer to the wrong question? :O Good thing Nesserev already answered this one
|
thanks guys, that's a lot of stuff you went through for me. luckily I think that's the majority of the questions I will be asking since the other 2 exams were a lot easier for me. I probably won't be asking many questions from here that I can just google the answer to anyways. I'll just check some work though, like this one
BST is created with these values inserted in order
6,5,2,8,10,3
this is as simple as it looks, right?
6 5 8 2 10 3
right?
P.S. blitz I think you may be off about the non static initialization block and Plant p = (Plant) fish
edit:
now it wants me to remove 5
uh I would just remove it and connect 2 to 6, right?
|
On December 16 2016 00:21 travis wrote:This was a random question on the 3rd exam, I am not sure why the prof put it on, I guess to be a dick Calculate the maximum number of edges possible in a directed graph with n vertices (no edges lead from a vertex to itself) Each vertices leads to (n-1) other vertices. But for each new vertices we can use 1 less of those edges. (n-1) + (n-2) + (n-3) .... + (1) + (0) 5 vertices this is 4+3+2+1 = 10 6 vertices this is 5+4+3+2+1 = 15 Is the answer N(n-1) / 2 ? I put n(n+1)/2 on the exam and missed it  oh wait no I did this wrong didn't I. Each node connects to n-1 other nodes it's n(n -1) ... no over 2 is n(n-1) correct? This probably doesn't matter I don't know why I am distracting myself Your latter answer is correct. In a fully connected non-reflexive directed graph there are 2 edges between any 2 nodes (one going in each direction). Thus, n nodes are connected to (n - 1) other nodes, so n(n-1) edges.
|
On December 16 2016 00:41 travis wrote:thanks guys, that's a lot of stuff you went through for me. luckily I think that's the majority of the questions I will be asking since the other 2 exams were a lot easier for me. I probably won't be asking many questions from here that I can just google the answer to anyways. I'll just check some work though, like this one BST is created with these values inserted in order 6,5,2,8,10,3 this is as simple as it looks, right? 6 5 8 2 10 3
right? P.S. blitz I think you may be off about the non static initialization block and Plant p = (Plant) fish edit: now it wants me to remove 5 uh I would just remove it and connect 2 to 6, right? Yup.
|
T/F: pre-order traversals are faster than post order traversals.
The answer is false, right? They all touch all of the nodes once?
List a reason benchmarking is better than analyzing complexity:
Because you may not be working with an "n" large enough for algorithmic complexity to have a practical effect? (this seems like a really shitty way to say this)
Why would you use an inner class?
my answer: To make a separate class that has access to private members of the enclosing class.
Why would you use an anonymous inner class?
my answer: To make an instantiation of a class with overloaded methods?
|
On December 16 2016 00:09 travis wrote: sorry, I meant when are static initialization blocks ran
so the answer is "when the class is first loaded" But... what does that actually mean? When are classes loaded? A class is loaded the first time it is used by your program. "Used" in this context means you try to instantiate it, try to access static variables, try to access static methods, use it as an array type, use the "class" object of the class, etc. The first time this happens in your code is the time the class gets loaded. At that point static initializers are run before any static members are accessed and before any instances are created. (Unless the static initializers themselves access static members or instantiate objects)
It is possible though to write your own ClassLoader which is able to load a class a second time (or even more times). In this case the static initializer will be run each time the class is loaded (if I remember correctly). Also note that if you load a class more then once then the class instance for each ClassLoader will have its own static attributes and Class-Object.
On December 16 2016 01:01 travis wrote: Why would you use an inner class?
my answer: To make a separate class that has access to private members of the enclosing class.
Why would you use an anonymous inner class?
my answer: To make an instantiation of a class with overloaded methods?
In both cases the answer is: Because its less code and because they can access private members. That are the only reasons. The "less code" one is the more important one in 99% of all cases.
|
On December 16 2016 01:01 travis wrote: T/F: pre-order traversals are faster than post order traversals.
The answer is false, right? They all touch all of the nodes once?
List a reason benchmarking is better than analyzing complexity:
Because you may not be working with an "n" large enough for algorithmic complexity to have a practical effect? (this seems like a really shitty way to say this)
Why would you use an inner class?
my answer: To make a separate class that has access to private members of the enclosing class.
Why would you use an anonymous inner class?
my answer: To make an instantiation of a class with overloaded methods?
I'd generally answer "false", because it depends on the specifics of the algorithm. If you have to traverse all nodes, it doesn't make any difference. If you can stop upon encountering an answer, then pre-order can be faster (or slower, if answers are only found in leaves). In terms of worst-case performance, it never makes a difference, but in terms of practical performance, it often does.
Your answer on benchmarking seems incomplete, although not completely wrong. Benchmarking (good benchmarking that is) tests your algorithm with realistic scenarios and can measure whether performance on such realistic data is acceptable. Algorithmic complexity gives you theoretical boundaries on the speed of your algorithm, but requires assumptions about the shape of your data, which may not correspond to reality. Moreover, if you focus too much on Big-O notation, you may miss important factors that make your algorithm extremely slow: a constant-time trillion operations is O(1), but will still take a significant amount of time.
Inner classes: as ^^^ answered.
|
practice question:
Define a class that implements a double-ended queue (dequeue) for integer values. Modify the class so it becomes a generic class.
Could someone help get me started on this? I am a bit confused just thinking about it
I'd use a linked list
add to the tail (tail.next = curr) add to the head (temp = head. head = curr. curr.next = temp) //is this right..?
remove from the head (head = head.next if it isn't null, otherwise it equals head). remove from the tail (iterate all the way through to find the one previous to tail and make it the tail...?)
So is this how I would do this?
or I could use a doubly linked list? which would be more work but could do the delete from the tail faster, right?
|
You're on the right track, but why single link your list? If you use a linked list, use a doubly linked list:
Think:
public class Node { int data; Node prev; Node next; }
Nasty ninja editor!
|
On December 16 2016 01:01 travis wrote:
Why would you use an anonymous inner class?
my answer: To make an instantiation of a class with overloaded methods?
if you would only ever need just one instance of a type you can make an anonymous inner class. In other words if you won't need to reuse it anywhere it can be an anonymous inner class.
example is a button click listener. if it's just a specific action for one button, instead of writing a separate class that implements the listener interface, you can use it as an anonym inner class.
other than that they are not a very critical feature of java. if it didn't exist you would just have a bunch of classes you only instantiate once in your life
|
On December 16 2016 02:16 travis wrote: practice question:
Define a class that implements a double-ended queue (dequeue) for integer values. Modify the class so it becomes a generic class. I read this and was bored. So I tried to implement an array based deque and see how long it would take me. Just in case you want to see an array based solution I will put the source code in spoilers. Note though that this code is neither documented nor properly tested (I did some basic testing; not enough to make any guarantees though) + Show Spoiler +import java.util.Arrays; import java.util.ConcurrentModificationException; import java.util.Iterator; import java.util.NoSuchElementException; import java.util.function.Function;
public class Q<E> implements Iterable<E> { public static final Function<Integer, Integer> DEFAULT_RESIZE_STRATEGY = length -> Math.min(length * 2, length + 1024); protected Function<Integer, Integer> resizeStrat = DEFAULT_RESIZE_STRATEGY; protected Object[] buffer; protected int head; protected int tail; protected long modCount; public Q(int capacity) { if (capacity <= 0) { throw new IllegalArgumentException("capacity <= 0; capacity="+capacity); } buffer = new Object[capacity]; } public void setResizingStrategy(Function<Integer, Integer> strategy) { if (strategy == null) { throw new IllegalArgumentException("strategy == null"); } resizeStrat = strategy; } public Function<Integer, Integer> getResizingStrategy() { return resizeStrat; } public int size() { if (tail > head) { return tail - head; } return head - tail; } public void pushAtHead(E value) { resizeIfNeeded(); buffer[calculateIndex(--head)] = value; modCount++; } @SuppressWarnings("unchecked") public E peekAtHead() { throwExceptionIfEmpty(); return (E) buffer[calculateIndex(head)]; } @SuppressWarnings("unchecked") public E popAtHead() { throwExceptionIfEmpty(); modCount++; return (E) buffer[calculateIndex(head++)]; } public void pushAtTail(E value) { resizeIfNeeded(); buffer[calculateIndex(tail++)] = value; modCount++; } @SuppressWarnings("unchecked") public E peekAtTail() { throwExceptionIfEmpty(); return (E) buffer[calculateIndex(tail - 1)]; } @SuppressWarnings("unchecked") public E popAtTail() { throwExceptionIfEmpty(); modCount++; return (E) buffer[calculateIndex(--tail)]; } @Override public Iterator<E> iterator() { final long modCountAtStart = modCount; return new Iterator<E>() { int current = head; @Override @SuppressWarnings("unchecked") public E next() { if (!hasNext()) { throw new NoSuchElementException(); } if (modCountAtStart != modCount) { throw new ConcurrentModificationException(); } return (E) buffer[calculateIndex(current++)]; } @Override public boolean hasNext() { return current != tail; } }; } protected int calculateIndex(int position) { int index = position % buffer.length; if (index < 0) { index += buffer.length; } return index; } protected void resizeIfNeeded() { if (size() != buffer.length) { return; } int newLength = getResizingStrategy().apply(buffer.length); Object[] newBuffer = new Object[newLength]; if (head < 0) { int headIndex = calculateIndex(head); int headLength = buffer.length - headIndex; System.arraycopy(buffer, headIndex, newBuffer, 0, headLength); int tailIndex = calculateIndex(tail); System.arraycopy(buffer, 0, newBuffer, headLength, tailIndex); } else { int headLength = buffer.length - head; System.arraycopy(buffer, head, newBuffer, 0, headLength); System.arraycopy(buffer, calculateIndex(tail-1), newBuffer, headLength, head); } head = 0; tail = buffer.length; buffer = newBuffer; } protected void throwExceptionIfEmpty() { if (size() == 0) { throw new IllegalStateException("size() == 0"); } } public String toDebugString() { StringBuilder builder = new StringBuilder(); builder.append("[buffer="); builder.append(Arrays.toString(buffer)); builder.append(", head="); builder.append(head); builder.append(", tail="); builder.append(tail); builder.append(", size="); builder.append(size()); builder.append("]"); return builder.toString(); } @Override public String toString() { int size = size(); if (size == 0) { return "[]"; } if (size == 1) { return "["+peekAtHead()+"]"; } StringBuilder sb = new StringBuilder(2 + 3 * size); sb.append('['); for (E value : this) { sb.append(value); sb.append(", "); } sb.delete(sb.length() - 2, sb.length()); sb.append(']'); return sb.toString(); } } You better stick to your doubly-linked-list implementation though. The array based one is nothing but trouble.
|
the exam went "alright". Probably got a B-.
One of the questions was actually about a queue (and about threading).
Basically we needed to make a thread safe queue, using an arraylist in our class as the basis for the queue
only methods we needed were enqueue and dequeue
and.. just to make sure I had the right idea here
to use an arraylist as a queue you need to:
add to end (simply use the arrays add)
and
return and remove the front (to do this, I used an integer that incremented every time I did a remove, to track the index)
is this the correct way to do that?
|
No that's not correct because you will run out of space very quickly as the popped front elements are never freed.
A thread safe queue backed by an arraylist would either be a circular buffer queue or some variant where you resized and/or reorganized the arraylist whenever the number of elements in the queue exceeds the size of the backing arraylist. I would assume they wanted the first case in which they should have specified some max number of elements or similar else this would be a stupid question.
|
that's a good point and actually I did think about resizing it but there were no specifications about how many elements it would be holding so it didn't seem like something I should be doing
it seemed like picking an arbitrary point to resize it made no sense, but maybe I should have done that anyways? but then it makes probably the longest code implementation even longer to write
|
arrayList.remove(0);
will always return the front element and shift the remaining ones to the left so you dont need to keep track of index since first element is always at index 0.
arraylist will resize itself upwards when its filled. You can call trimToSize() now and then to resize downwards but I don't think its that much necessary for an exam question.
make enqueue and dequeue methods synchronized, and then I think you are done?
+ Show Spoiler +
public class ThreadSafeQueue<T> {
ArrayList<T> backingList = new ArrayList<T>()
public synchronized T enqueue(T t) { backingList.add(t); return t; } public synchronized T dequeue() { if(backingList.isEmpty) { //throw some exception or return null } return backingList.remove(0); } }
|
On December 16 2016 12:43 Blisse wrote: No that's not correct because you will run out of space very quickly as the popped front elements are never freed.
A thread safe queue backed by an arraylist would either be a circular buffer queue or some variant where you resized and/or reorganized the arraylist whenever the number of elements in the queue exceeds the size of the backing arraylist. I would assume they wanted the first case in which they should have specified some max number of elements or similar else this would be a stupid question.
There's nothing about the circular buffer structure that makes this thread safe, unless I'm mistaken. I can't tell if that was your point, if it was I'm curious.
For example Java ArrayDeqeue is backed with a circular array setup but not thread safe. However Java does have a linked-list backed queue that uses cas operations to be safe for concurrent access with using locking.
mantequilla solution though seemingly horribly slow (.remove(0)) seems okay as everything synchronizes on `this`.
@travis you can read Java source for some of their queue implementations - Java collections are pretty decent to learn from I think and it's all open source with nice comments.
e.g. http://grepcode.com/file_/repository.grepcode.com/java/root/jdk/openjdk/8u40-b25/java/util/ArrayDeque.java/?v=source http://grepcode.com/file_/repository.grepcode.com/java/root/jdk/openjdk/8u40-b25/java/util/concurrent/ConcurrentLinkedQueue.java/?v=source
|
You use this:
List<Object> queue = Collections.synchronizedList(new ArrayList<>()); for synchronization and then call add to push and remove(0) to pop.
|
oh fuck
I was convinced they didn't shift over
|
in android, who takes care of the screens flow?
like a screen closes after an operation and another screen opens. I tried to do the flow in service, passing views as parameters to service methods to let the service decide when will the screen close and started new activities from services to decide which screen will be displayed next in what case etc.
But sometimes the screen needs to be closed without user interaction, so there's no calling a service method, in that case I used broadcasts from service, and activities as broadcast listeners.
I guess the norm is handling flow in activities but doesn't that give a lot of responsibility to them? They are both aware of the layout components and app flow logic. In that case, when I need to reuse a screen, should I only reuse the layout.xml file with a different activity?
I wrote some android in school but that was only to save the day, so whatever worked I didn't mess with.
|
Generally you'd do that with intents between activities, or with different fragments within a single activity. Not sure what the added benefit of having a service that controls your flow would be.
|
|
|
|