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

Advanced Object-Oriented Programming Assignment 2: Developing Java Classes Intro

ID: 3587282 • Letter: A

Question

Advanced Object-Oriented Programming
Assignment 2: Developing Java Classes

Introduction - This assignment is meant to introduce you to the design and development of a simple Java class for use in a visual application. In this assignment you are to implement a class called Pod that represents part of a very simple “video game”.

Game Structure and User Interface Class -

This is a simple game in which the player (marked “#”) moves around a 2D map (currently 15x9) to collect “pods” (marked “*”). The game provides buttons (“N”, “S”, “E”, and “W”) that moves the player one square in the direction north, south, east, or west respectively. The pods themselves are always in motion, moving is a diagonal direction (either NE, NW, SE, or SW). When one reaches the edge of the board, it “bounces” off of the wall, changing its direction. When the player reaches the current location of a pod, then that pod is considered “caught” and will no longer be displayed.

I have provided on the course web page a user interface class called PodApp. You can use it by:

1. Creating a new Java application project in NetBeans called PodGame (which will automatically create a package called podgame).

2. Creating a class called PodApp.

3. Copying and pasting the code into the PodApp class.

4. Creating another class called Pod.

Note that this application will not compile initially, as it contains references to Pod methods that you will have to implement.

While you may not understand everything this visual application does, I encourage you to take a look at the code. In particular, you may find the parts that refer to the Pod methods useful towards getting a better understanding of the requirements for that class. However, you are not to change any of the code in this class!

Requirements for the Pod class -

Internal Representation:

Abstractly, a Pod object must keep track of the following information:

(1) The location of the pod. (2) The direction it is currently moving in. (3) Whether it has been caught or not.

The internal representation of your class (that is, how all of this is represented, as well as what other information each pod needs to keep track of) is up to you. This is the main design decision you will need to make for this assignment.

Big hint: While the user of the class is thinking about “direction” in terms of a two-character string such as “NE”, you may represent it in your own class in whatever form is more convenient for writing your “moving” code. For example:

Since you are already representing the pod location as two components (x and y), you may find it convenient to also represent the current direction as two member variables, one for the horizontal component and the other for the vertical component.

Since you will be performing numeric computations based on the current direction in order to move a pod, you may also find it convenient to represent the directional components as numbers rather than strings.

Required Constructors and Methods - In order for your Pod class to work with the application, you must implement certain methods and constructors.

Constructor:

public Pod(int x, int y, String direction, int width, int height)
This constructor takes as its parameters:

(1) The initial x and y coordinates of the pod. (2) The initial direction it is moving in. This will be a string with value either “NE”, “NW”, “SE”, or “SW”. (3) The width and height of the game board (you will need to know this in order to “bounce” the pod off of the walls).

Inspector Methods:

The following methods are called by the application to get information about the current state of the pod and display it within the game:

public int getX()
This method returns the current X coordinate of the pod.

public int getY()
This method returns the current Y coordinate of the pod.

public boolean isCaught()
This method returns true if the pod has been caught yet, false otherwise.

Debugging Inspector:

You are also required to provide a simple “debugging inspector”, which you may find extremely useful for testing and debugging your program:

public String toString()
This method returns a String containing the current value of all member variables of the pod.

Pod Move Method:

public void move()
This method moves the pod in the direction of its current motion. Specifically:

Going north (either NE or NW) – increment the Y location by 1.

Going south (either SE or SW) – decrement the Y location by 1.

Going east (either NE or SE) – increment the X location by 1.

Going west (either NW or SW) – decrement the X location by 1.

As mentioned above, if the pod hits a wall (which can be computed from the parameters passed to the constructor), it should “bounce” off of the wall. More specifically:

If it hits the top or bottom wall, the vertical direction changes. For example, if one hits the top wall going NE its direction changes to SE, and if it was going NW, its direction changes to SW.

If it hits the left or right wall, the horizontal direction changes. For example, if one hits the right wall going NE its direction changes to NW, and if it was going SE, its direction changes to SW.

Note that this will probably involve a rather complex if statement to do all of this, unless you come up with a better way to represent directions, as mentioned above.

An important note:
Remember that arrays in Java (like C) are indexed from 0 to the size-1. This means that you should “change direction” when a pod reaches 0, or when it reaches width-1 or height-1.

Determining Whether the Pod has been Caught:

public void playerAt(int x, int y)
This method passes the current location of the player to the pod. It should then determine whether the player is at the same location as a pod. If so, the pod has been “caught”, and its isCaught() method should return true from that point on.

Big hint: This means that you will need to make this information – that is, about whether or not the pod has been already caught sometime in the past – part of your internal representation.

Testing and Debugging -

An important thing to note is that your Pod class is not meant to run independently, but is a support class for the PodApp class I have provided. This means that you can only test your class by running PodApp.

This also means that any exceptions or errors you encounter will have to do with the way your Pod class works, even though the JVM may report them in the context of PodApp. For example, the PodApp class maintains a 9x15 array of textfields, and displays each pod in the array element reported by that pod’s getX() and getY() methods.

If you encounter an ArrayIndexOutOfBoundsException, it means that your pod currently has an x and y location outside the legal indices of the array. This likely means that the “bouncing” code in your move method is not working correctly.

To isolate these errors, I strongly suggest that you use the toString debugging inspector listed in the required methods to display the current state of the pod if you are having errors or exceptions. Specifically, consider calling and printing the result of toString inside your move method to make sure that the pod is where you expect it to be.

Documentation - You are also required to document your Pod class. Specifically:

Give an overall description of the class itself at the beginning of the class.

Describe what each member variable represents.

Document each constructor and method in the class.

As always, the documentation is a significant portion of your grade!

*********************************************************************************************************************************************************************************************************************

PodApp.java -

/*

Explanation / Answer

package podgame;

//Pod.java class

public class Pod {

      int x,y,width,height;

      int horizontal_dir,vertical_dir;

      boolean caught;

     

      public Pod(int x, int y, String direction, int width, int height){

            this.x=x;

            this.y=y;

            setDirection(direction);

            this.width=width;

            this.height=height;

            caught=false;

           

      }

      public boolean isCaught(){

            return caught;

      }

      public String toString(){

            return "(x,y):("+x+","+y+") direction: "+getDirection()+" , isCaught?: "+caught+" , width: "+width+", height: "+height;

      }

      public int getX(){

            return x;  

      }

      public int getY(){

            return y;  

      }

      public void playerAt(int x,int y){

            if(this.x==x && this.y==y){

                  caught=true;

            }

      }

      public void move(){

           

     

            switch (horizontal_dir) {

            case 0:

                  if(vertical_dir==0){ // NW

                        x--;

                        y--;

                        checkCollision("NW"); // checking collision after every move

                  }else{                // SW

                        x--;

                        y++;

                        checkCollision("SW");

                  }

                  break;

            case 1:

                  if(vertical_dir==0){ // NE

                        x++;

                        y--;

                        checkCollision("NE");

                  }else{                // SE

                        x++;

                        y++;

                        checkCollision("SE");

                  }

                  break;

            default:

                  break;

            }

           

           

      }

     

      public void checkCollision(String dir){ //a function defined to check for collisions with left,right,top,bottom walls

            if(dir.equalsIgnoreCase("NW")){

                  if(x<0 && y<0){ //exact NW corner

                        setDirection("SE");

                        x+=2;

                        y+=2;

                       

                  }else if(x<0){ // hit left side

                        setDirection("NE");

                        x+=2;

                       

                  }else if(y<0){ //hit top side

                        setDirection("SW");

                        y+=2;

                       

                  }

            } else if(dir.equalsIgnoreCase("NE")){

                  if(x>(width-1) && y<0){ //exact NE corner

                        setDirection("SW");

                        x-=2;

                        y+=2;

                       

                  }else if(x>(width-1)){ // hit right side

                        setDirection("NW");

                        x-=2;

                       

                  }else if(y<0){ //hit top side

                        setDirection("SE");

                        y+=2;

                       

                  }

            } else if(dir.equalsIgnoreCase("SE")){

                  if(x>(width-1) && y>(height-1)){ //exact SE corner

                        setDirection("NW");

                        x-=2;

                        y-=2;

                       

                  }else if(x>(width-1)){ // hit right side

                        setDirection("SW");

                        x-=2;

                       

                  }else if(y>(height-1)){ //hit bottom side

                        setDirection("NE");

                        y-=2;

                       

                  }

            } else if(dir.equalsIgnoreCase("SW")){

                  if(x<0 && y>(height-1)){ //exact SW corner

                        setDirection("NE");

                        x+=2;

                        y-=2;

                       

                  }else if(x<0){ // hit left side

                        setDirection("SE");

                        x+=2;

                       

                  }else if(y>(height-1)){ //hit bottom side

                        setDirection("NW");

                        y-=2;

                       

                  }

            }

           

           

           

      }

      public void setDirection(String str){ // EAST:1 WEST:0 (horizontal) , SOUTH:1 NORTH:0 (vertical)

           

            if(str.equalsIgnoreCase("NE")){

                  horizontal_dir=1;

                  vertical_dir=0;

            }else if(str.equalsIgnoreCase("NW")){

                  horizontal_dir=0;

                  vertical_dir=0;

            }else if(str.equalsIgnoreCase("SW")){

                  horizontal_dir=0;

                  vertical_dir=1;

            }else if(str.equalsIgnoreCase("SE")){

                  horizontal_dir=1;

                  vertical_dir=1;

            }

      }

      public String getDirection(){

            if(horizontal_dir==1 && vertical_dir ==0){

                  return "NE";

            }else if(horizontal_dir==0 && vertical_dir ==0){

                  return "NW";

            } else if(horizontal_dir==0 && vertical_dir ==1){

                  return "SW";

            } else if(horizontal_dir==1 && vertical_dir ==1){

                  return "SE";

            }

            return "unavailable";

      }

     

}

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