|
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. |
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.
|
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, ArrayList<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 values (type is List<String>) if it contains the course that you want to search for. Accumulate results and done.
Remember that map is based on key : value. Your key's type is String. Your value's type is List<String>. List<String> is just an interface though. Your concrete type is ArrayList<String> in this example. As long as you follow interface of List and don't do any casts for it, you could later replace it with something else without much code change.
|
On October 19 2016 05:45 Shield wrote:Show nested quote +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
|
List is a superclass of ArrayList so that doesn't matter.
For starting questions:
Do you actually understand what a map is? Personally I've only ever used HashMaps, but the Java api does have other options; I'd assume that for an academic assignment a specific class of Map is required so you should figure out which one that is.
If you understand what a map is, but don't understand how to implement the methods then I'm going to suggest trying and post some code and we can help you debug it.
|
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.html
Concrete 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.
|
On October 19 2016 05:49 Blitzkrieg0 wrote: List is a superclass of ArrayList so that doesn't matter.
For starting questions:
Do you actually understand what a map is? Personally I've only ever used HashMaps, but the Java api does have other options; I'd assume that for an academic assignment a specific class of Map is required so you should figure out which one that is.
If you understand what a map is, but don't understand how to implement the methods then I'm going to suggest trying and post some code and we can help you debug it.
My current understanding is that a map is a data structure that is actually a combination of two data structures.
The first part is a set, with "key" values. Each key is a unique value.
The second part is a ... something. Some sort of collection? ( I don't technically know what a collection is I guess). And the second part contains objects/values that correspond to the set with the keys.
And then I imagine the map has some sort of method that looks up the objects or values based on the keys.
Is this kind of right?
|
On October 19 2016 05:59 travis wrote:Show nested quote +On October 19 2016 05:49 Blitzkrieg0 wrote: List is a superclass of ArrayList so that doesn't matter.
For starting questions:
Do you actually understand what a map is? Personally I've only ever used HashMaps, but the Java api does have other options; I'd assume that for an academic assignment a specific class of Map is required so you should figure out which one that is.
If you understand what a map is, but don't understand how to implement the methods then I'm going to suggest trying and post some code and we can help you debug it. My current understanding is that a map is a data structure that is actually a combination of two data structures. The first part is a set, with "key" values. Each key is a unique value. The second part is a ... something. Some sort of collection? ( I don't technically know what a collection is I guess). And the second part contains objects/values that correspond to the set with the keys. And then I imagine the map has some sort of method that looks up the objects or values based on the keys. Is this kind of right?
Map<Key, Value>
As you've said, the first parameter is key. The second part happens to be a collection (also known as container). Technically, it's called value if you want to be abstract. Collection is just a data structure which holds your data. (Hash)Set is a container. ArrayList is a container. (Hash)Map is a container. (Linked)List is a container and so on.
Edit: No specific order seems to be guaranteed. Check http://stackoverflow.com/a/17708526
|
On October 19 2016 05:59 travis wrote:Show nested quote +On October 19 2016 05:49 Blitzkrieg0 wrote: List is a superclass of ArrayList so that doesn't matter.
For starting questions:
Do you actually understand what a map is? Personally I've only ever used HashMaps, but the Java api does have other options; I'd assume that for an academic assignment a specific class of Map is required so you should figure out which one that is.
If you understand what a map is, but don't understand how to implement the methods then I'm going to suggest trying and post some code and we can help you debug it. My current understanding is that a map is a data structure that is actually a combination of two data structures. The first part is a set, with "key" values. Each key is a unique value. The second part is a ... something. Some sort of collection? ( I don't technically know what a collection is I guess). And the second part contains objects/values that correspond to the set with the keys. And then I imagine the map has some sort of method that looks up the objects or values based on the keys. Is this kind of right?
Yes, but you also want to think about why you'd use such a data structure. What's the purpose of mapping key and value pairs?
|
On October 19 2016 06:04 Blitzkrieg0 wrote:Show nested quote +On October 19 2016 05:59 travis wrote:On October 19 2016 05:49 Blitzkrieg0 wrote: List is a superclass of ArrayList so that doesn't matter.
For starting questions:
Do you actually understand what a map is? Personally I've only ever used HashMaps, but the Java api does have other options; I'd assume that for an academic assignment a specific class of Map is required so you should figure out which one that is.
If you understand what a map is, but don't understand how to implement the methods then I'm going to suggest trying and post some code and we can help you debug it. My current understanding is that a map is a data structure that is actually a combination of two data structures. The first part is a set, with "key" values. Each key is a unique value. The second part is a ... something. Some sort of collection? ( I don't technically know what a collection is I guess). And the second part contains objects/values that correspond to the set with the keys. And then I imagine the map has some sort of method that looks up the objects or values based on the keys. Is this kind of right? Yes, but you also want to think about why you'd use such a data structure. What's the purpose of mapping key and value pairs?
Okay, thinking about it..
Sets are fast but un-ordered. So we can look up our key quickly. If we get a reference from this, we are able to look in a much smaller collection (one that corresponds to the key), than if all the keys and values were stored in a giant collection together. Is this the idea?
|
On October 19 2016 05:59 travis wrote:Show nested quote +On October 19 2016 05:49 Blitzkrieg0 wrote: List is a superclass of ArrayList so that doesn't matter.
For starting questions:
Do you actually understand what a map is? Personally I've only ever used HashMaps, but the Java api does have other options; I'd assume that for an academic assignment a specific class of Map is required so you should figure out which one that is.
If you understand what a map is, but don't understand how to implement the methods then I'm going to suggest trying and post some code and we can help you debug it. My current understanding is that a map is a data structure that is actually a combination of two data structures. The first part is a set, with "key" values. Each key is a unique value. The second part is a ... something. Some sort of collection? ( I don't technically know what a collection is I guess). And the second part contains objects/values that correspond to the set with the keys. And then I imagine the map has some sort of method that looks up the objects or values based on the keys. Is this kind of right? Its not quite right. You name several things that exist, but I dont think you understand their roles and relationships yet.
A map is simply a data structure which "connects" two objects. A key is mapped ("connected") to a value. If you have the key you can look up the value. It does not go the other way. Each key can only be mapped to one value but any number of keys can map to the same value.
Imagine a map which connects letters to numbers: A -> 5 B -> 12 C -> 7 D -> 5 E -> 42
If we have a letter (the key) we can find out the mapped number (the value). We can ask our map now: "What is the number for the letter 'C'?" and our map will give us the answer 7. This is what the "get"-method of the Map interface does.
The things you said about sets and collections and whatnot is not important. That is implementation specific. You should not worry how the map actually works, at least not on your level. Just know that there are different map implementations, the most commonly used one is the HashMap.
In your example you want to map Strings to Lists of Strings. So every one String can have a List of other Strings mapped to it. For example: "abc" -> {"a", "b", "c"} "acd" -> {} "abd" -> null "bad" -> {"b", "c"}
How exactly this can be used to model Students and courses is your task to find out now.
Edit:
On October 19 2016 06:23 travis wrote:Show nested quote +On October 19 2016 06:04 Blitzkrieg0 wrote:On October 19 2016 05:59 travis wrote:On October 19 2016 05:49 Blitzkrieg0 wrote: List is a superclass of ArrayList so that doesn't matter.
For starting questions:
Do you actually understand what a map is? Personally I've only ever used HashMaps, but the Java api does have other options; I'd assume that for an academic assignment a specific class of Map is required so you should figure out which one that is.
If you understand what a map is, but don't understand how to implement the methods then I'm going to suggest trying and post some code and we can help you debug it. My current understanding is that a map is a data structure that is actually a combination of two data structures. The first part is a set, with "key" values. Each key is a unique value. The second part is a ... something. Some sort of collection? ( I don't technically know what a collection is I guess). And the second part contains objects/values that correspond to the set with the keys. And then I imagine the map has some sort of method that looks up the objects or values based on the keys. Is this kind of right? Yes, but you also want to think about why you'd use such a data structure. What's the purpose of mapping key and value pairs? Okay, thinking about it.. Sets are fast but un-ordered. So we can look up our key quickly. If we get a reference from this, we are able to look in a much smaller collection (one that corresponds to the key), than if all the keys and values were stored in a giant collection together. Is this the idea? You should not continue with this because it is wrong. A map has very little to do with a set.
|
Okay, so a map is less specific than I was thinking. I understand your explanation.
But then, as for what the purpose of a map is.
I would think, the purpose must have to do with the keys being unique. But if it's usefulness isn't in keys being able to be looked up from a set then I don't know what it is.
I mean, I understand how it might make sense from an "organization" perspective, but it isn't doing something you can't do with other data structures right?
|
On October 19 2016 06:40 travis wrote: Okay, so a map is less specific than I was thinking. I understand your explanation.
But then, as for what the purpose of a map is.
I would think, the purpose must have to do with the keys being unique. But if it's usefulness isn't in keys being able to be looked up from a set then I don't know what it is.
I mean, I understand how it might make sense from an "organization" perspective, but it isn't doing something you can't do with other data structures right? You could make a map implementation using two arrays, if that's what you're saying. It just wouldn't be very efficient. For instance, in your example, you'd have an array with student names (ids) and another array of the same length with lists of classes. To then add a class to a student "John", you search in the first array and find the index of student "John" and then retrieve the list in the second array at that index, and add the class to it.
But that wasn't even what people were getting at. The point is to abstract away from efficiency: the way you understand Map should be at the same level where you understand List, or Set. These are data structures with a specific behavior you expect from them, and you shouldn't care (at this level) whether a list is implemented as a linked list or as an array (for instance).
|
On October 19 2016 06:56 Acrofales wrote:Show nested quote +On October 19 2016 06:40 travis wrote: Okay, so a map is less specific than I was thinking. I understand your explanation.
But then, as for what the purpose of a map is.
I would think, the purpose must have to do with the keys being unique. But if it's usefulness isn't in keys being able to be looked up from a set then I don't know what it is.
I mean, I understand how it might make sense from an "organization" perspective, but it isn't doing something you can't do with other data structures right? You could make a map implementation using two arrays, if that's what you're saying. It just wouldn't be very efficient. For instance, in your example, you'd have an array with student names (ids) and another array of the same length with lists of classes. To then add a class to a student "John", you search in the first array and find the index of student "John" and then retrieve the list in the second array at that index, and add the class to it. But that wasn't even what people were getting at. The point is to abstract away from efficiency: the way you understand Map should be at the same level where you understand List, or Set. These are data structures with a specific behavior you expect from them, and you shouldn't care (at this level) whether a list is implemented as a linked list or as an array (for instance).
Well, couldn't you just make an implementation with one array, checking some "key" value within the object at each index of array until you find the right "key" ? Therefore just using a list to do the same thing that the map does?
(my point is that I think I understand the function of it, but that doesn't explain *why* it's used)
|
On October 19 2016 07:03 travis wrote:Show nested quote +On October 19 2016 06:56 Acrofales wrote:On October 19 2016 06:40 travis wrote: Okay, so a map is less specific than I was thinking. I understand your explanation.
But then, as for what the purpose of a map is.
I would think, the purpose must have to do with the keys being unique. But if it's usefulness isn't in keys being able to be looked up from a set then I don't know what it is.
I mean, I understand how it might make sense from an "organization" perspective, but it isn't doing something you can't do with other data structures right? You could make a map implementation using two arrays, if that's what you're saying. It just wouldn't be very efficient. For instance, in your example, you'd have an array with student names (ids) and another array of the same length with lists of classes. To then add a class to a student "John", you search in the first array and find the index of student "John" and then retrieve the list in the second array at that index, and add the class to it. But that wasn't even what people were getting at. The point is to abstract away from efficiency: the way you understand Map should be at the same level where you understand List, or Set. These are data structures with a specific behavior you expect from them, and you shouldn't care (at this level) whether a list is implemented as a linked list or as an array (for instance). Well, couldn't you just make an implementation with one array, checking some "key" value within the object at each index of array until you find the right "key" ? Therefore just using a list to do the same thing that the map does? How do you know a map doesnt do that? Thats the point. You dont need to know. A map is not an implementation. It has nothing to do with arrays, lists, sets or whatnot. Its just a definition for methods and how these methods work. What happens in the background is unimportant.
There are many many great uses for maps. For example dictionaries where you look up translations for a given term. Or perhaps grading where you map students to the grades they got in their exam. How exactly does it do that? You dont need to know. Not at the moment. Just know that a HashMap is really damn fast in all common use cases.
|
Okay so my problem is that I am thinking that a map is more than just an abstraction. A map is literally just the concept of key --> value ? of course it's also an interface but I guess there are potentially endless ways to implement it.
|
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.
On October 19 2016 06:40 travis wrote: I would think, the purpose must have to do with the keys being unique. But if it's usefulness isn't in keys being able to be looked up from a set then I don't know what it is.
I mean, I understand how it might make sense from an "organization" perspective, but it isn't doing something you can't do with other data structures right?
A map is literally just the concept of key --> value ?
You are right. That's all Map specifies. the Map enforces unique keys, which is valuable because your data structure is maintaining a database with a uniqueness constraint: no student should appear more than once.
it isn't doing something you can't do with other data structures right?
This is a tricky question to answer. In the strict sense, you really don't *need* any data structure for anything. As you can implement any function without any defined data structures. But in the loose sense yes, a map allows you to stucture your data that allows you to do things *better* than an array for example, particularly when doing look-up operations. That coupled with their simplicity and ease of modelling relationships makes them *utterly ubiquitous* in the programming world. So definitely get familiar with it.
So I just wanted to respond to those quickly, because I didn't want there to be any misconceptions about how Maps worked. I really think you should take a step back though, because this conversation is focusing way too much on implementations of core data structures, which only have accidental relevance to the problem you're solving: No matter what the implementation is, as long as it adheres to the contract of its interface you will have a correct solution. A HashMap is usually the default implementation of Map. It has very fast lookups. That's literally all you need to know about HashMaps.
If I were looking at this problem, I would start by modelling some example data. From your description, here's what I'm picturing. Basically you are representing a relationship between one student and many courses. one-to-many
Student [keyed by student id] Course [keyed by course id] ...
Tom [id=100] CS-101 MATH-300
Jerry [id=101] CS-105 LIT-300
So a Map<String, List<String>> models this pretty easily. The String key is the student id which is unique, and the value is a List of course ids ("CS-105", etc...). Knowing this you know what your data structure is supposed to look like *all the time*. Now you just need to implement each piece.
The class has a constructor to make an empty map
You already know exactly the question to ask for this:
For example, the constructor. I have no idea how to create an empty map. Just "Map name = new Map" ?
So you're one stack overflow / google search away, but I'll save you some time. Just use "Map<String, List<String>> name = new HashMap<String, List<String>>()". HashMaps are good, fast, and standard, and you don't have any crazy scaling, performance, or concurrency requirements. It's a simple choice.
It has an add method, which adds a student and a course, or just a course if the student is already in the map
Implementing this method is just a matter of taking into account the two conditions you've already described. If the student is NOT present in the map (which you can look up how to do) add the key with a new list containing the one course. So for this part, you'll need know how to "new up" a list (may as well use the ArrayList implementation), add a value to the list, and add a key-value pair to a map. If the student IS present in the map, add a course to the list which can safely be assumed to exist, since you added it when you initialized the student key, and you never need to null out the array.
it has a remove method, which removes the provided course from the student
Know how to look up a student's course, the value associated with that key. And know how to remove something from a list.
a boolean method which checks if anyone is taking a given course
Implementing this means iterating through the map and iterating through each list: a nested for-loop. Although, not required, I highly recommend looking up "java for each loop", which is an extremely useful syntax for looping through data structures that implement the Iterable interface, which is every Collection implements.
I didn't read every response to this thread, just kind of skimmed, so sorry if some of that's already been discussed. I just recommend start hammering away at it with a debugger, and ask google questions as they come up. Don't worry about doing everything *right* for now. Just make it work, and the "doing things *right*" part will come later.
Happy hacking
EDIT: forgot the generic part for the newing up the Map
|
On October 19 2016 07:39 travis wrote: Okay so my problem is that I am thinking that a map is more than just an abstraction. A map is literally just the concept of key --> value ? of course it's also an interface but I guess there are potentially endless ways to implement it.
For your task, all you really need to know is that map is a container of key-value structured data. You shouldn't care about efficiency unless they ask you. As others said, maps could be used for dictionaries. They could also be used as a telephone register (e.g. Map<NameOfPerson, TelephoneNumber>) and more than that. The point is, that's all you should be concerned about how maps work. Really, if I were you, I'd be more concerned about add, remove and search at this point (even though they are trivial from that description).
In your free time, try to understand when you need each collection. I wouldn't say every programmer knows every collection, but I'd say there is no serious programmer who doesn't know what map, array list (vector), set and linked list are.
|
Okay good news. My program passed all public tests. But 50% of it is secret tests.
But.. I am a bit confused about something.
my add method:
public void add(String student, String course) { if(courses.containsKey(student)) { courseList.add(course); courses.put(student, courseList); } else { courseList = new ArrayList<String>(); courseList.add(course); courses.put(student, courseList); } }
I had already defined courseList as a public class member. and I am confused how this works.. In particular:
if(courses.containsKey(student)) { courseList.add(course); courses.put(student, courseList); }
how does it keep track of which courseList is being added to? am I doing this wrong an I am just lucky(or unlucky rather) that the tests didn't test that properly?
edit:
Instead of courseList.add(course);
should I be doing:
courseList = courses.get(student); courseList.add(course);
|
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.
edit: Personally I would leverage the fact that the get method returns NULL if the key is not in the map, but there are many different implementations you can use that all work.
|
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.
|
|
|
|