014-008-007

Override equals Method

Medium

Problem Description

override equals method

In this problem, you will create a program that overrides the equals(Object obj) method in the Person class to determine equality based on matching name and age fields. Read three persons' data from standard input and display the comparison results.

Learning Objective: Override Object class equals() method to define equality

Overview

Create a program that overrides equals() in Person class to consider persons equal when name and age match.

Input Format

name1 age1
name2 age2
name3 age3

Each line contains a name (string) and age (integer) separated by a space.

Specifications

  • Define String name and int age fields in Person class
  • Override equals(Object obj) to determine equality
  • Create three Person instances from input and compare them

Output Format

p1.equals(p2): true/false
p1.equals(p3): true/false
p1 == p2: false

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