## MAO 2 years ago Anyone knows what is the best way to resolve the Exercise 11, Chapter 3 of the book: The Art and Science of Java?

• This Question is Open
1. Brenar

It might help to post the question here.

2. MCA2014

Give few questions dude dont have book.

3. MCA2014

4. MAO

11. Exercise 4 in Chapter 2 asks you to write a graphics program that produces a diagram of a simple frame house. If you wanted to make (the DrawHouse program easy to change, what named constants should you define to provide the necessary flexibility? Rewrite the program so that it uses these named constants rather than explicit coordinate values. Here it is the DrawHouse: import acm.graphics.*; import acm.program.*; public class JHouse extends GraphicsProgram{ public void run(){ GRect rectWalls = new GRect(((getWidth() - 300)/2), ((getHeight()-120)/2), 300, 120); GRect rectDoor = new GRect(((getWidth() - 50)/2), ((getHeight()-80)/2+20), 50, 80); GRect windLeft = new GRect(((getWidth() - 40)/2-90), ((getHeight()-40)/2), 40, 40); GRect windRight = new GRect(((getWidth() - 40)/2+90), ((getHeight()-40)/2), 40, 40); GLine roofRight = new GLine(527,177,370,50); GLine roofLeft = new GLine(228,177,369,50); GLine windFrameR = new GLine(287,255,287,216); GLine windFrameL = new GLine(467,255,467,216); GOval doorHandle = new GOval(387,255,7,7); add(rectWalls); add(rectDoor); add(windLeft); add(windRight); add(roofRight); add(roofLeft); add(windFrameR); add(windFrameL); add(doorHandle); } }

5. MAO

And here is what I came up with: Can you guys tell me if there is a better way to resolve this? I appreciate if you post the explanation as well, so I can understand how to think. Thanks import acm.graphics.*; import acm.program.*; public class JHouseRevised extends GraphicsProgram{ public void run(){ add(new GRect(((getWidth()-HOUSE_SIZE)/2),((getHeight()-HOUSE_SIZE+ROOF_SIZE)/2),HOUSE_SIZE,HOUSE_SIZE/2)); add(new GRect(((getWidth()-(DOOR_SIZE/2))/2),((getHeight()-DOOR_SIZE)/2+(DOOR_SIZE/2)),DOOR_SIZE/2,DOOR_SIZE)); add(new GRect(((getWidth()-(DOOR_SIZE/2))/2-90),((getHeight()-(DOOR_SIZE/2))/2),(DOOR_SIZE/2),(DOOR_SIZE/2))); add(new GRect(((getWidth()-(DOOR_SIZE/2))/2+90),((getHeight()-(DOOR_SIZE/2))/2),(DOOR_SIZE/2),(DOOR_SIZE/2))); add(new GLine(((getWidth()+HOUSE_SIZE)/2),((getHeight()+(ROOF_SIZE-HOUSE_SIZE))/2),(getWidth()/2),(getHeight()-(HOUSE_SIZE*2)+ROOF_SIZE))); add(new GLine(((getWidth()-HOUSE_SIZE)/2),((getHeight()+(ROOF_SIZE-HOUSE_SIZE))/2),(getWidth()/2),(getHeight()-(HOUSE_SIZE*2)+ROOF_SIZE))); add(new GLine((getWidth()/2-(HOUSE_SIZE/4)-15),((getHeight()/2)+DOOR_SIZE/4),(getWidth()/2-(HOUSE_SIZE/4)-15),((getHeight()/2)-DOOR_SIZE/4))); add(new GLine((getWidth()/2+(HOUSE_SIZE/4)+15),((getHeight()/2)-DOOR_SIZE/4),(getWidth()/2+(HOUSE_SIZE/4)+15),((getHeight()/2)+DOOR_SIZE/4))); add(new GOval(((getWidth()-DOOR_HANDLE)/2+(DOOR_SIZE/6)),(getHeight()/2+(DOOR_SIZE/2)),DOOR_HANDLE,DOOR_HANDLE)); } /*Constant*/ private static final int HOUSE_SIZE = 300; private static final int ROOF_SIZE = 200; private static final int DOOR_SIZE = 100; private static final int DOOR_HANDLE = 7; }