160 likes | 269 Vues
This document details the implementation of a `Square` class in Java, featuring instance variables for coordinates, color, and length. The class includes a constructor for initializing properties, and methods for drawing, erasing, and moving the square. It also offers getters and setters for accessing and modifying private attributes, ensuring encapsulation. This approach allows for effective manipulation of the square's properties and representation in a graphical context, making it a foundational piece in object-oriented programming and graphical applications.
E N D
publicclass Square { <instance variables> <constructors> <methods> }
public class Square { doublexCoord; doubleyCoord; Colorcolor; double length; …. }
public class Square { … publicSquare(double x, double y, doublelen, double c) { xCoord = x; yCoord = y; length = len; color = c; } … }
public class Square { … publicvoiddraw() { … } publicvoiderase() { … } publicvoidmove(doubledx, doubledy) { … } }
implementation hiding sq Square(x,y,len,color) draw() erase() move(dx, dy) API
private variables No one but you can touch your private parts
public class Square { privatedoublexCoord; privatedoubleyCoord; privateColor color; privatedouble length; …. }
getters getColor() getX() getY() getLength()
public class Square { … publicdoublegetX() { returnxCoord; } publicdoublegetY() { returnyCoord; } publicColorgetColor() { return color; } publicdoublegetLength() { returnlen; } }
setters setColor() setPos() setLength()
public class Square { … publicdoublesetPos(doublex, doubley) { xCoord = x; yCoord = y; } publicdoublesetColor(Colorc) { … } publicdoublesetLength(doublelen) { … } }
publicclass Circle { <instance variables> <constructors> <methods> }