The Java Journey Continues

If you ever want to feel exceptionally smart for a time, go back and ‘learn’ something you’ve already learnt in the past. I tell you, it’ll do wonders for your ego when you can ‘pick up’ the concepts required in no time at all. I mentioned in my first Java post just how strongly the memories were returning while going through the Udemy course. It has morphed since then into less fully-formed narrative style memories returning but doesn’t seem to have diminished much in the way of concept understanding returning relatively easily after covering it again here.

I’ve now completed Sections 6 and 7, which both focused on different elements of Object-Orientated Programming (OOP) concepts, starting with the basics of Class construction and wrapping up with Polymorphism.

The challenges set so far and the order of content to date has made a lot of sense, and I think stands to have a pretty good chance of bringing even those actually new to programming (and not just pretend-new) up to speed fairly quickly. I did find it perhaps a bit quirky not to cover Arrays alongside Loops but I shrugged and moved on at the time.

The decision not to cover Arrays then, or some other earlier point, really hurt when it came to the final OOP challenge task though. The concepts were still explained very well all the way through, but the challenges… Wowser. As an example, the last one tasked learners with helping ‘Bill’ craft a Burger Calculator of sorts, where Burgers could be constructed, including their addons, and an overall cost price arrived at. Then utilising inheritance, create two sub-types, DeluxeBurger and HealthyBurger.

There were some other requirements around return types and specific methods, but otherwise, it was fairly open to you. (At least in the video issued version of the challenge… I’ll come back to this later.)

Given this was the capstone project to the OOP sections, and we’d also recently covered composition, my freeform solution to this looked like this:

public class Hamburger {
    private String burgerName;
    private BreadRoll roll;
    private Meat meat;
    private Addition additionA, additionB, additionC, additionD;
    private double basePrice;
    public Hamburger (BreadRoll roll, Meat meat, double basePrice) {
        this.roll = roll;
        this.meat = meat;
        this.basePrice = basePrice;
    }
// SNIP -- this is class header and constructor only.

Where I created separate class definitions for each component of the hamburger, and — reluctantly — didn’t use an array (since we hadn’t covered it yet) to hold the additions.

As I started to write the method to addAddition() and getFinalPrice(), the level of code duplication was such that I was confident I was doing it wrong. There had to be some other, better, solution than what I was doing, even without arrays.

Here is what I did for adding extras onto the burger:

    public boolean addAddition(Addition addition) {
        boolean added = false;
        if (additionA == null) {
            additionA = addition;
            added = true;
        } else if (additionB == null) {
            additionB = addition;
            added = true;
        } else if (additionC == null) {
            additionC = addition;
            added = true;
        } else if (additionD == null) {
            additionD = addition;
            added = true;
        }
        return added;
    }

Beyond the duplication of code, I’m also not sure that I like using a return type to confirm whether or not the action requested actually worked. There has been talk of using return types to indicate error states, but I think more properly this would be handled by an exception. Although we haven’t covered try / catch yet (although this omission makes more sense to me, unlike the Arrays omission).

In any case, the sense of wrongness was enough that I ended up jumping into the formal version of the challenge that follows the video, where the requirements are even more explicitly defined. (They have to be, in order for the automated tester to determine whether the correct outputs have been arrived at or not).

The intended solution was even worse though. It didn’t use composition at all, so each addition name and price was stored directly as a String and double respectively. And the method for adding additions? Or methods, rather?

    public void addHamburgerAddition1(String name, double price) {
        addition1Name = name;
        addition1Price = price;
        System.out.println("Added " + name + " for an extra " + price);
    }
    public void addHamburgerAddition2(String name, double price) {
        addition2Name = name;
        addition2Price = price;
        System.out.println("Added " + name + " for an extra " + price);
    }
    public void addHamburgerAddition3(String name, double price) {
        addition3Name = name;
        addition3Price = price;
        System.out.println("Added " + name + " for an extra " + price);
    }
    public void addHamburgerAddition4(String name, double price) {
        addition4Name = name;
        addition4Price = price;
        System.out.println("Added " + name + " for an extra " + price);
    }

I swear to God, this is the intended solution for this piece of functionality. Anything that didn’t explicitly contain the 4 ‘addHamburgerAddition’ methods was flagged as incorrect for missing the methods.

For all my complaining on this particular challenge, I should reiterate that by and large I’ve been really happy with the course. AND, even this particular challenge did hold value in (re)cementing the concepts of method overriding and the utilisation of super both in that context and for child-class constructors.

Next up, Section 8 covers Arrays (lol), Java inbuilt Lists, Autoboxing and Unboxing.

The itch has started to start looking at other languages — JavaScript (then TypeScript) and Python being the two with the strongest pull at the moment — but I’m resisting for now as there is still heaps to do here. This course even ends up covering the basics of Database and Network programming (in sections 19 and 20 respectively).

JavaFX (section 13) is coming up too. I’m a little more apprehensive of this one, as I don’t have fantastic memories of working with this Graphics/GUI library in the past… Although admittedly, the last time I did was *cough*over ten years ago*cough* a long time ago, so I have no real idea what state it is in these days.

It will be nice escaping the limitations of the console again though! :)

Naithin

Gamer, reader, writer, husband and father of two boys. Former WoW and Gaming blogger, making a return to the fold to share my love of all things looty.

%d bloggers like this: