You probably are fond of games. This quiz though is not that complicated. You are simply going to work with a point on a screen, and a circle on a screen as well. And let's consider them as the characters in the game. We are going to call both of these characters as Movable objects. Objects are said to be Movable if they can be moved around the screen. The following simple movements are allowed:
public void moveLeft(); // moves the x to the left by 1 unit
public void moveRight(); // moves the x to the right by 1 unit
public void moveUp(); // moves the y 1 unit upward
public void moveDown(); // moves the y 1 unit downward
public void display(); // prints all the info about
import java.util.Scanner;
interface Movable {
public void moveLeft(); // moves the x to the left by 1 unit
public void moveRight(); // moves the x to the right by 1 unit
public void moveUp(); // moves the y 1 unit upward
public void moveDown(); // moves the y 1 unit downward
public void display(); // prints all the info about a Movable
}
class Point implements Movable {
public static final int MIN_CHAR = 0;
public static final int MAX_CHAR = 200;
public int x;
public int y;
public Point() {
}
@Override
public void moveLeft() {
if (x > MIN_CHAR) {
x--;
}
}
@Override
public void moveRight() {
if (x < MAX_CHAR) {
x++;
}
}
@Override
public void moveUp() {
if (y > MIN_CHAR) {
y--;
}
}
@Override
public void moveDown() {
if (y < MAX_CHAR) {
y++;
}
}
@Override
public void display() {
System.out.print(toString());
}
public String toString() {
return "Point: (" + x + ", " + y + ")";
}
}
class Circle implements Movable {
public Point point;
public int radius;
public Circle() {
point = new Point();
}
@Override
public void moveLeft() {
if ((point.x + radius) > Point.MIN_CHAR) {
point.moveLeft();
}
}
@Override
public void moveRight() {
if ((point.x + radius) < Point.MAX_CHAR) {
point.moveRight();
}
}
@Override
public void moveUp() {
if ((point.y + radius) > Point.MIN_CHAR) {
point.moveUp();
}
}
@Override
public void moveDown() {
if ((point.y + radius) < Point.MAX_CHAR) {
point.moveDown();
}
}
@Override
public void display() {
System.out.print(toString());
}
public String toString() {
return "Circle: " + point.toString() + ", " + radius;
}
}
class App {
public static void main(String[] args) {
Scanner keyBoard = new Scanner(System.in);
int t = keyBoard.nextInt();
Movable movableObj = null;
if (t == 1) {
movableObj = new Point();
((Point) movableObj).x = keyBoard.nextInt();
((Point) movableObj).y = keyBoard.nextInt();
}
if (t == 2) {
movableObj = new Circle();
((Circle) movableObj).point.x = keyBoard.nextInt();
((Circle) movableObj).point.y = keyBoard.nextInt();
((Circle) movableObj).radius = keyBoard.nextInt();
}
int m = keyBoard.nextInt();
for (int i = 0; i < m; i++) {
int ch = keyBoard.nextInt();
if (ch == 1) {
movableObj.moveLeft();
} else if (ch == 2) {
movableObj.moveRight();
} else if (ch == 3) {
movableObj.moveUp();
} else if (ch == 4) {
movableObj.moveDown();
}
}
movableObj.display();
keyBoard.close();
}
}
Comments
Leave a comment