Question: please help me Part B: Constructors In Part A, you created a Student object using the default constructor, which initializes all of the fields to

 please help me Part B: Constructors In Part A, you created

a Student object using the default constructor, which initializes all of the

please help me

Part B: Constructors In Part A, you created a Student object using the default constructor, which initializes all of the fields to default values. When you create a Student object, you may want to set the instance variables of that object - the fields - to values associated with the actual student this object represents. Since every student is different, we need a way to specify different values for each student. To do this, you need to have a constructor that takes parameters, which is non-default constructor. 1) In the Student.java, below the fields, let's add a constructor with the following parameters in the parenthesis: String fName, char mInitial, String Name, String ID, int studentAge, boolean isLiveOnCampus Remember that the constructor has the same name as the class, and it does not have a return type. Inside the constructor, assign the fields to these parameters. 2) After adding this constructor, you will notice that your driver class has errors. Why? Because Java only provides a default constructor if no constructor is given. However, since you defined your own constructor now, the Student class does not have a default constructor any more. To fix this, you can add a default constructor to the Student class. Remember that the default constructor does not take any argument. Furthermore, you may leave the body of the default constructor empty (still include the opening and closing curly brackets), and it initializes all of the fields to default values. After this, compile your project, the error message went away. You may run your program again, and it will give you the same output as before. Now there are two constructors in your Student class, and they are overloaded. 3) Let's try to use the constructor with parameters to create another object: Student maria = new Student ("Maria", 'L', "Garces", "800555555", 19, true); 4) Add some print statements at the end of the main method to print out all of the fields of object maria. Verify your constructor works properly

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!