QUESTION IMAGE
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);
🆕 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:
- A
Stringvalue first (for the parametern). - An
intvalue second (for the parametera).
Step 2: Evaluate the Options
- Option 1:
Person p1 = new Person(42, "Avery"); - This passes an
int(42) first and aString("Avery") second. This mismatches the expected(String, int)signature.
- Option 2:
Person p1 = new Person("Avery", 42); - This passes a
String("Avery") first and anint(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.
Snap & solve any problem in the app
Get step-by-step solutions on Sovi AI
Photo-based solutions with guided steps
Explore more problems and detailed explanations
Person p1 = new Person("Avery", 42);