Introduction to Classes and Method | My Assignment Tutor

T121 : ICT102 Introduction to ProgrammingTutorial 8 Topic:Submission:Introduction to Classes and MethodEnd of Week This tutorial will introduce you to classes, objects and methods. You should be able to writeyour own class, its methods and call them from a user class.Exercise 1: Class, Object and Method1. Familiarize yourself with following code and discuss with a partner/tutor, you shouldspecifically look for class and object declaration, class instance members or attributes,any local variables, how attributes are initiated in main method, returning a string fromclass method and how class method was invoked using object? What is output of thiscode?//Class Declarationpublic class Car{// Instance VariablesString make;String model;int engineSize;String color;// method 1public String getInfo() {return (“Make of this Car is: “+make+” the model is: “+ model+” Its engine is “+ engineSize + ” and the color is: ” +color);}public static void main(String[] args) {Car v1 = new Car();v1.make=”Toyota”;v1.model= “Corolla”;v1.engineSize=1800;v1.color=”white”;System.out.println(v1.getInfo());}}Now add another Car object, but this time ask the user for inputting make, model, engineSize and color and thenprint the info using the getInfo method.Submission Task: Circle Class2. What is printed by the following application?// Contains both Circle class and its user class//Add Circle class code here1. public class Circle{2. public double x, y; // centre of the circle3. public double r; // radius of circle//Methods to return circumference and area4. public double circumference(){5. return 2*3.14*r;}6. public double area(){7. return 3.14 * r * r;}}// User class MyMain8. class MyMain {9. public static void main(String args[]){10. Circle aCircle; // creating reference11. aCircle = new Circle(); // creating object12. aCircle.x = 10; // assigning value to data field13. aCircle.y = 20;14. aCircle.r = 5;15. double area = aCircle.area();16. double circumf = aCircle.circumference();17. System.out.println(“Radius=”+aCircle.r+” Area=”+area);18. System.out.println(“Circumference =”+circumf);}}3. What are the names of Class identifiers in two classes?4. What are identifiers x and y called on line 2?5. What is return type on line 4 in method header of circumference()?6. What is name of object for class Circle, give line number where it is initiated?7. Add another identifier in the class to save the color for the circle and also add a method toreturn the color of any object.


Leave a Reply

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