Fun Stuff > CLIKC
A programming thread!
snalin:
Yeah, I solved it now, and I remembered one trick you might want to use:
import java.util.Arrays;
This is one of the most overlooked libraries in the standard Java library. It has the really, really, really useful static method Arrays.toString, which you can use to get a nice string representation of all arrays (for stuff like debugging). It also has a fill method and a copy method that does what you expect them to.
What it can do for you right now that's really nice, is the Arrays.sort method. It sorts arrays for you. So if you make an array of points, you can sort it using Arrays.sort, and then check that the first and second element are equal, that the third and fourth are equal, and the fifth and six are equal. Then you can pick the first, third and fifth elements to represent your triangle. Makes what you're doing a lot easier.
You have to have your points implement the comparable interface, otherwise you'll get a ClassCastException (as sort casts to Comparable when it sorts), but if you don't know how to do that yet, you should really learn how to do that asap, as implementing interfaces is mandatory when you do Java.
This still doesn't fix the parallel problem, so you'll have to do some more work to make that check. It'll just allow you to skip using loops when you check for equal corners, and also give the nice plus of having your triangle's corners sorted, which kinda probably should be a requirement anyways.
If you're not allowed to use any imports at all, tell your lecturers to go fuck themselves. Even not allowing Lists is edging close to the line of "not teaching Java", as you'd really never write Java without using lists. ArrayLists are Arrays with free contains() and indexOf() and other super-useful operations that you're going to be using on Arrays all of the time. There was a short period where Java devs that worked on Android went back to Arrays because they needed the speed on the old, slow droids, but now that smartphones are getting faster, that's probably not a worry anymore.
jwhouk:
--- Code: ---10 LET X = 0
20 PRINT "BASIC turns 50 this year."
30 LET X = X + 1
40 IF X = 50, THEN GOTO 60
50 GOTO 20
60 PRINT "I feel old. This was the first language I learned how to program."
70 END
--- End code ---
ankhtahr:
Damn the TA who conceived this exercise sheet. I've never seen tasks as underspecified. I don't know what to do, if it's not possible to create a triangle from these line segments. I think I've solved everything else. I just don't know what to return in that case. I'm considering just returning a triangle with the three points (0,0), (0,0) and (0,0).
ChaoSera:
When in question about what to return, return null and specify that case in the javadoc comment.
snalin:
Yes, oh God, never return an invalid object to indicate that the constructor fails.
Since it's a constructor, you can't return null either. The only correct way to handle a faulty constructor input is to throw an exception. Consider making a custom exception (like a GeometryException or something), and throw it if the input is faulty. Specify in the javadoc (under @throws) that this exception is thrown if the input is invalid:
--- Code: ---/**
* Constructs a Triangle from three line segments
*
* @param l1
* The first line segment
* @param l2
* The second line segment
* @param l3
* The third line segment
* @throws GeometryException
* if the three lines does not represent a valid triangle
*/
public Triangle(Line l1, Line l2, Line l3) throws GeometryException {
...
}
--- End code ---
If you're not at the point where you're expected to throw and handle exceptions, simply refactor the code that checks the validity of the Triangle into a method that returns true if the lines gives a valid triangle, and assert that method:
--- Code: ---public Triangle(Line l1, Line l2, Line l3) {
assert isValidTriangle(l1,l2,l3);
...
}
--- End code ---
Of course, asserts are a notoriously bad idea because they're not activated by standard in java, so if you forget to turn them on, code that should throw errors simply runs fine.
Navigation
[0] Message Index
[#] Next page
[*] Previous page
Go to full version