finishing grade 12 comp sci in a weekend

how i finished grade 12 comp sci in a weekend, with no prior java experience.

screenshot of an employee management system window with a table of employees with none selected. add, update, delete, and view employee buttons are on the right sidebar, with the last 3 grayed out.
screenshot of an employee management system window with a table of employees with none selected. add, update, delete, and view employee buttons are on the right sidebar, with the last 3 grayed out.

did i read that right??

yeah. pretty much.

how !???

last friday, i attended my first class for grade 12 compsci. fun, our teacher talked mostly about protocols for the new semester, as well as stuff like where to gather for fire alarms. pretty normal stuff.

meanwhile, i was sitting in the back of the class on my laptop, tryna SPEEDRUN THE HELL out of this course. this started with firstly trying to figure out the hell that is... java build systems. keep in mind i have zero (0) prior java experience, so i was wandering in the dark for most of the time at the start 😅. i'd heard some good things about maven, so i tried to use the java extension in vscode to try to create a maven project, andddd it failed. of course.

this led me to google "yeah how do i make maven project pls help", and that is when i realized how much java devs rely on their ides. before a SINGLE mvn command answer, i found a billion answers just pointing to a button in eclipse to do it ??? i finally found the solution:

mvn archetype:generate

i spent another 30 minutes trying to understand what the heck artifact and group ids were, and finally ended up with a working project. i opened up the assignment requirements my friend had graciously provided me from last semester, and got to work. they all relied around a central class, StudentInfo.

public class StudentInfo {

    public int studentNumber;
    public String firstName;
    public String lastName;
    public double height;
    public double weight;
    public StudentInfo next;

    // ...

}

the first assignment was a pretty simple one, a linked list. i'd done this a few times before, and implemented it in no time. the next two were heavily just duplicated from the linked list, being a stack and a queue. a bit of renaming, a bit of order reversal, a bit of not paying attention through the first class in calculus, and i ended up with a nice stack and queue. finally, the first real challenge came.

public class StudentInfo {

    public int studentNumber;
    public String firstName;
    public String lastName;
    public double height;
    public double weight;
    public StudentInfo left;
    public StudentInfo right;

    // ...
}

the motivation to eat during lunch instead of coding. you really thought a binary tree was hard? lol. i implemented the three ordering algorithms, preorder, inorder, and postorder. i also decided, for fun, to write real unit tests using junit. probably the most fun part of this process so far, as it was at least a little challenging. i had an annoying time with a bug which was literally a one line fix, and it took me nearly like an hour to fix 😭. this was an example of the offending code:

public StudentInfo[] inorderArray(StudentInfo targetNode) {
    StudentInfo[] inorder = new StudentInfo[numInTree];
    int index = 0;

    if (targetNode.left != null) {
        StudentInfo[] left = inorderArray(targetNode.left);
        for (int i = 0; i < left.length; i++) {
            inorder[index] = left[i]; // java.lang.ArrayIndexOutOfBoundsException: Index 10 out of bounds for length 10
            index++;
        }
    }

    // ...
}

see the bug? neither did i lol. it happens that i was making the order the same length each time, meaning it kept making 10-length arrays padded with nulls, and adding 10 elements to the top-level array for each node 🤦🏽‍♂️. i patched it, and this is what it ended up looking like:

public StudentInfo[] inorderArray(StudentInfo targetNode) {
    StudentInfo[] inorder = new StudentInfo[numInTree];
    int index = 0;

    if (targetNode.left != null) {
        StudentInfo[] left = inorderArray(targetNode.left);
        for (int i = 0; i < left.length; i++) {
            if (left[i] == null) { // break on null, as there are no more useful elements left.
                break;
            }

            inorder[index] = left[i];
            index++;
        }
    }

    // ...
}

this resulted in all 3 unit tests passing 🥳.

screenshot of 3 passed unit tests: testPreorder, testInorder, and testPostorder.
screenshot of 3 passed unit tests: testPreorder, testInorder, and testPostorder.

finally! i committed my changes and sent them up the message tube to github, where they now live (repo, of course licensed with the most based of licenses, agpl. repo will be private until the end of the course to prevent plagiarism. i worked hard. go away.)

you may be wondering of course, how exactly i did all of this without knowing java beforehand? the answer is that i have realized that after learning a few languages, newer ones come in easier and easier. it also helps that one of the three languages which was the inspiration for go (my absolute fav language) was java! (along with c++ and python). the syntax was very natural, and i have always loved object-oriented programming (yea yea i know it sucks i dont need to hear it for the hundredth time). i was also sorta excited to learn java because it is one of the most essential program languages which was a stepping stone to the amazing languages we have today.

the behemoth.

now that i had finished the assignments, it was friday night, and i headed over to a friends house to hang out for the rest of the evening. i woke up bright and early on saturday morning, to start the final project of the course a day after it had started, an employee management system. i looked over the requirements from the previous semester, and started on work. i started off with javafx, then realized that the teacher only allowed us to use swing, according to my friend. i scrapped the project and restarted with swing.

my first part was the backend code, being the core of the project. i made an Employee class, as well as an EmployeeSystem. I also had to extend the FTE and PTE classes from the Employee class, being "full-time employee" and "part-time employee" respectively. the backing of the EmployeeSystem is a hashtable. i simply made the constructor accept an array of Employees, and assign them based on their empNumber attribute. emp numbers are unique, meaning they had to be generated by the EmployeeSystem rather than the Employee itself. i made a method (later found to be flawed) to generate it, which just set the empNumber to be the size of the hashtable, meaning it would be larger than all emp numbers in it. i added all the normal getters and setters for getting all employees, employee by emp number, as well as updating/deleting an employee.

i then started on my gui. i create a new gui package, and got to work. for the main frame, i create an EMS class which extended from JFrame. the first panel i added was a simple list of every employee in the system. i started off with a JList, but found its flexibility to be limited, as it could mostly only display strings. i moved onto JTable, which was a lot better. i made a EmployeeTableModel, basically an interface for the JTable to get data for each cell.

i then moved onto the sidebar with 4 buttons: "add employee", "update employee", "delete employee", and "view employee". add employee was the most straightforward, as i just created a dialog with some text fields, which then added the employee to the system on submit. the update one was a little more complicated. i wanted to make it exactly like the add dialog, but with the fields filled in already, and the emp number pre-defined. i did it, and then created the very simple no-dialog delete button. finally, i made a view employee button, which showed more details about the employee in a dialog. i added a listener to the table, which only allowed the latter 3 buttons to be clickable when a row was selected in the table (as they all relied on a pre-existing employee being selected). i ended up with this for my gui:

screenshot of an employee management system window with a table of employees with none selected. add, update, delete, and view employee buttons are on the right sidebar, with the last 3 grayed out.
screenshot of an employee management system window with a table of employees with none selected. add, update, delete, and view employee buttons are on the right sidebar, with the last 3 grayed out.

previous screenshot, but now with an employee selected. the latter 3 buttons are now clickable.
previous screenshot, but now with an employee selected. the latter 3 buttons are now clickable.

here are a few images of my dialogs:

add employee dialog. the first name field contains the word "hello", and the last name field contains the word "readers", and the rest are blank.
add employee dialog. the first name field contains the word "hello", and the last name field contains the word "readers", and the rest are blank.

update employee dialog. all the fields are pre-filled in with their current values.
update employee dialog. all the fields are pre-filled in with their current values.

view employee dialog. all the fields are not selectable, and all fields are visible with their current values.
view employee dialog. all the fields are not selectable, and all fields are visible with their current values.

i also added a toggle for full-time/part-time employee, which switches the three-part wage calculations to a single salary field.

update employee dialog. the "full-time" checkbox is ticked, and a salary field replaces the 3 wage parts for a part-time employee.
update employee dialog. the "full-time" checkbox is ticked, and a salary field replaces the 3 wage parts for a part-time employee.

the final requirement was to actually have the data save to a persistent file which would be read from on launch. i used an extremely simple format, being attributeName:attributeValue, and the separators between employees just being a double newline rather than a single one used to separate attributes of the same employee.

empNumber:2
firstName:Vidhan
lastName:Bhatt
gender:Male
workLoc:127.0.0.1
deductRate:0.0
isFTE:false
hourlyWage:15.0
hoursPerWeek:20.0
weeksPerYear:30.0

i then made an equally simple parser to turn it into the internal hashtable, and i was done!

what next?

well, i still have a tests as well as probably other stuff to do during computer science. i mostly did this to lower the stress of assignments (i also have calc, chem and physics this semester. 😬). i also get bragging rights, as well as the ability to never pay attention in computer science lol.

goodbye... :(

this was my first real blog post, and i would like to thank you, the reader, so much for spending your time here. if you really loved this post, please do let me know at me@vidhan.io, or message me on discord. more blog posts will be on the way, so check back soon! 😄