basically i am working on a project for uni, which is to create a form of an abacus model.
- peg_array[] stores the number of counters/beads present in each of the pegs(lines) of the abacus.

i am trying to use 2 boolean methods:

"boolean removeCounter(int thisPeg)"
"boolean removeCounter(int thisPeg)"

thisPeg - An integer that refgers to any of the pegs in the abacus

Please can someone help me solve this? All i am trying to do is to recognise where possible errors may occur and prevent them from happening. E.G. if conditions are true then add one counter to the peg peg_array[thisPeg]++. If they are false then do nothing... Similarly the same thing but for removing counters peg_array[thisPeg]--. I am not too great with java and any help people can give me would be hugely appreciated!

MY CODE
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
import java.awt.event.*;

class AbacusModel extends JPanel
{
    int num_of_pegs;
    int max_num_counters;
    int peg_array[];


    public AbacusModel(int num_pegs, int num_counters)
    {
        max_num_counters = num_counters;
        num_of_pegs = num_pegs;
        peg_array = new int[num_of_pegs];
    }

    boolean addCounter(int thisPeg)
    {
        // add a counter to peg_array[thisPeg] if it is a valid index
        if (peg_array[thisPeg] >= 0 || peg_array[thisPeg] <= num_of_pegs)
        {
            peg_array[thisPeg]++;
        }
        // if index is not valid then do nothing...
        else
            return false;

    } // error here "error: missing return statement"

    boolean removeCounter (int thisPeg)
    {
        // remove a counter from peg_array[thisPeg] if it is a valid index
        if (thisPeg >= 0 || thisPeg <= num_of_pegs)
        {
            peg_array[thisPeg]--;
        }
        // if index is not valid then do nothing...
        else
            return false;


    } // error here "error: missing return statement"

    int getNumCounters(int thisPeg)
    {
        // empty temporarily, for use in subsequent tasks...

    } // error here "error: missing return statement"

public static void main (String[] args)
{
    AbacusModel myAbacus = new AbacusModel(7, 9);
}

Recommended Answers

All 6 Replies

If your method declares a return type (boolean, int etc) then every possible execution of the method MUST return a suitable value. You have if/else tests that, if they are true, do not return a value. You must add return statements so that every possible path through yopur methods returns a suitable value.

so you mean i can do something like this....

boolean removeCounter (int thisPeg)
    {
        // remove a counter from peg_array[thisPeg] if it is a valid index
        if (thisPeg >= 0 || thisPeg <= num_of_pegs)
        {
            return true;
            peg_array[thisPeg]--;
        }
        // if index is not valid then do nothing...
        else
            return false;

would this work? Or would i have to put the 'true' in with the condition?

  • boolean removeCounter (int thisPeg)
  • boolean removeCounter (int thisPeg)
  • int getNumCounters(int thisPeg)

    All the three methods must return boolean,boolean and int value respectively.But in first two cases,you are returning value only for else condition and not for if condition.Add return type for if condition.Something like this
    line 36-38

    if (thisPeg >= 0 || thisPeg <= num_of_pegs)
        {
            peg_array[thisPeg]--;
            return true;
        }
    

    For third one,
    you have not returning any integer value.
    Either change the return type from int to void or return some int value

@l.worboyz

 if (thisPeg >= 0 || thisPeg <= num_of_pegs)
         {
             return true;
             peg_array[thisPeg]--; //unreachable code as return statement above it.
        }

you are returning first and then decrementing peg_array[thisPeg].So that is unreachable code

i can't thank you guys enough, knew it was something simple. Was looking to hard for solution and was missing the basics. Thanks very much. Program now compiles :)

i'm sorry.. i try to find the sulotion of your problem.. but i can't find..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.