announcements‎ > ‎

2015-12-10 Interface Lab

posted Dec 9, 2015, 6:29 PM by Samuel Konstantinovich   [ updated Dec 10, 2015, 9:10 AM ]
Interfaces Example:

We have some classes that inherit from a base class Shape as follows:


Some of them implement an Area interface.
Some of them implement a Volume interface.

Notice that the classes that need to implement each interface are not descended from each other. This is why interfaces are so important. 
You can have unrelated classes such as:
     Rug, SodaCan, Bathtub, SwimmingPool, WhiteBoard. 
It would be sensible for each of them to implement one of the two interfaces based on how you would measure them. This has nothing to do with what the object's other properties are, or what base class it inherits from. You just want to be able to have this new common functionality. 

Classname	Instance Variables	Constructor                      Implements
Shape		String name		Shape()		                 
Circle		Double radius		Circle(radius,name)	         Area
Sphere					Sphere(radius,name)		 Volume
Cylinder	Double height		Cylinder(radius,height,name)	 Volume
Rectangle	Double length,width	Rectangle(length,width,name)     Area
Prism		Double height		Prism(length,width,height,name)  Volume

Github stuff:
1. Make a new git repository. There are multiple ways of doing this, agree with your neighbors which way you should do this.
2. Name the repo interfaceTest
3. Clone the repository locally so you can add the base java files. At this point I assume you are using appropriate git workflow. I will just talk about your lab now.

Lab:
1. Write the two interfaces:
The Area interface requires :      double getArea()
The Volume Interface requires:    double getVolume()
2. Modify the classes to implement the appropriate interface. Start by implementing Area in the Circle and Rectangle, and test your code with the driver provided. 
Explain what the driver is doing. 




Base Code:

public abstract class Shape{

    private String name;

    public Shape(){
	name = "";
    }

    public Shape(String name){
	this.name = name;
    }

    public String getName(){
	return name;
    }

    public abstract String toString();
}





public class Circle extends Shape{

    private double radius;

    public Circle(double radius, String name){
	super(name);
	this.radius = radius;
    }

    public double getRadius(){
	return radius;
    }

    public String toString(){
	return "Circle "+getName()+" with radius of "+getRadius();
    }

}





public class Sphere extends Circle{

    public Sphere(double radius, String name){
	super(radius,name);
    }

    public String toString(){
	return "FIXME!";
    }
}





public class Cylinder extends Circle{

    private double height;

    public Cylinder(double radius, double height,String name){
	super(radius,name);
	this.height = height;
    }

    public double getHeight(){
	return height;
    }

    public String toString(){
	return "FIXME!";
    }
}




public class Rectangle extends Shape{

    private double length,width;

    public Rectangle(double length, double width, String name){
	super(name);
	this.length = length;
	this.width = width;
    }

    public double getLength(){
	return length;
    }

    public double getWidth(){
	return width;
    }

    public String toString(){
	return "Rectangle "+getName()+" that is "+length+" by "+width;
    }
}






public class Prism extends Rectangle{

    private double height;

    public Prism(double length, double width, double height, String name){
	super(length,width,name);
	this.height = height;
    }

    public double getHeight(){
	return height;
    }

    public String toString(){
	return "FIXME!";
    }
}





import java.util.ArrayList;
public class Driver{
    public static char letter(){
	return (char)('A'+(int)(Math.random()*26));
    }

    /*//UNCOMMENT THIS WHEN YOU DO Area
      public static Area makeRandomAreaShape(){
      String name = ""+letter()+"-2d";
      double value =Math.round(Math.random()*10.0);
      if(Math.random() < 0.5){
      return new Circle(value,name);
      }else{
      double value2 =Math.round(Math.random()*10.0);
      return new Rectangle(value,value2,name);
      }
      }
      /*

      /* //UNCOMMENT THIS WHEN YOU DO Volume
      public static Volume makeRandomVolumeShape(){
      String name = ""+letter()+"-3d";
      double value = Math.round(Math.random()*10.0);
      double choice = Math.random();
      if(choice < 0.33){
      return new Sphere(value,name);
      }else{
      double value2 = Math.round(Math.random()*10.0);
      if(choice < 0.66){
      return new Cylinder(value,value2,name);
      }else{
      double value3 = Math.round(Math.random()*10.0);
      return new Prism(value,value2,value3,name);
      }
      }
      }
    */

    public static void main(String[]args){
	
	/*//UNCOMMENT THIS WHEN YOU DO AREA
	  Area[] twos = new Area[5];
	  for(int i = 0; i < twos.length; i++){
	  twos[i]=makeRandomAreaShape();
	  }
	  System.out.println("Area tests:");
	  for(Area a : twos){
	  System.out.println(a);
	  System.out.println(a.getArea());
	  }
	*/

	/*//UNCOMMENT THIS WHEN YOU DO VOLUME
	  Volume[] threes = new Volume[6];
	  for(int i = 0; i < threes.length; i++){
	  threes[i]=makeRandomVolumeShape();
	  }
	  System.out.println("\nVolume tests:");
	  for(Volume v : threes){
	  System.out.println(v);
	  System.out.println(v.getVolume());
	  }
	*/
    }
}


Comments