|
dvinnen posted:yea, i know that of course, but judging by the fact that the instructor wants him to implement the Comparable interface I think he is suppose to roll his own Good question! JulianD, can you post the whole assignment, please, so we know how in-depth the problem really is?
|
# ? Apr 21, 2009 02:10 |
|
|
# ? May 9, 2024 17:40 |
|
dvinnen posted:As for how to implement the function, nobody is going to do your homework but here is a hint. What primitive type makes up a String object? And what other kind of primitive type can it be represented as? I know no one is going to do my homework for me, and I wasn't asking anyone to; I was only expressing confusion at the vagueness of Mill Town's hint. Anyway, I think I've figured it out now: code:
Then I move into the body of the outer if...else statement, so that the last names aren't the same now. If my passenger's last name comes after the one I'm comparing to, I return 1 and break. Since I'm out of the body of the loop otherwise, I return -1 and break. It looks like I won't take care of the case where the last names are the same but my passenger's first name comes before the one I'm comparing to, but I think that this actually will because the only time it would return -1 is if my passenger's last name comes before the one I'm comparing to or if I go through all those other cases and don't return anything else, so I still return a -1. Mill Town, I'd rather not post the whole project because I've occasionally seen the tendency people have to go too far in explaining something, and I don't want that to happen with a part of the project I haven't gotten to just yet. If you guys could take a look at my approach there and see if it makes sense to you, too, that'd be great before I start trying to build this into my other classes. Thanks agian for the help!
|
# ? Apr 21, 2009 05:12 |
|
Since you did try to solve it yourself, I'll give you some sudo-code that may help you simplify what you are trying to do:code:
|
# ? Apr 21, 2009 05:38 |
|
I've never delt with compiling or exporting java code outside of an IDE like Netbeans, help me out here? I'm trying to submit some java code to a 'codegolf' site, where you have to write the smallest amount of code to satisfy a task. This means it has to compile it and run it server-side. The site in question: http://golf.shinh.org/p.rb?hexagon+2nd+fixed This is the start of my code: code:
code:
code:
EDIT: Also, it doesn't seem to have java.util...in the absense of a scanner, is the best way to get an integer of input really to wrap System.in in an InputStreamReader in a BufferedInputStream? Patashu fucked around with this message at 06:24 on Apr 21, 2009 |
# ? Apr 21, 2009 06:21 |
|
The reason its freaking out if your class is named h is because in Java your class's name has to match the filename it is in. Rename the file to h.java and that should clear up the first problem. As for the second, I don't know that's kinda weird.
|
# ? Apr 21, 2009 06:33 |
|
I -can't- rename it h.java, whatever I send it renames it to tmp.java then executes it as test.java or something like that. I don't even know.
|
# ? Apr 21, 2009 06:53 |
|
I can't remember if you can invoke non-public classes, but try removing 'public' from before 'class' and calling the class test. If it works you'll save 7 characters on your golf thing too.
|
# ? Apr 21, 2009 08:18 |
|
JulianD posted:I know no one is going to do my homework for me, and I wasn't asking anyone to; I was only expressing confusion at the vagueness of Mill Town's hint. Anyway, I think I've figured it out now: Fair enough. This is pretty much exactly where I was trying to point you. As hlfrk414 said, it could be simpler, but really this is perfectly fine for an intro programming course, no one expects your answer to be the most efficient. If you want to check and make sure that it compares properly when the last names match and the passenger's first name comes first, just try it out. From a quick glance, though, I think what you're doing is correct. Edit: wait, I think I'm wrong. Do you get a "not all code paths return a value" error while compiling? I think you need to make sure that you really always do return -1 if none of the other comparisons work. Mill Town fucked around with this message at 09:21 on Apr 21, 2009 |
# ? Apr 21, 2009 09:18 |
|
zootm posted:I can't remember if you can invoke non-public classes, but try removing 'public' from before 'class' and calling the class test. If it works you'll save 7 characters on your golf thing too. Breakthrough! Compiles and runs server-side. Is there a shorter way (in terms of characters) to get one integer from stdin? Can't use a Scanner. code:
|
# ? Apr 21, 2009 10:06 |
|
Patashu posted:Breakthrough! Compiles and runs server-side. As long as it's in the range 0-9, you could use code:
|
# ? Apr 21, 2009 11:48 |
|
If you're allowed to use deprecated functions: Integer.parseInt(new DataInputStream(System.in).readLine())
|
# ? Apr 21, 2009 13:50 |
|
Anyone have any idea when the website for Hibernate will be back up? Its been down for a bit now, and its a bit frusturating that I need to learn how to use it, but all the documentation is on a website that just says "Down for maintainance. Back up soon" or thereabouts.
|
# ? Apr 23, 2009 04:19 |
|
What do you need to know?
|
# ? Apr 23, 2009 04:30 |
|
Issue with implementing Java.Observable. I have a JFrame observing another class. And this works fine when I launch the JFrame normally. However when I create the JFrame from within another JFrame, the Observing JFrame does not observe. It just stays with its default values for everything. I'm adding the observer using object.addObserver(this); Edit: Just to make it clearer, opening the new JFrame in a new window. Donald Duck fucked around with this message at 17:30 on Apr 23, 2009 |
# ? Apr 23, 2009 15:09 |
|
duck monster posted:Anyone have any idea when the website for Hibernate will be back up? Its been down for a bit now, and its a bit frusturating that I need to learn how to use it, but all the documentation is on a website that just says "Down for maintainance. Back up soon" or thereabouts. The docs are included in the latest distro, so just download that.
|
# ? Apr 23, 2009 15:32 |
|
Hi guys, I feel bad asking for help here with out having contributed in this thread, yet. My java teacher is just... awful, so I'm really struggling trying to get any help with this, even though I've gone to him twice. Anyway, we've just started learning about inheritence, which is fine, I understand how it works. However, for our latest assignment, he requests we use the toString class to output our results from these seperate classes. I guess I really don't know how to do that with inheritence. I assumed values needed to be passed through an abstract class before they can be output. If you're printing from inside the class, I don't know how I can pass them through the abstract class, or if I even need to. Anyways, what I have so far... Main Class code:
code:
code:
code:
|
# ? Apr 23, 2009 16:50 |
|
The way your class is right now won't let you take advantage of the toString method because you hardcoded the number of sides. So what you should do is declare a variable called sides and define it as 4 in the square and 12 in the cube class. That way you can reuse the toString method you defined in your square class.
|
# ? Apr 23, 2009 17:34 |
|
Citanu541, from your post and code it is not clear at all what you are trying to do or what you need to do. Also, cubes are not squares.
|
# ? Apr 23, 2009 17:45 |
|
Oops, I forgot to post the assignment specifications. This is exactly what the sheet says word for word: Create a class called PerfectSquare. The perfect Square will have a parameter for the length of a side. The square will also have a parameter for surface area. The toString method of class PerfectSquare should print the length of the side(in meters) and the surface area. Create a class called PerfectCube. The cube toString method will print the length of a side and the surface area of the cube. You will also need a method for calculating the surface areas. I realize cubes are not square, but I'm just following what he's asking. Or following what I can some-what understand from that explination. MEAT TREAT's suggestion has given me an idea, hopefully I can remember it for when I return home.
|
# ? Apr 23, 2009 17:54 |
|
Citanu541 posted:I realize cubes are not square, but I'm just following what he's asking. Or following what I can some-what understand from that explination. Here's what I mean by specialization. For example, if you had a Square class you could make a Rhombus class be a subclass of Square, because a Rhombus is a special type of Square. Squares have all equal sides, and all interior angles are 90 degrees, Rhombuses don't have all 90 degree interior angles. Squares and Rhombuses share similar attributes. The length of a side, surface area and perimeter calculation would all be the same. However if asked a Square to draw it self, you couldn't use the same drawing logic for the Rhombus. That is why you would want to create a special Rhombus class. There is a lot that can be shared, and can be contained in the Square class (length of side, surface area calculation and perimeter) that Rhombus can use, but when it comes time for drawing, the Rhombus class would need to override the drawing method to draw it self correctly. I think you can figure out the rest now that you know how and when to use inheritance and know that cubes are not squares.
|
# ? Apr 23, 2009 18:16 |
|
Man, if you are going to use geometric examples, you need to at least get your examples right. A square is a special type of rhombus, and the surface area calculation is totally different.
|
# ? Apr 23, 2009 18:43 |
|
rjmccall posted:Man, if you are going to use geometric examples, you need to at least get your examples right. A square is a special type of rhombus, and the surface area calculation is totally different. Not to backlash at you, but I know this, also. I'm not exaggerating when I say my teacher is nuts. Those are the calculations he wants. Half the time I don't think he actually know what he wants, so he makes something up. Believe me, I'm embarrassed to post such broken code because of the way I was taught Java. I have a working example of the correct formulas for a square and the surface area of a square if you would like to see. The different is, I wrote it with out using the toString method, so it doesn't help me out on this assignment.
|
# ? Apr 23, 2009 19:23 |
|
Citanu541 posted:Not to backlash at you, but I know this, also. Sorry, that was directed at Warblade, not you. Your teacher is indeed nuts, and I'm worried that he doesn't know what "parameter" means. If you have a working function that calculates the area of a square, you can just call it from your toString method.
|
# ? Apr 23, 2009 19:36 |
|
An interface might not be the best way to go about this. I envision an abstract Shape class:code:
Note that you cannot make a new Shape() or new Shape(2.0) or something along those lines, because the class is marked as abstract and there is a method missing. When you extend Shape, you must implement getArea() in the subclass, or it won't compile. EDIT: Also, in the constructor of a subclass you can call super() or super(whatever parameters you want) to call constructors of the superclass. Like calling Shape(double) from PerfectSquare(double).
|
# ? Apr 23, 2009 21:53 |
|
I have a JDBC program which in itself is pretty short, it connects to the datbase and does a bit of sql, the most expensive part of this program is the db connect. Now, this program gets called over and over for the same user. I was trying to think of a way to reuse the connection without creating some sort of daemon. Connections aren't serializable so JNDI isn't going to work. RMI is out also. Connection pooling isn't going to work because the virtual machine exits and so the connection pool closes at this point. Any thoughts on a good way to solve this?
|
# ? Apr 24, 2009 00:53 |
|
You're not going to be able to share a db connection between runs if the vm stops - in particular the vm will close the connections on shutdown anyway.
|
# ? Apr 24, 2009 08:20 |
|
rjmccall posted:Man, if you are going to use geometric examples, you need to at least get your examples right. A square is a special type of rhombus, and the surface area calculation is totally different. What can I say, I suck at geometry. Hope I didn't confuse you Citanu541.
|
# ? Apr 25, 2009 00:17 |
|
I'm trying to create some widgets that extend from Component. These will be used on a cable set-top box and there is limited access to Java libraries. I'm stuck with using Java 1.3. Anyway I'm only at the point where I'm trying to paint my widget on the screen but when I try to draw multiple widgets they're not being placed where I would think they should be placed. For example, here's what I'm getting: The green square is supposed to be at 100, 200 with a width and height of 100. The blue square is supposed to be at 100, 50 also with a width and height of 100. I would think that the blue square should be lined up with the green square on the X axis and there should be a 50 pixel gap between them. What am I doing wrong? This is my "ScreenFrame" class. It's just the JFrame/container for the widgets. code:
code:
|
# ? Apr 25, 2009 00:31 |
|
OK, here is a suitably dumb Java question: I'm using NetBeans on OS X, and I have one directory structure with a set of source packages, and another directory structure with another set of packages. When I load up a source file from the latter set, NetBeans gives me a ton of errors, on every single "import" line that refers to the former set of packages. I've already gone through Tools > Libraries and added the correct paths to the former set of packages (actually, to both sets, since NetBeans does not recognize source files in the same package as the file I'm editing), but that hasn't fixed it. Do I need to "refresh" the source file somehow to get it to re-check the paths?
|
# ? Apr 27, 2009 01:06 |
|
Am I doing things wrong by using a Persistence framework like Toplink directly for a web app? I tried the prescribed EJB 3.0 Entity Bean > Session Bean > Struts Action when building my first sample app but I found the whole process to be incredibly time consuming and annoying just to get some data out of my database. Now I have a project setup where I'm getting data out of Toplink POJOs directly from Struts Actions with named queries and the whole process seems a lot more sane to me. I'm still not entirely sold on the persistence frameworks either. It looks great for DML but the vast majority of our web work is just getting data out. My experience so far has been that the frameworks sometimes makes this easy and other times a nightmare, especially when I have to try to join a lot of tables together. Is it common to use a combination of plain old SQL queries and persistence frameworks in a web app? Anyone have any ideas?
|
# ? Apr 27, 2009 04:36 |
|
Well, on the upside, I think I figured out how to manage external source packages, but I still can't get NetBeans to recognize the actual sourcefile's package. e: Created a new project, chose the next higher directory in the source tree, fixed that. Now there's just a few packages much further down in the library that it thinks are in the wrong place. The Evan fucked around with this message at 11:46 on Apr 27, 2009 |
# ? Apr 27, 2009 11:38 |
|
Is there a way to add an actionlister to a tab in a JTabbedPane that fires every time that pane is selected? Google is being stupid. I'm using swing btw, bosses' orders.
|
# ? Apr 27, 2009 16:04 |
|
The Evan: Sounds like you might have some kind of misunderstanding with how Java or NetBeans works, could you be more specific about your layout here with an example or something?
|
# ? Apr 27, 2009 16:05 |
|
Warblade posted:I'm trying to create some widgets that extend from Component. These will be used on a cable set-top box and there is limited access to Java libraries.... I'll take a quick stab in the dark at this. Because I feel your pain when developing in old versions of Java with a questionably complete/implemented libraries on set-top boxes. Normally, when using JFrame, you'd see myJFrame.getContentPane().add(component) instead of just myJFrame.add(), but we'll assume the JFrame you're using isn't too far off from normal in that it has a layout manager attached to it. Is it possible whatever layout manager it's using is switching up your x,y coordinates because of how its deciding to pack the components? Something to check anyway.
|
# ? Apr 28, 2009 03:55 |
|
zootm posted:The Evan: Sounds like you might have some kind of misunderstanding with how Java or NetBeans works, could you be more specific about your layout here with an example or something? Yeah I'm probably confused on some level. Basically I have a set of example code, say "packageA". It's a bunch of .java sourcefiles in a small directory structure, and each sourcefile has the correct package declaration (e.g. in packageA/gui, a file is listed as "package packageA/gui". There is a Main.java file (I didn't write this), and it has a series of import declarations, referring to packageB. Everything in packageB similarly has the correct package declarations corresponding to their directory structure. packageB also imports from packageC. So, I created a new NetBeans project from existing source, viz. packageA, and from the project properties under "libraries > compile" I added packageB and packageC. Now under "libraries" in the project browser it displays all of the packages correctly, e.g. packageB, packageB.framework, etc. HOWEVER, some of the source files in packageB say "incorrect package", and some of the places where they refer to methods in packageC, specifically import statements, it NetBeans says the packages don't exist. packageC is all .class files, if that makes a difference. Thanks.
|
# ? Apr 28, 2009 04:15 |
|
LilMike posted:I'll take a quick stab in the dark at this. Because I feel your pain when developing in old versions of Java with a questionably complete/implemented libraries on set-top boxes. Well here's the thing. The code I posted up is running on a Windows box. I'm coding on Windows just to prototype this thing since it takes 10 minutes to reboot the set-top just to see if something I did "worked". But yeah, layout manager might be what's doing it. EDIT: This may be a big duh moment for me. Maybe I shouldn't be trying to place things in a coordinate system and instead rely on the layout managers? I guess I could use a GridBayLayout or something similar to place things. EDIT2: Just for shits, I decided to set the layout manager to null. The squares now appear as they would in a coordinate system. Thanks LilMike! Warblade fucked around with this message at 16:50 on Apr 28, 2009 |
# ? Apr 28, 2009 16:40 |
|
Does anyone know of a way to get the compiler to generate errors (or at least warnings) when you're including code that is above whatever target compatibility you've set it at? For example, I code and build using Eclipse (an old version, 3.2). I'm running JDK 1.6 but I'm building for target and source compatibility with 1.3. The other day I ran into a problem using StringBuffer.append(StringBuffer). Apparently this was added in 1.4, but I didn't get any kind of warning, just a runtime exception since my target didn't support this method call. I'm also making builds with Ant, but I don't see any errors when trying to compile with that either. I thought turning on -Xlint might give some kind of warning with that, but, no.
|
# ? Apr 30, 2009 21:54 |
|
Window -> Preferences -> Java -> Compiler -> Compiler Compliance level.
|
# ? Apr 30, 2009 22:25 |
|
Alternatively, Warblade, check out the command line parameters for javac -source and -target, I think you'd want something like javac -source 1.3 -target 1.3... So it would only accept 1.3 compatible java and output 1.3 compatible bytecode. Bonus: you can specify these as attributes to the javac tag in your any build scripts.
|
# ? May 1, 2009 05:26 |
|
|
# ? May 9, 2024 17:40 |
|
LilMike posted:Alternatively, Warblade, check out the command line parameters for javac -source and -target, I think you'd want something like javac -source 1.3 -target 1.3... So it would only accept 1.3 compatible java and output 1.3 compatible bytecode. Bonus: you can specify these as attributes to the javac tag in your any build scripts. I don't think that would help, since warblade was running into a library issue and not a classload or verifier issue. Eclipse allows you to set up different jvms to build against, I'm on the train right now but I'm pretty sure sun still provides legacy jvms for download. e: here -> http://java.sun.com/j2se/1.3/download.html trex eaterofcadrs fucked around with this message at 13:48 on May 1, 2009 |
# ? May 1, 2009 13:06 |