简体   繁体   中英

Why is this getter functions only returning the initial value

I'm learning object-oriented programming and started learning about inheritance. The assignment my teacher gave me was to make a counter object with 6 "buttons": Increment, Decrement, Reset, AddMemory, ResetMemory, and Quit. It is fairly straight-forward what each button does.

The requirements are that I have to use the JOptionPane command, I have to make a Counter class with a counter attribute, increment, decrement, reset, and quit methods, I have to make a MemoryCounter class with a memory attribute, restMemory, and addMemory method. I also have to make a MemoryCounterConsoleMenu class which makes the input box from the JOptionPane command and executes the appropriate method. The final thing I have to do is make a MemoryCounterTest class that brings the MemoryCounterConsoleMenu and MemoryCounter classes together

So I did all that and here it is:

The first one is the Counter class

public class Counter 
{
    private int counter = 0;

    public void increment()
    {
        setCounter(getCounter() + 1);
    }

    public void decrement()
    {
        setCounter(getCounter() - 1);
    }

    public void reset()
    {
        setCounter(0);
    }

    public void setCounter(int counter) {
        this.counter = counter;
    }

    public int getCounter() {
        return counter;
    }
}

This is the MemoryCounter class

public class MemoryCounter extends Counter
{
    private int memory = 0;

    public void resetMem()
    {
        setMemory(0);
    }

    public void addMem()
    {
        setMemory(getCounter());
    }

    public void setMemory(int memory)
    {
        this.memory = memory;
    }

    public int getMemory()
    {
        return memory;
    }
}

Next is the MemoryConsoleMenu

public class MemoryCounterConsoleMenu
{
    static MemoryCounter memCounter = new MemoryCounter();

    static Counter counter = new Counter();

    public static int console()
    {
        System.out.println(memCounter.getMemory());
        Object[] options = {"Reset Mem", "Add Mem", "Increment", "Decrement", "Reset", "Quit" };
        int objectIndex = JOptionPane.showOptionDialog(null, "Counter = " + counter.getCounter() +  "Memory = " 
        + memCounter.getMemory(), "MemoryCounter",JOptionPane.PLAIN_MESSAGE, 
        JOptionPane.PLAIN_MESSAGE, null,    options, options[5]);
    
        return objectIndex;
    }   

public static int change(int objectIndex)
{
        if(objectIndex == 0)
        {
            memCounter.resetMem();
            return 1;
        }
    
        else if(objectIndex == 1)
        {
            memCounter.addMem();
            return 2;
        }
    
        else if(objectIndex == 2)
        {
            counter.increment();
            return 3;
        }
        else if(objectIndex == 3)
        {
            counter.decrement();
            return 4;
        }
    
        else if(objectIndex == 4)
        {
            counter.reset();
            return 5;
        }
    
        else
        {
            return 6;
        }
    }
}

Finally, there is the MemoryCounterTest

public class MemoryCounterTest 
{
    public static void main(String[] args)
    {
        MemoryCounterConsoleMenu memoryConsole = new MemoryCounterConsoleMenu();
    
        for(int i = 0; i != 6;)
        {
            i = memoryConsole.change(memoryConsole.console());
        }
    }
}

Everything works properly except for the memory value. It stays at a constant zero. I've done some troubleshooting myself and found that the only problem in the code is in the "addMem()" method is the MemoryCounter class particularly the implementation of the "getCounter()" method. It will only return 0 for some reason.

After figuring this out I have made no ground on why the problem is occuring or how to fix it

It stays at 0 because they are two separate counters.

MemoryCounter class extends the Counter class, so you don't need a separate

static Counter counter = new Counter();

Just do everything via memCounter .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM