Subscribe to Non Static        RSS Feed
-----

Shapes and Objects

Icon 1 Comments
Martyr2 recently blogged on Building Shapes with Loops. He goes on to show code for various patterns. Such code is a common request at DIC. Upon looking at the code offered, I had a thought. A rows and columns loop could be kept constant with only a boolean test needed to create a shape.

The thought now in my brain, I had to test it.

First, a base class to do the work:
abstract class BaseShape {
	protected int rows, cols;
	
	public BaseShape(int rows, int cols) {
		this.rows = rows;
		this.cols = cols;
	}
	
	protected abstract boolean hasPixel(int row, int col);
	
	public void drawShape() {
		for (int row=0; row < rows; row++) {
			for (int col=0; col < cols; col++) {
				if (hasPixel(row, col)) {
					System.out.print("*");
				} else {
					System.out.print(" ");
				}
			}
			System.out.println();
		}
	}
}



First, an easy shape, a rectangle:
class ShapeRectangle extends BaseShape {
	public ShapeRectangle(int rows, int cols) { super(rows, cols); }
	protected boolean hasPixel(int row, int col) { return true; }
}



Draw a 10x10 rectangle like so:
new ShapeRectangle(10,10).drawShape();



I know, that's kind of too easy. Complexity is not beyond the framwork, though. Here's the complete program:
abstract class BaseShape {
	protected int rows, cols;
	
	public BaseShape(int rows, int cols) {
		this.rows = rows;
		this.cols = cols;
	}
	
	protected abstract boolean hasPixel(int row, int col);
	
	public void drawShape() {
		for (int row=0; row < rows; row++) {
			for (int col=0; col < cols; col++) {
				if (hasPixel(row, col)) {
					System.out.print("*");
				} else {
					System.out.print(" ");
				}
			}
			System.out.println();
		}
	}
}

class ShapeRectangle extends BaseShape {
	public ShapeRectangle(int rows, int cols) { super(rows, cols); }
	protected boolean hasPixel(int row, int col) { return true; }
}

class ShapeRightTriangle extends BaseShape {
	public ShapeRightTriangle(int rows) { super(rows, rows); }
	protected boolean hasPixel(int row, int col) { 
		return col<=row;
	}
}

class ShapeLeftRightTriangle extends BaseShape {
	public ShapeLeftRightTriangle(int rows) { super(rows, rows); }
	protected boolean hasPixel(int row, int col) { 
		return col>=(rows-row-1);
	}
}

class ShapeEquilateralTriangle extends BaseShape {
	private int cCol;
	public ShapeEquilateralTriangle(int rows) { 
		super(rows, rows*2);
		this.cCol = (this.cols/2)-1;
	}
	protected boolean hasPixel(int row, int col) { 
		return (Math.abs(col-cCol)<(row+1));
	}
}


public class Shapes {
	public static void main(String args []) {
		int rows = 10;
		int cols = 10;
		
		BaseShape [] shapes = new BaseShape []{
			new ShapeRectangle(rows,cols),
			new ShapeRightTriangle(rows),
			new ShapeLeftRightTriangle(rows),
			new ShapeEquilateralTriangle(rows)
		};
		
		for(BaseShape shape : shapes) {
			System.out.println(shape.getClass().getName());
			shape.drawShape();
			System.out.println();
		}
	}
}



This is more a proof of concept. Completely unique shapes would defy simple boolean logic. However, the base object could still be used. The only public method is drawShape; encapsulation is your friend.

Don't know if this will help or amuse anyone. There are several OO design concepts floating around in here, but it's really just for fun. I was at least amused.

1 Comments On This Entry

Page 1 of 1

mostyfriedman Icon

17 March 2010 - 03:39 PM
nice post Baavgai..I would like to see more entries from you. keep em coming :)
0
Page 1 of 1

September 2014

S M T W T F S
 1 2 3456
78910111213
14151617181920
21222324252627
282930    

Recent Entries

Recent Comments

Search My Blog

0 user(s) viewing

0 Guests
0 member(s)
0 anonymous member(s)