Answer to Question #300184 in Java | JSP | JSF for Angel

Question #300184

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

1
Expert's answer
2022-02-20T09:22:25-0500


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();
	}


}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS