007-001-015

Creating and Using a Class

Medium

Problem Description

Creating and Using a Class

In this problem, you will define a Person class with name (String) and age (int) fields and a greet() method.

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
  • Add name (String) and age (int) fields (public access)
  • Define a greet() method that prints "Hello, I am [name]. I am [age] years old." to standard output

Usage Example

Person p = new Person();
p.name = "Alice";
p.age = 25;
p.greet(); // Hello, I am Alice. I am 25 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