Title: ACE 1050 Design Computing
1ACE 1050 Design Computing
- Tutorial 4
- 8thFebruary, 2007
2Overview
- Course homepage
- http//www.acae.cuhk.edu.hk/ace1050
- Course newsgroup
- news//news.erg.cuhk.edu.hk/cuhk.acae.ace1050
- Java Class Structure
- instance variable
- constructor
- mutator
- accessor
- method
3Custom Class Java Programming
- Object-oriented programming
- Create the default object with different
variables - Manipulate objects with attributes and behaviors
- Advantages
- Simple to control and access specific behaviors
- Can be reused for different situations
4Class Structure
- instance variable, method (attribute, behavior)
- constructor, mutator, accessor
5Demo SnakeSnake Class
- demonstrate the java program to interactive with
the Class (SnakeSnake)
6Class structure (step 1)
- declare the SnakeSnake Class
- declare the instance variables
import javax.swing. import java.awt. public
class SnakeSnake // instance variables to
describe object attributes private int width //
rectangle width private int height //
rectangle height private int x // rectangle
x-coordinate position private int y //
rectangle y-coordinate position private JFrame
window // window displaying rectangle private
Color color // color of rectangle //
constructors, mutators, accessors, methods
7Class structure (step 2)
- declare the default constructor (only execute
once at the beginning) - prepare the default variables
- constructor name must be as the class name
public class SnakeSnake //
snakesnake() default constructor public
SnakeSnake() window new JFrame("snake
snake") window.setSize(500, 500) width
20 height 20 x 230 y 230 color
Color.BLUE window.setVisible(true) // other
constructors, mutators, accessors, methods
8Class structure (step 3)
- declare the mutators
- change specific variable individually
public class SnakeSnake // setX() x
mutator public void setX(int ulx) x
ulx // setY() y mutator public void setY(int
uly) y uly //other constructors,
accessors, methods
No return function
Instance Variables
New Input
9Class structure (step 4)
- declare the accessors
- return instance variable individually
public class SnakeSnake // getX() x
accessor public int getX() return x //
setY() y accessor public int getY() return
y // getColor() color accessor public Color
getColor() return color //other
constructors, accessors, methods
accessor
Return instance variable
Return variable type
10Class structure (step 5)
- declare the other constructor - public classname
(int, char, String,) - update the all class variables when requested
- constructor name must be as the class name
public class SnakeSnake //
snakesnake() specific constructor public
SnakeSnake(int w, int h, int ulx, int uly, JFrame
f, Color c) setWidth(w) setHeight(h) setX(u
lx) setY(uly) setWindow(f) setColor(c) //
methods
11Class structure (step 6)
- declare the method
- Perform specific task (just like simple normal
function)
public class SnakeSnake // method
paint() public void paint() Graphics g
window.getGraphics() g.setColor(color) g.fillR
ect(x, y, width, height)
Method name
Display the rectangle with the instance variable
of the class
A complete/full class should combine with
instance variables, constructors, mutators,
accessors, methods.
12Class Usage
- use the custom class (SnakeSnake)
- SnakeSnake r new SnakeSnake()
- declare the class
- use the class constructor
- SnakeSnake(int w, int h, int ulx, int uly, JFrame
f, Color c) - update all instance variables
- use the class mutator
- r.setX(int ulx)
- change individual instance variable
- use the class accessor
- obtainY r.gety()
- return individual instance variables to the
external program - use the class method
- r.paint()
- perform some tasks
13Class Usage(e.g. By another Java program)
- Java program demo Obtain user input and
interactive with the class (SnakeSnake)
public class MovingSnake while( UserInputC
! '5' ) // check if exit the
program UserInputC System.in.read() if
(UserInputC '6') // move right r.update(
r.getX() 5 , r.getY() ) System.out.println("Cur
rent position at " r.getX() " , "
r.getY()) else if (UserInputC '4') //
move left r.update( r.getX() - 5, r.getY()
) System.out.println("Current position at "
r.getX() " , " r.getY()) else // nothing
to do with other buttons r.paint() System.
out.println("\nProgram closed!")
14Class structure(custom new method)
- declare the method - update(int newx, int newy)
- label the old path and highlight the head
public class SnakeSnake public void
update(int newx, int newy) Graphics
gwindow.getGraphics() // label the original
position as path by changing the color to
red setColor(Color.RED) g.setColor(color) g.fill
Rect(x,y,width,height) // update the new
position setX(newx) setY(newy) // change back
to color(blue) and label the new position on the
screen. setColor(Color.BLUE) g.setColor(color) g
.fillRect(x,y,width,height)
15Summary
- Java class declaration
- A complete/full class combines instance
variables, constructors, mutators, accessors and
methods. - External program to use the class
- declare the object to the class - e.g. newobject
IamNew new newobject() - use the class - e.g. IamNew.SomeMethod()
- Exercise
- Try to download the demo. program in course
homepage - Make the rectangle box to be moved to 4 different
direction by the key 2, 4, 6, 8 - hint add the if cases in the MovingSnake.java
for other input, or by appending else if block - Make a class method to scale the rectangle box
- hint define a function in SnakeSnake.java to
interact with scaling mutators - Make a class method to change the color rectangle
box randomly - hint 1 define a function in SnakeSnake.java to
access the setColor mutator - hint 2 modify the updating mutators if want to
keep the generated color after movement scaling