21.08.2013 Views

Software Engineering for Students A Programming Approach

Software Engineering for Students A Programming Approach

Software Engineering for Students A Programming Approach

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

204 Chapter 15 ■ Object-oriented programming<br />

><br />

><br />

}<br />

public Alien(int newX, int newY, int newSize) {<br />

x = newX;<br />

y = newY;<br />

size = newSize;<br />

alienImage = new ImageIcon("c:/alien.jpg");<br />

}<br />

public void moveLeft(int amount) {<br />

x = x - amount;<br />

}<br />

public void moveRight(int amount) {<br />

x = x + amount;<br />

}<br />

public void display(JPanel panel) {<br />

Graphics paper = panel.getGraphics();<br />

alienImage.paintIcon(panel, paper, x, y);<br />

}<br />

public int xCoord {<br />

get {<br />

return x;<br />

}<br />

set {<br />

x = value;<br />

}<br />

}<br />

The header <strong>for</strong> a property looks similar to a method header, except that there are<br />

no brackets to specify parameters. The property consists of two complementary components<br />

– one has get as the heading and the other has set as the heading. The get<br />

part is like a function method – it returns the desired value. The set part is like a<br />

method – it assigns the value using the special keyword value as shown. Note that<br />

this code is in the style of Java, but it is not Java, since Java does not support a property<br />

mechanism.<br />

If we only need a way of viewing a property (but not changing its value), we write<br />

the property declaration like this:<br />

public int xCoord {<br />

get {<br />

return x;<br />

}<br />

}<br />

>

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!