011-003-008

Call Constructor with this()

Medium

Problem Description

Call Constructor with this()

In this problem, you will create a program that defines a Person class with a full-parameter constructor Person(String name, int age) and a name-only constructor Person(String name), implements constructor chaining using this(), reads a name from standard input, and displays the result.

Learning Objective: Understand how to call another constructor in the same class using this()

Overview

Define name (String) and age (int) fields in the Person class. Create a full-parameter constructor and a name-only constructor (with default age of 0), and chain them using this().

Specifications

  • Create a Person class (Person.java)
  • Person(String name, int age): Initialize all fields
  • Person(String name): Call full-parameter constructor via this(name, 0)
  • display() method to show information
  • In Main class, read a name from standard input and create an instance using the name-only constructor

Input

name (String)

Output Format

Name: <name>, Age: 0

Example

Input:

Taro

Output:

Name: Taro, Age: 0

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