Answer to Question #298982 in Java | JSP | JSF for Ekoyy

Question #298982

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();
public void moveRight();
public void moveUp();
public void moveDown(); 
public void display();
public void moveLeft();
public void moveRight();
public void moveUp(); 
public void moveDown();
public void display();


Input

- 1 moveLeft()

- 2 moveRight()

- 3 moveUp()

- 4 moveDown()

1
0·0
5
4
4
2
2
1


Output

A single line simply printing the string returned by toString().

Point:·(1,2)


More info here, Important!

pastebin.com/05W0gkTx




1
Expert's answer
2022-02-19T16:57:05-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--;
		} else {
			System.out.println("BUMP: Bounces off left side");
		}


	}


	@Override
	public void moveRight() {
		if (x < MAX_CHAR) {
			x++;
		} else {
			System.out.println("BUMP: Bounces off right side");
		}
	}


	@Override
	public void moveUp() {
		if (y > MIN_CHAR) {
			y--;
		} else {
			System.out.println("BUMP: Bounces off top");
		}


	}


	@Override
	public void moveDown() {
		if (y < MAX_CHAR) {
			y++;
		} else {
			System.out.println("BUMP: Bounces off bottom");
		}
	}


	@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();
		} else {
			System.out.println("BUMP: Bounces off left side");
		}


	}


	@Override
	public void moveRight() {
		if ((point.x + radius) < Point.MAX_CHAR) {
			point.moveRight();
		} else {
			System.out.println("BUMP: Bounces off right side");
		}
	}


	@Override
	public void moveUp() {
		if ((point.y + radius) > Point.MIN_CHAR) {
			point.moveUp();
		} else {
			System.out.println("BUMP: Bounces off top");
		}


	}


	@Override
	public void moveDown() {
		if ((point.y + radius) < Point.MAX_CHAR) {
			point.moveDown();
		} else {
			System.out.println("BUMP: Bounces off bottom");
		}
	}


	@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);


		// The first input is either 1 (a Point) or 2 (a Circle).
		int type = keyBoard.nextInt();
		Movable object = null;
		// If it is a Point, 2 non-negative integers follow,
		if (type == 1) {
			object = new Point();
			((Point) object).x = keyBoard.nextInt();
			((Point) object).y = keyBoard.nextInt();
		}
		if (type == 2) {
			object = new Circle();
			// if it is a Circle, 3 non-negative integers follow,
			// the 3rd being the radius.
			((Circle) object).point.x = keyBoard.nextInt();
			((Circle) object).point.y = keyBoard.nextInt();
			((Circle) object).radius = keyBoard.nextInt();
		}


		// Then a positive integer m is read representing the number of operations that
		// have to be performed followed by the operations themselves.
		int m = keyBoard.nextInt();
		for (int i = 0; i < m; i++) {
			int operation = keyBoard.nextInt();
			// 1 - moveLeft()
			// - 2 - moveRight()
			// - 3 - moveUp()
			// - 4 - moveDown()
			if (operation == 1) {
				object.moveLeft();
			} else if (operation == 2) {
				object.moveRight();
			} else if (operation == 3) {
				object.moveUp();
			} else if (operation == 4) {
				object.moveDown();
			}
		}
		object.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