|
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 October 19 2016 09:17 travis wrote:Show nested quote +On October 19 2016 09:05 Blitzkrieg0 wrote: You're on the right track already with "how does it keep track of which courseList is being added to?"
Why did you make courseList a field in your class? Having an ArrayList field in Database class means that the database has a unique List. Each value in your map should have an ArrayList so it should not be a member of the Database class.
Well, courseList isn't instantiated until it needs to be added.
Consider the difference between your code and the following:
else { ArrayList courseList = new ArrayList<String>(); courseList.add(course); courses.put(student, courseList); }
If you're going to instantiate the variable at that time you don't need a member variable to do so. Member variables are specific to the object it resides in. By declaring an ArrayList in your Database you are saying that my database has an ArrayList. Each key in the Map has an associated value of ArrayList. The HashMap class would have the member variable of ArrayList, not your Database class.
|
Nice information about Maps, thanks guys
|
I can think of a test that your code won't pass.
Add "blitz", "elite hacking" Add "travis", "cs101" Retrieve class list for "blitz"
Write the test, and look at your results. What is wrong? And try to fix it (hint: remove the field courseList, edit: your edit is headed in the right direction).
|
On October 19 2016 05:50 Shield wrote:+ Show Spoiler +On October 19 2016 05:47 travis wrote:Show nested quote +On October 19 2016 05:45 Shield wrote:On October 19 2016 05:30 travis wrote: I need a hand with this little java lab I need to do
I missed the lecture that goes with this, so I am pretty in the dark
I've been provided with a class (called database)
It has a member, public Map<String, List<String>>
The class has a constructor to make an empty map
It has an add method, which adds a student and a course, or just a course if the student is already in the map
it has a remove method, which removes the provided course from the student
and a boolean method which checks if anyone is taking a given course
And so, I don't really know where to begin. I really just need some help getting started. For example, the constructor. I have no idea how to create an empty map. Just "Map name = new Map" ? But it seems like there are many types of maps. Are you required to use List<String> (assuming this is the list which holds courses for student)? If not, then I suggest OOP approach. Create a class which represents course or an enum at the very least. It's been a while since I wrote code in Java, but I believe this should do it: HashMap<String, List<String>> students = new HashMap<String, List<String>>(); Add and remove are trivial. We shouldn't really do your homework. Your last method is to check if anyone takes a given course. If you really have to use List<String> for courses, then the simple approach might be to iterate your map and check its value (type is List<String>) if it contains the course that you want to search for. Accumulate results and done. yeah yeah I don't want you guys to do my homework, just get me started  The handout is so weird. It says "you will see a public instance variable, defined as follows(don't change it): public Map<String, ArrayList<String>> courses; but then in the actual lab, when I checked it out, the code says public Map<String, List<String>> courses; I'll try checking with my TA Check my edited comment. List is an interface. ArrayList is concrete implementation of List. See https://docs.oracle.com/javase/7/docs/api/java/util/List.htmlConcrete classes are listed after this line: "All Known Implementing Classes:". Edit: It doesn't matter if you use one of the following: HashMap<String, ArrayList<String>> courses = new HashMap<String, ArrayList<String>>(); Map<String, ArrayList<String>> courses = new HashMap<String, ArrayList<String>>(); Map<String, List<String>> courses = new HashMap<String, ArrayList<String>>(); Hopefully, they should all compile. I say hopefully because I haven't developed Java applications for more than a year. The only difference is the last one gives you the flexibility to change concrete implementations later (the right side where you initialise map) without much changes to code.
Thought I'd chime in with one correction to your post (the last entry won't compile as is) because if you don't know it might not be obvious why it doesn't work, and I think it's pretty interesting. Some stuff around generics in Java isn't quite intuitive, so I'd say the brief Java tutorial is worth reading.
> Note: Given two concrete types A and B (for example, Number and Integer), MyClass<A> has no relationship to MyClass<B>, regardless of whether or not A and B are related. The common parent of MyClass<A> and MyClass<B> is Object.
The point here being that Foo<String, List<String>> has no inheritance relationship to Foo<String, ArrayList<String>>. But really the tutorial is pretty brief and worth reading imo if you haven't as a refresher. Also you can read more about the overall reasons in articles such as this overview of the relevant vocabulary.
So to get the effect you want you'd want something like: ``` Map<String, List<String>> courses = new HashMap<String, List<String>>(); // Or really Java 6 long dead even Android has this, please... Map<String, List<String>> courses = new HashMap<>(); // diamond types since Java 7 ```
I'd say that using the most generic base class is appropriate rather then ever declaring ArrayList or HashMap directly unless you specifically need some behavior, like using a SortedMap for ordering in your map. It's not something to get hung up but I think if you're gonna code in a language it's worth writing code that looks like you know what you're doing so use base classes, diamond types, the new Java 8 apis if they're appropriate
|
On October 19 2016 07:47 FunkyLich wrote:Show nested quote +You should not continue with this because it is wrong. A map has very little to do with a set. Half a map is a set, it has very very much to do with a set. In fact the standard implementation of Set -- HashSet -- is based on the implementation of HashMap. Although it is true that a Set can be implemented using a Map that doesnt mean that the concept of Sets and Maps are somehow linked. Each of them is a seperate data structure with a well defined purpose. Their implementations might overlap, but the implementations are irrelevant to most programmers and should indeed be so.
I stand by my point that thinking of Maps in terms of Sets or Lists or whatnot is just wrong and will lead somebody in the wrong direction.
|
I would suggest getting a copy of Effective Java (by Bloch), and reading relevant chapters here.
|
Is anyone here good with cryptography?
I'm wondering if such code would be sufficient to encode/decode some sensitive data?
final public static function encrypt($data) { $serialized = serialize($data);
$iv = static::getIv(); $key = static::getKey(); $mac = static::getMac($serialized, $key);
$serialized .= $mac;
$passcrypt = mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $serialized, MCRYPT_MODE_CBC, $iv);
return sprintf('%s|%s', base64_encode($passcrypt), base64_encode($iv)); }
final public static function decrypt($data) { $key = static::getKey(); $decrypt = explode('|', $data);
if (count($decrypt) !== 2) { return false; }
$decoded = base64_decode($decrypt[0]); $iv = base64_decode($decrypt[1]);
if (!static::validIv($iv)) { return false; }
$decrypted = trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $decoded, MCRYPT_MODE_CBC, $iv));
if (!static::validMac($decrypted, $key)) { return false; }
return unserialize(substr($decrypted, 0, -64)); }
final public static function getIvSize() { return mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC); }
final public static function getIv() { return mcrypt_create_iv(static::getIvSize(), MCRYPT_DEV_URANDOM); }
final public static function validIv($iv) { return strlen($iv) === static::getIvSize(); }
final public static function getMac($data, $key) { return hash_hmac('sha256', $data, substr(bin2hex($key), -32)); }
final public static function validMac($decrypted, $key) { return substr($decrypted, -64) === static::getMac(substr($decrypted, 0, -64), $key); }
final public static function getPublicKey() { return static::generateRandomKey(); // obviously there's something else here (doesn't return random key) }
final public static function getPrivateKey() { return static::generateRandomKey(); // obviously there's something else here (doesn't return random key) }
final public static function getKey() { $compositeKey = static::getPublicKey(); $compositeKey .= static::getPrivateKey();
return pack('H*', hash('sha256', $compositeKey)); }
final public static function generateRandomKey() { return md5(uniqid(mt_rand(), true)); // to illustrate sample keys }
The encryption has to be based on 2 separate keys. One is per-application and the other one is per-client, ensuring that even if you get a hold of one key it's no good. Getting hold on both keys in one system doesn't compromise other systems.
Cool thing about it is that it allows you to encrypt not just text but even arrays and objects (which turn into fully functional PHP arrays/objects upon decryption). Which can let you for example send objects (of specific class with their fields set) through some API and share them between systems.
|
I had been using Windows for 20 years before switching to Ubuntu 16.04 4-5 months ago. Today, I had to use Windows for 2 minutes, and even that long of an exposure to Windows was fucking painful. How can people even live with that shit.
|
On October 20 2016 22:54 Djagulingu wrote: I had been using Windows for 20 years before switching to Ubuntu 16.04 4-5 months ago. Today, I had to use Windows for 2 minutes, and even that long of an exposure to Windows was fucking painful. How can people even live with that shit.
I have installed Linux on my wife's laptop (against all protests from her side) some years ago. After a couple of months of using it (she's no power user mind you) she was appalled whenever she had to use Windows for some reason.
|
On October 20 2016 22:54 Djagulingu wrote: I had been using Windows for 20 years before switching to Ubuntu 16.04 4-5 months ago. Today, I had to use Windows for 2 minutes, and even that long of an exposure to Windows was fucking painful. How can people even live with that shit. What are some of things that you had a problem with?
|
I don't get it. Windows is not nearly as bad as people make it out to be. In fact, Windows 10 is actually quite pleasant. I probably prefer it over Mac OS X now, actually. Much of the GUI is very similar to how you interact with Ubuntu's (Unity) GUI. I understand that if you have to get under the hood, things are easier to twiddle with in Linux, but for a normal user, Windows 10 is rather good, imho.
|
On October 20 2016 23:00 Manit0u wrote:Show nested quote +On October 20 2016 22:54 Djagulingu wrote: I had been using Windows for 20 years before switching to Ubuntu 16.04 4-5 months ago. Today, I had to use Windows for 2 minutes, and even that long of an exposure to Windows was fucking painful. How can people even live with that shit. I have installed Linux on my wife's laptop (against all protests from her side) some years ago. After a couple of months of using it (she's no power user mind you) she was appalled whenever she had to use Windows for some reason. She is ready for the rite of passage.
On October 20 2016 23:27 Ilikestarcraft wrote:Show nested quote +On October 20 2016 22:54 Djagulingu wrote: I had been using Windows for 20 years before switching to Ubuntu 16.04 4-5 months ago. Today, I had to use Windows for 2 minutes, and even that long of an exposure to Windows was fucking painful. How can people even live with that shit. What are some of things that you had a problem with? For starters:
- Windows doesn't have and never had a native ssh client - The most popular ssh client for windows can't even use keys with .pem extension - Windows doesn't have and never had a native sftp client - The most popular sftp client for windows wants to get updated every other day - The most popular sftp client for windows needs to get updated every other day - The most popular sftp client for windows can't even use keys with .pem extension - At least 50% of the node packages doesn't work on windows (the number grows up to 100% for geospatial packages) - Windows doesn't even have a native C++ compiler - The previous thing is fucking the thing that's before him right in the butt - Windows doesn't even have docker natively (you fucking need a VM just to use Docker) - Windows fucking hides your files everywhere in the file system and you don't even have a control over it. One minute you need to install a few things just to get something work and next thing you know, you only have 20 gigs of free disk space. - Regarding the previous point: You need to install a metric fuckton of shit just to get stuff working - Don't even bother installing apache web server or postgresql or some other shit on Windows. Windows will lose all your disk space and never give it back.
Windows has one good thing going for it and that's Windows Media Player. No matter what everyone says, Windows is the more friendly OS (out of what I've used ofc, never used Mac so it might also be more friendly) if you have a few gigs of mp3s and want to listen to them. The only good music player Ubuntu has is Clementine and it's fucking unpredictable as fuck.
On October 20 2016 23:33 Acrofales wrote: I don't get it. Windows is not nearly as bad as people make it out to be. In fact, Windows 10 is actually quite pleasant. I probably prefer it over Mac OS X now, actually. Much of the GUI is very similar to how you interact with Ubuntu's (Unity) GUI. I understand that if you have to get under the hood, things are easier to twiddle with in Linux, but for a normal user, Windows 10 is rather good, imho. I used Windows 10 right before switching to Ubuntu 16.04 (I switched because Windows 10 blew right in my face). It's not pleasant. Not a single bit. Package managers and repositories are the most pleasant thing that was ever created and Windows has none of that shit either. Windows, on the same machine, is way slower. Did I mention Windows was fucking unstable too? Yes sir, it is.
|
Linux doesn't have a native ssh or sftp client nor a C++ compiler. Pretty much all distributions include those, but they are not included in Linux by default. If you bootstrap your own Linux, which I actually did a few years back, you have to install all of those on your own. You are confusing Linux with Linux distributions. Also, those are hardly things a normal user requires.
As for hiding files, I found Linux to be a bigger pain. Some files are in /usr/bin, /usr/lib, /usr/local/bin, /usr/local/lib, /usr/include, /usr/local/include, /opt (Debian based distributions -.-), /etc, /var, ... and a lot of distributions have their own layout, e.g. apache config on RedHat based distributions is in /etc/httpd, because apache is the company and httpd the product, on Debian based distributions it's /etc/apache2, because Debian users are stupidmisguided, ... and god help you if you want to install something that doesn't have a native package for your specific distribution. Most desktop distributions these days aren't even actually safer, because they have the default user in sudoer without password or use root without a password, because they noticed that people don't actually want to be bothered by having to enter a password for what they consider simple things.
I like Linux, it's a great operating system. I've used it extensively for years, I've even spent a week or two installing it completely from scratch to understand it better, but these days I use it only on servers or in VMs, because I can't bring myself to deal with it anymore.
Windows came a long way from where it was and, while I only like a few Windows releases (pretty much only 3, 2000 and 7), it's now the better operating system for desktops. They are running off in the wrong direction with Windows 10 again, but that is not a problem that only Microsoft has (Gnome...).
|
Can somebody tell me if it is worth learning more about automaton theory after you learn how to convert a regex -> nfa -> dfa and back?
|
|
On October 21 2016 02:18 Nesserev wrote:Show nested quote +On October 21 2016 01:49 sabas123 wrote: Can somebody tell me if it is worth learning more about automaton theory after you learn how to convert a regex -> nfa -> dfa and back? Definitely YES! "After regex, nfa and dfa" come CFGs (Context Free Grammars) and PDAs (Push Down Automatas), which eventually lead to parser territory (LL, LR, etc. parsers). You should at least go for a practical understanding of those. Then there are Decidable and Semidecidable languages, Turing Machines, P and NP, etc. It probably doesn't hurt if you know about this stuff too  Thank you, I started getting into this theory because of lexical parsing (Currently reading the dragon book).
Do you have any recommendations? Currently I'm reading the 2nd edition of "Introduction To Automata Theory, Languages, And Computation" .
|
On October 21 2016 00:20 Djagulingu wrote: - Windows doesn't have and never had a native ssh client - The most popular ssh client for windows can't even use keys with .pem extension - Windows doesn't have and never had a native sftp client - The most popular sftp client for windows wants to get updated every other day - The most popular sftp client for windows needs to get updated every other day - The most popular sftp client for windows can't even use keys with .pem extension - At least 50% of the node packages doesn't work on windows (the number grows up to 100% for geospatial packages) - Windows doesn't even have a native C++ compiler - The previous thing is fucking the thing that's before him right in the butt - Windows doesn't even have docker natively (you fucking need a VM just to use Docker) - Windows fucking hides your files everywhere in the file system and you don't even have a control over it. One minute you need to install a few things just to get something work and next thing you know, you only have 20 gigs of free disk space. - Regarding the previous point: You need to install a metric fuckton of shit just to get stuff working - Don't even bother installing apache web server or postgresql or some other shit on Windows. Windows will lose all your disk space and never give it back.
Those clearly are mighty important for the average user.
I used Ubuntu a bunch during my time at uni. I had my fair share of problems with both Ubuntu and Windows. Windows still is more convenient for the stuff I usually do.
|
On October 21 2016 01:49 sabas123 wrote: Can somebody tell me if it is worth learning more about automaton theory after you learn how to convert a regex -> nfa -> dfa and back? No. Unless you actually care about it and are interested in learning, then yes. Or if you need it for college / university. D'uh.
|
|
On October 21 2016 00:20 Djagulingu wrote: Windows has one good thing going for it and that's Windows Media Player. No matter what everyone says, Windows is the more friendly OS (out of what I've used ofc, never used Mac so it might also be more friendly) if you have a few gigs of mp3s and want to listen to them. The only good music player Ubuntu has is Clementine and it's fucking unpredictable as fuck.
First, if you're using anything other than Foobar for music listening in Windows you're misguided WMP is absolute crap. Second, MPD on Linux is the way to listen to your music. You just start a background daemon with your playlist and control it with global keys (you can even control it over the net). You don't even need a music player If you want nice graphics, album covers and all that jazz then you should go with Amarok, which is quite amazing.
|
|
|
|