You are on page 1of 105

SD2054

Software Development
(c) Aaron Kans
Hit
START

(c) Aaron Kans


(c) Aaron Kans
start( )

(c) Aaron Kans


start( )

Hit
START

(c) Aaron Kans


right( )
Hit
Hit
RIGHT
SQUARE

(c) Aaron Kans


Hit
CIRCLE

(c) Aaron Kans


Hit
CIRCLE

(c) Aaron Kans


Hit
TRIANGLE

(c) Aaron Kans


Hit
TRIANGLE

(c) Aaron Kans


Hit
CROSS

(c) Aaron Kans


(c) Aaron Kans
GAME
OVER

(c) Aaron Kans


Playable
left()
right()
start()
circle()
square()
triangle()
cross()

(c) Aaron Kans


Interface and Adapter Classes

Learning Objectives

• explain the purpose of an interface class;


• create your own interfaces in Java;
• make use of adapters in programs;
• explain the purpose of the toString method.

(c) Aaron Kans


Abstract classes
and methods…

(c) Aaron Kans


Shape
abstract Shape
abstract draw() We will never need
abstract methods to create Shape
must be objects
overridden in
subclasses

Circle Triangle
draw() draw()

What if we definitely want to be able to draw


shapes like Circle and Triangle?

(c) Aaron Kans


Java code….

(c) Aaron Kans


public abstract class Shape
{
// other attributes and methods here

abstract public void draw();


}

public class Circle extends Shape


{
// other attributes and methods here
public void draw()
{
// code for this method goes here
}
} (c) Aaron Kans
Inheritance and Types …

(c) Aaron Kans


Shape

3DCircle
is aiskind
CircleObject a kind of Shape
of Shape
type
Circle c1 = new Circle ( );
Shape
Circle

3DCircle
Objectis a kind of Circle
type
3DCircle Shape c3d = new 3DCircle ( );
Circle
3DCircle

(c) Aaron Kans


public class RunStatusTester
{
public static void main(String[ ] args)
{
Circle c = new Circle ( );
Triangle t = new Triangle ( );
myMethod( c );
myMethod( t );
}

private static void myMethod(Shape shapeIn)


{
shapeIn.draw();
} A bit like a
} contract
(c) Aaron Kans
How do I make my
class a kind of Shape if
it is already inheriting
from another class?

(c) Aaron Kans


AnotherClass Shape

draw()

SomeClass

(c) Aaron Kans


AnotherClass Shape

draw()

In Java you
cannot inherit
SomeClass from more than 1
class

(c) Aaron Kans


AnotherClass

SomeClass

(c) Aaron Kans


As long as the Shape
class has no
attributes or methods,
turn into an interface.

(c) Aaron Kans


AnotherClass Shape

draw()

SomeClass

(c) Aaron Kans


AnotherClass <<interface>>
Shape
draw()

SomeClass

(c) Aaron Kans


• An interface is a class that just contains abstract
methods.

public interface Shape


{
public void draw ( );
}

An interface
is like a
contract

(c) Aaron Kans


If an interface class can
be thought of as a
contract, how do you
meet that contract?

You meet a contract


by implementing the
methods given in the
interface.

(c) Aaron Kans


public SomeClass implements Shape
{
// attributes and methods here

public void draw ( )


{
// code goes here
}
}

(c) Aaron Kans


A class may implement
many interfaces….

(c) Aaron Kans


<<interface>> <<interface>>
Shape ActionListener
draw() actionPerformed (ActionEvent e)

SomeClass

(c) Aaron Kans


Java code….

(c) Aaron Kans


public SomeClass implements ActionListener , Shape
{
// attributes and methods here

public void actionPerformed (ActionEvent e)


{
// code goes here
}

public void draw ()


{
// code goes here
}
}

(c) Aaron Kans


Interfaces and types…

(c) Aaron Kans


<<interface>> <<interface>>
Shape ActionListener
draw() actionPerformed (ActionEvent e)

SomeClass

An object is the type of its class and


the type of any interfaces implemented
by that class.
(c) Aaron Kans
<<interface>> <<interface>>
Shape ActionListener
draw() actionPerformed (ActionEvent e)

SomeClass

SomeClass obj = new SomeClass ();

(c) Aaron Kans


<<interface>> <<interface>>
Shape ActionListener
draw() actionPerformed (ActionEvent e)

SomeClass

SomeClass obj = new SomeClass ();

(c) Aaron Kans


<<interface>> <<interface>>
Shape ActionListener
draw() actionPerformed (ActionEvent e)

SomeClass

ActionListener obj = new SomeClass ();

(c) Aaron Kans


<<interface>> <<interface>>
Shape ActionListener
draw() actionPerformed (ActionEvent e)

SomeClass

Shape obj = new SomeClass ();

(c) Aaron Kans


Creating your own interface
classes….

(c) Aaron Kans


TestingApp

(c) Aaron Kans


TestingApp

(c) Aaron Kans


TestingApp

(c) Aaron Kans


TestingApp

(c) Aaron Kans


TestingApp

(c) Aaron Kans


It would be good if every object given
to TestingApp had a check method.

(c) Aaron Kans


This check method should return a
boolean value indicating if the object
is valid.

(c) Aaron Kans


Turn this into a contract by creating
an interface!

(c) Aaron Kans


We will call the interface Checkable.

(c) Aaron Kans


We will call the interface Checkable.

public interface Checkable


{

// check method goes here

(c) Aaron Kans


We will call the interface Checkable.

public interface Checkable


{

public boolean check( );

(c) Aaron Kans


We will call the interface Checkable.
Remember –
interface methods
public interface Checkable are abstract so have
{ no code!

public boolean check( );

(c) Aaron Kans


Now let’s make some classes
implement the Checkable interface.

(c) Aaron Kans


Let’s look at the Oblong class.

(c) Aaron Kans


<<interface>>
Checkable
check(): boolean

Oblong

length: double
height: double

// methods

(c) Aaron Kans


<<interface>>
Checkable
check(): boolean

Oblong

length: double
height: double

// methods
check(): boolean
(c) Aaron Kans
public class Oblong
{
private double length;
private double height;

// other methods here as before

(c) Aaron Kans


public class Oblong
{
private double length;
private double height;

// other methods here as before

public boolean check()


{
// code here
}

}
(c) Aaron Kans
public class Oblong
{
private double length;
private double height;

// other methods here as before

public boolean check()


{
return length > 0 && height > 0;
}

}
(c) Aaron Kans
public class Oblong implements Checkable
{
private double length;
private double height;

// other methods here as before

public boolean check()


{
return length > 0 && height > 0;
}

}
(c) Aaron Kans
<<interface>>
Checkable
check(): boolean

Robot

xValue: int
yValue: int

// methods

(c) Aaron Kans


<<interface>>
Checkable
check(): boolean

Robot

xValue: int
yValue: int

// methods
check(): boolean
(c) Aaron Kans
TestingApp…

(c) Aaron Kans


public class TestingApp
{
public static void main(String[ ] args)
{
// create objects to test here and send to testObject
}

private static void testObject( ? objectIn)


{
if( objectIn.check() )
{
System.out.println("Valid object");
}
else
{
System.out.println("Invalid object");

}
}
} (c) Aaron Kans
public class TestingApp
{
public static void main(String[ ] args)
{
// create objects to test here and send to testObject
}

private static void testObject( Checkable objectIn)


{
if( objectIn.check() )
{
System.out.println("Valid object");
}
else
{
System.out.println("Invalid object");

}
}
} (c) Aaron Kans
public class TestingApp
{
public static void main(String[ ] args)
{
// create objects to test here and send to testObject
}

private static void testObject( Checkable objectIn)


{
// call the check method
}
}

(c) Aaron Kans


public class TestingApp
{
public static void main(String[ ] args)
{
Oblong oblong1 = new Oblong(0, 8.2);
Oblong oblong2 = new Oblong(10.5, 8.75);
Robot robot1 = new Robot(5, 120);
Robot robot2 = new Robot(10, 20);

private static void testObject( Checkable objectIn)


{
// call the check method
}
}

(c) Aaron Kans


public class TestingApp length
{ should not
public static void main(String[ ] args) be 0
{
Oblong oblong1 = new Oblong(0, 8.2);
Oblong oblong2 = new Oblong(10.5, 8.75);
Robot robot1 = new Robot(5, 120);
Robot robot2 = new Robot(10, 20);

private static void testObject( Checkable objectIn)


{
// call the check method
}
}

(c) Aaron Kans


public class TestingApp
{ yValue
public static void main(String[ ] args) should not be
{ greater than
Oblong oblong1 = new Oblong(0, 8.2); 100
Oblong oblong2 = new Oblong(10.5, 8.75);
Robot robot1 = new Robot(5, 120);
Robot robot2 = new Robot(10, 20);

private static void testObject( Checkable objectIn)


{
// call the check method
}
}

(c) Aaron Kans


public class TestingApp
{
public static void main(String[ ] args)
{
Oblong oblong1 = new Oblong(0, 8.2);
Oblong oblong2 = new Oblong(10.5, 8.75);
Robot robot1 = new Robot(5, 120);
Robot robot2 = new Robot(10, 20);
testObject(oblong1);
testObject(oblong2);
testObject(robot1);
testObject(robot2);
}

private static void testObject( Checkable objectIn)


{
// call the check method
}
}
(c) Aaron Kans
Invalid object
Valid object
Invalid object
Valid object

(c) Aaron Kans


The RedCircle program

(c) Aaron Kans


Keep trying!!!

(c) Aaron Kans


The MouseMotionListener and
MouseListener interfaces …..

(c) Aaron Kans


The methods of MouseMotionListener

mouseMoved Specifies the behaviour that occurs


when the mouse is moved when no
button is depressed.

Specifies the behaviour that occurs


mouseDragged when the mouse is moved with the
left-hand button depressed.

(c) Aaron Kans


The methods of MouseListener

Specifies the behaviour that occurs


mousePressed when the left-hand button is pressed.

mouseReleased Specifies the behaviour that occurs


when the left-hand button is released.

Specifies the behaviour that occurs when the


mouseClicked left-hand button is clicked on a component.

Specifies the behaviour that occurs when


mouseEntered the cursor enters a component.

mouseExited Specifies the behaviour that occurs when


the cursor leaves a component.

(c) Aaron Kans


The code for the RedCircle class

(c) Aaron Kans


import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class RedCircle extends JFrame


implements MouseMotionListener, MouseListener
{
private int xPos;
private int yPos;
private int width;
private int height;
private boolean mouseDown;

(c) Aaron Kans


public RedCircle(int widthIn, int heightIn)
{
setTitle("Red Circle Game");
addMouseMotionListener(this);
addMouseListener(this);
width = widthIn;
height = heightIn;
xPos = width/2 -20;
yPos = height/2 - 20;
setSize(width, height);
setLocation(300,300);
setVisible(true);
}

(c) Aaron Kans


public void paint(Graphics g)
{
super.paint(g);
g.clearRect(0,0, width, height);
g.drawString("Click on the red circle", width/2 - 60, 50);
g.setColor(Color.red);
g.fillOval(xPos,yPos,20,20);

if(mouseDown)
{
g.drawString("Keep trying!!!", width/2 - 40, height -10);
}
}

(c) Aaron Kans


public void mouseMoved(MouseEvent e)
{
xPos = e.getX() - 50;
yPos = e.getY() - 50;
repaint();
}

public void mouseDragged(MouseEvent


e)
{
xPos = e.getX() - 50;
yPos = e.getY() - 50;
repaint();
}
(c) Aaron Kans
public void mousePressed(MouseEvent e)
{
mouseDown = true;
repaint();
}

public void mouseReleased(MouseEvent e)


{
mouseDown = false;
repaint();
}

(c) Aaron Kans


public void mouseClicked(MouseEvent e)
{

public void mouseEntered(MouseEvent e)


{

public void mouseExited(MouseEvent e)


{

}
}
(c) Aaron Kans
It’s a bit annoying to
have to write dummy
methods to meet an
interface. Is there an
easier way?

Yes! Just use an


Adapter class if you
can.

(c) Aaron Kans


<<interface>>
MouseListener
mousePressed (MouseEvent e)
mouseReleased (MouseEvent e)
mouseClicked (MouseEvent e)
mouseEntered (MouseEvent e)
mouseExited (MouseEvent e)

We have to
provide code for 5
methods above.

SomeClass

(c) Aaron Kans


<<interface>>
MouseListener
mousePressed (MouseEvent e)
mouseReleased (MouseEvent e)
mouseClicked (MouseEvent e)
mouseEntered (MouseEvent e)
mouseExited (MouseEvent e)

This class
implements
MouseListener by
providing 5 dummy
methods. MouseAdapter

(c) Aaron Kans


<<interface>>
MouseListener
mousePressed (MouseEvent e)
mouseReleased (MouseEvent e)
mouseClicked (MouseEvent e)
mouseEntered (MouseEvent e)
mouseExited (MouseEvent e)

MouseAdapter
No we just inherit
from the adapter
and override the
methods we need
SomeClass
to.
mousePressed
mouseReleased
(c) Aaron Kans
public class MouseAdapter implements MouseListener
{
public void mousePressed(MouseEvent e)
{
}
public void mouseReleased(MouseEvent e)
{
}
public void mouseClicked(MouseEvent e)
{
}
public void mouseEntered(MouseEvent e)
{
}
public void mouseExited(MouseEvent e)
{
}
}
(c) Aaron Kans
Just before we finish….

…The toString method

(c) Aaron Kans


Object
toString(): String

BankAccount

(c) Aaron Kans


public class RunAccount
{
public static void main(String[ ] args)
{
BankAccount acc1 = new BankAccount("001", "Sarah Patel");
System.out.println (acc1.getAccountNumber());
System.out.println (acc1.getAccountName());
System.out.println (acc1.getBalance());

}
}

(c) Aaron Kans


001
Sarah Patel
0.0

(c) Aaron Kans


public class RunAccount
{
public static void main(String[] args)
{
BankAccount acc1 = new BankAccount("001", "Sarah Patel");
System.out.println (acc1.getAccountNumber());
System.out.println (acc1.getAccountName());
System.out.println (acc1.getBalance());

}
}

(c) Aaron Kans


public class RunAccount
{
public static void main(String[ ] args)
{
BankAccount acc1 = new BankAccount("001", "Sarah Patel");
System.out.println (acc1.toString());
}
}

(c) Aaron Kans


public class RunAccount
{
public static void main(String[ ] args)
{
BankAccount acc1 = new BankAccount("001", "Sarah Patel");
System.out.println (acc1);
}
} Automatically
calls toString

(c) Aaron Kans


BankAccount@458990

(c) Aaron Kans


Object
toString(): String

BankAccount

toString(): String

(c) Aaron Kans


public String toString()
{
return "Account Number: "
+ accountNumber
+ "\nAccount Name: "
+ accountName
+ "\nCurrent Balance: "
+ balance
+ "\n";
}

(c) Aaron Kans


public class RunAccount
{
public static void main(String[ ] args)
{
BankAccount account1 = new BankAccount("001", "Sarah Patel");
BankAccount account2 = new BankAccount("002", "Robinder Grewel");
System.out.println (account1);
System.out.println (account2);
}
}

RUN
(c) Aaron Kans
Account Number: 001
Account Name: Sarah Patel
Current Balance: 0.0

Account Number: 002


Account Name: Robinder Grewel
Current Balance: 0.0

RUN
(c) Aaron Kans
(c) Aaron Kans

You might also like