Serendipity Posted May 2, 2011 Share Posted May 2, 2011 I'm not sure you guys can help me, but I'm struggling with this assignment in school. We're creating a graph data structure with the instance variable: Map<String, Map<String, Integer>> and we have to write a series of methods. I've written public void addNode(String s)throws IllegalArgumentException { if (data.containsKey(s)) throw new IllegalArgumentException("Node already exists in graph"); data.put(s, new HashMap<String, Integer>());//or null } Is that correct? And also, I'm not exactly sure how to write the addArc method. I wrote this but again, I'm not sure public void addArc(String a, String b, int i) { // check if there's a connection yet HashMap<String, Integer>secondPart=new HashMap<String, Integer>(); secondPart.put(b, i); data.put(a, secondPart); } Also, I have absolutely no idea how to approach removing an arc because I'm not sure how the data is exactly entering the map? Like, how do you remove only a connection in this method: • removeEdge(String , String) Yeah, to put it lightly, I'm pretty confused and this is only the half of it. I'm not sure if TDN is the place for homework help, but I thought I'd give it a shot? I apologize if I'm not supposed to be asking this here, but it said in the description you could ask programming help so here I am. Kinda confused, and extremely desperate. THANKS if anyone helps me. Oh, and also I'm not sure if we need another instance variable to make this assignment easier/more efficient. Our teacher left that up to us. =/ This post has been edited by a member of staff (Spritzie) because of a violation of the forum rules. Please don't double post. If you would like to add something, use the 'Edit' button. Please check your user inbox to see if you have been contacted regarding this incident, then review our rules. Quote Link to comment Share on other sites More sharing options...
antiaircraft Posted May 3, 2011 Share Posted May 3, 2011 While I'm not too familiar with Java (okay, let's be honest here, I absolutely hate Java :P ), I can't see any obvious conceptual errors in what you're doing. Your best bet is probably to write up a few test cases, try them out, and see if your code works. If you run into more specific problems, we might be able to assist a little more. :) Quote Link to comment Share on other sites More sharing options...
Serendipity Posted May 3, 2011 Author Share Posted May 3, 2011 Thanks for looking at it. I got the add/remove stuff clarified but now I'm struggling on the different traverse types (breadth and depth). Is anyone familiar with that? I'm not even sure how to approach them. Quote Link to comment Share on other sites More sharing options...
antiaircraft Posted May 3, 2011 Share Posted May 3, 2011 Well, do you have methods that can be used to list out the neighbours of a node? If so, all you have to do is write traversal methods that use that method. I'm assuming you're already familiar with the concept of breadth-first and depth-first searches. If not, this xkcd strip might be useful: Okay, more seriously, Wikipedia has a couple of great articles on the subject. xD For a depth-first search, you could have a method that takes a root node and target (and an optional previous node to exclude from the search), and checks if that node is the one you're searching for. If it is, all it has to do is return that node. If not, it can loop through each neighbouring node of the root node (with the exception of the previous node), calling itself for each one of them, and passing the current root node as the previous node. If one of these searches returns a valid node, then the method just has to return that node. If all the neighbours have been checked and there are no matches, the method could then return null. So you could call the depth-first search method and pass it any root node, and it would traverse recursively down each branch until it found what it was looking for and returned it, or ran out of nodes to search and just returned null. For a breadth-first search, well, the algorithm on Wikipedia should work just fine. Quote Link to comment Share on other sites More sharing options...
Serendipity Posted May 8, 2011 Author Share Posted May 8, 2011 Thanks for your help! :) I don't know if you have any advice for Dijkstra's Algorithm? Because that's the next part of the project. Quote Link to comment Share on other sites More sharing options...
antiaircraft Posted May 8, 2011 Share Posted May 8, 2011 You're welcome! Although honestly I doubt I've really been that much help. xD Dijkstra's Algorithm is something I'm not particularly familiar with, but once again I'd recommend checking out the algorithm summary and pseudocode (although personally I find pseudocode pretty useless :P ) on Wikipedia. At first glance I think I'd want to use some sort of data structure (a pair of lists or something similar perhaps) that would allow me to keep track of all the nodes and their corresponding distances (although you may have accounted for this already). Then I'd duplicate the list of nodes into a separate list to track unvisited nodes, and run all the Dijkstra processing on those nodes one by one, removing them as I did so. As far as the actual processing of each node goes, you should be able to re-use your existing code to find all the neighbours of the current node (and filter out any nodes that aren't in the unvisited list), and the distance to each of them from the current node. Then for each neighbour, just compare (the current node's distance + the distance to the neighbour) with the neighbour's existing distance value - if your new distance is less, record it. Once all the neighbour nodes are done, remove the current node from the visited nodes list and keep going. :) Quote Link to comment Share on other sites More sharing options...
Serendipity Posted May 15, 2011 Author Share Posted May 15, 2011 Hey, thanks for your help for the other assignment. Some help is better than none. XD Well I have this homework (I hope you respond soon) but um here's the question. I'm not really sure how you do this. =/ Do you make all of the items listed objects or are some instance variables or what? Here's the question: Design a simulation of a basketball conference. Each conference has 10 teams. Each team has 12 players. Each player has a specific height, speed, and accuracy. Players know which team they belong to. Some players are scholarship players. Scholarship players need to record their current grade-point average. Players may be transferred between teams. Teams play basketball games against other teams in the conference. The result of each game is determined using a function based on the height, strength, speed, and accuracy of the players on each team. a. What are the objects in your object-oriented solution? b. What are the interactions between your objects? c. Which objects “have” other objects? (Also list target object) d. Which objects “use” other objects? (Also list target object) e. Which objects “are” other objects? (Also list target object) f. Draw a UML diagram of your solution. Quote Link to comment Share on other sites More sharing options...
antiaircraft Posted May 15, 2011 Share Posted May 15, 2011 Hmm... well I guess I'd keep things simple and represent each player with an object with height, strength, speed, accuracy, and GPA properties (where the GPA can be set to NULL for non-scholarship players). I'd also make a team object, with a property holding a list of the team's players, as well as add and remove methods for individual players. Depending on the use cases, I might also give each player object a 'parent' property pointing to their current team (which would be changed by the add and remove methods). To implement transfers between teams, all I'd need is a function that calls remove on one team object and add on another. :yes: To 'play' a basketball game between two teams, I'd add an additional method to team objects that loops through the list of players and performs all the necessary calculations with the players' attributes, then create a function that computes the stats of two teams and compares them. I'm not sure how I'd answer those questions exactly, but I guess that would be the general layout of the simulation program. :) Quote Link to comment Share on other sites More sharing options...
Serendipity Posted May 15, 2011 Author Share Posted May 15, 2011 Hey! All your tips seem correct, except I think you could make another object for Scholarship Player (that's like an Is-A relationship)? So like height, strength, speed, accuracy, and GPA are all instance variables? And also, is there any object oriented relationship that "uses" something? Like in question d from above? Quote Link to comment Share on other sites More sharing options...
antiaircraft Posted May 15, 2011 Share Posted May 15, 2011 I'm not familiar with Java object relationship terms, but I presume the team objects would be 'using' player objects when the method to obtain the team's overall stats was called. And yes, having a scholarship player be a subclass of the standard player object would work as well. :yes: Also, instance variables = properties in this context, so height, strength, speed, accuracy, and GPA would indeed all be instance variables. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.