Sovi.AI - AI Math Tutor

Scan to solve math questions

QUESTION IMAGE

a person class is to be created that will include these private instanc…

Question

a person class is to be created that will include these private instance variables:

  • string name
  • int age

with the following constructor:

public person(string n, int a)
{
name = n;
age = a;
}

which option is a valid way to construct a person object?

  • person p1 = new person(42, \avery\);
  • person p1 = new person(\avery\, 42);
  • person p1 = new person(\avery\, \jones\, 42);

Explanation:

🆕 New Concept Discovered: Object Construction and Constructors
Matching arguments to parameter types and order.

Step 1: Analyze the Constructor Signature

The Person class defines its constructor as:

public Person(String n, int a)

This constructor requires exactly two arguments in a specific order:

  1. A String value first (for the parameter n).
  2. An int value second (for the parameter a).

Step 2: Evaluate the Options

  • Option 1: Person p1 = new Person(42, "Avery");
  • This passes an int (42) first and a String ("Avery") second. This mismatches the expected (String, int) signature.
  • Option 2: Person p1 = new Person("Avery", 42);
  • This passes a String ("Avery") first and an int (42) second. This matches the constructor signature perfectly.
  • Option 3: Person p1 = new Person("Avery", "Jones", 42);
  • This passes three arguments: String, String, int. The constructor only accepts two arguments.

Answer:

Person p1 = new Person("Avery", 42);