011-002-008
Constructor: Field Initialization
Easy
Problem Description
constructor: Field Initialization
In this problem, you will create a program that 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.
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, create Person object and output information
Output Format
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