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

Please answer this using Java! The objective of this assignment is to learn how

ID: 3689569 • Letter: P

Question

Please answer this using Java!

The objective of this assignment is to learn how to define a class with data and methods, along with multiple constructors. Assignment Task(s) Define a class called Counter. An object of this class is used to count things. The class can be used to either count nonnegative whole numbers or nonnegative floating point numbers. The class should include two instance variables: one long and one double. The class should include a three Constructors: a default Constructor w ith no parameters which initializes both variables to 0. and one Constructor each for initializing each instance variable. The class should include two overloaded methods: add (2) and subtract (2). Each overloaded method is responsible for the count of each variable. Be sure that no method allows the value of the counter to become negative. The class should include a to String() method, which can be used to display the two instance variables. The class should include a static method reset(). Which will set each of the two variables to 0. Write a program to test your class definition. Each method in the class should contain documentation as to its function. Assignment Evaluation Your assignment will be graded based upon all tasks being performed and handed in with all required documentation. two instance variables constructors [x3] add methods [x2] subtract methods [x2] to String method static reset method Proper compilation and source code documentation of all classes and methods

Explanation / Answer

public class Counter {
  
   /* two instance variables */
   private long countWholeNumber;
   private double countDouble;
  
   /* default constructor , set each instance variables to 0*/
   public Counter(){
       countDouble = 0;
       countWholeNumber = 0;
   }
  
   /* this constructor initialize long value with specified value and double value with zero*/
   public Counter(long num){
       countWholeNumber = num;
       countDouble = 0;
   }
   /* this constructor initializes double value with specified value and long value with zero*/
   public Counter(double num){
       countDouble = num;
   }
   /* add method for long value*/
   public void add(long num){
       countWholeNumber = countWholeNumber + num;
   }
   /* add method for double value*/
   public void add(double num){
       countDouble = countDouble + num;
   }
   /* subtract method for long value */
   public void subtract(long num){
       if((countWholeNumber - num) < 0)
           countWholeNumber = 0;
       else
           countWholeNumber = countWholeNumber - num;
   }
   /*subtract method for double value*/
   public void subtract(double num){
       if((countDouble - num) < 0)
           countDouble = 0;
       else
           countDouble = countDouble - num;
   }
   /* static method that return a object with count of each instance zero*/
   public static Counter reset(){
       return new Counter();
   }
   /* to string method */
   @Override
   public String toString() {
       return "Non-Negative Whole NUmber: "+countWholeNumber+", Non-Negative Double Number: "+countDouble;
   }
}

######### Test Class ############

class TestCounter{
  
   public static void main(String[] args) {
      
       Counter counter = new Counter();
      
       counter.add(12.21);
       counter.add(23.23);
      
       counter.add(12);
       counter.add(32);
      
       System.out.println(counter.toString());
      
       counter.subtract(21.21);
       counter.subtract(20);
      
       System.out.println(counter.toString());
      
       // reseting
       counter = Counter.reset();
       System.out.println(counter.toString());
      
       counter.add(32);
       counter.add(32.12);
      
       System.out.println(counter.toString());
   }
}

/*

sample run:

Non-Negative Whole NUmber: 44, Non-Negative Double Number: 35.44
Non-Negative Whole NUmber: 24, Non-Negative Double Number: 14.229999999999997
Non-Negative Whole NUmber: 0, Non-Negative Double Number: 0.0
Non-Negative Whole NUmber: 32, Non-Negative Double Number: 32.12

*/