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

Need help with java. Need help with code in area where there are comments. publi

ID: 3561377 • Letter: N

Question

Need help with java. Need help with code in area where there are comments.

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 "";
      
    }
  
  


}

Explanation / Answer

public class Counter
{
    // PUT PRIVATE VARIABLES HERE
    private int value;
   private int min,max;
    /**
     * 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 = Integer.MAX_VALUE;
       value = 0;
    }
    /**
     * 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
       this.min = min;
       this.max = max;
       value = min;
    }
    /**
     * 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;
           result = (this.value()==otherCounter.value());
        }
        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.
       value++;
       if(value > max)
       value = min;
    }
     /**
     * 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
       value--;
       if(value>min)
       value = max;
    }
    /**
     * 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 value;
    }
    /**
     * 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 (value==min || value == max);
    }
    /**
     * 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 "";
    }
}

public class TestCounter
{
   public static void main(String[] args)
   {
       Counter C1 = new Counter();
       C1.increase();
       System.out.println("Counter1 value is "+C1.value());
       Counter C2 = new Counter(3,7);
       System.out.println("Counter2 value is "+C2.value());
       C2.increase();
       C2.increase();
       C2.increase();
       System.out.println("Counter2 value is "+C2.value());
       System.out.println("is Counter2 rolled over ? "+ (C2.rolledOver()?"Yes":"No"));
       C2.increase();
       System.out.println("Counter2 value is "+C2.value());
       System.out.println("is Counter2 rolled over ? "+ (C2.rolledOver()?"Yes":"No"));
       C2.increase();
       System.out.println("Counter2 value is "+C2.value());
   }
}