import java.util.Scanner; import java.io.IOException; /** Triangle is a class that represents a Triangle, for purposes of calculating its area. We can create a Triangle simply by specifying its base and its height. Note that this Triangle class absolutly DOES NOT capture everything that one could capture about a triangle---it doesn't need to. We are developing this Triangle object for a particular purpose---calculating area---so we do "the simplest thing that could possibly work". If we need something else later--e.g the exact position of the vertexes of the triangle in 2-space or 3-space---we can "refactor" the code and add that extra complexity later. Leaving this out for now is an example of the "YAGNI" principle (look it up---link provided below!) For now, we aren't even providing getters and setters for base and height---we don't do this until we NEED it for some reason. Is this class overkill for a problem this small? Absolutely yes! But this is intended to be a small example to show the principle of using "plain old Java objects" in an MVC fashion--separating computational details from user-interface details. So, if you need to, suspend your disbelief and pretend that this object is something a lot more complex. Don't lose sight of the main idea---there is no I/O code in this class. (And by the same token, there will be no signficant "computing" in the user-interface parts of the code--just routing of input and output to where it needs to flow.) @author Phill Conrad @version 2011.01.01 @see Wikipedia article on YAGNI */ public class Triangle { private double base; private double height; /** self-explanatory */ public Triangle(double base, double height) { this.base = base; this.height = height; } /** getter for area */ public double getArea() { // return -42.0; // stub! (for testing the test) return 0.5 * base * height; } } // class Triangle