012-001-005

Static Variable: Counter

Medium

Problem Description

static variable: Counter

In this problem, you will create a program that defines a static variable count in a Counter class, automatically increments it in the constructor, reads an integer n from standard input, creates n objects in a loop, and displays the total count to standard output.

Learning Objective: Understand that static variables are shared across the class

Overview

Static variables are shared by all objects. Create a program that counts the number of objects created.

Specifications

  • Define static variable count in Counter class
  • Increment count in constructor
  • Read integer n from standard input, create n objects in a loop, then display count

Input Format

n

Integer n (number of objects to create, 0 or more)

Output Format

Total count: n

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