Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java Question Fill in where there are comments and use the other two .java files

ID: 3561472 • Letter: J

Question

Java Question

Fill in where there are comments and use the other two .java files to test. Thanks

public class Counter
{
    // PUT PRIVATE VARIABLES HERE

    /**
     * The default constructor for objects of class Counter. Minimum is 0 and the maximum
     * is the largest possible integer.
     */
    public Counter()
    {
        // ADD CODE FOR THE CONSTRUCTOR
    }
  
  
    /**
     * The alternate constructor for objects of class Counter. The minimum and maximum values are given as parameters.
     */
    public Counter(int min, int max)
    {
        // ADD CODE FOR THE ALTERNATE CONSTRUCTOR
    }
  
    /**
     * Determine if two counters are in the same state
     *
     * @param otherObject   the object to test against for equality
     * @return     true if the objects are in the same state
     */
    public boolean equals(Object otherObject)
    {
        boolean result = true;
        if (otherObject instanceof Counter)
        {
           Counter otherCounter = (Counter)otherObject;
        }
        return result;
    }
  
  

    /**
     * Increases the counter by one. If after increase the Counter becomes bigger than the maximum value it should roll over to the min value.
     */
    public void increase()
    {
        // ADD CODE TO INCREASE THE VALUE OF THE COUNTER.
    }


     /**
     * Decreases the counter by one. If after decrease the Counter becomes less than the minimum value it should roll over to the max value.
     */
    public void decrease()
    {
        // ADD CODE TO DECREASE THE VALUE OF THE COUNTER. If the
    }
  
    /**
     * Get the value of the counter
     *
     * @return     the current value of the counter
     */
    public int value()
    {
        // CHANGE THE RETURN TO GIVE THE CURRENT VALUE OF THE COUNTER
        return -50;
      
    }
  
  
    /**
     * Accessor that allows the client to determine if the counter
     *             rolled over on the last count
     *
     * @return     true if the counter rolled over
     */
    public boolean rolledOver()
    {
        // CHANGE THE RETURN TO THE ROLLOVER STATUS OF THE COUNTER
        return true;
    }
  
    /**
     * Override the toString method to provide a more informative
     * description of the counter
     *
     * @return     a descriptive string about the object
     */
    public String toString()
    {
        // CHANGE THE RETURN TO A DESCRIPTION OF THE COUNTER
        return "";
      
    }
  
  


}

Use these to test

public class CounterInitializationException extends RuntimeException
{

   public CounterInitializationException(String reason)
   {
       super(reason);
   }
}

public class CounterTest
{
    public static void main (String args[])
    {
        testConstructor();
        testToString();
        testEquals();
        testIncrease();
        testDecrease();
        testCombined();
    }
  
  
    /**
     * testConstructor - test if the appropriate exception is thrown
     */

    public static void testConstructor()
    {
        System.out.println("TESTING the constructor");
        System.out.println("Trying min < max");
        try{
            Counter c1 = new Counter(10, 11);
            System.out.println("     Passes");
        }
        catch(CounterInitializationException e)
        {
            System.out.println("**** Fails - exception thrown");
        }


        System.out.println("Trying min = max");
        try{
            Counter c1 = new Counter(10, 10);
            System.out.println("**** Fails - no exception thrown");
        }
        catch(CounterInitializationException e)
        {
            System.out.println("     Passes");
        }


        System.out.println("Trying min > max");
        try{
            Counter c1 = new Counter(11, 10);
            System.out.println("**** Fails - no exception thrown");
        }
        catch(CounterInitializationException e)
        {
            System.out.println("     Passes");
        }
    }
  
    /**
     * testEquals - test the equals method
     */

    public static void testEquals()
    {
        Counter c1 = new Counter(10,20);
        Counter c2 = new Counter(10,20);      
        Counter c3 = new Counter(11,20);
        Counter c4 = new Counter(10,21);
        System.out.println();
        System.out.println();
        System.out.println("TESTING the equals method");
        System.out.println("trying two counters that should be in the same state");
        if(c1.equals(c2))
        {
            System.out.println("     passes");
        }
        else
        {
            System.out.println("**** fails");
        }

        System.out.println("trying two counters that should be in a different state");
        if(c1.equals(c3))
        {
            System.out.println("**** fails");
        }
        else
        {
            System.out.println("     passes");
        }

        System.out.println("trying two counters that should be in a different state");
        if(c1.equals(c4))
        {
            System.out.println("**** fails");
        }
        else
        {
            System.out.println("     passes");
        }
      
      
    }
  

    /**
     * testToString - test the toString method
     */
    public static void testToString()
    {
        System.out.println();
        System.out.println();
        System.out.println("TESTING the toString method");
        Counter c1 = new Counter(1, 9);
        System.out.println("Displaying the counter using toString:");
        System.out.println(c1);
        System.out.println("The counter should have the value 1");
        System.out.println("    the minimum should be 1, the maximum should be 9");
        System.out.println("    it should not have rolled over");
      
    }
  
    /**
     * testIncrease - verify that the increase method works
     */

    public static void testIncrease()
    {
        Counter c1 = new Counter(10,11);
        Counter c2 = new Counter(-1,1);
        Counter c3 = new Counter(-10,30);
        System.out.println();
        System.out.println();
        System.out.println("TESTING the increase method");
        System.out.println("Increasing counter 1 once");
        c1.increase();
        if(c1.value() != 11)
        {
            System.out.println("**** fails - bad value");
        }
        else
        {
            System.out.println("     passes");
        }
      
        if(c1.rolledOver())
        {
            System.out.println("**** fails - should not roll over");
        }
        else
        {
            System.out.println("     passes");
        }

        System.out.println("Increasing counter 1 again");
        c1.increase();
        if(c1.value() != 10)
        {
            System.out.println("**** fails - bad value");
        }
        else
        {
            System.out.println("     passes");
        }
      
        if(!c1.rolledOver())
        {
            System.out.println("**** fails - should roll over");
        }
        else
        {
            System.out.println("     passes");
        }

        System.out.println("Increasing counter 1 a third time");
        c1.increase();
        if(c1.value() != 11)
        {
            System.out.println("**** fails - bad value");
        }
        else
        {
            System.out.println("     passes");
        }
      
        if(c1.rolledOver())
        {
            System.out.println("**** fails - should not roll over");
        }
        else
        {
            System.out.println("     passes");
        }
      
        System.out.println("Increasing counter 2 until it rolls over");
        int count = 0;
        while(!c2.rolledOver())
        {
            c2.increase();
            count++;
        }
          
        if(c2.value() != -1)
        {
            System.out.println("**** fails - bad value");
        }
        else
        {
            System.out.println("     passes");
        }
      
        if(count != 3)
        {
            System.out.println("**** fails - wrong number of increases; count was " + count);
        }
        else
        {
            System.out.println("     passes");
        }

        System.out.println("Increasing counter 3 until it rolls over");
        count = 0;
        while(!c3.rolledOver())
        {
            c3.increase();
            count++;
        }
          
        if(c3.value() != -10)
        {
            System.out.println("**** fails - bad value");
        }
        else
        {
            System.out.println("     passes");
        }
      
        if(count != 41)
        {
            System.out.println("**** fails - wrong number of increases; count was " + count);
        }
        else
        {
            System.out.println("     passes");
        }

    }
  
    /**
     * testDecrease - verify that the decrease method works
     */

    public static void testDecrease()
    {
        Counter c1 = new Counter(10,11);
        Counter c2 = new Counter(-1,1);
        Counter c3 = new Counter(-10,30);
        System.out.println();
        System.out.println();
        System.out.println("TESTING the decrase method");
        System.out.println("Decreasing the counter once");
        c1.decrease();
        if(c1.value() != 11)
        {
            System.out.println("**** fails - bad value");
        }
        else
        {
            System.out.println("     passes");
        }
      
        if(!c1.rolledOver())
        {
            System.out.println("**** fails - should roll over");
        }
        else
        {
            System.out.println("     passes");
        }

        System.out.println("Decreasing the counter again");
        c1.decrease();
        if(c1.value() != 10)
        {
            System.out.println("**** fails - bad value");
        }
        else
        {
            System.out.println("     passes");
        }
      
        if(c1.rolledOver())
        {
            System.out.println("**** fails - should not roll over");
        }
        else
        {
            System.out.println("     passes");
        }

        System.out.println("Decreasing the counter a third time");
        c1.decrease();
        if(c1.value() != 11)
        {
            System.out.println("**** fails - bad value");
        }
        else
        {
            System.out.println("     passes");
        }
      
        if(!c1.rolledOver())
        {
            System.out.println("**** fails - should roll over");
        }
        else
        {
            System.out.println("     passes");
        }
      
      
        System.out.println("Decrease counter 2 twice, then decrease counter 2 until it rolls over again");
        int count = 0;
        c2.decrease();
        c2.decrease();
        while(!c2.rolledOver())
        {
            c2.decrease();
            count++;
        }
          
        if(c2.value() != 1)
        {
            System.out.println("**** fails - bad value");
        }
        else
        {
            System.out.println("     passes");
        }
      
        if(count != 2)
        {
            System.out.println("**** fails - wrong number of decreases; count was " + count);
        }
        else
        {
            System.out.println("     passes");
        }

        System.out.println("Decrease counter 3 twice, then decrease counter 3 until it rolls over again");
        count = 0;
        c3.decrease();
        c3.decrease();
        while(!c3.rolledOver())
        {
            c3.decrease();
            count++;
        }
          
        if(c3.value() != 30)
        {
            System.out.println("**** fails - bad value");
        }
        else
        {
            System.out.println("     passes");
        }
      
        if(count != 40)
        {
            System.out.println("**** fails - wrong number of decreases; count was " + count);
        }
        else
        {
            System.out.println("     passes");
        }

    }
  
  
    /**
     * testCombined - test combinations of the increase and decrease methods
     */

    public static void testCombined()
    {
        Counter c1 = new Counter(10,20);
        Counter c2 = new Counter(10,20);
        Counter c3 = new Counter(10,20);
        System.out.println();
        System.out.println();
        System.out.println("TESTING combinations of the increase and decrease methods");
        System.out.println("Increasing counter 2 once");
        c2.increase();
        if(c1.equals(c2))
        {
            System.out.println("**** fails - bad state");
        }
        else
        {
            System.out.println("     passes");
        }
      
        System.out.println("Decreasing counter 2 once");
        c2.decrease();
        if(!c1.equals(c2))
        {
            System.out.println("**** fails - should be back in the initial state");
        }
        else
        {
            System.out.println("     passes");
        }

        System.out.println("Decreasing then increasing counter 3");
        c3.decrease();
        c3.increase();
        if(c1.equals(c3))
        {
            System.out.println("**** fails - state should differ in rollover");
        }
        else
        {
            System.out.println("     passes");
        }

    }
}

Explanation / Answer

public class Counter
{
    // PUT PRIVATE VARIABLES HERE
    private int min;
    private int max;
    private int count;
    private boolean isRolledOver;

    /**
     * The default constructor for objects of class Counter. Minimum is 0 and the maximum
     * is the largest possible integer.
     */
    public Counter()
    {
        // ADD CODE FOR THE CONSTRUCTOR
        min = 0;
        max = 2147483647;
        count = min;
        isRolledOver = false;
    }


    /**
     * The alternate constructor for objects of class Counter. The minimum and maximum values are given as parameters.
     */
    public Counter(int min, int max)
    {
        // ADD CODE FOR THE ALTERNATE CONSTRUCTOR
        if(min==max)
        {
            throw new CounterInitializationException("min=max");
        }
        if(min>max)
        {
            throw new CounterInitializationException("min>max");
        }
        this.min = min;
        this.max = max;
        count = min;
        isRolledOver = false;
    }

    /**
     * Determine if two counters are in the same state
     *
     * @param otherObject   the object to test against for equality
     * @return     true if the objects are in the same state
     */
    public boolean equals(Object otherObject)
    {
        boolean result = false;
        if(otherObject instanceof Counter)
        {
            Counter otherCounter = (Counter)otherObject;
            if((this.min == otherCounter.min) && (this.max == otherCounter.max) && (this.count == otherCounter.count) && (this.isRolledOver == otherCounter.isRolledOver))
            {
                result = true;
            }
        }
        return result;
    }

    /**
     * Increases the counter by one. If after increase the Counter becomes bigger than the maximum value it should roll over to the min value.
     */
    public void increase()
    {
        // ADD CODE TO INCREASE THE VALUE OF THE COUNTER.
        if(count == max)
        {
            count = min;
            isRolledOver = true;
        }
        else
        {
            count++;
            isRolledOver = false;
        }
    }


    /**
     * Decreases the counter by one. If after decrease the Counter becomes less than the minimum value it should roll over to the max value.
     */
    public void decrease()
    {
        // ADD CODE TO DECREASE THE VALUE OF THE COUNTER. If the
        if(count == min)
        {
            count = max;
            isRolledOver = true;
        }
        else
        {
            count--;
            isRolledOver = false;
        }
    }

    /**
     * Get the value of the counter
     *
     * @return     the current value of the counter
     */
    public int value()
    {
        // CHANGE THE RETURN TO GIVE THE CURRENT VALUE OF THE COUNTER
        return count;

    }


    /**
     * Accessor that allows the client to determine if the counter
     *             rolled over on the last count
     *
     * @return     true if the counter rolled over
     */
    public boolean rolledOver()
    {
        // CHANGE THE RETURN TO THE ROLLOVER STATUS OF THE COUNTER
        return isRolledOver;
    }

    /**
     * Override the toString method to provide a more informative
     * description of the counter
     *
     * @return     a descriptive string about the object
     */
    public String toString()
    {
        // CHANGE THE RETURN TO A DESCRIPTION OF THE COUNTER
        return ("min = " + String.valueOf(min) + ", max = " + String.valueOf(max) + ", count = " + String.valueOf(count) + ", isRolledOver = " + String.valueOf(isRolledOver));

    }
}

-------------------------------------------------------------------------------------------------------------------------------------------------------------------

public class CounterInitializationException extends RuntimeException
{

    public CounterInitializationException(String reason)
    {
        super(reason);
    }
}


-------------------------------------------------------------------------------------------------------------------------------------------------------------------

public class CounterTest
{
    public static void main (String args[])
    {
        testConstructor();
        testToString();
        testEquals();
        testIncrease();
        testDecrease();
        testCombined();
    }


    /**
     * testConstructor - test if the appropriate exception is thrown
     */

    public static void testConstructor()
    {
        System.out.println("TESTING the constructor");
        System.out.println("Trying min < max");
        try{
            Counter c1 = new Counter(10, 11);
            System.out.println("     Passes");
        }
        catch(CounterInitializationException e)
        {
            System.out.println("**** Fails - exception thrown");
        }


        System.out.println("Trying min = max");
        try{
            Counter c1 = new Counter(10, 10);
            System.out.println("**** Fails - no exception thrown");
        }
        catch(CounterInitializationException e)
        {
            System.out.println("     Passes");
        }


        System.out.println("Trying min > max");
        try{
            Counter c1 = new Counter(11, 10);
            System.out.println("**** Fails - no exception thrown");
        }
        catch(CounterInitializationException e)
        {
            System.out.println("     Passes");
        }
    }

    /**
     * testEquals - test the equals method
     */

    public static void testEquals()
    {
        Counter c1 = new Counter(10,20);
        Counter c2 = new Counter(10,20);
        Counter c3 = new Counter(11,20);
        Counter c4 = new Counter(10,21);
        System.out.println();
        System.out.println();
        System.out.println("TESTING the equals method");
        System.out.println("trying two counters that should be in the same state");
        if(c1.equals(c2))
        {
            System.out.println("     passes");
        }
        else
        {
            System.out.println("**** fails");
        }

        System.out.println("trying two counters that should be in a different state");
        if(c1.equals(c3))
        {
            System.out.println("**** fails");
        }
        else
        {
            System.out.println("     passes");
        }

        System.out.println("trying two counters that should be in a different state");
        if(c1.equals(c4))
        {
            System.out.println("**** fails");
        }
        else
        {
            System.out.println("     passes");
        }


    }


    /**
     * testToString - test the toString method
     */
    public static void testToString()
    {
        System.out.println();
        System.out.println();
        System.out.println("TESTING the toString method");
        Counter c1 = new Counter(1, 9);
        System.out.println("Displaying the counter using toString:");
        System.out.println(c1);
        System.out.println("The counter should have the value 1");
        System.out.println("    the minimum should be 1, the maximum should be 9");
        System.out.println("    it should not have rolled over");

    }

    /**
     * testIncrease - verify that the increase method works
     */

    public static void testIncrease()
    {
        Counter c1 = new Counter(10,11);
        Counter c2 = new Counter(-1,1);
        Counter c3 = new Counter(-10,30);
        System.out.println();
        System.out.println();
        System.out.println("TESTING the increase method");
        System.out.println("Increasing counter 1 once");
        c1.increase();
        if(c1.value() != 11)
        {
            System.out.println("**** fails - bad value");
        }
        else
        {
            System.out.println("     passes");
        }

        if(c1.rolledOver())
        {
            System.out.println("**** fails - should not roll over");
        }
        else
        {
            System.out.println("     passes");
        }

        System.out.println("Increasing counter 1 again");
        c1.increase();
        if(c1.value() != 10)
        {
            System.out.println("**** fails - bad value");
        }
        else
        {
            System.out.println("     passes");
        }

        if(!c1.rolledOver())
        {
            System.out.println("**** fails - should roll over");
        }
        else
        {
            System.out.println("     passes");
        }

        System.out.println("Increasing counter 1 a third time");
        c1.increase();
        if(c1.value() != 11)
        {
            System.out.println("**** fails - bad value");
        }
        else
        {
            System.out.println("     passes");
        }

        if(c1.rolledOver())
        {
            System.out.println("**** fails - should not roll over");
        }
        else
        {
            System.out.println("     passes");
        }

        System.out.println("Increasing counter 2 until it rolls over");
        int count = 0;
        while(!c2.rolledOver())
        {
            c2.increase();
            count++;
        }

        if(c2.value() != -1)
        {
            System.out.println("**** fails - bad value");
        }
        else
        {
            System.out.println("     passes");
        }

        if(count != 3)
        {
            System.out.println("**** fails - wrong number of increases; count was " + count);
        }
        else
        {
            System.out.println("     passes");
        }

        System.out.println("Increasing counter 3 until it rolls over");
        count = 0;
        while(!c3.rolledOver())
        {
            c3.increase();
            count++;
        }

        if(c3.value() != -10)
        {
            System.out.println("**** fails - bad value");
        }
        else
        {
            System.out.println("     passes");
        }

        if(count != 41)
        {
            System.out.println("**** fails - wrong number of increases; count was " + count);
        }
        else
        {
            System.out.println("     passes");
        }

    }

    /**
     * testDecrease - verify that the decrease method works
     */

    public static void testDecrease()
    {
        Counter c1 = new Counter(10,11);
        Counter c2 = new Counter(-1,1);
        Counter c3 = new Counter(-10,30);
        System.out.println();
        System.out.println();
        System.out.println("TESTING the decrase method");
        System.out.println("Decreasing the counter once");
        c1.decrease();
        if(c1.value() != 11)
        {
            System.out.println("**** fails - bad value");
        }
        else
        {
            System.out.println("     passes");
        }

        if(!c1.rolledOver())
        {
            System.out.println("**** fails - should roll over");
        }
        else
        {
            System.out.println("     passes");
        }

        System.out.println("Decreasing the counter again");
        c1.decrease();
        if(c1.value() != 10)
        {
            System.out.println("**** fails - bad value");
        }
        else
        {
            System.out.println("     passes");
        }

        if(c1.rolledOver())
        {
            System.out.println("**** fails - should not roll over");
        }
        else
        {
            System.out.println("     passes");
        }

        System.out.println("Decreasing the counter a third time");
        c1.decrease();
        if(c1.value() != 11)
        {
            System.out.println("**** fails - bad value");
        }
        else
        {
            System.out.println("     passes");
        }

        if(!c1.rolledOver())
        {
            System.out.println("**** fails - should roll over");
        }
        else
        {
            System.out.println("     passes");
        }


        System.out.println("Decrease counter 2 twice, then decrease counter 2 until it rolls over again");
        int count = 0;
        c2.decrease();
        c2.decrease();
        while(!c2.rolledOver())
        {
            c2.decrease();
            count++;
        }

        if(c2.value() != 1)
        {
            System.out.println("**** fails - bad value");
        }
        else
        {
            System.out.println("     passes");
        }

        if(count != 2)
        {
            System.out.println("**** fails - wrong number of decreases; count was " + count);
        }
        else
        {
            System.out.println("     passes");
        }

        System.out.println("Decrease counter 3 twice, then decrease counter 3 until it rolls over again");
        count = 0;
        c3.decrease();
        c3.decrease();
        while(!c3.rolledOver())
        {
            c3.decrease();
            count++;
        }

        if(c3.value() != 30)
        {
            System.out.println("**** fails - bad value");
        }
        else
        {
            System.out.println("     passes");
        }

        if(count != 40)
        {
            System.out.println("**** fails - wrong number of decreases; count was " + count);
        }
        else
        {
            System.out.println("     passes");
        }

    }


    /**
     * testCombined - test combinations of the increase and decrease methods
     */

    public static void testCombined()
    {
        Counter c1 = new Counter(10,20);
        Counter c2 = new Counter(10,20);
        Counter c3 = new Counter(10,20);
        System.out.println();
        System.out.println();
        System.out.println("TESTING combinations of the increase and decrease methods");
        System.out.println("Increasing counter 2 once");
        c2.increase();
        if(c1.equals(c2))
        {
            System.out.println("**** fails - bad state");
        }
        else
        {
            System.out.println("     passes");
        }

        System.out.println("Decreasing counter 2 once");
        c2.decrease();
        if(!c1.equals(c2))
        {
            System.out.println("**** fails - should be back in the initial state");
        }
        else
        {
            System.out.println("     passes");
        }

        System.out.println("Decreasing then increasing counter 3");
        c3.decrease();
        c3.increase();
        if(c1.equals(c3))
        {
            System.out.println("**** fails - state should differ in rollover");
        }
        else
        {
            System.out.println("     passes");
        }

    }
}

-------------------------------------------------------------------------------------------------------------------------------------------------------------

TESTING the constructor
Trying min < max
     Passes
Trying min = max
     Passes
Trying min > max
     Passes


TESTING the toString method
Displaying the counter using toString:
min = 1, max = 9, count = 1, isRolledOver = false
The counter should have the value 1
    the minimum should be 1, the maximum should be 9
    it should not have rolled over


TESTING the equals method
trying two counters that should be in the same state
     passes
trying two counters that should be in a different state
     passes
trying two counters that should be in a different state
     passes


TESTING the increase method
Increasing counter 1 once
     passes
     passes
Increasing counter 1 again
     passes
     passes
Increasing counter 1 a third time
     passes
     passes
Increasing counter 2 until it rolls over
     passes
     passes
Increasing counter 3 until it rolls over
     passes
     passes


TESTING the decrase method
Decreasing the counter once
     passes
     passes
Decreasing the counter again
     passes
     passes
Decreasing the counter a third time
     passes
     passes
Decrease counter 2 twice, then decrease counter 2 until it rolls over again
     passes
     passes
Decrease counter 3 twice, then decrease counter 3 until it rolls over again
     passes
     passes


TESTING combinations of the increase and decrease methods
Increasing counter 2 once
     passes
Decreasing counter 2 once
     passes
Decreasing then increasing counter 3
     passes

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote