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

  1. Counter class with an int count field (initial value 0)
  2. increment(): increases count by 1
  3. decrement(): decreases count by 1
  4. getValue(): returns the current count as an int
  5. countResult(int increments, int decrements): creates a Counter object, calls increment() increments times, calls decrement() decrements times, and returns the value from getValue()

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