0

I am making an ABACUS MODEL program which adds counters to pegs. The program compiles fine but when i use the test that was issued to me (as pictured) it appears as though my program does not work as intended. I don't know if anyone will be able to help me solve this but any attempts or suggestions will be very helpful. Not particularly talented with Java.

Variables:

thisPeg - an integer that refers to any of the pegs in the abacus
peg_array[] - stores the number of counters present in each of the pegs in the abacus
max_num_counters - controls the maximum number of counters stored in each element (9 counters)
num_of_pegs - stores the number of pegs the abacus has (7 pegs)

MY CODE AbacusModel

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)
    {
        // Trying to add a counter when that column of the abacus is full
        // add a counter to peg_array[thisPeg] if it is a valid index

        // its these conditions below that are probably incorrect as returning undesired results????
        if (peg_array[thisPeg] >= 0 || peg_array[thisPeg] < num_of_pegs
            || max_num_counters < 8)
        {
            peg_array[thisPeg]++;
            return true;
        }
        // if not then do nothing...
            else
                return false;

    }

    boolean removeCounter (int thisPeg)
    {
        // its these conditions below that are probably incorrect as returning undesired results????

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


    }
    int getNumCounters(int thisPeg)
    {
        // possibly these conditions too??
        if (peg_array[thisPeg] >= 0 && peg_array[thisPeg] <= num_of_pegs)
        {
            return peg_array[thisPeg];
        }
        else
            return 0;
    }

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

Anything somebody may be able to provide help with will help me greatly. Thanks

THE CMD TEST showing one possible example result of what will happen

6515905bb5084796a8f5455c5131b235

2
Contributors
1
Reply
17
Views
4 Years
Discussion Span
Last Post by bguild
0

You should not be using possibly incorrect conditions. When you check for preconditions on your methods you are doing the only thing that can keep your methods safe from unruly clients; it is your only line of defense. Test for exactly the conditions that you want.

I would say that your conditions in all three methods are wrong, and not in a subtle way. Perhaps it will help if you read the conditions out loud to yourself. Think about what you want each condition to actually say; when can you add a counter to a peg? There are just three conditions that need to be satisfied, and they have nothing to do with comparing the number of counters to the number of pegs.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.