007-001-015

Creating and Using a Class

Medium

Problem Description

Creating and Using a class

In this problem, you will create a program that defines a Person class with name and age fields and a greet() method, instantiates two objects, and displays greetings to standard output.

Learning Objective: Understand how to define Java classes and create objects with fields and methods

Overview

A class is a blueprint for objects. It defines fields (data) and methods (behavior). Instances are created with the new keyword.

Specifications

  • Create a Person class with a name (String) field and an age (int) field
  • Implement a greet() method that prints a greeting in the format "Hello, I am [name]. I am [age] years old."
  • Create two Person objects (Alice age 25, Bob age 30) and call greet() on each

Output Format

Hello, I am Alice. I am 25 years old.
Hello, I am Bob. I am 30 years old.

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