SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Guru worchyld's Avatar
    Join Date
    Jul 2003
    Location
    Newcastle upon Tyne
    Posts
    909
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Creating a sumAreas() class with JS?

    I am currently doing a small test, but I am having trouble understanding what this question is asking me to do.

    a. Produce an object to represent a circle. Your circle object must implement two instance methods getCircumference() and getArea(). These methods will return the circumference and area of the circle respectively. The object should also include a class method sumAreas(), which takes two radii as parameters and returns the sum of their two areas.
    I've seen an article on Sitepoint that uses getCircumfrance and getArea;

    Code:
    	function Circle(radius){
    	 
    	 this.radius = radius;
    	 
    	 this.getArea = function(){
    	   return (this.radius*this.radius*3.14);
    	 }
    	 
    	 this.getCircumference = function(){
    	   var diameter = this.radius*2;
    	   var circumference = diameter*3.14;
    	   return circumference;
    	 }
    	}
    	
    	var bigCircle = new Circle(100);
    	
    	alert(bigCircle.getArea()); // displays 31400
    But what does it mean by the last sentence, and a setAreas() class?

    The object should also include a class method sumAreas(), which takes two radii as parameters and returns the sum of their two areas.
    How do I do this?

  2. #2
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The question has nothing to do with native javascript functions. The question is asking you to create an object, and then create three functions yourself for that object:

    getCircumference(), getArea()

    which are typical 'get' functions that don't do any calculations themselves, and instead just retrieve values from the object, as well as

    sumAreas()

    which has two parameters and returns the result of a mathematical calculation involving the two parameters.

    Here is a beginning article on how to create objects in javascript:

    http://www.sitepoint.com/article/oriented-programming-1
    http://www.sitepoint.com/article/oriented-programming-2

    But what does it mean by the last sentence, and a setAreas() class?
    It doesn't quite say that--it says a "class method setAreas()". In object orientated programming 'speak', objects are called 'classes', and object properties that are values (as opposed to functions) are called 'members' while object properties that are functions are called 'methods'. So, all the above statement is saying is that you are supposed to create a third function for the object.

  3. #3
    SitePoint Guru worchyld's Avatar
    Join Date
    Jul 2003
    Location
    Newcastle upon Tyne
    Posts
    909
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, I was confused. I was confused on whether it was asking for a function or a class, or something else.

    Regardless, I want to thank you for your help with this matter and I will research the links you've provided.

  4. #4
    SitePoint Guru worchyld's Avatar
    Join Date
    Jul 2003
    Location
    Newcastle upon Tyne
    Posts
    909
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is right, isn't it?

    Code:
    	function Circle(radius){
    	 
    		this.radius = radius;
    		
    		/* #################### */
    		
    		this.getArea = function(){
    			return (this.radius*this.radius*3.14);
    		}
    		
    		/* #################### */
    			 	 
    		this.getCircumference = function(){
    			var diameter 		= this.radius*2;
    			var circumference 	= diameter*3.14;
    			return circumference;
    		}		
    	}
    		
    	function setAreas(radii1, raddi2) {
    		
    		var	radius1 = new Circle(radii1);
    		var area1	= radius1.getArea();
    		
    		var radius2	= new Circle(radii2);
    		var area2	= radius2.getArea();
    		
    		return total = (area1 + area2);
    	}

  5. #5
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by worchyld
    The object should also include a class method sumAreas(), which takes two radii as parameters and returns the sum of their two areas.
    The keyword is "class method". Class (aka static) method, as opposed to "ordinary" member, applies to the class, not to particular class instance i.e. object. The term "class method" isn't 100% correct in javascript because there are no "classes" in this language, but the same can be done by assigning something to object's constructor.

    Code:
    // define constructor
    function Circle(radius) ...
    
    // define ordinary member function
    Circle.prototype.getArea = function() ...
    
    // define "class" or "static" function
    
    Circle.sumAreas = function() ...
    and this is how they are called
    Code:
    // constuctor
    someObject = new Circle();
    
    // member function
    someObject.getArea()
    
    // static function
    Circle.sumAreas();

  6. #6
    SitePoint Guru worchyld's Avatar
    Join Date
    Jul 2003
    Location
    Newcastle upon Tyne
    Posts
    909
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah, looks like I'm out of my depth with that question - I've attempted it, and that's all anybody can ask anybody to do.....

    Thanks.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •