Friday, 23 November 2012

From JAVA to C: Spot the Difference.

10 weeks ago we were tasked with creating a program in the JAVA programming language, primarily to get ourselves refreshed with some coding after a lazy summer, but also to use as a comparison piece for our final project in this AC21008 programming module. We had an idea that we may have to return to this JAVA program, we were correct, after learning C++ and C we were asked to go back to our week 1 program, and re-write it in the C programming language. Easy, you would think, however C is a very different programming language from JAVA and a lot of our JAVA code had to be either scrapped, or totally re-written to give the same functionality.


Firstly, let’s talk about the main program structure. Our JAVA program had to adhere to the Object Orientated Programming model; Encapsulation, Inheritance and Polymorphism. In JAVA you cannot code in any other way, you MUST use classes otherwise your program is not going to work. Even for the simple “Hello World” program you have to create a class, then run a method contained in the class that prints “hello world” to the screen.
Our JAVA implementation had 4 classes in the final version; Clock, AdminGUI, Controller and Employee. Both Clock and AdminGUI are classes that handle our GUI interface, which could have arguably been merged into one GUI class, however, it’s the Clock and Employee class that contain the main program functionality. The Employee class is used to store data for each employee our program is working with; it contains the fields for the employee’s data, and the setter and getter methods for these fields. Our control class creates objects of type employee, using the accessible methods to set the details of each new employee created, and stores these employees in an array, ready for clocking them in and out, handling their files and working with the time library. The 2 other classes handle the GUI, one makes the basic menu that’s shown to the employees, the main interface of the program and the other creates the admin panel which is used by the manager to review clocking times of the employees. Standard Object Oriented Programming.

Moving to C:

I will always remember the first lecture we received from Karen about the C programming language, “As programmers you will feel like your arms have been chopped off”. She wasn’t joking! The first thing on the list of removed functionality in C was Classes. Wait, what? No Classes? At all! After relying so heavily on them in JAVA and slightly in C++ we had to change our approach to our C program. Enter, the Struct. A Struct is used in C for grouping together variables of the same or different types, which can then all be referenced using the name of the Struct used,  perfect for creating a Struct for our Employee’s. It should be noted that we could have exclusively used files to store all the data we needed, with each file containing an employee, their details and the code would just modify the files based on input, but this would have been messy and the Struct contains everything we need. Our JAVA program used an array to store employee objects; in the end our C program did the same, an array holding Structs. We did initially start with a linked list of Structs, but we had to scrap that plan, it ended up being far too complicated and we realised that we don’t actually have to add new employees when the program is running.  The arrays are defined in the code so it is of a fixed size, however, if more employees are required, the array size can be changed without issue and the new employees loaded from file as per normal.

This takes us nicely onto the next concept we have to deal with, memory use. In JAVA the language itself manages memory use for us, if we create an object, it knows how much memory space is needed and then discards it when the object drops out of scope. C does this to a certain extent, variables declared at global scope and static variables defined in functions all get memory allocated at program execution and have their memory freed once the program is complete, the program knows how much space these will take up as they are fixed. All well and good, but what if we want to return an array from a function? For example, in our C code we have a method that counts how many user files are contained in the program folder and uses an array of pointers to store the file names for use in other functions. This method returns an array of pointers that can change in size, depending on how many files are in the folder. In JAVA we could initialise the array in the method, fill it up then return it no problem at all. In C, it’s a different story. This array is not of a static type and can change in size when the program is executed. The program has no idea how much memory space it will need when executed so we have to do it ourselves in the code. This is where we use the malloc command in C to assign enough memory for our array. The size required is counted first, then we malloc the required space, fill the array up then return it. Conversely, we have to take note of how much memory our method is creating so we can then free it once we no longer need it. In our code we only every use one non static array, and the space for it is only created once the program is executed, then this memory is given back to the OS once our program stops. However, if we were using a large data base program, with multiple malloc calls we would have to free up any memory that is no longer required using the free command. If you don’t do this, you will end up with a memory leak; we have assigned space but are no longer using it, causing the program to slowly fill up your computer’s memory until it either gets so slow or the computer crashes. If you malloc it, you free it, end of story.

The final major difference between our JAVA and C implementation is the fact that our C program has no GUI. In JAVA we used the Swing library which was easy enough to work out how to use ourselves. Almost like CSS in HTML coding, our basic terminal program was the HTML and we plugged in the Swing GUI on top, similar to how a style sheet works. However to implement our program in C we would have had to use the GTK+ library, just looking at the code for it was enough to give us nightmares, it is pretty complex, for example the GNOME desktop environment for Linux is written using GTK. This wasn’t too much of an issue though, the C programming language is not designed for creating fancy front end programs, more for creating backend functionality, such as OS Kernels, device drivers and embedded systems for example. The terminal implementation we generated in C feels nice and clean cut though, it tells what you need to know, and most of us have been using Linux, which is very much a Command Line Interface OS, rather than the GUI driven Microsoft Windows.

All in all we have managed to create the same functionality, in C as we did in JAVA. We feel that our C version is far more efficient than our JAVA implementation. It does the same job with less space and less lines of code. Even after presenting we have noted areas that could be improved, for example implementing functionality to add and delete employees using the program, rather than editing files.

1 comment:

  1. Above is the Blog post for part B of the report, detailing the differences between our JAVA and C implementations.
    Author: Duncan Hamilton

    ReplyDelete