Docsity

Calculating Fencing Needs for Rectangular Yards: An Object-Oriented Solution - Prof. Irene, Study notes of Information Technology

Toggle sidebar
bg1
IT 108
April 9, 2009
Problem: Create an OO solution to calculate the amount of fencing needed
to surround a rectangular yard. The minimum dimensions the company can
handle is 500 feet.
800*800 = 64000 stops working correctly
oWhat to limit any rectangle to 800
Instance Variables
oGets its very own copy of everything in the class
oRectangle yard = new Rectangle();
(+)setlenght(int):boolean
(+)setWidth(int):boolean
Process
Activity involves of the values in one object in order to solve the
problem
Anything that has multiple job is taken care of
Rectangle Code from Last Class
public class RectangleClassDef
{
private int length;
private int width;
public int getLength() {return length;}
Creates
Instance
and calls
construct
or
yard
o
Construct
Validatin
g
getLength()
getWidth() getPerimeter()
getArea()
Rectangle()
setLength() setWidth()
pf3
pf4

Partial preview of the text

Download Calculating Fencing Needs for Rectangular Yards: An Object-Oriented Solution - Prof. Irene and more Study notes Information Technology in PDF only on Docsity!

April 9, 2009 Problem : Create an OO solution to calculate the amount of fencing needed to surround a rectangular yard. The minimum dimensions the company can handle is 500 feet.  800*800 = 64000  stops working correctly o What to limit any rectangle to 800  Instance Variables o Gets its very own copy of everything in the class o Rectangle yard = new Rectangle(); (+)setlenght(int):boolean (+)setWidth(int):boolean Process  Activity involves of the values in one object in order to solve the problem  Anything that has multiple job is taken care of Rectangle Code from Last Class public class RectangleClassDef { private int length; private int width; public int getLength() {return length;} Creates Instance and calls construct or yard o Construct Validatin g getLength() getWidth() getPerimeter() getArea() Rectangle() setLength() setWidth()

April 9, 2009 public int getWidth() {return width;} public void setLength(int l) {length = l;} public void setWidth (int w) { width = w;} public int perimeter () {return 2 * length + 2* width;} public int area () {return length * width;} } Items Purpose (-) length: int Length of rectangle (-) width: int Width of rectangle (+) getLenth(): int Return value of length (+)getWidth(): int Return value of width (+)setLength(int) : void Accept a value and without validation, assign it to length (+)setWidth(int): void Accept a value and without validation, assigned it to width (+)perimeter(): int Return the perimeter (2L + 2W) of the rectangle (+) area(): int Return the area (LW) of the rectangle Changes to Class Code class Rectangle { private int length; private int width; public int getLength () {return length;} public int getWidth () {return width;} public Boolean setLength(int L) if (L > 0 && L < 800) { length = L; return true; } else return false; w = input from user width = dimension of valid rectangle

April 9, 2009 } Changes to Class Code (Create and Fill an Object) import javax.swing.JOptionPane; public class Fence { public static void main (String [ ] args) { Rectangle myYard; input ( ); JOptionPane.showMessageDialog(null, “A yard with length: yard.getLength ( ) + “feet and width:” + yard.getWidth ( ) + “ feet requires ” + “ feet requires” + yard.permiter ( ) + “feet of fencing ”): } private static void input () { rectangle yard ; int yardLength, yardWidth; do{ yardLength = Interger.paraseInt(JOptionPane.showInputDialog(“Enter length of yard: ”)); } while (!yard.setLength(yardLength)); yardWidth = Interger.paraseInt(JOptionPane.showInputDialog(“Enter width of yard: ”)); while (!yard.setLength(yardWidth)); yardWidth = Interger.paraseInt(JOptionPane.showInputDialog(“ERROR: Enter width of yard: ”)); } } Due Dates  Project will be due a week after due date  Phrase One: class code and  Phrase Two: use an array of objects  Phrase Three: