/* House.java * Ultimately we want to be able to compute the cost of building * a new house. To do this, we need 3 attributes: the dimensions * of the lot, and the amount of interior living space. * Then, we'll have 3 constructors (default, initial-value, copy). * The default constructor will ask for input, so that we'll know * what values to use for the house dimensions. * Finally, we'll need to implement the calculations. * * Let's assume the house is on a rectangular lot. * It's likely that the user will only be interested in integer values * for the house dimensions, but "double" will give us more flexibility, * and also the final ($) result has to be double anyway. */ import java.util.Scanner; public class House { private double lotLength; private double lotWidth; private double livingSpace; private static final double INSIDE_RATE = 80; private static final double OUTSIDE_RATE = 0.75; // 3 Constructors /* The default constructor does I/O. * Reminder - don't redeclare the attributes! * (Also, generally a good idea to close I/O stream once you're * finished with it, in case some other part of the program would * need it.) */ public House() { Scanner in = new Scanner(System.in); System.out.printf("Enter sq. ft. of living space: "); livingSpace = in.nextDouble(); System.out.printf("Enter length of lot: "); lotLength = in.nextDouble(); System.out.printf("Enter width of lot: "); lotWidth = in.nextDouble(); in.close(); } // Although we may not need them in this program, it's generally a // good idea to include all 3 kinds of constructors, just in case // we want to expand our program later. public House(double length, double width, double sqft) { lotLength = length; lotWidth = width; livingSpace = sqft; } public House(House h) { lotLength = h.lotLength; lotWidth = h.lotWidth; livingSpace = h.livingSpace; } // Return the total cost of the house, including the cost of the interior // as well as the lot. public double findCost() { double landArea = lotLength * lotWidth; return livingSpace * INSIDE_RATE + landArea * OUTSIDE_RATE; } /* Just for fun, let's implement a simple function that can determine * if "this" house has a bigger lot than some other house. * Note that the special word "this" is optional. * We could have used "this" in every single function in this class, * but often it's not necessary because we know which object we're * talking about. */ public boolean greaterLotThan(House other) { double myArea = this.lotLength * this.lotWidth; double otherArea = other.lotLength * other.lotWidth; return myArea > otherArea; } /* Generally a good idea to include a function that returns a string * representation of the object. * Don't forget to put spaces around words. */ public String toString() { return lotLength + "x" + lotWidth + " lot with " + livingSpace + " sq. ft. interior space"; } }