|
icehewk posted:After six weeks of my first programming course, I know absolutely nothing. The instructor doesn't teach along with the book, nor does he teach much of the ideas behind the code. To borrow a lovely analogy, it's like he goes over what you can nail where without explaining where the spot to be nailed is located or why it's being nailed. As such, I've taken it upon myself through the resources provided here to learn Java outside of class and hope I can catch up before the next project is assigned. Unfortunately, my latest project is due Wednesday and I don't know where to begin. Can you guys help? Let's walk through plus(): First, how do you add fractions? If you have two fractions a/b and c/d, what is their sum in terms of a, b, c, and d? Second, which field of which object corresponds to a, b, c, and d?
|
# ¿ Feb 26, 2008 08:43 |
|
|
# ¿ Apr 27, 2024 13:52 |
|
icehewk posted:It would be a+c when b=d. How would you go about finding the LCD? Is the data field the part that corresponds to a, b, c and d? And, as previously mentioned, don't worry about the LCD. Look at the example output for a hint as to what denominator the result should have. NotHet posted:Given the following conditions: Incoherence fucked around with this message at 20:39 on Feb 26, 2008 |
# ¿ Feb 26, 2008 20:36 |
|
icehewk posted:So what would I write to find the common denominator? I really don't understand much of the actual language beyond syntax and reference.
|
# ¿ Feb 26, 2008 22:21 |
|
icehewk posted:Seems the uni knows how tough the class is so they provide a free tutor three times a week. Algebraically, a/b and c/d would require b*d and d*b along with a*d and c*b. That would be (a*d)+(c*b) over (b*d), right? Okay, now that's step 1. Step 2 is to figure out how to express each of a, b, c, and d in terms of the two objects. code:
|
# ¿ Feb 27, 2008 00:17 |
|
Emo.fm posted:I'm writing a program (yes, homework for intro CS) that plays a quick card game between two computer players and then returns who won (player 1 or 2, it's a random game of luck so theoretically I should be able to run it 1000000 times and see 50/50 winning percentages). I believe my code is correct, and I can run it as many times as I want with no exceptions if I actually run it repeatedly by hand -- but if I insert a for loop to get it to run multiple trials, I invariably end up with an array index out of bounds exception. I'm really confused by this because all of the variables (arrays of cards, stuff like that) are declared either in the for loop or the method called from within the for loop. What kinds of things should I be looking for? What could cause the program to screw up when run multiple times within itself, when I can run it so many times from outside with no incidents, so to speak? Off the top of my head, make sure you realize that Java (and C and its derivatives) arrays are 0-based, and an array of N elements has valid indices from 0 to N-1 inclusive (and index N will cause an exception). It has nothing to do with your for loop, which I assume is just counting runs and isn't actually doing anything in the body of the loop, except that running it 1 million times makes it more likely that a bug based on random input (and presumably there's random input somewhere in your program) will manifest itself. It may be worth posting your code, or at least posting the exception message.
|
# ¿ Mar 10, 2008 07:33 |
|
MEAT TREAT posted:I agree with posting your code. Your problem doesn't make much sense, it should work.
|
# ¿ Mar 10, 2008 09:16 |
|
itsasnake posted:Yeah, i sorted the list into an ArrayList of 1050 elements. Can you elaborate please? Something like this: code:
|
# ¿ Mar 14, 2008 03:19 |
|
Sterra posted:How does one go about implementing the externalizable interface so that a treelike object can't overflow the stack?
|
# ¿ Mar 15, 2008 23:06 |
|
Ranma4703 posted:I just started a co-op (my first job programming), and one thing I see in a lot of the code is the following: code:
The better answer is csammis's: ask them about it. If there's some good reason for it you might want to know what it is. quote:They also do multiple return statements in methods often, which I thought was another no-no.
|
# ¿ Mar 19, 2008 00:54 |
|
Drumstick posted:yay, thanks for that catch. Your question makes me unsure whether you understand what's going on here, so I'm being intentionally general to make sure we're not doing your homework for you or something. If you're having trouble, draw a picture of one of your grids and label each row and column with an index, then try and generalize to the question I asked above. There's one catch: you'll want to check that each of your four/eight adjacent points are inside the matrix, but what you want to do about that will depend on whether you want to wrap around on edges or just drop them.
|
# ¿ Apr 11, 2008 00:43 |
|
MEAT TREAT posted:I've always wondered what people do when you just want to drop them. In the past I've surrounded that block of code with a try/catch that doesn't do anything when an IndexOutOfBoundsException is thrown. code:
Drumstick posted:if i understand what your asking, you want the positions of the 8 adjacent 'boxes' quote:As for checking, they will need to drop off and not wrap around. Let me know if i understood what you were asking.
|
# ¿ Apr 11, 2008 04:20 |
|
clayburn posted:Haha it might be homework. I really just needed a point in the right direction like you gave, just some methods to look at. I've considered putting the words in some sort of array, I'm just worried about the efficiency of putting them there in the first place. I'll definitely check those methods out though, thanks.
|
# ¿ Apr 17, 2008 18:37 |
|
clayburn posted:I have a question about the HashMap class. I have populated a HashMap with a simple object I have created, basically consisting of two strings. One of these strings holds a segment of text from a file, the other holds the file name. What I want to do is to use the HashMap.contains method to determine if a particular string matches any of the text segments, and then use whatever get method is in the class to determine the file name. I figured that overriding the hashcode method would do the trick; I defined the hashcode method of the class to return the hashcode value of the string that we will be searching for. Apparently, this doesn't quite do the job. I am fairly new to hash tables, so if anyone could give me a few tips it would be very appreciated. 2. You're not being exactly clear on what you're doing. What it sounds like is that you're building an index and searching it, so your hash map looks like this: "foo" -> "file1.txt" "bar" -> "file2.txt" "baz" -> "file3.txt" "cat" -> "file1.txt" and your search term is "foo". In this case, the strings should be the keys, and the filenames should be the values, and contains() will do what you want. But it sounds like either (a) the strings aren't the keys (what is, then?), (b) you're actually using HashSet instead of HashMap (you want HashMap), or (c) this is not what you mean at all, at which point you should clarify.
|
# ¿ Apr 22, 2008 03:34 |
|
poopiehead posted:Unless I'm reading this wrong, you got it backwards. If you override either hashCode() or equals(), you should override both such that a.hashCode() == b.hashCode() iff a.equals(b) (and b.equals(a))
|
# ¿ Apr 23, 2008 03:29 |
|
poopiehead posted:Sorry. That't not correct though. Multiple values can have the same hash code. You want to minimize collisions, but they will happen. quote:These are both valid hash functions but wouldn't be if a.hashcode()==b.hashcode() -> a.equals(b)
|
# ¿ Apr 23, 2008 04:12 |
|
CrusaderSean posted:So in terms of programming practice, which of the following is better?
|
# ¿ Apr 25, 2008 00:55 |
|
Emo.fm posted:Sorry, I'm still a little confused. Let's say I have two files, MyApplet.java and SmallClass.java. If I want to load an image in SmallClass.java, how do I do it? Moving all of the image related operations that occur in SmallClass to MyApplet is not an option for me right now. There must be some way to load an image in a class that isn't an applet, right?
|
# ¿ May 8, 2008 22:57 |
|
Phillyt posted:If we can't assume tab characters, how exactly can I parse this file? I don't want code written but I am curious what someone would do. Basically, you want to tokenize "words" such that each word belongs to exactly one field, even if each field is not contained in a single word. So, in your example: code:
|
# ¿ May 23, 2008 04:38 |
|
Phillyt posted:How would you distinguish between the three words at the top to make it the name? I am probably going to just write the program assuming /t as the token and just get partial credit. Seems a lot easier. 1. None of the fields except for the name can have spaces in them. 2. Fields are separated by something, just not necessarily a tab (it could very well be a space), and that "something" obviously doesn't belong to any of the other fields. Ideally, fields are separated by some sort of white space. Given that, if there are N words in the line, the Nth word is the email address, the N-1th is the login ID, the N-2nd is the student ID, and the first N-3 words are the name. Another acceptable answer would be that the student ID is in a specified format (which you could look for, and everything before it is obviously the name), and the email address is in a specified format which you could pull out. Incoherence fucked around with this message at 09:07 on May 23, 2008 |
# ¿ May 23, 2008 09:04 |
|
adante posted:hi, I want to readLine() from multiple BufferedReaders and do the same thing with the result (after some transforms, depending on the specific BufferedReader instance. This article is old, but explains what I just said in more detail: http://www.ibm.com/developerworks/library/j-jtp0730.html
|
# ¿ Aug 23, 2008 01:19 |
|
adante posted:Right, I'm familiar with thread pools to an extent but my question was more: how do I do it when readLine() is a blocking operation and there is (as far as I can see) no non-blocking alternative? Basically, the only change I suggested is that instead of starting each "read, DoThing" task in its own thread, as your example does, you'd start each in a thread pool, which would have approximately the same result but wouldn't need to set up quite so many threads.
|
# ¿ Aug 23, 2008 07:55 |
|
adante posted:right, and what I meant is, I don't know how to do this. I don't see how it is possible to have N threads servicing M BufferedReaders where N < M. Maybe there is a misunderstanding here, but I think code speaks for itself. If you could provide some code/pseudocode that illustrates how to do this I'd be really interested. In particular, what I think you want is this. This code sample is pretty close to what you're doing, I guess.
|
# ¿ Aug 23, 2008 09:11 |
|
adante posted:Ok, MY point is that you guys keep suggesting I use a threadpool. Yes, a N-thread threadpool means my M threads will be very simple and the servicing goes into the N threads, but now I have M + N threads. This is not less than M threads. This is more than M threads! To re-iterate, my original question was if I could do it with less than M threads (well literally, 'Is there a better way of doing it than instantiating a thread for each reader?'). Here's an example (borrowing heavily from triplekungfu, and probably not working): code:
|
# ¿ Aug 25, 2008 22:11 |
|
pogothemonkey0 posted:That is exactly what I thought too, but it doesnt work well. After taking the time to implement, I realized that it wont work. Because you can only have one instance of a "key," I cant have different values associated with one string.
|
# ¿ Sep 28, 2008 11:19 |
|
dvinnen posted:The keyword 'this' tells java you mean to change the object's global variable. If you use the same variable name in a method Java assumes the the method variable is not the same as the global. For example from some code I am looking at: This might explain why changing salary to this.salary doesn't do anything in Sarah Sherman's example, but why it would do something in the BinaryFileMessage example.
|
# ¿ Sep 29, 2008 20:48 |
|
Hm, somehow missed this earlier.Sarah Sherman posted:Also, did you guys ever have a teacher who forced you to code in a certain style? My Java teacher is forcing us to use Allman indenting (which I use anyway so it doesn't really matter), and having us put those large comments before each method or class. I was surprised that he is making us do this; I thought that an important aspect of programming was being able to code in your own style.
|
# ¿ Sep 30, 2008 10:15 |
|
|
# ¿ Apr 27, 2024 13:52 |
|
I suspect that part of your problem is that you're checking q1.size() and q2.size() every time through the loop, even though you're also decreasing the size of the queue, so you only get halfway through before the loop ends. Also, you only put four pop() statements at the end, not five.
|
# ¿ Oct 1, 2008 19:15 |