008-002-010
Object Method Call: Counter
Hard
Problem Description
Object method Call: Counter
In this problem, you will create a program that instantiates a Counter class, calls the increment() method a specified number of times to update the count, and displays the initial and final values to standard output.
Learning Objective: Call object methods to manipulate state
Create a counter class and manipulate values with methods.
Input
Line 1: Number of increments (integer)
Output
Initial: 0
After [count] increments: [final value]
Specifications
- Create Counter class
- Field: int count (initial value 0)
- Methods: increment() (increases count by 1), getCount() (returns count value)
Examples
Example 1
Input:
3
Output:
Initial: 0
After 3 increments: 3
Example 2
Input:
5
Output:
Initial: 0
After 5 increments: 5
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