In: Computer Science
(In Matlab) Create a base class named Point that has the properties for x and y coordinates. From this class derive a class named Circle having an additional property named radius. For this derived class, the x and y properties represent the center coordinates of a circle. The methods of the base class should consist of a constructor, an area function that returns 0, and a distance function that returns the distance between two points. The derived class should have construtor and an overloaded function named area that returns the area of a circle. Write a script that has two objects of each class and calls all of the methods.
%CLASS FOR POINT
classdef Point
properties
x
y
end
methods
function obj = Point(x,y)
obj.x = x;
obj.y=y;
end
function ar = area(obj)
ar=0;
end
function r = distance(obj,obj2)
r = sqrt((obj2.x-obj.x)^2+(obj2.y-obj.x)^2);
end
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%CLASS FOR CIRCLE
classdef Circle < Point
properties
radius
end
methods
function obj = Circle(x,y,r)
obj@Point(x,y);
obj.radius=r;
end
function ar = area(obj)
ar=pi*obj.radius^2;
end
end
end
%%%%%%%%%%%%%%%%%
%SCRIPT
point1=Point(0,0)
point2=Point(3,4)
fprintf('Distance between 2 points:
%g\n',point1.distance(point2))
fprintf('Area point1: %g\nArea point2:
%g\n',point1.area,point2.area)
circle1=Circle(0,0,5)
circle2=Circle(3,4,5)
fprintf('Distance between 2 circle centers:
%g\n',circle1.distance(circle2))
fprintf('Area circle1: %g\nArea Circle2:
%g\n',circle1.area,circle2.area)