|
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. |
hi guys, I am going through a pdf of a lecture I missed and it's going on about defining equals methods
in the example it gives, it is showing how to do an equals method, taking a parameter of type "object", to see if that "object" equals the current object, which is of type "student"
here is what they write
public boolean equals(Object obj) { if (obj == this) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Person other = (Person) obj; return name.equals(other.name) && idNum.equals(other.idNum); }
my question is, why do they do this line:
Person other = (Person) obj;
at this point, haven't they confirmed that the object is a person already?
|
Hyrule18969 Posts
Yes, but not that the object to check is the same person as the one being checked against, which is what the return line after is doing.
|
Well, you don't mean literally the same person object, right? because that's what this line does:
if (obj == this) return true;
so my question is, if the object has aready been determined to be a person, then why do they cast that object to type person to check that it's fields are equal to the current object
couldn't they skip
Person other = (Person) obj;
and just at the end do
return name.equals(obj.name) && idNum.equals(obj.idNum);
or does java not let you do that because it can't recognize an object as being a person? and if that's the case then can't they do
Person other = obj;
instead of
Person other = (Person) obj;
or is that not allowed
|
Stongly typed languages (like Java) cannot implicitly convert an object to a more specific object. In this case, Person is a more specific version of Object. If the object is not of class Person but another class, you probaly get an InvalidCastException (or whatever it is in Java) - that is why the check on Class is there.
It is possible to implicitly convert a Person to an Object however
Object myObject = person;
|
On December 10 2015 01:33 travis wrote:hi guys, I am going through a pdf of a lecture I missed and it's going on about defining equals methods in the example it gives, it is showing how to do an equals method, taking a parameter of type "object", to see if that "object" equals the current object, which is of type "student" here is what they write Show nested quote + public boolean equals(Object obj) { if (obj == this) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Person other = (Person) obj; return name.equals(other.name) && idNum.equals(other.idNum); }
my question is, why do they do this line: Person other = (Person) obj; at this point, haven't they confirmed that the object is a person already? The check if it's exactly the same Person object is this part, you got that right:
if (obj == this)
At the end, we know that obj is a Person, so now we know it's safe to cast it to Person. Which we need for the next line because we can't write this:
return name.equals(obj.name) && idNum.equals(obj.idNum); While we as humans know that obj is a Person and thus know that it has these fields, the type of the obj reference still is Object. We can't access fields declared in Person on a variable of type Object because Java is a statically typed language. First we tell the compiler to treat the variable as a Person and then we tell it what to do with it.
In dynamically typed languages we can just write
return name.equals(obj.name) && idNum.equals(obj.idNum); and it will compile just fine, but it will throw an exception during runtime. In statically typed languages we have to make sure that the fields and methods we access match the declared types of the variables at compile time.
On December 10 2015 02:24 supereddie wrote:Stongly typed languages (like Java) cannot implicitly convert an object to a more specific object. In this case, Person is a more specific version of Object. If the object is not of class Person but another class, you probaly get an InvalidCastException (or whatever it is in Java) - that is why the check on Class is there. It is possible to implicitly convert a Person to an Object however Object myObject = person;
Implicit conversion is not relevant here because at no point in the last line we tell the compiler or runtime what type we want to convert it into. There could be a Person type and an Apple type both with the name and idNum fields and both would fit the needs of the last line. We can't convert to anything if it's not clear what we should convert to. We can access fields that are only known to be there at runtime, but that doesn't require conversion.
|
On December 10 2015 01:01 BByte wrote:You can also sometimes trick Excel by changing the file extension to txt -- assuming it's currently csv. Alternatively "Paste Special" or similar functionality may work. Excel should also handle large integers correctly. What are you using to read the file in PHP? Both fgetcsv and str_getcsv should return an array of strings whether you have quotes around numbers or not.
I'm using fgetcsv. And I'm totally dumb (guess it happens after 7.5hrs of coding session). The problem was not with PHP or Notepad++ but with Excel/OpenOffice which was rounding the values. I thought it was the other way around but I've discovered it not to be so by sheer happenstance - I dumped one of the arrays which should be ['arr' [ 'arr2' [ 'val1', 'val2']]] according to the Excel data but turned out to be [ 'arr' [ 'arr2' ['val1'], 'arr3' ['val2']]]. This got me thinking and I've noticed that 2 numbers ending in 41 and 42 respectively were displayed in Excel as 40.
Seriously, fuck that bullshit
|
On December 10 2015 05:09 Manit0u wrote:Show nested quote +On December 10 2015 01:01 BByte wrote:You can also sometimes trick Excel by changing the file extension to txt -- assuming it's currently csv. Alternatively "Paste Special" or similar functionality may work. Excel should also handle large integers correctly. What are you using to read the file in PHP? Both fgetcsv and str_getcsv should return an array of strings whether you have quotes around numbers or not. I'm using fgetcsv. And I'm totally dumb (guess it happens after 7.5hrs of coding session). The problem was not with PHP or Notepad++ but with Excel/OpenOffice which was rounding the values. I thought it was the other way around but I've discovered it not to be so by sheer happenstance - I dumped one of the arrays which should be ['arr' [ 'arr2' [ 'val1', 'val2']]] according to the Excel data but turned out to be [ 'arr' [ 'arr2' ['val1'], 'arr3' ['val2']]]. This got me thinking and I've noticed that 2 numbers ending in 41 and 42 respectively were displayed in Excel as 40. Seriously, fuck that bullshit  Excel is a pain in the ass. We end up using a lot of excel at work for various reasons and it's always annoying. I especially hate it when it changes things numbers with a decimal place or two into dates. And forgets what the original value was, so no amount of reformatting your cells will bring it back.
Always blame excel first. The odds are with you.
|
Had a pair programming interview with Square (squareup.com)
Did it in C++. Bad idea.
+ Show Spoiler + /cygdrive/c/Users/Victor/Documents/GitHub/Interviews/problems/questions/src/states.cpp: In function ‘bool isValidScrambledTransition(std::string, std::set<std::basic_string<char> >, std::vector<std::vector<std::basic_string<char> > >, std::vector<std::basic_string<char> >)’: /cygdrive/c/Users/Victor/Documents/GitHub/Interviews/problems/questions/src/states.cpp:66:107: error: no matching function for call to ‘find(std::vector<std::basic_string<char> >::iterator, std::vector<std::basic_string<char> >::iterator, std::string&)’ vector<string>::const_iterator current_state_iter = find(scrambled.begin(), scrambled.end(), start_state); ^ /cygdrive/c/Users/Victor/Documents/GitHub/Interviews/problems/questions/src/states.cpp:66:107: note: candidate is: In file included from /usr/lib/gcc/i686-pc-cygwin/4.9.2/include/c++/bits/locale_facets.h:48:0, from /usr/lib/gcc/i686-pc-cygwin/4.9.2/include/c++/bits/basic_ios.h:37, from /usr/lib/gcc/i686-pc-cygwin/4.9.2/include/c++/ios:44, from /usr/lib/gcc/i686-pc-cygwin/4.9.2/include/c++/ostream:38, from /usr/lib/gcc/i686-pc-cygwin/4.9.2/include/c++/iostream:39, from /cygdrive/c/Users/Victor/Documents/GitHub/Interviews/problems/questions/src/states.cpp:1: /usr/lib/gcc/i686-pc-cygwin/4.9.2/include/c++/bits/streambuf_iterator.h:369:5: note: template<class _CharT2> typename __gnu_cxx::__enable_if<std::__is_char<_CharT2>::__value, std::istreambuf_iterator<_CharT> >::__type std::find(std::istreambuf_iterator<_CharT>, std::istreambuf_iterator<_CharT>, const _CharT2&) find(istreambuf_iterator<_CharT> __first, ^ /usr/lib/gcc/i686-pc-cygwin/4.9.2/include/c++/bits/streambuf_iterator.h:369:5: note: template argument deduction/substitution failed: /cygdrive/c/Users/Victor/Documents/GitHub/Interviews/problems/questions/src/states.cpp:66:107: note: ‘__gnu_cxx::__normal_iterator<std::basic_string<char>*, std::vector<std::basic_string<char> > >’ is not derived from ‘std::istreambuf_iterator<_CharT>’ vector<string>::const_iterator current_state_iter = find(scrambled.begin(), scrambled.end(), start_state); ^ /cygdrive/c/Users/Victor/Documents/GitHub/Interviews/problems/questions/src/states.cpp:83:79: error: no matching function for call to ‘find(std::vector<std::basic_string<char> >::iterator, std::vector<std::basic_string<char> >::iterator, std::string&)’ if (find(potential_next_states.begin(), potential_next_states.end(), state) != potential_next_states.end()) { ^ /cygdrive/c/Users/Victor/Documents/GitHub/Interviews/problems/questions/src/states.cpp:83:79: note: candidate is: In file included from /usr/lib/gcc/i686-pc-cygwin/4.9.2/include/c++/bits/locale_facets.h:48:0, from /usr/lib/gcc/i686-pc-cygwin/4.9.2/include/c++/bits/basic_ios.h:37, from /usr/lib/gcc/i686-pc-cygwin/4.9.2/include/c++/ios:44, from /usr/lib/gcc/i686-pc-cygwin/4.9.2/include/c++/ostream:38, from /usr/lib/gcc/i686-pc-cygwin/4.9.2/include/c++/iostream:39, from /cygdrive/c/Users/Victor/Documents/GitHub/Interviews/problems/questions/src/states.cpp:1: /usr/lib/gcc/i686-pc-cygwin/4.9.2/include/c++/bits/streambuf_iterator.h:369:5: note: template<class _CharT2> typename __gnu_cxx::__enable_if<std::__is_char<_CharT2>::__value, std::istreambuf_iterator<_CharT> >::__type std::find(std::istreambuf_iterator<_CharT>, std::istreambuf_iterator<_CharT>, const _CharT2&) find(istreambuf_iterator<_CharT> __first, ^ /usr/lib/gcc/i686-pc-cygwin/4.9.2/include/c++/bits/streambuf_iterator.h:369:5: note: template argument deduction/substitution failed: /cygdrive/c/Users/Victor/Documents/GitHub/Interviews/problems/questions/src/states.cpp:83:79: note: ‘__gnu_cxx::__normal_iterator<std::basic_string<char>*, std::vector<std::basic_string<char> > >’ is not derived from ‘std::istreambuf_iterator<_CharT>’ if (find(potential_next_states.begin(), potential_next_states.end(), state) != potential_next_states.end()) { ^ CMakeFiles/states.dir/build.make:54: recipe for target 'CMakeFiles/states.dir/src/states.cpp.o' failed make[2]: *** [CMakeFiles/states.dir/src/states.cpp.o] Error 1 CMakeFiles/Makefile2:165: recipe for target 'CMakeFiles/states.dir/all' failed make[1]: *** [CMakeFiles/states.dir/all] Error 2 Makefile:86: recipe for target 'all' failed make: *** [all] Error 2
couldn't compile code. that was the output. ran out of time.
the fix?
+ Show Spoiler +
FUCK THAT
+ Show Spoiler + I made it to the next round though.
|
Hm but the std::find you're using is defined in algorithm after all - why FUCK THAT? Or are you complaining about #include in general?
|
Can anyone reference some good resources for basic webapp pentesting?
|
Random curiosity question - if you have two Java Linked Lists is there a more efficient way to append them together, abusing the fact that they are both Linked Lists? For example, pointing the last element in the 0th list to the 0th element in the 1st list? Note: This is not what .addAll() does.
|
On December 11 2015 00:54 WarSame wrote:Random curiosity question - if you have two Java Linked Lists is there a more efficient way to append them together, abusing the fact that they are both Linked Lists? For example, pointing the last element in the 0th list to the 0th element in the 1st list? Note: This is not what .addAll() does. There is not. But you shouldnt be using LinkedLists anyways. There is very little reason to use them over ArrayLists or ArrayDeques. And even if there is, you wouldnt be needing micro optimizations like this.
|
On December 10 2015 17:42 Khalum wrote: Hm but the std::find you're using is defined in algorithm after all - why FUCK THAT? Or are you complaining about #include in general? I was thinking he forgot to #include <algorithm> for a significant portion of the interview
|
edit: got the help i needed!
|
On December 11 2015 01:10 RoomOfMush wrote:Show nested quote +On December 11 2015 00:54 WarSame wrote:Random curiosity question - if you have two Java Linked Lists is there a more efficient way to append them together, abusing the fact that they are both Linked Lists? For example, pointing the last element in the 0th list to the 0th element in the 1st list? Note: This is not what .addAll() does. There is not. But you shouldnt be using LinkedLists anyways. There is very little reason to use them over ArrayLists or ArrayDeques. And even if there is, you wouldnt be needing micro optimizations like this. You're right, I was just curious about it. Why is an ArrayList superior to a LinkedList, though?
Also, I'd like a bit of help optimizing some code. I'm trying to find the number of unique ways to make change for an int(i.e. 5 nickels and a penny is the same as a penny and 5 nickels, and only count for one way). My method of doing this is a recursive search. I want to use a HashSet for a memo table, but I'm making 4 new CoinCollection(has a number of pennies, nickels, dimes and quarters) each time, with the first taking the input CoinCollection and adding 1 penny, the second adding a nickel, the third a dime, the fourth a quarter. 4 new objects is a lot for a recursive function. However, I don't see how I can check if my current solution is in the HashSet without making these objects. It would save a lot of computation and stack space if I could avoid making them. Does anyone know how to get around making them? Is there something besides a HashSet I can use?
The source code is here. coinCollections is a global variable to avoid having to put it on the stack every call, but if you have a better way of dealing with that please let me know.
|
Hyrule18969 Posts
I would suggest breaking it into smaller parts. Set an order for coins such that everything is set in stone: ie always QDNP or PNDQ or it doesn't even matter. With a set order you automatically see that 5 nickels and 1 penny is the same as 1 penny and 5 nickels and you can discard one result.
|
Anyone know anything about .NET's SqlCommand.CommandTimeout property? According to the documentation, it is the amount of time that the program will wait before terminating an attempt to execute a command and then generate an error. However, it would seem that it sometimes stops a query but then doesn't generate any error at all.
There's an automated report that I'm responsible for that queries our database and then uses the results to build the contents of an email to send to a small group of people who need that data. The report runs at 7:30 in the morning and usually completes within about 1 minute. It's a pretty important report though, so it got the standard CommandTimeout = 10000 that our other important batches get. And today it didn't complete in 1 minute; instead, it finally sent out the email at 10:17 (read: 10000 seconds later), without ever throwing an exception, and the email's contents indicated that the query returned no results and that is not good because it definitely returned something when I ran it in SSMS. So what am I missing here?
|
ok guys i'm back with a question i hope this is not too vague, but long story short as possible ^_^ Ok so i'm making a site and it will require some admin type stuff but i don't wanna spend time programming a giagantic advanced admin system, its not needed. Althought i have never built an admin system i know how to if need be However again i am gunna be the only admin for a small site but must be its important to be a very secure site. So that being said i'm trying to avoid the whole separate admin login section and tons of admin pages (maybe a few)because i just don't need it too much. Alot of the stuff i could just go in to phpmyadmin but some i wanna have a basic way to define a special user being me and have my log in more secure.
So my thought process is use the regular login system but have some kind of conditional statement that determines that when i log on it will know its me and i want to encrypt my password stronger so i'm so lets say for the regular users there passwords are made using blowfish $2y$modifier of what ever something not to high so it doesn't take for ever to log in for them) then use a salt to create a password hash for them but when i log in it will recognize my user name and and then probably run same blowfish stuff but high modifier. there is a column to differentiate from regular user and admin and me being only admin.
So then using that plan to login in a slight more secure way (ill make a nice large password with lots of knicknacks and a higher modifer). So that would be how i login and then most of the stuff echoed out on the pages if it needed a some extra admin stuff i could have a condition that specifies that if this is the admin logged in etc then echo whatever on the page the normal stuff for users but with extra features" So does this plan sound bad or should i have separate pages for all my admin stuff?
So i'm sorry that was so vauge i didn't wanna make it too long with tons of unneeded code,i know how to code and i know you guys replying know how to way better then me but my question being would this be a secure way of making a login to admin system, Now keep in mind people would be able to enter my user name (they would know it) and potentials try to gain admin access but my pass will wayyy stronger and they won't know the information described in here?
and i guess other then that i feel pretty confident in my ability to protect against sql injection but again it would be quite important that no one gains access to this and i guess that would depend on how i code it and if i cleanse all data being placed into a data base as well as make sure there is coding that prevents a regular user from being on a page he shouldn't be, so if i did have 1 or 2 admin pages have conditions that check to see who the the user is and if they are not me then they get ushered away from the page the shouldn't be on. I do that on all pages if a user has no need to be there the set conditions that user them away...
I feel get i'm gunna get shit for this post but i'm sorry i have taken the time to learn html css php javascript mysql etc but i'm a newb and i have started projects and never finished them but this one i HAVE to finish and i wanna get it right so if you see any red flags about what i stated above then let me know this sounds like a bad idea and why.. thanks again i love this thread and TL thank you seeya
|
@hooktits:
First of all, making your admin password more encrypted than regular users won't help that much. Just using password_hash($password, PASSWORD_BCRYPT) should be sufficient in most cases (you can expand it to password_hash($password, PASSWORD_BCRYPT, [ 'cost' => 10 ]) if you think it'll help you much). You generally want all users using the same thing since admin will be saved along them. Where they differ is their permissions.
Website security isn't all about how secure your admin account is. It doesn't mean crap if you have the most secure hashing algorithm and some crazy 128-character length password if someone can go around it by tampering with other parts of your application (giving regular users admin access, breaking into your server and seeing the code etc.). There are pretty crazy things you can do with even pretty basic XSS attacks.
Second of all, unless you absolutely have to write this thing from scratch I'd suggest you don't reinvent the wheel and go with some reliable framework instead. This will give you access to more tools right off the bat and save you a lot of time. Frameworks such as Symfony or Laravel make developing PHP apps pretty enjoyable and you get all the basic security you want with them right off the bat (SQLI, CSRF, admin/user/anonymous user authentication etc. all built-in). On top of that they're built with modern approaches and best practices in mind.
The worst thing you could possibly do here is trying to do it yourself without much experience. You'll end up with a page that's in the 90's or early 2000's security-wise (SQL statements in the views and other bullshit like that). Also, don't ever use WordPress...
Also, try to tackle one thing at a time. Right now it seems like you have a very vague idea on how your project is going to work/look like. One day spent planning can save you a week of work. Focus on that first.
|
On December 11 2015 05:32 WarSame wrote: You're right, I was just curious about it. Why is an ArrayList superior to a LinkedList, though? [...]
This feels like some dirty secret that's not mentioned when learning about data structures.
Here's a video of a presentation about linked lists vs. arrays from the guy that created C++:
+ Show Spoiler +
|
|
|
|