014-005-007
Override toString Method
Easy
Problem Description
override toString method
In this problem, you will create a program that overrides the toString() method in a Person class to return a custom string representation in "Person[name=name, age=age]" format and displays the result to standard output.
Learning Objective: override Object class toString() method to implement custom string representation
Overview
Create a program that overrides toString() in Person class to return object information as string.
Specifications
- Define String name and int age fields in Person class
- override toString() to return "Person[name=name, age=age]" format
- Read name (String) and age (int) via Scanner, create Person, and output with System.out.println()
Input Format
name
age
Output Format
Person[name=name, age=age]
Example
Input:
Hanako
25
Output:
Person[name=Hanako, 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