007-003-011

Class Instantiation

Easy

Problem Description

Class Instantiation

In this problem, you will define a Person class that can be instantiated and have its fields accessed.

Learning Objective: Understand class definition and instantiation mechanics

Overview

Create a Person class that meets the following specifications.

Specifications

  • name field (String, public)
  • age field (int, public)
  • introduce() method: returns a string in the format "Name: " + name + "\nAge: " + age

Usage Example

Person p = new Person();
p.name = "Alice";
p.age = 30;
System.out.println(p.introduce());

Example Output

Name: Alice
Age: 30

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