Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a class called World and a class called Driver. The Driver class is going

ID: 3692900 • Letter: W

Question

Write a class called World and a class called Driver. The Driver class is going to contain our main method. THE WORLD CLASS The World class will contain three private data fields. The first private data field is a 2 dimensional char array that represents the world. The size of the world will depend on what’s passed to the constructor. The next private data field is a characterRow and the last private data field is characterColumn. The constructor will accept two values, width and height. Using width and height, set the size of the 2 dimensional array. Then fill the array with a dash, -. The characterRow and characterColumn data fields are both set to 0 and then place the character, ‘P’, at position characterRow and characterColumn. So if your world were to be printed with width 9, or 9 columns, and height 4, or 4 rows, it would like something like this after creation: P-------- --------- --------- --------- The world class will initially contain 5 public methods. These methods are moveUp, moveDown, moveLeft, moveRight and displayWorld. moveUp and moveDown The methods moveUp and moveDown will change the character’s location. If the user is about to move out of bounds then do nothing. Here is what happens when moveUp is called: Before moveUp is called After moveUp is called --- -P- -P- --- --- --- Here is what happens when moveDown is called: Before moveDown is called After moveDown is called --- --- -P- --- --- -P- moveLeft and moveRight The methods moveLeft and moveRight will change the character’s location. If the user is about to move out of bounds then do nothing. Here is what happens when moveLeft is called: Before moveLeft is called After moveLeft is called --- --- -P- P-- --- --- Here is what happens when moveRight is called: Before moveRight is called After moveRight is called --- --- -P- --P --- --- displayWorld This method simply prints the 2 dimensional array to the console. Example using a 2 dimentional array with 4 rows and 3 columns (height of 4 and width of 3): P-- --- --- --- THE DRIVER CLASS The driver class will contain our main method and will do the following. First, get a width and height from the user. Then, create a world object passing in the width and height. Then, start an infinite loop that begins by calling the world’s displayWorld method. After, you will ask the user for an action. The actions are “left”, “right”, “up”, “down”, and “exit”. If the user types “exit” then break out of the loop. If the user types the other 4 actions then call the world’s respective method. For instance, if “left” is typed call the world object’s moveLeft method. If the user types and action not part of the 5 available actions then display not a valid action. Finally, add something extra to this project. Be creative. Just whatever you do don’t try to go over the top. You only need to add one additional feature on top of what’s been give. On the next page is an example run with the addition of falling rocks and a score when you don’t get hit. The rocks are represented by ‘#’. World width: 3 World height: 3 P-- --- --- ACTION > down #-- P-- --- ACTION > right #-- -P- -#- ACTION > up #P- -#- -#- ACTION > right #-P -#- -## ACTION > down #-- -#P -## Character was crushed by a rock Score: 4

This is my code, it does not diisplay the array like it should.

public class World
{
private char[][] world1;
private int characterRow;
private int characterColumn;
private int height;
private int width;

public World(int height, int width)
{
char[][] world1 = new char[height][width];
  
// fill world array with -'s
for (int r = 0; r < height; r++)
{
for (int c = 0; c < width; c++)
{
world1[r][c] = '-';
} // end for c
  
characterRow = 0;
characterColumn = 0;
world1[characterRow][characterColumn] = 'P';
} // end for r
} // end World constructor method
  
public void displayWorld()
{
for (int r = 0; r < height; r++)
{
for (int c = 0; c < width; c++)
{
System.out.print(world1[r][c]);
} // end for c
System.out.println();
} // end for r
} // end displayWorld method
  
public void moveUp()
{
if (characterRow > 0)
characterRow--;
} // end moveUp method
  
public void moveDown()
{
if (characterRow < (height - 1))
characterRow++;
} // end moveDown method
  
public void moveLeft()
{
if (characterColumn > 0)
characterColumn--;
} // end moveLeft method
  
public void moveRight()
{
if (characterColumn < (width - 1))
characterColumn++;
} // end moveRight method
  
} // end class World

import java.util.*;
public class Driver
{
public static void main(String[] args)
{
int width;
int height;
String action;
  
Scanner input = new Scanner(System.in);
System.out.print("World width: ");
width = input.nextInt();
System.out.print("World height: ");
height = input.nextInt();
  
World myWorld = new World(height, width);
  
while (true)
{
myWorld.displayWorld();
System.out.print("ACTION > ");
action = input.next().trim();
if (action.equalsIgnoreCase("left"))
myWorld.moveLeft();
else if (action.equalsIgnoreCase("right"))
myWorld.moveRight();
else if (action.equalsIgnoreCase("up"))
myWorld.moveUp();
else if (action.equalsIgnoreCase("down"))
myWorld.moveDown();
else if (action.equalsIgnoreCase("exit"))
break;
else
System.out.println("Not a valid action.");
} // end while
  
} // end main method
} // end class Driver

Explanation / Answer

World.java

package com.rbc.test;

public class World {
   private char[][] world1;
   private int characterRow;
   private int characterColumn;
   private int height;
   private int width;

   public World(int height, int width) {
       this.height = height;// You need to assign these incoming values to
                               // instance variables
       this.width = width;
       world1 = new char[height][width];// You have already declared this
                                           // variable at class level there is
                                           // no need to declare again.Thats
                                           // why i removed char[][]

       // fill world array with -'s
       for (int r = 0; r < height; r++) {
           for (int c = 0; c < width; c++) {
               world1[r][c] = '-';
           } // end for c

           characterRow = 0;
           characterColumn = 0;
           world1[characterRow][characterColumn] = 'P';
       } // end for r
   } // end World constructor method

   public void displayWorld() {
       // Here previously you are trying run the loop when r<height.It means
       // you are checking for a condition 0<0 condition not satisfied the loop
       // wont run.Why because you didn't set the
       // values for height and width in your constructor so made like
       // this.hegiht=height and this.width=width
       for (int r = 0; r < height; r++) {
           for (int c = 0; c < width; c++) {
               System.out.print(world1[r][c]);
           } // end for c
           System.out.println();
       } // end for r
   } // end displayWorld method

   public void moveUp() {
       // Previous code doesn't fulfill you requirements.Because you are just
       // incremented the column or row size it wont effect the array in any
       // manner until you specify as below
       if (characterRow > 0) {
           world1[characterRow][characterColumn] = '-';// I am writing this
                                                       // line
                                                       // because
                                                       // as per your
                                                       // requirement you want
                                                       // to move
                                                       // the character up the
                                                       // and
                                                       // current position
                                                       // should be filled with
                                                       // '-'
           characterRow--;
           world1[characterRow][characterColumn] = 'P';// I am writing this
                                                       // line
                                                       // because
           // as per your requirement you want to move
           // the character up the and
           // current position
           // should be filled with
           // 'P'
       }
   } // end moveUp method

   public void moveDown() {
       if (characterRow < (height - 1)) {
           world1[characterRow][characterColumn] = '-';
           characterRow++;
           world1[characterRow][characterColumn] = 'P';
       }
   } // end moveDown method

   public void moveLeft() {
       if (characterColumn > 0) {
           world1[characterRow][characterColumn] = '-';
           characterColumn--;
           world1[characterRow][characterColumn] = 'P';
       }
   } // end moveLeft method

   public void moveRight() {
       if (characterColumn < (width - 1)) {
           world1[characterRow][characterColumn] = '-';
           characterColumn++;
           world1[characterRow][characterColumn] = 'P';
       }
   } // end moveRight method

} // end class World

Driver.java

package com.rbc.test;

import java.util.*;

public class Driver {
   public static void main(String[] args) {
       int width;
       int height;
       String action;

       Scanner input = new Scanner(System.in);
       System.out.print("World width: ");
       width = input.nextInt();
       System.out.print("World height: ");
       height = input.nextInt();

       World myWorld = new World(height, width);

       while (true) {
           myWorld.displayWorld();
           System.out.print("ACTION > ");
           action = input.next().trim();
           if (action.equalsIgnoreCase("left"))
               myWorld.moveLeft();
           else if (action.equalsIgnoreCase("right"))
               myWorld.moveRight();
           else if (action.equalsIgnoreCase("up"))
               myWorld.moveUp();
           else if (action.equalsIgnoreCase("down"))
               myWorld.moveDown();
           else if (action.equalsIgnoreCase("exit"))
               break;
           else
               System.out.println("Not a valid action.");
       } // end while
       input.close();// Closing the scanner
   } // end main method
} // end class Driver

Output:

World width: 3
World height: 4
P--
---
---
---
ACTION > left
P--
---
---
---
ACTION > right
-P-
---
---
---
ACTION > down
---
-P-
---
---
ACTION > up
-P-
---
---
---
ACTION > right
--P
---
---
---
ACTION > left
-P-
---
---
---
ACTION > left
P--
---
---
---
ACTION > down
---
P--
---
---
ACTION > down
---
---
P--
---
ACTION > down
---
---
---
P--
ACTION > down
---
---
---
P--
ACTION > right
---
---
---
-P-
ACTION > right
---
---
---
--P
ACTION > up
---
---
--P
---
ACTION > left
---
---
-P-
---
ACTION > left
---
---
P--
---
ACTION > up
---
P--
---
---
ACTION > right
---
-P-
---
---
ACTION > right
---
--P
---
---
ACTION > up
--P
---
---
---
ACTION > left
-P-
---
---
---
ACTION > left
P--
---
---
---
ACTION > exit

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote