vendredi 27 mars 2015

Should I initialize a member variable in declaration when it is initialized by a constructor parameter?


Which is recommended:



class Foo
{
public X x = new X();
public Foo(X _x)
{
x = _x;
}
}


Or



class Foo
{
public X x;
public Foo(X _x)
{
x = _x;
}
}


Please note in the second version, it is probable that I introduce other constructors where x may not be initialized!





How to draw rectangles for 2D arrays


I have an 2D boolean array that looks like this:



[0][0][0][0][0][0][0][0][0][0]
[0][0][0][0][0][0][0][0][0][0]
[0][0][0][0][0][0][0][0][0][0]
[0][0][0][1][1][1][0][0][0][0]
[0][0][0][1][1][1][0][0][0][0]
[0][0][0][1][1][1][0][0][0][0]
[0][0][0][1][1][1][0][0][0][0]
[0][0][0][1][1][1][0][0][0][0]
[0][0][0][0][0][0][0][0][0][0]
[0][0][0][0][0][0][0][0][0][0]


(0 = false, 1 = true)


I've found a way to combine neighbouring zeros on the horizontal plane like like:



[----------------------------]
[----------------------------]
[----------------------------]
[-------][1][1][1][----------]
[-------][1][1][1][----------]
[-------][1][1][1][----------]
[-------][1][1][1][----------]
[-------][1][1][1][----------]
[----------------------------]
[----------------------------]


The code for achieving this is as such:



for y = 0 to ySize
for x = 0 to xSize
if array[y][x] = 0
set width = 1

for x2 = x + 1 to xSize
if array[y][x2] = 0
width++
else
break

add object at x, y and set width to width
set x = x + width - 1


So it loops the 2D array looking for zeros, and if it finds one, it loops until the end of the row to find where it is no longer a zero.


It then creates an object at the position where it first found a zero, and stretches it to fill the part until it is not a zero.


I want to adapt this code in order to make rectangles instead of long planks, like:



____________________________
| |
| | <------ rect at 0, 0 with size (10, 3)
|____________________________|
| |[1][1][1]| |
| |[1][1][1]| |
| |[1][1][1]| | <------ rect at 6, 3 with size (4, 5)
| |[1][1][1]| |
| |[1][1][1]|__________|
| | |
|_______|____________________| <------ rect at 3, 8 with size (7, 2)

^
|
|
rect at 0, 3 with size (3, 7)


So instead of making 15 horizontal planks, it makes 4 rectangles that surround the ones.


In short, I want to create an algorithm that will, when given an 2D array of ones and zeros, create rectangles around the ones.


For a smaller example:



{
[0][0][0] { 0, 0, 0 },
[0][1][0] { 0, 1, 0 },
[0][0][0] { 0, 0, 0 }
}


Will return the position and dimension of rectangles such as:



[-------]
[-][1][-]
[-------]

{
{0, 0, 3, 1}, //rect at (0, 0) with width 3, height 1
{0, 1, 1, 1}, //rect at (0, 1) with width 1, height 1
{2, 1, 1, 1}, //rect at (2, 1) with width 1, height 1
{0, 2, 3, 1} //rect at (0, 2) with width 3, height 1
}


What should this algorithm look like?


Edit: There should be some effort to minimize number of rectangles used (otherwise, it could just leave it as is).


Also, it doesn't matter if it prefers horizontal or vertical rectangles. Example:



[-------]
[-][1][-]
[-------]


is the same as



_ _
| |[ ]| |
| |[1]| |
|_|[ ]|_|


In the end, both have 4 rectangles surrounding the [1].





Programming Concepts [on hold]


Can someone tell me the all the programming concept to be an expert programmer ? like Delegation, Classes etc.. any good books to learn those all the programming concepts that i can go from beginner to advance ? Please give me all the programming concepts that i need to know to be a good programmer. I know this may be off the topic of this forum. But I really really need help from you guys. This is the only place i know where i can get advice from expert programmers. So please help.


Thank you very much





how we can use class and components?


ans me that ques immediadiately its urgent





How is this data filter technique called


A while ago I read about a data filter technique or algorithm that could quickly determine if an object was not similar to an object in a group of objects. It was explained as having multiple overlaying stencils where each object is one stencil. The object (imagined as a cut out piece of paper) is placed underneath these group of stencils.

If some of the object is visible, than you know for certain that it is not similar to one of the other objects/stencils; if the object was not visible, there is a change it is similar to one of the objects, but that is not certain.


This is used for fast determining negatives, while a possible positive takes some more time (it must be compared to all of the other stencils to proof similarity).


I can not remember the name of this technique and I want to read more about it. How is it called?





Reading from html page that needs cookies passed and form data


I am writing a program in java in order to practice tough coding problems and learn more.


Program description The program reads first names from a text file one by one, then enters them into facebook search bar, and WITHOUT hitting enter, it sees who the first person is that pops up(when you type a name into facebook searchbar people drop down) and takes the name and stores it into a text file. Aka it sees which people are the first result for a list of a bunch of names.


The problem I am having with this is 2 fold.



  1. the search option on facebook isnt avalible unless you are logged in, meaning the cookies for the log in session would need to be passed else facebook.com just reads its home login page.

  2. the search bar needs the name entered into it in order for the results to drop down and apear in the source code/element info, I am especially unsure how to do this part since it seems like a special form of query since you arnt really submiting it, its just typed and results apear even before you hit search. (like googles recommend search feature)


Does anyone have any code examples or references to code examples that would help explain how to go about doing either or both of these 2 things. Have been stuck for a long time, thanks.





Should I ever reset object state?


I have a series of TextBlocks which are drawn on frames to make a video of a book. I do it when user click on Make Video. User may change the content of some blocks and click again on Make Video. I should reset some state of the TextBlocks (e.g reset LastWrittenWord to null). As I discussed it earlier it is a sign that I have mixed two responsibility for TextBlock (Drawing, Preserving content).


I would like to know can I conclude, every time which I need to reset some state of an object, I have had a bad design? I think on each click on Make Video I should instantiate some TextBlockDrawer to draw the TextBlocks again instead of resetting the object state!.