008-002-013
Method Chaining with Object Methods
Medium
Problem Description
Calling Object Methods
In this problem, you will implement increment(), decrement(), and getValue() methods in a Counter class, and implement a static countResult(int increments, int decrements) method that creates a Counter object, calls the methods, and returns the final value.
Learning Objective: Understand how to create object instances, call their methods, and use the return value
Overview
Implement the Counter class and the countResult static method.
Specifications
Counterclass with anint countfield (initial value 0)increment(): increases count by 1decrement(): decreases count by 1getValue(): returns the current count as anintcountResult(int increments, int decrements): creates aCounterobject, callsincrement()incrementstimes, callsdecrement()decrementstimes, and returns the value fromgetValue()
Input Format
<increments>
<decrements>
Output Format
<return value of countResult>
Example
Input:
3
1
Output:
2
Ready to Try Running Code?
Log in to access the code editor and execute your solutions for this problem.
Don't have an account?
Sign Up