Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Introduction This weeks lab will look at a bad design using a combination of static and instance variables. Like in the homework, we have a

Introduction This weeks lab will look at a bad design using a combination of static and instance variables. Like in the homework, we have a LandPlot class. There is a problem with the interaction between static and instance variables. Your task is to identify a problem and come up with a design change. The Design The LandPlot class has four private object fields: a String for the owner, a double for the length, a double for the width, and a double for the value of the LandPlot. It also has one private static class field: a double for the pricePerSquareFoot for all LandPlots. (You can see static variables in the debugger by using Show Static Variables see the picture in the slides) The constructor takes values for the owner, width, and length from its parameters, and it computes the value based in this plots length and width, and the current pricePerSquareFoot. The only method that changes the state of the object is updatePrice, which takes a new value to update the static pricePerSquareFoot, and then updates the value for this LandPlot based on the new pricePerSquareFoot. The Problem You can infer from the unit tests that there is an expectation about LandPlot objects. We expect that, at any given time, if we multiply the length and width of a LandPlot with the pricePerSquareFoot for LandPlots, we should get the value of the plot. This assumption is being violated. To Get Credit Bare minimum: write an explanation of the design problem. Why is the test failing where it is? What is wrong with the design? Good: come up with and describe a better design for the LandPlot class that has the same public methods, but will also pass the given test. Excellent: implement this better design in the project. You may write answers on the reverse of this document. public class LandPlot { //private object fields private String owner; private double length; private double width; private double value; //private static mutable variable private static double pricePerSquareFoot = 1; public LandPlot(String name, double lengthInFeet, double widthInFeet) { owner = name; length = lengthInFeet; width = widthInFeet; value = length * width * pricePerSquareFoot; } public String getOwner() { return owner; } public double getLength() { return length; } public double getWidth() { return width; } public double getValue() { return value; } //static getter public static double getPricePerSquareFoot() { return pricePerSquareFoot; } /** * object method to update the current price Per Acre * set the static pricePerSquareFoot to equal the passed value * then update the value field accordingly * @param newPrice */ public void updatePrice(double newPrice) { pricePerSquareFoot = newPrice; value = length * width * pricePerSquareFoot; } } import junit.framework.TestCase; public class TestLandPlot extends TestCase{ private static final double EPSILON = 0.005; public void setUp() { } //test default values (for primitive numeric variables these are generally 0 values) public void test00() { assertEquals(LandPlot.getPricePerSquareFoot(), 1.0, EPSILON); LandPlot plotA = new LandPlot("A", 20, 20); LandPlot plotB = new LandPlot("B", 20, 10); assertEquals(computeValue(plotA), plotA.getValue(), EPSILON); assertEquals(computeValue(plotB), plotB.getValue(), EPSILON); plotA.updatePrice(1.5); assertEquals(computeValue(plotA), plotA.getValue()); assertEquals(computeValue(plotB), plotB.getValue()); } private double computeValue(LandPlot plot) { return plot.getLength() * plot.getWidth() * LandPlot.getPricePerSquareFoot(); } }

image text in transcribed
image text in transcribed
can i get an anwer please? any information will help
public class Landplot { //private object fields private String owner; private double length; private double width: private double value; //private static mutable variable private static double pricepe saware Eret 1; public Landplet(String name, double length East double widthInfeet) Owner name: length = lengthineet: width = widthineet: value = length * Width RicePer Squarefoot public String aster) { return owner: public double getLength() { return length: public double stwidth) { return width; public double getValue { return value; //static getter public static double gacice Per Squacstoot return pricePerseware + object method to update the current price Per Acre - set the statie prices.cSquat to equal the passed value * then update the value field accordingly Oparan newerice public void setelice double p rice of Sawacert Pete Value = length width - Ribes ed States) n import unit.framework Test Case: public class TestLandelet extends Testcase private static final double EPSILON = 0.005; public void setup() { //test default values for primitive numeric variables these are generally values) public void testo() { assertEquals(Landplot.getPricePersaucefeet(), 1.0, EPSILON); Landplat RetA new Landplot("A", 20, 20); Landelet let = new LandPOR "B", 20, 10); assertEva Couteams RIRTA), latet value EPSILON): assertEquals computeva uslots), Rio BetValue(), EPSILON); let undateprise(1.5); assertEn conauteValue Dota), plotA.getValue)); SECREQUISIC outera URIAR), Riot Bastatus (1) private double contealellandelot plot) { return photothenath() > plotontwidth(); t eciper SCREARE(); Land 00 K public class Landplot { //private object fields private String owner; private double length; private double width: private double value; //private static mutable variable private static double pricepe saware Eret 1; public Landplet(String name, double length East double widthInfeet) Owner name: length = lengthineet: width = widthineet: value = length * Width RicePer Squarefoot public String aster) { return owner: public double getLength() { return length: public double stwidth) { return width; public double getValue { return value; //static getter public static double gacice Per Squacstoot return pricePerseware + object method to update the current price Per Acre - set the statie prices.cSquat to equal the passed value * then update the value field accordingly Oparan newerice public void setelice double p rice of Sawacert Pete Value = length width - Ribes ed States) n import unit.framework Test Case: public class TestLandelet extends Testcase private static final double EPSILON = 0.005; public void setup() { //test default values for primitive numeric variables these are generally values) public void testo() { assertEquals(Landplot.getPricePersaucefeet(), 1.0, EPSILON); Landplat RetA new Landplot("A", 20, 20); Landelet let = new LandPOR "B", 20, 10); assertEva Couteams RIRTA), latet value EPSILON): assertEquals computeva uslots), Rio BetValue(), EPSILON); let undateprise(1.5); assertEn conauteValue Dota), plotA.getValue)); SECREQUISIC outera URIAR), Riot Bastatus (1) private double contealellandelot plot) { return photothenath() > plotontwidth(); t eciper SCREARE(); Land 00 K

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Expert Oracle Database Architecture

Authors: Thomas Kyte, Darl Kuhn

3rd Edition

1430262990, 9781430262992

More Books

Students also viewed these Databases questions

Question

Does your product/program have a descriptive and memorable slogan?

Answered: 1 week ago