Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Basics of refactoring

Status
Not open for further replies.

Stretchwickster

Programmer
Apr 30, 2001
1,746
GB
Hi everyone,

I'm fairly new to Java but have a lot of experience (including object-oriented) in other languages such as Delphi. I'm trying to get to grips with the basics of Extreme Programming (XP) and in particular "Refactoring". I need a starting point on some code I have - and this is not a homework assignment! The aim of refactoring is to improve the code, often by making it simpler to read.

Code:
class GeomFigure
{
   public:
       char type[20];
       float area;
       float radius;
       float major_axis;
       float minor_axis;

       float calcArea()
       {
           if(strcmp("circle", type))
           {
               area = radius * radius * 3.1415929;
           }
           if(strcmp("ellipse", type))
           {
               area = major_axis * minor_axis * 3.1415929;
           }
           return area;
        }
};
I've started by introducing a new variable Pi so I've added the line
Code:
 final float Pi = 3.1415929
and substituted Pi for the 3.1415929 parts.

The 2 area expressions basically perform the same calculation with different parameters so I was thinking of pulling this out into a separate method and calling it each time.

Is there a simpler form of the conditional statements I've got i.e. that don't use a string comparison?

I know this is a very basic example, but what would be your starting point for improving the readability of this code. Any help would be much appreciated.

Clive [infinity]
 
Code:
class GeomFigure
{
final static double PI = 3.1415929; //The precision of float is not enough, so double literal is used
      public double calcArea(double radius) // Uppercase letters are suggested for final literal 
      {
       double area = 0.0;
       area = radius * radius * PI;
       return area;
            }
      public double calcArea(double parameter1, double parameter2, String type)
      {
       double area = 0.0;
       if (type.equals("elipse")) // character array is not equal to String in Java
          {
           area = parameter1 * parameter2 * PI;           
          }
       else
          {
           // calculate the area of a rectangle
           area =  parameter1 * parameter2;
          }
       return area;              
      } 
     public static void main(String args[])
            {
             double circleArea = 0.0, squareArea = 0.0, elipseArea = 0.0;
             GeomFigure geomFigureObj = new GeomFigure();
             circleArea = geomFigureObj.calcArea(2.2);
             elipseArea = geomFigureObj.calcArea(3.5,2.7,"elipse");
             squareArea = geomFigureObj.calcArea(6.3,6.3,"square");
             System.out.println("circleArea "+circleArea);
             System.out.println("elipseArea "+elipseArea);
             System.out.println("squareArea "+squareArea);
             System.out.println("Pi value "+GeomFigure.PI);
            }
}
 
Or you could do it in a object oriential way:

Code:
public interface GeomFigure {
	
	public double PI = Math.PI;
	public double area();
}


public class Ellipse implements GeomFigure {
	
	double major_axis;
	double minor_axis;

	public Ellipse (double major_axis, double minor_axis ) {
		this.major_axis = major_axis;
		this.minor_axis = minor_axis;
	}

	public double area() {
		return this.major_axis * this.minor_axis * PI;
	}

}



public class Circle extends Ellipse {

	public Circle(double radius) {
		super(radius, radius);
	}
}


To use it

GeomFigure circle_10 = new Circle(10);
GeomFigure ellipse_5_10 = new Ellipse(5, 10);
double totalArea = circle_10.area() + ellipse_5_10.area();
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top