Define a class, Window, that implements the GUIComponent interface, and has the following members

Define a class, Window, that implements the GUIComponent interface, and has the following members

LANGUAGE: Java

CHALLENGE:

Assume the existence of an interface, GUIComponent with the following methods:
– open and close: no parameters, returns boolean
– move and resize: accepts two integer parameters and returns void

Define a class, Window, that implements the GUIComponent interface, and has the following members:
– width, height, xPos, and yPos integer instance variables , with xPos and yPos initialized to 0
– a constructor that accepts two integer variables (width followed by height) which are used ti initialize the width and height instance variables
– An implementation of open: that sends “Window opened” to System.out, and returns true
– An implementation of close that sends “Window closed” to System.out, and returns true
– An implementation of resize that modifies the width and height variables to reflect the specified size
– An implementation of move that modifies xPos and yPos to reflect the new position

SOLUTION:

public class Window implements GUIComponent{
    private int width;
    private int height;
    private int xPos = 0;
    private int yPos = 0;
    public Window(int width, int height){
        this.width = width;
        this.height = height;
    }
    public boolean open(){
        System.out.println("Window opened");
        return true;
    }
    public boolean close(){
        System.out.println("Window closed");
        return true;
    }
    public void resize(int width, int height){
        this.width = width;
        this.height = height;
    }
    public void move(int xPos, int yPos){
        this.xPos = xPos;
        this.yPos = yPos;
    }
}

Add comment