011-002-008

Constructor: Field Initialization

Easy

Problem Description

Constructor: Field Initialization

In this problem, you will create a program that reads a name and age from standard input, initializes name (String) and age (int) fields in a Person class using a constructor, and displays the object's information in "Name: [name], Age: [age]" format to standard output via a getInfo method.

Learning Objective: Understand how to use constructors to set the initial state of objects

Overview

Create a Person class and initialize name and age using a constructor. Implement a getInfo method that returns information as a string.

Input Format

name (String)
age (int)

Specifications

  • Person class fields: name (String type), age (int type)
  • Constructor: Accepts name and age as arguments and initializes fields
  • getInfo method: Returns string in format "Name: [name], Age: [age]"
  • In main method, use Scanner to read input, create a Person object, and output information

Input/Output Example

Input:
Alice
25

Output:
Name: Alice, Age: 25

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