/** * class PizzaServer, version 4. * * A PizzaServer knows how to answer basic * questions about the area of pizzas. * Also, this version adds a salary, and uses javadoc comments. * * @author Ian Barland * @version 4, after lab4's adding of various methods. */ class PizzaServer { /** The width of crust all the way around the edge of a pizza. */ static final double CRUST_WIDTH = 2.0; /** Minimum wage, in $/hr. (Should be defined in some other class, gov.deptOfLabor ?) */ static final double MINIMUM_WAGE = 5.15; /** The salary of this particular PizzaServer (in $/hr). */ double salary = MINIMUM_WAGE; /** Ask this particular PizzaServer about their salary. * @return This PizzaServer's salary (in $/hr). */ double getSalary() { return salary; } /** * Set this particular PizzaServer's salary. * @param newSalary The new salary for this employee (in $/hr). */ void setSalary( double newSalary ) { salary = newSalary; } /** * Create two new PizzaServers, set the salary of the first to sal, * and return the sum of their salaries. * * After Tuesday's lecture, we realize this would better be a * static method, since it's not like we walk up to one particular * server and ask them to create other servers. * * @param sal the rate to set one server's salary to (in $/hr). * @return the sum of two pizzaserver's salaries (in $/hr). */ static double twoServersPoolMoney( double sal ) { PizzaServer p1 = new PizzaServer(); PizzaServer p2 = new PizzaServer(); p1.setSalary( sal ); return p1.getSalary() + p2.getSalary(); } /** * Give a textual description of this PizzaServer. The text will * mention that that it describes a PizzaServer, and will include * the value of its field(s). * * @return A string representation of this PizzaServer. * A sample return value might be "[PizzaServer -- salary: $4.23/hr]" * but with the salary field reported appropriately. */ String asString() { return "[PizzaServer -- salary: $" + getSalary() + "/hr]"; /* Note: If we ever add more fields to this class, * we should come back and update asString to print those out too. */ } /** * Construct a new PizzaServer with the desired salary. * @param sal The salary (in $/hr) of the server to be created. * @return a new PizzaServer. */ PizzaServer constructSalariedServer( double sal ) { PizzaServer newServer = new PizzaServer(); newServer.setSalary( sal ); return newServer; } /** * Return a copy of the current PizzaServer. * @return the new PizzaServer clone.. */ PizzaServer makeClone() { PizzaServer newServer = new PizzaServer(); newServer.setSalary( getSalary() ); return newServer; } /** * Run some tests, and verify that clone() returns a new Server * which just looks like the old server, but really is different. */ static void testMakeClone() { PizzaServer jo = new PizzaServer(); jo.setSalary( 12.34 ); System.out.println( "Jo before cloning: " + jo.asString() ); PizzaServer moe = jo.makeClone(); System.out.println( "Jo after cloning: " + jo.asString() ); System.out.println( "Moe, the new clone: " + moe.asString() ); moe.setSalary( 3.45 ); System.out.println( "Moe, salary lowered: " + moe.asString() ); System.out.println( "Jo, unaffected by Moe's change: " + jo.asString() ); } /** * Calculate the area of a pizza, given its diameter. * * After Sep.19 (Tue)'s lecture, we realize this would better be static. * * @param diam The diameter of a pizza (in inches). * @return The area (in sq.in.). *
*
Test cases: *
pieArea( 0) = 0 *
pieArea( 2) =~ 3.14 *
pieArea(20) =~ 314.0 * *
pieArea( 4) =~ 12.52 *
pieArea(16) =~ 201.5ish */ static double pieArea( double diam ) { final double radius = diam/2.0; return Math.PI * radius * radius; } /** * Given a pizza diameter, calculate how much crust there is (in sq.in.). * It is guaranteed that there is {@value CRUST_WIDTH}" of crust around the edge; * hey, this means 2*{@value CRUST_WIDTH}" is the smallest allowable pizza. * * After Sep.19 (Tue)'s lecture, we realize this would better be static. * * @param diam The diameter of the pizza (in inches); diam must be >= 4. * @return The amount of crust (in sq.in.). *
*
Test cases: *
crustArea( 4) = pieArea( 4) =~ 12.52 *
crustArea(20) = pieArea(20) - pieArea(16) =~ 314 - 201.5ish = 112.5ish */ static double crustArea( double diam ) { return pieArea(diam) - pieArea( diam - 2*CRUST_WIDTH ); } }