008-002-014

Object Method Invocation: Counter

Easy

Problem Description

Object Method Invocation: Counter

In this problem: You will define a Counter class with a count field, increment(), decrement(), and getCount() methods, then create an instance in Main and call methods to display state changes.

Learning Objective: Understand object method invocation and instance state changes

Overview

Define the Counter class with:

  • count field (initial value 0)
  • increment(): increases count by 1
  • decrement(): decreases count by 1
  • getCount(): returns current count

Specifications

  • Create a Counter instance and display initial value
  • Call increment() 3 times and display value
  • Call decrement() once and display value

Output Format

Initial: 0
After 3 increments: 3
After 1 decrement: 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