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

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 pods 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 -

/*

 * This class represents a visual application for the "pod chase" game. */ package podgame; /** * * @author jrsullins */ import javax.swing.*; import java.awt.*; import java.awt.event.*; public class PodApp extends JFrame implements ActionListener { // Member variables for visual objects. private JLabel[][] board; // 2D array of labels. Displays either # for player, // * for pod, or empty space private JButton northButton, // player presses to move up southButton, // player presses to move down eastButton, // player presses to move right westButton; // player presses to move left // Current width and height of board (will make static later). private int width = 15; private int height = 9; // Current location of player private int playerX = 7; private int playerY = 4; // Pod object stored in array for efficiency private Pod[] pods; int podCount = 4; public PodApp() { // Construct a panel to put the board on and another for the buttons JPanel boardPanel = new JPanel(new GridLayout(height, width)); JPanel buttonPanel = new JPanel(new GridLayout(1, 4)); // Use a loop to construct the array of labels, adding each to the // board panel as it is constructed. Note that we create this in // "row major" fashion by making the y-coordinate the major // coordinate. We also make sure that increasing y means going "up" // by building the rows in revers order. board = new JLabel[height][width]; for (int y = height-1; y >= 0; y--) { for (int x = 0; x < width; x++) { // Construct a label to represent the tile at (x, y) board[y][x] = new JLabel(" ", JLabel.CENTER); // Add it to the 2D array of labels representing the visible board boardPanel.add(board[y][x]); } } // Construct the buttons, register to listen for their events, // and add them to the button panel northButton = new JButton("N"); southButton = new JButton("S"); eastButton = new JButton("E"); westButton = new JButton("W"); // Listen for events on each button northButton.addActionListener(this); southButton.addActionListener(this); eastButton.addActionListener(this); westButton.addActionListener(this); // Add each to the panel of buttons buttonPanel.add(northButton); buttonPanel.add(southButton); buttonPanel.add(eastButton); buttonPanel.add(westButton); // Add everything to a main panel attached to the content pane JPanel mainPanel = new JPanel(new BorderLayout()); getContentPane().add(mainPanel); mainPanel.add(boardPanel, BorderLayout.CENTER); mainPanel.add(buttonPanel, BorderLayout.SOUTH); // Size the app and make it visible setSize(300, 200); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Auxiliary method to create game setup createGame(); } // Auxiliary method used to create board. Sets player, treasure, and walls. public void createGame() { // Construct array of Pod objects pods = new Pod[podCount]; // Construct each Pod in the array, passing it its initial location, // direction of movement, and the width and heigh of board. This will // later be modified to be done at random. pods[0] = new Pod(1, 5, "NE", width, height); pods[1] = new Pod(2, 1, "SW", width, height); pods[2] = new Pod(12, 2, "NW", width, height); pods[3] = new Pod(13, 6, "SE", width, height); // Call method to draw board drawBoard(); } // Auxiliary method to display player and pods in labels. public void drawBoard() { // "Erase" previous board by writing " " in each label for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { board[y][x].setText(" "); } } // Get location of each pod and write * into that label. We only // do this for pods not yet caught. for (int p = 0; p < podCount; p++) { if (!pods[p].isCaught()) { board[pods[p].getY()][pods[p].getX()].setText("*"); } } // Write the player onto the board. board[playerY][playerX].setText("#"); } public void actionPerformed(ActionEvent e) { // Determine which button was pressed, and move player in that // direction (making sure they don't leave the board). if (e.getSource() == southButton && playerY > 0) { playerY--; } if (e.getSource() == northButton && playerY < height-1) { playerY++; } if (e.getSource() == eastButton && playerX < width-1) { playerX++; } if (e.getSource() == westButton && playerX > 0) { playerX--; } // Move the pods and notify the pods about player location. for (int p = 0; p < podCount; p++) { pods[p].move(); pods[p].playerAt(playerX, playerY); } // Redraw the board drawBoard(); } /** * @param args the command line arguments */ public static void main(String[] args) { PodApp a = new PodApp(); } } 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!