Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

public class Grid { private static Piece [][] grid; private Grid() { // private so you don't try to create a Grid object. } public

image text in transcribed

public class Grid { private static Piece [][] grid; private Grid() { // private so you don't try to create a Grid object. } public static void setup( int numRows, int numCols ) { grid = new Piece[ numRows ][ numCols ]; } public static int numRows() { return grid.length; } public static int numCols() { return grid[0].length; }

public static boolean isValid( int r, int c ) { if ( r = grid.length ) return false; if ( c = grid[0].length ) return false; return true; } public static void add( Piece p, int r, int c ) { if ( ! isValid( r, c ) ) return; grid[r][c] = p; } public static Piece get( int r, int c ) { if ( ! isValid( r, c ) ) return null; return grid[r][c]; } public static void clear( int r, int c ) { if ( ! isValid( r, c ) ) return; grid[r][c] = null; } public static void show() { for ( Piece [] row : grid ) { for ( Piece p : row ) { if ( p == null ) System.out.print( ".\t" ); else System.out.print( p + "\t" ); } System.out.println(); } } }

public class Main { public static void main(String[] args) { Grid.setup( 3, 5 ); Jumpy j1 = new Jumpy( 0, 0, 1 ); // row, col, id Piece p2 = new Piece( 0, 1, 2 ); Jumpy j3 = new Jumpy( 2, 4, 3 ); Jumpy j4 = new Jumpy( 1, 3, 4 ); System.out.println("Initial setup"); Grid.show(); System.out.println("----------------------------------------"); j1.moveRight(); // moves right 2 squares, erases the object at (0,1) j3.moveUp(); // moves up 2 squares j4.moveLeft(); // moves to the left 2 squares System.out.println("j1 jumps right, j3 jumps up, j4 jumps left, p2 is removed"); Grid.show(); System.out.println("----------------------------------------"); j4.moveDown(); // can only move 1 square down j1.moveUp(); // cannot move up System.out.println("j4 tries to jump down, j1 tries to jump up"); Grid.show(); } }

public class Main { public static void main(String[] args) { Grid.setup( 3, 5 ); Jumpy j1 = new Jumpy( 0, 0, 1 ); // row, col, id Piece p2 = new Piece( 0, 1, 2 ); Jumpy j3 = new Jumpy( 2, 4, 3 ); Jumpy j4 = new Jumpy( 1, 3, 4 ); System.out.println("Initial setup"); Grid.show(); System.out.println("----------------------------------------"); j1.moveRight(); // moves right 2 squares, erases the object at (0,1) j3.moveUp(); // moves up 2 squares j4.moveLeft(); // moves to the left 2 squares System.out.println("j1 jumps right, j3 jumps up, j4 jumps left, p2 is removed"); Grid.show(); System.out.println("----------------------------------------"); j4.moveDown(); // can only move 1 square down j1.moveUp(); // cannot move up System.out.println("j4 tries to jump down, j1 tries to jump up"); Grid.show(); } }

public class Piece { private int row; private int col; private int id; public Piece( int r, int c, int id ) { row = r; col = c; this.id = id; Grid.add( this, row, col ); } public int row() { return row; } public int col() { return col; } public void moveUp() { move( row-1, col ); } public void moveDown() { move( row+1, col ); } public void moveLeft() { move( row, col-1 ); } public void moveRight() { move( row, col+1 ); } private void move( int r, int c) { if ( !Grid.isValid( r, c ) ) return; if ( Grid.get( row, col ) == null ) // this Piece is no longer in the Grid return; Grid.clear( row, col ); // removes the Piece from its current location row = r; col = c; Grid.add( this, r, c ); // adds the Piece to its new location } public boolean equals( Object x ) { if ( x instanceof Piece ) { Piece other = (Piece) x; return this.row == other.row && this.col == other.col && this.id == other.id; } return false; } public String toString() { // return "\u25CE" + id; // unicode return "⊗" + id; // html entity for circle with an X } }

/* Write the Jumpy class, a subclass of the Piece class. The Jumpy class has no instance variables. You must: - write the constructor with this header: public Jumpy( int row, int col, int id ) - override the moveUp method so that it always tries to move up two rows whenever the method is called. Note: this will remove whatever other pieces are in its path. - override the moveDown, moveLeft, and moveRight methods so that moves two squares in the specified direction. - override the toString method so that it calls the toString method from the Piece class and adds "⊙" (an html entity) to the end of the string that is returned.

Do NOT add any other methods to the Jumpy class. */

Write the Jumpy class, a subclass of the Piece class. The Jumpy class has no instance variables. You must: write the constructor with this header: public Jumpy(int row, int col, int id ) Override the moveUp method so that it always tries to move up two rows whenever the method is called. Note: this will remove whatever other pieces are in its path. Override the moveDown, moveLeft, and moveRight methods so that moves two squares in the specified direction. override the toString method so that it calls the toString method from the Piece class and adds "⊙" (an html entity) to the end of the string that is returned. When printed in codeboard, it looks like #O where the # is replaced by its id. When the Jumpy class is written, this is what the main method should print. Initial setup 10 2 40. 30 j1 jumps right, j3 jumps up, j4 jumps left, p2 is removed 10. 30 40 34 tries to jump down, ji tries to jump up 10. 30 Write the Jumpy class, a subclass of the Piece class. The Jumpy class has no instance variables. You must: write the constructor with this header: public Jumpy(int row, int col, int id ) Override the moveUp method so that it always tries to move up two rows whenever the method is called. Note: this will remove whatever other pieces are in its path. Override the moveDown, moveLeft, and moveRight methods so that moves two squares in the specified direction. override the toString method so that it calls the toString method from the Piece class and adds "⊙" (an html entity) to the end of the string that is returned. When printed in codeboard, it looks like #O where the # is replaced by its id. When the Jumpy class is written, this is what the main method should print. Initial setup 10 2 40. 30 j1 jumps right, j3 jumps up, j4 jumps left, p2 is removed 10. 30 40 34 tries to jump down, ji tries to jump up 10. 30

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Driven Web Sites

Authors: Mike Morrison, Joline Morrison

1st Edition

061901556X, 978-0619015565

More Books

Students also viewed these Databases questions

Question

What are the best practices for managing a large software project?

Answered: 1 week ago

Question

How does clustering in unsupervised learning help in data analysis?

Answered: 1 week ago