Assignment #8

Your max grade for this assignment is 15, and you get points from the following parts:
  • 12 pts for requirements [#1-#3] on the assignment sheet],
  • 3 pts for coding and documentation.
The Grading Base is going to be like "15 [12 3]" for a max grade on the Grades page for this assignment.

Class Inheritance

In this assignment, you were asked to extend a class, Cone, from some other appropriate classes such as Circle or Cylinder. By the nature of extension, you had to overwrite some methods of the parent class.

However, this was not the case I saw in your assignments. You picked an appropriate class, extended a new class from it, but did not overwrite the methods. This means that you had to write either a completely different method, or a method with some extensions to its parent's methods. Most of you extended Cone from Cylinder, but kept all of its methods without a change, or made some incorrect extensions.

Here is the public interface of the Cylinder class in which we see one data field, which is not public meaning that you cannot reach it directly but through methods, two constructors and five methods in the Cylinder class defined as either new methods, or overwritten methods.

public class Cylinder extends Circle { 
   protected double height;  // height of Cylinder 

   public Cylinder(); 
   public Cylinder( double h, double r, int a, int b); 
   public void setHeight( double h )  ; 
   public double getHeight() ; 
   public double area() ; 
   public double volume() ; 
   public String toString(); 
} 

When you want to define Cone, you naturally look for a very similar object from which you can borrow some properties. So, Cylinder is a good example for this.

First of all, extend the class from its parent class.

public class Cone extends Cylinder { 

} 

At this point, you already have all the public methods of Cylinder also defined in Cone. That means that you can create a Cone object now. See the following lines.

Cone a_cone_object = new Cone(); 
System.out.println( a_cone_object.toString() );  
System.out.println( a_cone_object.area() ); 
Output:
Center = [0, 0]; Radius = 0; Height = 0 
0 

We can do this only because the Java compiler automatically defines a no-argument constructor if no constructor is defined. Actually, our simple extension is more than the above. It is like this:
public class Cone extends Cylinder {  
      public Cone() { 
           super(); // for no-argument constructors, 
                    // this call to the parent's constructor 
                    // is not needed. 
      } 
}  

However this forbids us to construct Cone objects with different parameters. Hence we add our constructor,

public Cone( double h, double r, int a, int b ); 

Unfortunately, when we add a constructor of our own, the compiler gives up defining one, and we have to add the no-argument constructor by ourselves.

 
public class Cone extends Cylinder {  
      public Cone() { 
           super(); // for no-argument constructors, 
                    // this call to the parent's constructor 
                    // is not needed. 
      } 
      public Cone( double h, double r, int a, int b ) { 
           super(h, r, a, b ); 
      } 
}
Now, we can do this:
Cone another_cone_object = new Cone(20, 10, 1, 1);  
System.out.println( another_cone_object.toString() );   
System.out.println( another_cone_object.area() );  
Output:
Center = [1, 1]; Radius = 10; Height = 20 
1884.96  

Are we done yet? Of course, not. Do cones and cylinders have the same amout of area and volume? No. If they had, why would we give both objects different names?

From the link that Tom provides us about cones, we learn that the volume of a cone is the one third of its base area times its height. We know that the volume of a cylinder is its base area times its height. This makes life easy. Hence here is the Cone's volume method:

public double volume() { 
     return (1/3) * super.volume(); 
} 

Again, from the same page, we can find the formula and calculate the area of a cone.

public double area() { 

   double radius_square = getRadius() * getRadius(); 
   double height_square = getHeight() * getHeight(); 

   double base_area = Math.PI * radius_square; 
   double sides     = Math.PI * getRadius() * 
                      Math.sqrt( radius_square + height_square ); 

   return base_area + sides; 
}; 

Finally, here is the test application and the complete Cone class you needed to submit.

class Test {
        public static void main(String argv[]) {

           Cone last_cone_object = new Cone(20, 10, 1, 1);
           System.out.println( last_cone_object.toString() );
           System.out.println( last_cone_object.area() );
           System.out.println( last_cone_object.volume() );
        }
}
public class Cone extends Cylinder {

   public Cone() {  
   };

   public Cone( double h, double r, int a, int b )
   {
      super( h, r, a, b );
   }

   public double area() {

      double radius_square = getRadius() * getRadius();
      double height_square = getHeight() * getHeight();

      double base_area = Math.PI * radius_square;
      double sides     = Math.PI * getRadius() *
                         Math.sqrt( radius_square + height_square );

      return base_area + sides;
   };

   public double volume() {
      return (1/3) * super.volume();
   }
 
}

If you have not gotten a max grade, it is most probably you have not submitted these two files as well as other accompanying class files.

Ozgur Balsoy
balsoy@npac.syr.edu


Northeast Parallel Architectures Center