QUESTION IMAGE
Question
a teacher is creating a class that will help keep track of student grades. he will be using the following instance variables:
- studentname (string)
- studentgrade (double)
- lettergrade (string)
and this constructor:
public student(string n, double d, string g){
studentname = n;
studentgrade = d;
lettergrade = g;
}
which of the following would be the best way to create a student object?
- student s1 = new student(98.2, \a\, \ima coder\);
- student s1 = new student(\a\, \ima coder\, 98.2);
- student s1 = new student(\ima coder\, 98.2, \a\);
🆕 New Concept Discovered: Object Instantiation and Constructors
Matching arguments to constructor parameters.
Step 1: Analyze the Constructor Signature
The class constructor defines the exact order and data types of the values (arguments) you must pass when creating a new object:
public Student(String n, double d, String g)
This signature tells us the constructor expects three parameters in this specific order:
- A
Stringvalue for the name (n) - A
double(decimal number) value for the grade (d) - A
Stringvalue for the letter grade (g)
Step 2: Evaluate the Options
We need to find the option where the arguments match the required sequence: (String, double, String).
- Option 1:
Student s1 = new Student(98.2, "A", "Ima Coder"); - Types passed:
(double, String, String)$
ightarrow$ Incorrect order
- Option 2:
Student s1 = new Student("A", "Ima Coder", 98.2); - Types passed:
(String, String, double)$
ightarrow$ Incorrect order
- Option 3:
Student s1 = new Student("Ima Coder", 98.2, "A"); - Types passed:
(String, double, String)$
ightarrow$ Correct order
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
Student s1 = new Student("Ima Coder", 98.2, "A");