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

Please help me with this Java program... I don\'t even know where to begin and I

ID: 643670 • Letter: P

Question

Please help me with this Java program... I don't even know where to begin and I don't understand, everything I have tried gives an error. I have to create the class in the first image to make the second image execute correctly.

The Java program to execute:

Please help me with this Java program... I don't even know where to begin and I don't understand, everything I have tried gives an error. I have to create the class in the first image to make the second image execute correctly. The class code to type: The Java program to execute: 1) A private variable to store the current count. 2) Another private variable to store the maximum value this counter can count up to. 3) A constructor that takes a single integer as an argument and uses it to set the maximum counter value. The count should be set to 0. 4) An increment () method that increases the count value by 1, but sets it back to 0 if the count goes above the maximum. No arguments, returns nothing 5) A decrement () method that decreases the count value by 1, but sets it to the maximum value if it goes below 0. no arguments, returns nothing 6) A toString() method that returns a String representing the current count value, no arguments. 7) A reset() method that sets the count value back to O. no arguments, returns nothing. 8) The constructor should throw an exception if the argument for the maximum value is not a positive number (0 or less)

Explanation / Answer

/**
* The java class constructor that sets the maximum counter
* value of the RolloverCounter class object.
* The methods increment and decrement increments the
* counter value increases by one and decreases by one.
* The method reset reset the counter to zero.
* The contructor throws the Exception of the MAXIMUM_COUNT is less than
* or equal to zero.
* */
//RolloverCounter.java
public class RolloverCounter
{
   //instance variable to count the val
   private int currentCount;
   //set maximum count
   private int MAXIMUM_COUNT;
  
   //constructor to set the maximum count
   //and set current count to zero.
   //Throws an exception if the MAXIMUM_COUNT is less than zero.
   public RolloverCounter(int MAXIMUM_COUNT) throws Exception
   {
       if(MAXIMUM_COUNT<=0)
           throw new Exception();
          
      
       this.MAXIMUM_COUNT=MAXIMUM_COUNT;
       this.currentCount=0;
   }
   //The method increment that increments the currentCount by
   //one and set maximum count to zero if the current is greater
   //than maximum count
   public void increment()
   {
       //increment the count
       currentCount=currentCount+1;
       //check if the currentCount is greater than MAXIMUM_COUNT
       //set currentCount to zero.
       if(currentCount>MAXIMUM_COUNT)
           currentCount=0;
      
   }
   //The method decrement that decrements the currentCount by
       //one and set maximum count to zero if the current is greater
       //than maximum count
       public void decrement()
       {
           //decrement the count
           currentCount=currentCount-1;
           //check if the currentCount is greater than MAXIMUM_COUNT
           //set currentCount to zero.
           if(currentCount<0)
               currentCount=MAXIMUM_COUNT;
          
       }
      
      
       //Returns the string representaion of current count value
       @Override
       public String toString()
       {
           //returns the string representation of currentCount value
           return "Current Count : "+currentCount;
       }
      
       //reset current count to zero
       public void reset()
       {
           //reset current count to zero
           currentCount=0;
       }
}

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

//The class test the RolloverCounter class
//CounterTest.java
public class CounterTest
{
   public static void main(String[] args) throws Exception
   {
       System.out.println("creating new counters...");
       System.out.println("creating counter c1 with max value = 5...");
       //create an intsance of class RolloverCounter with maximum count to 5
       RolloverCounter c1=new RolloverCounter(5);
       System.out.println("creating counter c1 with max value = 3...");
       //create an intsance of class RolloverCounter with maximum count to 3
       RolloverCounter c2=new RolloverCounter(3);
      
       //Throw an exception of negative maximum count
       try {
           System.out.println("creating counter c3 with max value = -2( this should not work)...");
           RolloverCounter c3=new RolloverCounter(-2);
       } catch (Exception e) {
           System.out.println("Erro crating counter: "+e);
       }
      
       System.out.println(" increment the counts 10 times and printing countes");      
       for(int i=1;i<=10;i++){
           //calling increment method on c1 and c2
           c1.increment();
           c2.increment();
           System.out.println("c1: "+c1+" c2:"+c2);          
       }
      
      
       System.out.println(" decrement the counts 7 times and printing countes");      
       for(int i=1;i<=7;i++){
           //calling decrement method on c1 and c2
           c1.decrement();
           c2.decrement();
           System.out.println("c1: "+c1+" c2:"+c2);          
       }
      
       System.out.println(" resetting counters...");
       //reset counters of c1 and c2 objects
       c1.reset();
       c2.reset();
       System.out.println("c1: "+c1+" c2:"+c2);
      
      
   }
}


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

Sample output:

creating new counters...
creating counter c1 with max value = 5...
creating counter c1 with max value = 3...
creating counter c3 with max value = -2( this should not work)...
Erro crating counter: java.lang.Exception

increment the counts 10 times and printing countes
c1: Current Count : 1   c2:Current Count : 1
c1: Current Count : 2   c2:Current Count : 2
c1: Current Count : 3   c2:Current Count : 3
c1: Current Count : 4   c2:Current Count : 0
c1: Current Count : 5   c2:Current Count : 1
c1: Current Count : 0   c2:Current Count : 2
c1: Current Count : 1   c2:Current Count : 3
c1: Current Count : 2   c2:Current Count : 0
c1: Current Count : 3   c2:Current Count : 1
c1: Current Count : 4   c2:Current Count : 2

decrement the counts 7 times and printing countes
c1: Current Count : 3   c2:Current Count : 1
c1: Current Count : 2   c2:Current Count : 0
c1: Current Count : 1   c2:Current Count : 3
c1: Current Count : 0   c2:Current Count : 2
c1: Current Count : 5   c2:Current Count : 1
c1: Current Count : 4   c2:Current Count : 0
c1: Current Count : 3   c2:Current Count : 3

resetting counters...
c1: Current Count : 0   c2:Current Count : 0

Hope this helps you

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