4
\$\begingroup\$

I've been trying to learn java recently and I finally finish my first code. Any suggestions on where I can improve? Also, is it possible to send an argument to a class using something like an init function in python and are there any good and bad habits that I should keep in mind?

class LIFO {
    
    //Create an empty list with the lenght of the given size
    //Initialize our top value as 0
    int my_list[] = new int[5];
    int top = 0;
    
    //Adds a value to the list
    //Add one to our top
    public void push(int data)
    {
        my_list[top] = data;
        top++;
    }
    
    //Removes the top value from the list
    //Substract one from our top
    public int pop()
    {
        int data;
        top--;
        data = my_list[top];
        my_list[top] = 0;
        return data;

    }

    //Prints our list
    public void show()
    {
        for(int n : my_list)
        {
            System.out.print(n + " ");
        }
    }
}

//Driver Class
class Main {
    public static void main(String args[])
    {
        LIFO s = new LIFO();
        s.push(1);
        s.push(2);
        s.push(3);
        s.show();
        System.out.println("\n" + s.pop() + " Popped from stack");
        s.show();
    

    }
}
\$\endgroup\$
  • 2
    \$\begingroup\$ Title says LILO, code says LIFO, which one is it? \$\endgroup\$ – TorbenPutkonen Aug 10 at 5:35
  • \$\begingroup\$ Oh my mistake, it's suppose to be Last In First Out \$\endgroup\$ – john1999 Aug 11 at 18:07
3
\$\begingroup\$

I believe what you're looking for is a constructor.

class LIFO {
    
    //Create an empty list with the lenght of the given size
    //Initialize our top value as 0
    int my_list[];
    int top = 0;

    public LIFO(int size) {
        my_list = new int[size];
    }
...

Call like so.

public static void main(String[] args) {
    LIFO lifo = new LIFO(10);
    lifo.push(10);
    lifo.push(20);
    System.out.println(lifo.pop());
    System.out.println(lifo.pop());
}
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Thank you so much! I don't know why I though it would be super different from python lol. \$\endgroup\$ – john1999 Aug 10 at 4:00
4
\$\begingroup\$

@Ted Brownlow has answered your constructor question, but here's a few other points to consider going forward...

Consider your class interface

Your class needs to present an interface that is functional,usable and focused. At the moment, your class is responsible for both implementing a LIFO and outputting them to the console. These are really different concerns. Consider if instead of writing to the console you wanted to write to a web page / different window. Building the display logic straight into the class makes it less flexible. A good exercise might be to remove the show method and decide what method(s) you would need to add to your class in order to allow it to be implemented from outside the class.

Bounds checking

At the moment your class has a fixed maximum size of 5. However, other than by reading the class there's no way for the client to know (possibly missing a maxSize method or similar). There's also no way for the client to know how many items are in the list, unless they keep track of it themselves (possibly missing a currentSize method or similar).

Without this size information, there's no way for the client to know if it's safe to call push or pop. Consider what would happen if you already had 5 items when push was called. What would happen if 0 items were present and pop was called? At the moment, you're relying on the framework to handle these scenarios. This can be ok, if it's a conscious decision, but you probably want to consider adding some bounds checks and throwing exceptions that more explicitly help the user of your class know what they did wrong.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ While I absolutely support thinking about edge cases, I don't support the "exceptions that more explicitly help the user" approach. Exceptions are meant for situations where a method can't fulfill its contract, and specifying exception types and situations makes exceptions part of the contract. \$\endgroup\$ – Ralf Kleberhoff Aug 13 at 11:50
  • \$\begingroup\$ Thank you, those are really good points. I'll keep them in mind when writing code for now on. \$\endgroup\$ – john1999 Aug 18 at 6:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.