Uploaded by caramel cinnamon

Comp200 SI Worksheet 7 - Suggested Solution

advertisement
Comp200 SI Worksheet 7
Question One
1. State whether the following are True or False. If False, provide a reason for your answer.
a. An interface can extend another interface.
True.
b. Fields in an interface can be public and non-static.
False.
All fields should be static and final. All fields are implicitly public, static, final
c. Since an interface can be used as a datatype, we can define a constructor for each
interface.
False.
We are not allowed to define constructors for an interface. Furthermore, each field in an
interface is static and final, making it a constant, thereby eliminating the need for field
initialization.
d. It is legal to define an interface as follows: public abstract interface MyInterface
True.
e. A class may implement one interface but extend multiple classes.
False.
A class can only extend ONE class. A class may implement MANY interfaces.
f.
If we implement an interface in an abstract class without implementing all of its
methods, then the program will still compile.
True
g. If we implement an interface in class without implementing all of its methods, then the
program will still compile.
False.
A class that implements an interface should implement all of its methods. If the class is abstract,
then you don’t have to implement all of the methods
Question Two
1. Consider the abstract class below. Will the following classes successfully extend the abstract
class? State the reason for your answer.
ABSTRACT CLASS
import java.awt.Graphics;
public abstract class VisibleShape{
private int x, y;
public VisibleShape(int x, int y){
this.x = x;
this.y = y;
}
public int getX(){return this.x;}
public int getY(){return this.y;}
public abstract double getArea();
public abstract void draw(Graphics g);
}
(A)
import java.awt.Graphics;
public class Square extends VisibleShape{
public int sl; // side length
public Square(int sideLength){
super();
this.sl = sideLength;
}
public void draw(Graphics g){
g.drawRect(super.x, super.y, this.sl, this.sl);
}
}
Errors:
1. Class does not provide an implementation for the area method. It should provide an
implementation or the classs should be abstract.
2. super() - the super class does not have a no arguement/default constructor defined.
3. super.x, super.y - x and y are private attributes in the super class. You cannot access them
directly. Use the get methods. super.getX() and super.getY()
(B)
public abstract class Circle extends VisibleShape{
public Circle(int x, int y){
super(x, y);
}
}
Will work as intended. Since the class is abstract, it does not have to provide method
implementations for each of the abstract methods in the super class.
Question Three
Consider the Interface and classes below and answer the questions that follow.
public interface PlayerMovement{
void moveLeft();
void moveRight();
void fall();
void jump();
}
public class MovablePlayer implements PlayerMovement{
private int x, y;
public void moveLeft(){x--;}
public void moveRight(){x++;}
public void fall(){y = y - 5;}
public void jump(){
y = y +5;
fall();
}
}
1. After defining the classes, we decide that we would like to take a gravity effect into
consideration when the player jumps and falls. How would we add this field to the
PlayerMovement interface?
double GRAVITY = 9.8;
// implicity public static final double GRAVITY = 9.8;
Note: Saying “double gravity;” would be incorrect. All constants [final] need to have a value assigned to
them.
2. Suppose we want to add a method header to the PlayerMovement class. void slide(); How
would we add this method into the MovablePlayer class.
public void slide() { //code here}
3.
What would be the consequence of using the protected keyword on the moveRight method in
the MovablePlayer class.
The class would not compile. Protected is more restrictive than public. This is not allowed and we would
get a compiler error.
Download