Introduction to Method Overloading and Constructors | My Assignment Tutor

T121: ICT102 Introduction to ProgrammingTutorial 9 Topic:Submission:Description:Introduction to Method Overloading and ConstructorsOne day after your tutorial This tutorial will introduce you to method overloading and overloaded constructors inclasses. You should be able to differentiate between instance/fields/attributes and localvariables. Object creation and constructors calling and how methods with same name, butdifferent signature can be used in classes.Exercise 1: Method Overloading and Default Constructor1. Familiarize yourself with following code and discuss with a fellow student, you shouldspecifically look for two overloaded disp() method, what formal parameters they have,any local variables declared, how these methods are called from main() with differentactual parameters? On which line default DisplayOverloading constructor is called?class DisplayOverloading{public void disp(){System.out.println(“I am method with no arguments”);}public void disp(char c){System.out.println(“I am the method with one argument: “+ c);}public void disp(char c, int num){System.out.println(“I am the method with two arguments” +c + ” “+num);}}class Sample{public static void main(String args[]){DisplayOverloading obj = new DisplayOverloading();Obj.disp();obj.disp(‘a’);obj.disp(‘a’,10);}}Exercise 2: Overloaded Constructors1. What is printed by the following application? Note, you have two classes, Box and aOverLoadConstructor class to it.class Box{double width;double height;double depth;// construct clone of an objectBox(Box ob){// pass object to constructorwidth = ob.width;height = ob.height;depth = ob.depth;}// constructor used when all dimensions specifiedBox(double w, double h, double d){width = w;height = h;depth = d;}// constructor used when no dimensions specifiedBox(){width = -1; // use -1 to indicateheight = -1; // an uninitializeddepth = -1; // box}// constructor used when cube is createdBox(double len){width = height = depth = len;}// compute and return volumedouble volume(){return width * height * depth;}}class OverLoadConstructor{public static void main(String args[]){// create boxes using the various constructorsBox box1 = new Box(10, 20, 15);Box box2 = new Box(); Boxcube = new Box(7);Box clone1 = new Box(box1);double vol;// get volume of first boxvol = box1.volume();System.out.println(“Volume of box1 is ” + vol);// get volume of second boxvol = box2.volume();System.out.println(“Volume of box2 is ” + vol);// get volume of cubevol = cube.volume();System.out.println(“Volume of cube is ” + vol);// get volume of clone1vol = clone1.volume();System.out.println(“Volume of clone1 is ” + vol);box2.width=5; /*width,height,depth are default mumbers*/box2.height=10;box2.depth=15;Box clone2 = new Box(box2);// get volume of clone1vol = clone2.volume();System.out.println(“Volume of clone2 is ” + vol);}}2. You should look for how different Box constructors are built and called from testclass? (Ko•) mg s wnv InstituteK. ‘ 0 A Kiran IjazSubmission Task:Look at the following partial class definition, and then respond to the questions that follow it:public class Student{private String Name;private String ID;private String Email;}a. Write a constructor for this class. The constructor should accept an argument for eachof the fields.b. Write accessor and mutator methods for each field.


Leave a Reply

Your email address will not be published. Required fields are marked *