Post date: Feb 09, 2016 9:35:17 AM
/!\ Quiz 1 on Tuesday
Chapter 13: Interface and Inner Classes
13.1 Interfaces
13.2 Simple Uses of Inner Classes
Lab Exercises:
Ex 1:
click to enlarge
Suppose that we have a set of objects with some common behaviors: they could move up, down, left or right. The exact behaviors (such as how to move and how far to move) depend on the objects themselves. One common way to model these common behaviors is to define an interface called Movable, with abstract methods moveUp(),moveDown(), moveLeft() and moveRight(). The classes that implement the Movable interface will provide actual implementation to these abstract methods.
Let's write two concrete classes - MovablePoint and MovableCircle - that implement the Movable interface.
The code for the interface Movable is straight forward.
public interface Movable { // saved as "Movable.java" public void moveUp(); ...... }
For the MovablePoint class, declare the instance variable x, y, xSpeed and ySpeed with package access as shown with '~' in the class diagram (i.e., classes in the same package can access these variables directly). For the MovableCircle class, use a MovablePoint to represent its center (which contains four variable x, y, xSpeed andySpeed). In other words, the MovableCircle composes a MovablePoint, and its radius.
public class MovablePoint implements Movable { // saved as "MovablePoint.java" // instance variables int x, y, xSpeed, ySpeed; // package access // Constructor public MovablePoint(int x, int y, int xSpeed, int ySpeed) { this.x = x; ...... } ...... // Implement abstract methods declared in the interface Movable @Override public void moveUp() { y -= ySpeed; // y-axis pointing down for 2D graphics } ...... }
public class MovableCircle implements Movable { // saved as "MovableCircle.java" // instance variables private MovablePoint center; // can use center.x, center.y directly // because they are package accessible private int radius; // Constructor public MovableCircle(int x, int y, int xSpeed, int ySpeed, int radius) { // Call the MovablePoint's constructor to allocate the center instance. center = new MovablePoint(x, y, xSpeed, ySpeed); ...... } ...... // Implement abstract methods declared in the interface Movable @Override public void moveUp() { center.y -= center.ySpeed; } ...... }
After overridding the methods toString() in both classes MovablePoint and MovableCircle , write a test program and try out these statements:
Movable m1 = new MovablePoint(5, 6, 10, 5); // upcast System.out.println(m1); m1.moveLeft(); System.out.println(m1); Movable m2 = new MovableCircle(2, 1, 2, 20, 20); // upcast System.out.println(m2); m2.moveRight(); System.out.println(m2);
Ex 2:
click to enlarge
Write a new class called MovableRectangle, which composes two MovablePoints (representing the top-left and bottom-right corners) and implementing the Movable interface. Make sure that the two points have the same speed.
What is the difference between an interface and an abstract class?
Source: Adapted from Java Programming Tutorial