THESE FORUMS NOW CLOSED (read only)

  • 28 Mar 2024, 14:36
  • Welcome, Guest
Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 [2] 3 4   Go Down

Author Topic: A programming thread!  (Read 42565 times)

snalin

  • Vulcan 3-D Chess Master
  • *****
  • Offline Offline
  • Posts: 3,540
  • You may Baste me
Re: A programming thread!
« Reply #50 on: 09 May 2014, 02:42 »

Write some stuff. It's kinda hard to get started without a specific task, but you should write some stuff. Make sure you're using Java 7, by the way. And also grab an IDE as discussed at length up-thread - Eclipse is fine.

Java is... pretty close to Python when it comes to capabilities and how you think about the language, so you're pretty far ahead already. Python allows you to do everything as a script rather than a proper object oriented language, so I guess it depends on how you've been using it.

There's stuff like codingbat, but that gives problems that you could solve in the same way in any language - it doesn't really get into the java-specific stuff. Which is probably what you need.

Basically, you need to get on terms with what classes and objects are, and how they relate - this also means knowing static methods from non-static ones. Depending on how advanced the course is, you want to know how packages work, how private/default/protected/public access works (and maybe when to use them!), have a basic grip on what recursion is, know how interfaces works, how to implement interfaces, and finally how inheritance works.

If you want a texty overview, check out the docs at oracle. You could also look into getting the book for your CS course - that might have some proper exercises that allows you to work with the stuff I mentioned above.
Logged
I am a cowboy / on a steel horse I ride
I am wanted / Dead or alive

Masterpiece

  • Older than Moses
  • *****
  • Offline Offline
  • Posts: 4,364
  • No time for Claireification
Re: A programming thread!
« Reply #51 on: 09 May 2014, 03:26 »

The Java Docs are great if you want to know how a certain library in Java works.

snalin

  • Vulcan 3-D Chess Master
  • *****
  • Offline Offline
  • Posts: 3,540
  • You may Baste me
Re: A programming thread!
« Reply #52 on: 09 May 2014, 06:11 »

For introductory courses, you probably only need the Collections library and the Scanner class. If somebody tells you to use arrays and/or IOStreams, they're probably dumb and/or from the 80s.

(click to show/hide)
Logged
I am a cowboy / on a steel horse I ride
I am wanted / Dead or alive

danpaul88

  • Not quite a lurker
  • Offline Offline
  • Posts: 7
Re: A programming thread!
« Reply #53 on: 09 May 2014, 07:14 »

Anyone here done any work with Java EE? I'm not looking to get too deep into the guts of it, my plan is to use it as the backend for a website in preference to something like PHP or ASP.NET (which is limited to Windows servers, I'm aiming to make this code OS agnostic to some extent) whilst avoiding any dependency on Java on the client machine.

Links to useful resources to get started would be appreciated, if you happen to have any lying around... or if you'd like to suggest an alternative I've perhaps not considered that'd be great too. There's not a lot of heavy lifting required (it'll mostly be reading and writing JSON, doing database manipulation and some heavy number crunching, hence why I decided against node.js). I'll also be running some code independently so it can update states without any client requests occurring, ideally in the same process as the one listening for web requests.

I have used Java a fair bit in the past for command line applications so I have a decent understanding of the language itself, it's more the EE part of it I'm looking to learn more about. Intending to use NetBeans as an IDE since I understand it's EE support is far better than that offered in Eclipse.
Logged

snalin

  • Vulcan 3-D Chess Master
  • *****
  • Offline Offline
  • Posts: 3,540
  • You may Baste me
Re: A programming thread!
« Reply #54 on: 09 May 2014, 07:53 »

I have not touched EE, so I can't help you there. Though it doesn't really sound like you need it - JSON is just string parsing, and both database manipulation (JDBC) and number crunching is available in SE.

Other than that, EE is just a bunch of libraries that allows you to do advanced, heavy duty stuff. If you want big, multithreaded applications that talks with everything on the web and interfaces with a ton of things, EE is probably a given - but if you're doing low-key stuff, I dunno.
Logged
I am a cowboy / on a steel horse I ride
I am wanted / Dead or alive

danpaul88

  • Not quite a lurker
  • Offline Offline
  • Posts: 7
Re: A programming thread!
« Reply #55 on: 09 May 2014, 09:05 »

Yeah, I'm mostly just using SE features but from what I've read it looks like the EE stuff can be leveraged to make a cleaner interface for accessing the Java code from a web server without some sort of intermediary between the two.
Logged

Pilchard123

  • Older than Moses
  • *****
  • Offline Offline
  • Posts: 4,131
  • I always name them Bitey.
Re: A programming thread!
« Reply #56 on: 09 May 2014, 10:43 »

Netbeans is a good IDE as well, though that opinion might be coloured by it being the first one I used seriously.
Logged
Piglet wondered how it was that every conversation with Eeyore seemed to go wrong.

ankhtahr

  • GET ON THE NIGHT TRAIN
  • *****
  • Offline Offline
  • Posts: 2,700
  • A hacker spathe night owl
Re: A programming thread!
« Reply #57 on: 09 May 2014, 15:10 »

So I think I've managed this line segment intersection stuff. There's room for errors due to rounding errors, but I'm relatively sure I don't have to compensate for that.

I now have a different problem, more programming related. Anybody have a nicer way for doing what I have to do here?

l still have the same structure, so a point is an object with two double values for coordinates, a line is defined by two points, a triangle by three. I now have to write a constructor to create a triangle from three lines.

I'd now put all the points I have into an array, and for every element I'd have to check, whether it's equal to another element. If I don't get three matched pairs I can't make a triangle from it. But this doesn't really feel elegant to me…
Logged
Quote from: Terry Pratchett
He had the look of a lawn mower just after the grass had organised a workers' collective.

snalin

  • Vulcan 3-D Chess Master
  • *****
  • Offline Offline
  • Posts: 3,540
  • You may Baste me
Re: A programming thread!
« Reply #58 on: 09 May 2014, 15:48 »

It's not elegant, and it doesn't work - it allows stuff like three parallel lines. You'll need to test both that there are three matched pairs, and no element that matches more than one other element.

I'd just put the endpoints of the three lines in a list of some sort (or array if you wanna be old fashioned about it), and test that every element has two objects in the list that's equal to themselves - the other point and itself. Also, use object equality on the Points, don't manually check their x- and y-axis values in your triangle constructor.

Also, you probably know this, but a reminder to everybody that's starting up with Java - '.equals' is not the same as '==' , unless you forgot to write the equals method in which case it still only check for reference equality. So this code prints false:
Code: [Select]
ArrayList<Integer> list = new ArrayList<>();
ArrayList<Integer> list2 = new ArrayList<>();
list.add(5);
list2.add(5);
System.out.println(list == list2);

This is where you really, really, really want to start using unit tests in a proactive way - test driven development is the nice way. If you don't know JUnit, familiarize yourself with it. Write tests for triangles that should work, and triangles that shouldn't. Start with obvious examples, and continue with harder ones. It's not really necessary, but if you are familiar with the technique, everything in your life will be a lot easier. Trust me.
« Last Edit: 09 May 2014, 15:54 by snalin »
Logged
I am a cowboy / on a steel horse I ride
I am wanted / Dead or alive

ankhtahr

  • GET ON THE NIGHT TRAIN
  • *****
  • Offline Offline
  • Posts: 2,700
  • A hacker spathe night owl
Re: A programming thread!
« Reply #59 on: 09 May 2014, 15:57 »

JUnit tests and stuff like that are exactly what we're taught in the software engineering class.

Sadly we're not allowed to use lists, but I'll see what I can do with arrays.

And thanks for pointing out the parallel lines. I really didn't think of that.
Logged
Quote from: Terry Pratchett
He had the look of a lawn mower just after the grass had organised a workers' collective.

snalin

  • Vulcan 3-D Chess Master
  • *****
  • Offline Offline
  • Posts: 3,540
  • You may Baste me
Re: A programming thread!
« Reply #60 on: 09 May 2014, 16:35 »

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.
Logged
I am a cowboy / on a steel horse I ride
I am wanted / Dead or alive

jwhouk

  • Awakened
  • *****
  • Offline Offline
  • Posts: 11,022
  • The Valley of the Sun
Re: A programming thread!
« Reply #61 on: 09 May 2014, 16:44 »

Code: [Select]
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
Logged
"Character is what you are in the Dark." - D.L. Moody
There is no joke that can be made online without someone being offended by it.
Life's too short to be ashamed of how you were born.
Just another Joe like 46

ankhtahr

  • GET ON THE NIGHT TRAIN
  • *****
  • Offline Offline
  • Posts: 2,700
  • A hacker spathe night owl
Re: A programming thread!
« Reply #62 on: 10 May 2014, 15:58 »

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).
Logged
Quote from: Terry Pratchett
He had the look of a lawn mower just after the grass had organised a workers' collective.

ChaoSera

  • Scrabble hacker
  • *****
  • Offline Offline
  • Posts: 1,405
Re: A programming thread!
« Reply #63 on: 10 May 2014, 16:27 »

When in question about what to return, return null and specify that case in the javadoc comment.
Logged

snalin

  • Vulcan 3-D Chess Master
  • *****
  • Offline Offline
  • Posts: 3,540
  • You may Baste me
Re: A programming thread!
« Reply #64 on: 10 May 2014, 17:01 »

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: [Select]
/**
* 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 {
            ...
        }


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: [Select]
public Triangle(Line l1, Line l2, Line l3) {
    assert isValidTriangle(l1,l2,l3);
    ...
}

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.
Logged
I am a cowboy / on a steel horse I ride
I am wanted / Dead or alive

snalin

  • Vulcan 3-D Chess Master
  • *****
  • Offline Offline
  • Posts: 3,540
  • You may Baste me
Re: A programming thread!
« Reply #65 on: 10 May 2014, 17:05 »

By the way, the reason for wanting the code to throw an exception is this: if the constructor is a part of a bigger application, and a faulty constructor call simply makes the code return an invalid object, the error (the faulty call) will cause an explosion - but not where it happened. Instead, the faulty object will propagate through the system, and cause seemingly completely unrelated errors somewhere else.

In this case, I can imagine a flaw in the code that constructs the Triangle causing a division-by-zero exception much further on in the program, where some other piece is trying to figure out the angles of the triangle. Or something like that. A very important software engineering principle is this: an exception should be thrown in the first place it is possible that it is thrown, as that will make finding out why it happened easier.
Logged
I am a cowboy / on a steel horse I ride
I am wanted / Dead or alive

ChaoSera

  • Scrabble hacker
  • *****
  • Offline Offline
  • Posts: 1,405
Re: A programming thread!
« Reply #66 on: 11 May 2014, 03:47 »

Oh, I didn't see that the method in question was a constructor. In that case, yes, throw an exception. In all other cases, returning null is usually the way to handle faulty input. Just always remember to specify that in the javadoc. Because if you'd be working in a larger team and somebody else used the method you wrote, he knows he has to check the outcome for Nullpointers. (It always somewhat bugged me that in Java a "NullPointerException" is thrown in that case, java doesn't have pointers...)
Logged

snalin

  • Vulcan 3-D Chess Master
  • *****
  • Offline Offline
  • Posts: 3,540
  • You may Baste me
Re: A programming thread!
« Reply #67 on: 11 May 2014, 04:23 »

Java does have pointers. In fact, it only has pointer. So a nullpointer is correct - you're trying to access a method or variable from a pointer, but that pointer points to null instead of an actual object.

You can see the pointer behavior in code like:

Code: [Select]
public static void main(String[] args) {
String s = "This is a pointer to a String";
change(s);
System.out.println(s); //prints "This is a pointer to a String
}

private static void change(String s) {
s = "This makes the local s variable point to a different String";
}

This is a pretty good article about how things work: Java is Pass-by-Value, Dammit!
Logged
I am a cowboy / on a steel horse I ride
I am wanted / Dead or alive

ankhtahr

  • GET ON THE NIGHT TRAIN
  • *****
  • Offline Offline
  • Posts: 2,700
  • A hacker spathe night owl
Re: A programming thread!
« Reply #68 on: 11 May 2014, 16:14 »

Seriously, thank you so much, Snalin, I feel like I've learnt more about Java from your help with this exercise sheet than from the lecture itself.

I'm finished with it now, and can now turn to the fun part of the exercise sheets which are due tomorrow. The technical computer science exercise sheet which is mostly C.

The more I do with Java and the more I do with C, the more I dislike the first and the more I like the latter.
Logged
Quote from: Terry Pratchett
He had the look of a lawn mower just after the grass had organised a workers' collective.

Masterpiece

  • Older than Moses
  • *****
  • Offline Offline
  • Posts: 4,364
  • No time for Claireification
Re: A programming thread!
« Reply #69 on: 11 May 2014, 17:13 »

See, I think that is weird, because I liked Java, adore C# and always watch C with caution.

Why hate on higher programming languages?

snalin

  • Vulcan 3-D Chess Master
  • *****
  • Offline Offline
  • Posts: 3,540
  • You may Baste me
Re: A programming thread!
« Reply #70 on: 11 May 2014, 17:21 »

Heartbleed wouldn't have happened had it been written in a higher level language!

No, each to their own. You get shit done in C as well, you just have the whole additional layer of complexity between raw values and data types that you kinda have to handle yourself. And segfaults exist. Oh lawd, the segfaults. "Yr code did something wrong somewhere, good fucking luck!"

These days I write more C# than Java, and I find the languages pretty comparable in how easy they're to use. There's no free C# IDE that's at the quality level of the free Java ones, but Visual Studio is supposed to be the shit, so I guess there's a trade off to consider there. Javadoc is vastly superior to the stupid xml scheme in C#, which is  why I still prefer Java, but that's only slightly.

I also have an irrational hatred for anything with the Lisp-syntax and anything purely functional just doesn't seem worth the hassle.
Logged
I am a cowboy / on a steel horse I ride
I am wanted / Dead or alive

ankhtahr

  • GET ON THE NIGHT TRAIN
  • *****
  • Offline Offline
  • Posts: 2,700
  • A hacker spathe night owl
Re: A programming thread!
« Reply #71 on: 11 May 2014, 23:07 »

I don't know why, but to me C is a lot more intuitive than Java. I tell the computer what to do, it does what I tell it to do, and if it does something wrong, then I can be relatively sure that I made a mistake somewhere.

I think I prefer C over Java simply because I have my most experience with Assembler (AVR Assembler, but still), so I like to be able to project my code into stuff the CPU could do, or the memory. That's not at all possible with Java, it's too abstract.

But I somehow like both extremes, as I'm also a great fan of the functional Erlang.

Anyway, I'm finished with my C exercise sheets, and will go hand them in now, and then I'll go to sleep. It's 8 am. And then I should do the exercise sheet for software engineering! Yay! More Java!
Logged
Quote from: Terry Pratchett
He had the look of a lawn mower just after the grass had organised a workers' collective.

ev4n

  • Scrabble hacker
  • *****
  • Offline Offline
  • Posts: 1,328
  • Shameless Shamy Shipper
Re: A programming thread!
« Reply #72 on: 12 May 2014, 08:03 »

Heartbleed wouldn't have happened had it been written in a higher level language!

I'm always leery of statements like this.  I know what you're saying, but compilers and OSs have bugs too...

I don't know why, but to me C is a lot more intuitive than Java. I tell the computer what to do, it does what I tell it to do, and if it does something wrong, then I can be relatively sure that I made a mistake somewhere.

Which, as snalin rightly points out, is fine when you have 12 lines of code you wrote running independently.  A system of 100,000 lines of C code written by 3 organizations and couple of dozen developers?  Have fun....
Logged

ankhtahr

  • GET ON THE NIGHT TRAIN
  • *****
  • Offline Offline
  • Posts: 2,700
  • A hacker spathe night owl
Re: A programming thread!
« Reply #73 on: 15 May 2014, 11:01 »

uh. Now this i a slightly more challenging task. It's for the algorithms lecture. I need to implement a LRU Cache in Java, by combining a doubly linked list with a hash table. For maximum points I need to implement the list and the hash table myself, without relying on java.util.HashMap or any list class. I've got a few files where I have to fit it in. I have to put it into the LRUCache class.
Logged
Quote from: Terry Pratchett
He had the look of a lawn mower just after the grass had organised a workers' collective.

snalin

  • Vulcan 3-D Chess Master
  • *****
  • Offline Offline
  • Posts: 3,540
  • You may Baste me
Re: A programming thread!
« Reply #74 on: 15 May 2014, 12:05 »

Quote
Ihr code hier
machen Sie das schneller

I love how they use the formal pronouns when addressing you. We barbaric Norwegians stopped doing that sometime around the 1960s. They insist on writing the Javadoc in German (they do the same thing here), which makes it a tad bit harder for me to get you some hints, but I'll give it a god*.

Doubly linked lists are pretty easy - you need a class DoublyLinkedList, and a (possibly inner) class DoublyLinkedListNode. The head should always have a previous value of null, and the tail should always have a next value of null. That's the java way of doing it.

Code: [Select]
public class DoublyLinkedList<DataType> {

MyLinkedListNode<DataType> head;
MyLinkedListNode<DataType> tail;
}

public class DoublyLinkedListNode<DataType> {
DataType data;
MyLinkedListNode<DataType> previous;
MyLinkedListNode<DataType> next;
}

Then the insertFirst method in the DoublyLinkedList would be:

Code: [Select]
public void insertFirst(DataType data) {
DoublyLinkedListNode<DataType> node = new DoublyLinkedListNode<DataType>(data);
head.setPrevious(node);
head = node;
}

And so on.

The hash table is also really straight forward - it's a map from an int to a resizable list of some kind, and you use the hashCode of the element you insert as the key of the map. The map can be implemented as an array, where you use the index of the array as the key.

This means that you have an array of lists (for example your doubly linked ones):

map = DoublyLinkedList[]

And inserting an element o means inserting into the array at index o.hashCode:

Code: [Select]
public void insert(DataType data) {
    map[data.hashCode].addFirst(data);
}

Check that the map has that index. If it doesn't (IE. it's too small), you can make an array of double the size of the old one, and transfer all of the elements. Insertion is still an O(1) operation, if you look at the average insertion time (I'll save the proof for when you get it on an exercise sheet in some Data Structures or Algorithms course :p). You could also just use ArrayLists:

map = ArrayList<DoublyLinkedList>();

The way the built in ArrayList inserts elements is exactly the algorithm I just described. To do the LRU (if I've understood what the LRU is), simply removeLast on the LinkedList at the array space you're inserting in if it's got more than the max number of elements. Also, you shouldn't fill the array space with a list before you need that list, so the insert method will also need to check if the index of the array is currently null.

Hope that helps.

* When I found that typo, I realized that it needed to stay, because giving you a God is a pretty cool thing to do, I recon.
Logged
I am a cowboy / on a steel horse I ride
I am wanted / Dead or alive

ankhtahr

  • GET ON THE NIGHT TRAIN
  • *****
  • Offline Offline
  • Posts: 2,700
  • A hacker spathe night owl
Re: A programming thread!
« Reply #75 on: 15 May 2014, 12:38 »

It actually is a Algorithms and Data Structures lecture…

The exercise is number four on this exercise sheet.
I've tried to translate the given task as good as I can here: (refer to the PDF for the picture)

Quote
In computer science results of a time-wise expensive operation are often saved to be able to access them faster later on. Such an expensive operation is for example loading of data from the hard drive to the RAM, or the rendering of downloading of map tiles on a GPS. A data structure which allows this is called "cache". Usually the memory in which the data is to be buffered is too small to back up all requests. In this case some entries need to be removed. Which those are is decided by an eviction strategy.
One of the most common stategies is "Least Recently Used (LRU)", where the entry which hasn't been read for the longest time is deleted. To find this entry one can, instead of using timestamps, combine a doubly linked list with a hash table in an elegant way. The following picture shows the schematical setup of this data structure:
<picture here>
Your task is to implement this in Java. Your data structure should implement a method ValueType retrieve(KeyType key) in O(1), which returns the value to a given key. The hashtable contains all cached elements. The doubly linked list displays the age of these elements. A retrieve first checks whether an entry is already cached, using the hashtable. If this is the case, it needs to be removed from it's current position in the list and moved to the front. Else it needs to be read from the background container (expensive!) and inserted into hashtable and chained list. To solve this task use the following data, which can be retrieved from the lecture website:
ContainerInterface.java: An interface for the underlying operations.
SlowRam.java: An example container which implements this interface.
PagerInterface.java: An interface for the Cache.
Pager.java: An abstract class which implements this interface.
LRUCacheTest.java: A JUnit-class which you can use for testing your class. It's also the task your handed in solution will be pre-checked.

Create a Java Class LRUCache in a file named "LRUCache.java". Your class needs to extend Pager. Only upload this single file. You'll receive one point, if you hand in a non-trivial solution, which passes the JUnit test. You'll receive additional three points if you implement the described structure correctly. You'll then receive four additional points if you implement the hashtable and the chained list yourself, so using neither java.util.HashMap nor a list class.
Hint: Questions like this are often being asked in job interviews.

I'd probably want to implement it using java.util.HashMap first, and implement it afterwards.
Logged
Quote from: Terry Pratchett
He had the look of a lawn mower just after the grass had organised a workers' collective.

ankhtahr

  • GET ON THE NIGHT TRAIN
  • *****
  • Offline Offline
  • Posts: 2,700
  • A hacker spathe night owl
Re: A programming thread!
« Reply #76 on: 15 May 2014, 17:32 »

I'd probably want to implement it using java.util.HashMap first, and implement it afterwards.

Done.

All I need to do now is implement my own HashMap, which behaves like the java.util one, at least in regards to the put, get, remove and containsKey method.
Logged
Quote from: Terry Pratchett
He had the look of a lawn mower just after the grass had organised a workers' collective.

ankhtahr

  • GET ON THE NIGHT TRAIN
  • *****
  • Offline Offline
  • Posts: 2,700
  • A hacker spathe night owl
Re: A programming thread!
« Reply #77 on: 15 May 2014, 20:49 »

Motherfucking Java.

I need an array. Of elements of my self defined list. What do I need to do? Of course:
Code: (java) [Select]
DoublyLinkedListElement[] table = (DoublyLinkedListElement[]) new Object[11] with a damn @suppressWarnings("unchecked") beforehand. Because Java.

Sadly the system we use to hand in our solutions denies your solution anyway. We've been trying to fuck our way around this fucking language barrier for two hours now. It's 5:49 am. I need some sleep. Also I need to hand it in until noon. But as I have a partner, who has done basically the rest of the exercise sheet, I feel obliged to finish this. I guess I'll stay here until he appears and tells me to go home.
Logged
Quote from: Terry Pratchett
He had the look of a lawn mower just after the grass had organised a workers' collective.

pwhodges

  • Admin emeritus
  • Awakened
  • *
  • Offline Offline
  • Posts: 17,241
  • I'll only say this once...
    • My home page
Re: A programming thread!
« Reply #78 on: 16 May 2014, 00:13 »

I'm amused at all this Java shenanigans. When I had to write a cache of this sort (commercially, for money), I sketched it out in pseudocode and then implemented it in assembly language. As I recall it took about 300 bytes. The improvement in OS performance was similar to installing a SSD in modern terms.
Logged
"Being human, having your health; that's what's important."  (from: Magical Shopping Arcade Abenobashi )
"As long as we're all living, and as long as we're all having fun, that should do it, right?"  (from: The Eccentric Family )

Masterpiece

  • Older than Moses
  • *****
  • Offline Offline
  • Posts: 4,364
  • No time for Claireification
Re: A programming thread!
« Reply #79 on: 16 May 2014, 00:18 »

Code: (java) [Select]
DoublyLinkedListElement[] table = (DoublyLinkedListElement[]) new Object[11]
I'm guessing you haven't had generics yet.

ankhtahr

  • GET ON THE NIGHT TRAIN
  • *****
  • Offline Offline
  • Posts: 2,700
  • A hacker spathe night owl
Re: A programming thread!
« Reply #80 on: 16 May 2014, 01:15 »

I had to resort to this, because I was getting a "cannot create a generic array".

In the end I gave up, because I finally need some sleep. I've been up for 27 hours now, and can't think straight.

Now the next upcoming exercise sheet is technical computer science, and that is the only lecture I seriously enjoy. Currently we're looking at a theoretical processor designed by our professor. This week our tasks are mostly implementing assembly-commands in micro operations. I enjoy this!
Logged
Quote from: Terry Pratchett
He had the look of a lawn mower just after the grass had organised a workers' collective.

snalin

  • Vulcan 3-D Chess Master
  • *****
  • Offline Offline
  • Posts: 3,540
  • You may Baste me
Re: A programming thread!
« Reply #81 on: 16 May 2014, 04:00 »

I had to resort to this, because I was getting a "cannot create a generic array".

Yup!

So, a generic is just a placeholder for a class that will be supplied later. This is implemented in Java (and C#) through type erasure - the type of the element will only be checked during compile time, and at runtime, no information about the class will be available.

The way to solve the problem is to use an ArrayList, as that will handle type checking for you.


I'm amused at all this Java shenanigans. When I had to write a cache of this sort (commercially, for money), I sketched it out in pseudocode and then implemented it in assembly language. As I recall it took about 300 bytes. The improvement in OS performance was similar to installing a SSD in modern terms.

Those were the days, huh? 300 bytes of of code is ~350 ASCII characters. That's not a lot of code.
Logged
I am a cowboy / on a steel horse I ride
I am wanted / Dead or alive

Schwungrad

  • Pneumatic ratchet pants
  • ***
  • Offline Offline
  • Posts: 345
Re: A programming thread!
« Reply #82 on: 18 May 2014, 13:12 »

This is a pretty good article about how things work: Java is Pass-by-Value, Dammit!
Urgs. Thanks for reminding me why I prefer C++ over Java.
Logged

cesium133

  • Preventing third impact
  • *****
  • Offline Offline
  • Posts: 6,148
  • Has a fucked-up browser history
    • Cesium Comics
Re: A programming thread!
« Reply #83 on: 18 May 2014, 13:30 »

Complaint: why, when compiling a program, do the libraries that need to be linked go after the file that requires them in the command line rather than before? I always think of them as prerequisites for the program, so when writing the command line I always end up putting them before and getting frustrated when it refuses to work...  :psyduck:
Logged
The nerdy comic I update sometimes: Cesium Comics

Unofficial character tag thingy for QC

snalin

  • Vulcan 3-D Chess Master
  • *****
  • Offline Offline
  • Posts: 3,540
  • You may Baste me
Re: A programming thread!
« Reply #84 on: 18 May 2014, 13:47 »

In Java it would be:

javac -cp [stuff to put on class path like .jar or .class files] [.java source files]

So then it's before the sources. Or you could just set PATH temporarily.

In C++ (gcc), on the other hand, options are after the sources. I think. So then it's the other way around. So the answer to your question is language-dependent.
Logged
I am a cowboy / on a steel horse I ride
I am wanted / Dead or alive

cesium133

  • Preventing third impact
  • *****
  • Offline Offline
  • Posts: 6,148
  • Has a fucked-up browser history
    • Cesium Comics
Re: A programming thread!
« Reply #85 on: 18 May 2014, 13:52 »

I'm using Fortran, and the syntax I always end up typing in is:

gfortran -lblas -llapack arss.f

when it should be

gfortran arss.f -lblas -llapack

I suppose I should get unlazy and make a proper makefile for the program (I was given just the Fortran source for it).
Logged
The nerdy comic I update sometimes: Cesium Comics

Unofficial character tag thingy for QC

Schwungrad

  • Pneumatic ratchet pants
  • ***
  • Offline Offline
  • Posts: 345
Re: A programming thread!
« Reply #86 on: 18 May 2014, 14:26 »

In C++ (gcc), on the other hand, options are after the sources. I think. So then it's the other way around. So the answer to your question is language-dependent.
http://linux.die.net/man/1/gcc says the options come before the input file(s). Same should go for gfortran. :?

However, dynamic linking works just the other way round: The compiler makes your source file into an object file that just contains the instruction "put args on the stack and call libraryfunction(args)". Then, in a second step, the linker makes the object file (or several object files) into an executable by adding the information that libraryfunction(args) is found in the file library.so (on Linux) or library.dll (on Windows) at address 0x00abcdef. At least that's how I think it works. So when you do linking and compiling in two different steps, you have to specify the libraries in the second step.
Logged

cesium133

  • Preventing third impact
  • *****
  • Offline Offline
  • Posts: 6,148
  • Has a fucked-up browser history
    • Cesium Comics
Re: A programming thread!
« Reply #87 on: 18 May 2014, 14:30 »

My problem was I was doing the compiling and linking as one command (rather than doing one command to build the object file then another to link it), and when doing it as one command gfortran expects the libraries to be after the source file that requires them.
Logged
The nerdy comic I update sometimes: Cesium Comics

Unofficial character tag thingy for QC

Schwungrad

  • Pneumatic ratchet pants
  • ***
  • Offline Offline
  • Posts: 345
Re: A programming thread!
« Reply #88 on: 18 May 2014, 15:18 »

That's odd. Judging from the manpage and from my own experience with gcc, putting the -llib options before the input file should also be the syntax for one-step compilation/linking. Not because the libraries are a prerequisite to linking, but because the invocation syntax demands all options being put before the input files.

Just out of curiosity, though it shouldn't make a difference: are you doing this on Windows?
Logged

cesium133

  • Preventing third impact
  • *****
  • Offline Offline
  • Posts: 6,148
  • Has a fucked-up browser history
    • Cesium Comics
Re: A programming thread!
« Reply #89 on: 18 May 2014, 15:21 »

Yes, and I'm using Cygwin.
Logged
The nerdy comic I update sometimes: Cesium Comics

Unofficial character tag thingy for QC

Schwungrad

  • Pneumatic ratchet pants
  • ***
  • Offline Offline
  • Posts: 345
Re: A programming thread!
« Reply #90 on: 18 May 2014, 16:24 »

OK, I just learnt that -l options are indeed supposed to go after the object files where they're referenced, though some linkers might not care (but cygwin-gfortran seems to care). Explained here: http://www.network-theory.co.uk/docs/gccintro/gccintro_18.html
Logged

cesium133

  • Preventing third impact
  • *****
  • Offline Offline
  • Posts: 6,148
  • Has a fucked-up browser history
    • Cesium Comics
Re: A programming thread!
« Reply #91 on: 23 May 2014, 13:20 »

Now I'm apparently running into a bug in gfortran that according to the bug tracker was fixed in 2006. Apparently the maximum record length is 10000, and I'm trying to output a record length of 25000. The bugfix is supposed to raise the limit to 1 GB, and there's no way I'm exceeding that...  :psyduck:

edit -- D'oh! The OPEN command was overriding the system default and setting the maximum record length to 10000...
« Last Edit: 23 May 2014, 13:39 by cesium133 »
Logged
The nerdy comic I update sometimes: Cesium Comics

Unofficial character tag thingy for QC

ankhtahr

  • GET ON THE NIGHT TRAIN
  • *****
  • Offline Offline
  • Posts: 2,700
  • A hacker spathe night owl
Re: A programming thread!
« Reply #92 on: 25 May 2014, 13:20 »

So I'm supposed to write a program for a theoretical architecture our lecturer has developed, called MiMa (minimal machine), which sorts an array of numbers. I feel tired, and don't feel like optimising anything here. The task didn't specify that it's supposed to be efficient, so I'm implementing bubblesort. And not even in a very effective way. But well, it should work. And this architecture is far from effective anyway. It's an extremely inefficient CISC architecture with less instructions than AVR. I want AVR back. That was great.

Here's a bit of documentation about the MiMa, written by a friend of mine (who has written the MiMa assembler mimasm and the MiMa simulator mimasim)
« Last Edit: 25 May 2014, 13:36 by ankhtahr »
Logged
Quote from: Terry Pratchett
He had the look of a lawn mower just after the grass had organised a workers' collective.

ankhtahr

  • GET ON THE NIGHT TRAIN
  • *****
  • Offline Offline
  • Posts: 2,700
  • A hacker spathe night owl
Re: A programming thread!
« Reply #93 on: 26 May 2014, 15:04 »

Have I ever told you how much I despise UML?  :-(

Once again it's midnight, and I didn't even get to the programming part of the exercise sheet yet.
Logged
Quote from: Terry Pratchett
He had the look of a lawn mower just after the grass had organised a workers' collective.

snalin

  • Vulcan 3-D Chess Master
  • *****
  • Offline Offline
  • Posts: 3,540
  • You may Baste me
Re: A programming thread!
« Reply #94 on: 26 May 2014, 16:25 »

Oh lawd, UML.

I TA'd a course that was... I'd say 50% UML, 50% actual software development. It was a real power trip to make the students sweat over bad diagrams, but I really, really see why they didn't want to put much effort into it. Knowing the basics of how to draw class diagrams on a whiteboard is a great way to communicate what you want, and I guess that if you start making complicated structures and can't really keep the overview, some informal UML can be great to express the important parts of the system. You need to be able to draw how classes interact in a pseudo-standard way, so I like that.

But the fucking standard is just such unhelpful pile of shit that I can't even begin to describe it. Software is an ever evolving beast, and diagrams just doesn't have the agility to keep up, and you keep getting stuck and having to go back to keep them in order. UML exists to help you with developing software, but that scenario is just not helpful at all.
Logged
I am a cowboy / on a steel horse I ride
I am wanted / Dead or alive

cesium133

  • Preventing third impact
  • *****
  • Offline Offline
  • Posts: 6,148
  • Has a fucked-up browser history
    • Cesium Comics
Re: A programming thread!
« Reply #95 on: 26 May 2014, 16:33 »

Oh God, UML.

I just had a bad flashback to college, before I dropped my CS major.
Logged
The nerdy comic I update sometimes: Cesium Comics

Unofficial character tag thingy for QC

celticgeek

  • GET ON THE NIGHT TRAIN
  • *****
  • Offline Offline
  • Posts: 2,697
  • Linux Geek
    • The Celtic Geek
Re: A programming thread!
« Reply #96 on: 26 May 2014, 16:37 »

But the fucking standard is just such unhelpful pile of shit

You are being FAR TOO KIND to UML.
Logged
a 'dèanamh nan saighdean airson cinneadh MacLeòid
We Wear Woad When We Write Code
Ní féidir liom labhairt na Gaeilge.
Seachd reultan, agus seachd clachan, agus aon chraobh geal.

ankhtahr

  • GET ON THE NIGHT TRAIN
  • *****
  • Offline Offline
  • Posts: 2,700
  • A hacker spathe night owl
Re: A programming thread!
« Reply #97 on: 26 May 2014, 17:31 »

Yeah, this exercise sheet has four tasks which are basically "Here you have a text, make a UML Diagram from it". The texts are extremely underspecified, and as I'm a perfectionist and want to do things the right way it just stops me from getting anywhere with these tasks. So far I finished one of these four. Sidenote: the four tasks give 5 points each, and the programming task, which will take me a few hours as well, gives 13 points…

And as I didn't get too many points in the last exercise sheet I need those points…

(the last exercise sheet had UML too, and my diagram had far too few classes for these object orientation extremists. Apparently they believe that the more classes you have, the better it is. I had about four. They wanted eleven.)
Logged
Quote from: Terry Pratchett
He had the look of a lawn mower just after the grass had organised a workers' collective.

Masterpiece

  • Older than Moses
  • *****
  • Offline Offline
  • Posts: 4,364
  • No time for Claireification
Re: A programming thread!
« Reply #98 on: 04 Jun 2014, 09:54 »

So.
I'm learing JavaFX.
I'm writing a UI with two labels whose CSS text-fill attribute I want to turn from grey to white when I hover over them with a mouse.
It's basically album title and song title for a music player I'm writing.
They are on a black background, and I want them to be grey when unneeded, so the album cover in the middle pops more.
And when I mouse over the info, I want them to turn white, so you can see them better.

At first, I tried using the :hover modifier class in my CSS, but quickly noticed that that would only work on one label, not both at the same time.
So I removed the :hover property, and added a style in runtime during every "mouseEntered" and "mouseExited" event.
Then I wanted transitions between the two states.
I wanted to use Keyframe animation, since I'm used to keyframing from AfterEffects.
I thought I could bind a SimpleStringProperty to the StyleProperties of the labels and wrote the style attributes into the SimpleStringProperty.
That wouldn't create transitions though, it would just blip from one state to another at the relevant keyframe.

I was getting annoyed.
SO. Instead, I didn't change the SimpleStringProperty value during the keyframe animation, but I changed a SimpleObjectProperty<Color> object.
I added a change listener to that Object, that fired a method that would change the SimpleStringProperty which contained my styling.
The styling was still bound to the StyleProperty of the labels.
EUREKA! Working transitions.

...that broke if I fired them again before the previous animation finished.
Since I had a "fadeIn" and a "fadeOut" animation, firing the fadeIn before the fadeOut ended broke fadeIn and vice versa.
So I wrote a while-loop that waited for the fadeIn animation to finish before fadeOut could play.
That created a while-loop that would never finish, freezing the application.

The Timeline-Animation API revealed that the animations had a "setDelay", a "getCycleDuration" and a "getCurrentTime" -method.
Through these, I could see how much time was remaining in one animation, and could set that as the animation delay on the other.
FadeOut would now wait until fadeIn was finished.
That made sense, since fadeOut would wait five seconds and then turn the labels gray again.
FadeIn should work instantly, so I told it to stop whatever fadeOut was doing and play fadeIn immediately.

...and now it works! Yay!

This is how I program, everybody!

Sorflakne

  • Duck attack survivor
  • *****
  • Offline Offline
  • Posts: 1,721
Re: A programming thread!
« Reply #99 on: 06 Jun 2014, 16:08 »

This post technically isn't programming-related, but it relates to program testing :P

So I set up a Filezilla server and client on my computer to test php with, but after loading up the necessary files, to include a html, php, css and jpg, to the client directory, I open the html file up, and it's just a raw webpage; the css rules aren't being applied and the image is a broken link.  When I enter some data and tell it to run (it's basically an email form), I get taken to a 'server not found' page.  Php is installed for the browser and all the files are in the same directory, but not sure what I'm missing when trying to open it from Filezilla.
Logged
If you want to see what God and Satan look like, look in the mirror.
Pages: 1 [2] 3 4   Go Up