ulrail.gif

HW J9: 2-D Arrays

  ur-2c.gif urrail.gif
blrail-nonavbar.gif

Home | Resources | Homeworks | Exams
Lectures | Labs | Contacts | Submit | TAs

br-2a.gif   br-2c.gif brrail.gif
spacer.gif spacer.gif

Course Project

This homework will complete to the course project, an implementation of a simple computerized version of the classic game of Battleship.  Because we are now at the end of the semester, we would like to put the course project in context and to leave you with a broader view both of what you've done in CS101 and of the vast possibilities and responsibilities that lie beyond CS101. In addition to the programming part of this assignment, you are also responsible for being familiar with the material in the following few short paragraphs, which we reserve the right to test on the upcoming semester exam. Thanks for taking a few minutes to read this material.

Professional Competency and Ethics in Software Architecture and Engineering

In industrial practice, many software developers start out as programmers. In this role, the job is to produce program implementations that satisfy given specifications. As one gains experience in software development, one begins to take on tasks in the area of software architecture, and software engineering more broadly. Beyond mere programming, software architects and engineers decide not just how to implement specifications as programs but also what specifications to implement, which involves understanding the given application domain, ongoing interactions with subject domain experts, and so forth. In many cases, software engineers are responsible for the key computational components of larger engineering systems, such as aircraft, medical imaging systems, gene sequencing technologies and global entertainment networks. Software is now the major determinant of cost, schedule and success and the major place where innovation is happening in most engineering domains and in many other areas of our society, including business, medicine and defense. Beyond mere programming, the software engineering tasks required to develop software for such applications also involves rigorous verification of programs (including but not limited to software testing) to provide adequate assurance that programs satisfy their specifications. After all, you would not want to fly in a software-controlled commercial jetliner (and today many literally cannot fly unless their software and computers are working), unless the most stringent and demanding techniques had been used to assure that the software wouldn't crash in the air, leading the airliner to crash on the surface of the earth. Beyond purely technical considerations, software engineering in practice also involves the design and management of complex engineering processes and organizations. Directing a team of 1000 or 5000 developers, which includes partitioning and managing the work of developing large systems, is a very different task than writing small programs. Software engineers, like all engineers, also attend to human, economic, environmental, ethical and political considerations that arise in large-scale engineering practice. Our technological future will increasingly be shaped by software: human intelligence automated and running the machinery of our civilization. Software architecture and engineering thus involve diverse technical and human aspects and are major tasks of software professionals. Programming per se, although demanding great intelligence, diligence and creativity, is but one small part of computer science, in general, and of software engineering, in particular.

Many people assume that because they understand programming, they are competent to act as industrial software architects and engineers. That is generally not true. You finish CS 101 knowing that you understand far better one of the most exciting and powerful concepts in all of science and engineering---software. You have taken a first important step to becoming a competent software architect, engineer or even computer scientist. However, CS101 is not intended to and it does not give you the knowledge or the experience to perform competently as a software architect or engineer. As one of the last and most important lessons in CS101, we wish to emphasize that knowledge of mere programming does not qualify one as a professional in software engineering or software architecture; and it is a crucial mark of the professional to recognize and acknowledge the limits of one's competence and to decline to operate professionally in a manner for which one is not adequately trained and competent.

An Invitation to Take the Next Steps Toward Professional Competency in Computer Science and Engineering

If you are interested in exploring the next steps toward professional competency, whether in software architecture or engineering or in research in computer science (and we really hope that you are interested!), we encourage you to consider joining the Computer Science (CS) department as a major or a minor. CS is now the main catalyst for major advances in nearly all fields of human inquiry, from the arts to engineering. Recognizing this fact, UVa has made Computer Science the only department in the university to offer degrees in both Engineering (BS) and Arts and Science (BA). The CS at UVa department is highly ranked nationally, by far the best ranked department in Virginia, and one the best departments in the world in several major areas of computer science and engineering, including software engineering. We are active in research and teaching in most of the major areas of contemporary research and practice. We encourage undergraduate participation in faculty research projects, and we routinely place our graduates in the best graduate schools and professional positions in the country.

Job Prospects for Software and Computing Professionals Relative to Other Engineering Disciplines: 2004-2014.

Many students are drawn to computing, but some are concerned about being isolated as a mere programmer, or, worse, (a) in the wake of the .COM implosion, either not being able to find a job, or (b) being laid off due to the off-shoring of software development. Although we obviously cannot make promises about your specific career path or the future (asteroids are still possible:-), we can give you a  few facts. First, for those who are concerned about being isolated, understand that, in general, computing is generally one of the most human-intensive of professions. As a software architect or engineer, one is at the center of activity in the conception and development of next-generation systems across all application domains. Software engineers generally interact with people from many other areas in order to do their jobs, because their jobs centrally involve understanding other domains (whether music or transportation or radar) and representing them in terms of software constructs. Second, for those who are concerned about jobs, the facts are clear: according to the U.S. Bureau of Labor Statistics (BLS) the computing professions even excluding mere programming are by far (and we mean, by a long shot) the largest and the fastest growing professions in computing and engineering. There is only one other engineering area the BLS ranks as growing much faster than average in the 2004-2014 time, and it is among the very smallest of engineering fields, employing today only a few tens of thousands of people, and expected to produce only a few thousand new jobs in the coming decade. By contrast, there are over 2 million professionals already at work in advanced computing fields, and the BLS expects hundreds of thousands of new, high-paying jobs to be created in the next ten years. The best information that is available today thus clearly indicates that computer science and engineering, including software engineering, remains a very large, vibrant, important, robust and promising sector of the economy. If you would like to learn more, including projected job prospects for engineering and other fields, we encourage you to check the BLS web site for yourself (www.bls.gov).

And Finally, Your Assignment

The rest of this final CS101 semester project assignment is written in a way meant to emphasize the distinction between mere programming and the separate issue of software architecture and specification, and to encourage you to think about making the transition from programmer to engineer/designer. In particular, it is written in the form of the kind of programming task that you might see in practice. We provide the class outline (design). You provide the detailed programming implementations. As you do this work, think about the task of the architect: to organize the development of much more complex systems be the careful design of the interfaces by which such systems are decomposed (broken down) into subsystems and even smaller parts (such as classes) that can be implemented by smaller teams and individuals. We've done that task for you this semester. We encourage you to consider continuing in the field to deepen your ability to design more complex systems yourself.

You will now complete your game by providing an implementation of the Board class. We provide a specification of the class. You will produce an implementation satisfying this specification. The specification defines the data members of the class and the syntax and semantics of its methods. By syntax we mean names and signatures. By semantics we mean the required behaviors of the methods. We define the syntax in the Java language, as is typical. We describe the semantics informally in natural language (English). Your job is to provide implementations of the methods, using the given data members, to meet the given semantic specifications.  You should test your programming before turning it in, in two ways: first, by developing a simple BoardTest class (as we have done on previous assignments) that minimally tests each method in your class implementation, and, second, by playing at least 5 games! For this purpose, you can use our implementation of the Game class.

In this homework, you will develop the Board class and a BoardTest class that will serve as a "test harness" to test your Board class (at a minimum testing each method). We will provide you with one class for this homework, namely Game, the class with the main() method that will run the game.  It ties together all the other classes. Lab 11 developed the last class, MapPrinter, that allows for easy printing of the board to the text-based screen.

Project Files

For each class, you can either download our version (right-click and select "save as..."), or use your own (if you are sure that your code is bug-free).  Save all your code, both old and new, in a separate directory than the previous labs and homeworks, so you do not overwrite your files.

  • Ship (HW J6): Represents a ship, of course.  This can be any size ship, placed anywhere on the board.
  • Cell (HW J6): A cell is a single spot on a board.  Thus, a sample board could have a 15x15 grid of cells.
  • Descriptions (Lab 9): This class allows one to pick the names of their ships.
  • ShipList (HW J7): A list of ships.  For example, each player (human or computer) will have a list of 5 (or so) ships that were placed on the board.
  • Parser (Lab 10): This class reads in commands from the keyboard, and figures out (or 'parses') what the player means by them.
  • AI (HW J8): This class represents the computer player in the game (AI stands for Artificial Intelligence).  It is responsible for making the decisions as to which move to make next.
  • Human (HW J8): A class to represent the player in the game.  This class will be in charge of reading in input from the keyboard as to where to take the next shot, for example
  • MapPrinter (Lab 11): This allows easy printing of the board to the text-based screen.

We will provide you with one class for this homework:

  • Game: The class with the main() method that will run the game.  It ties together all the other classes.

This assignment will develop the remaining class:

  • Board (HW J9): This class represents the board that players place their ships on, and takes shots on as well.

And one more class that you need to develop:

  • BoardTest: This class tests the methods in the Board class.  We are not providing skeleton code for this.

You will need to import the java.awt library to use the Point class.  You will need to import the java.util class for the Vector and/or Random classes.

Documentation

As one develops more classes for a project, one comes to rely on previously defined classes and methods.  Programmers and designers often need to refer to the details of the previously defined classes.  To this end, we have provided documentation of all the classes online.  The format of this documentation is the same as the Java SDK.  In particular, the comments in the code below are contained in the documentation for the Board class.

Specification The Board Class

Here, below, in the Java language, is the specification of the Board class. This class specification is available online here. The syntax is specified by the method declarations. The semantics are specified in the comments above each method declaration. The places where you are to write your code are indicated by the comments in place of method bodies. Replace these comments with your code. At the same time (and we recommend doing it incrementally and in parallel), write a BoardTest class to test your newly coded methods. When you're done, use our Game class to test your Board class and system. Turn in your Board and BoardTest classes.

/**
 * Represents the board .
 **/

public class Board {

      // DATA MEMBERS
    

      // The game board represents a rectangular array of cells.
      // Each cell is in one of several possible states.
      // The following constants represent these states.
      public static final int NOT_SHOT_AT = 0;
      public static final int MISSED = 1;
      public static final int SAME_TARGET = 2;
      public static final int SHIP_HIT = 3;
      public static final int SHIP_SUNK = 4;
 
     
      // The following data members represent the board dimensions (size).
      int sizex, sizey;
     
      // The cells are represented as a two-dimensional array of Cell objects.
      private Cell [][] cells;
     
 
      // The ships present on the board are represented as an object that
      // stores a collection (list) of ship objects. 
      private ShipList ships;
 
      // METHODS
           
      /**
       * This constructor for the Board object should initialize the board,
       * setting its x and y dimensions to the given values and initializing
       * the cells and shiplist to their initial states. This method should
       * use the clearBoard method (specified next, below) to initialize the
       * cells and ships.
       *
       * @param x the x-coordinate.
       * @param y the y-coordinate.
       **/
      public Board(int x, int y) {
            // your implementation
      }
     
      /**
       * This method intializes the cell array and ship list references. When
       * this method completes, the board should be a in state that is ready
       * for play to begin.
       **/
      public void clearBoard() {
            // your implementation
      }
 
      /**
       * Given a reference to a ship in the ship list, this method returns
       * the integer index of the given ship in the list.
       *
       * @param ship theinstance to be searched.
       * @return the index.
       **/
      public int getShipNumber (Ship ship) {
            // your implementation
      }
     
      /**
       * Given an integer index to a ship in the ship list, this method returns
       * returns the ship at the requested index.
       *
       * @param index the index requested.
       * @return the Ship instance
       **/
      public Ship getShip (int index) {
            // your implementation
      }
     
      /**
       * Given the x and y coordinates of a cell in the board, this method
 	   * returns a reference to the cell at that position (x,y). Indexing is
 	   * zero-based. That is, the first cell is at index (0,0)
       * 
       * @param x the x-coordinate.
       * @param y the y-coordinate.
       * @return the Cell object.
       **/
      public Cell getCellAt (int x, int y) {
            // your implementation
      }
     
      /**
       * This method takes a ShipList object as a parameter. For each ship in
 	   * the list, this method uses the placeShip method (below) to place the
 	   * ship on the board.
       *
       * @param ships the list of ships.
       **/
      public void placeShips (ShipList ships) {
            // your implementation
      }
     
      /**
       * This method returns true if all ships on the board have been sunk,
       * and it returns false otherwise. This method uses the allShipsSunk
       * method of the ShipList class to compute the required result.
       *
       * @return true if all the ships are sunk, false otherwise.
       **/
      public boolean allShipsSunk() {
            // your implementation
      }
     
      /**
       * This method returns true if the board configuration is valid, and
       * it returns false otherwise. A board is considered valid if and only
       * if there are no two ships occupying the same cell on the board. This
 	   * method uses the getShipCount, getShip, and hasPoint methods of the Ship
 	   * and ShipList classes.
       */
      public boolean isValidBoard () {
            // your implementation
      }
     
      /**
       * This method resets the board to a new state containing the number of
       * ships given by the numberOfShips parameter. The method should work by
       * clearing the board, randomly placing the given number of ships (using
       * the placeship method, and then testing the board to see if it's in a
       * valid state. If the board is valid, the method terminates, otherwise it
       * tries again and keeps trying (in a do/while loop) until a valid board
       * is obtained.
       *
       * @param numberOfShips number of Ships.
       **/
      public void generateRandomBoard(int numberOfShips) {
            // your implementation
      }
     
      /**
       * This method places the given ship (aShip) on the board by marking each
       * cell that the ship occupies (with a call to setShipOnCell) and then by
       * adding the ship to the shipList. The cells to be marked start with x
       * and y coordinates of the given ship and include the additional cells
       * determined by the ship length its orientation (which are obtained by
       * use of the appropriate methods of the Ship class).
       *
       * @param aShip the instance of ship to be placed
       **/
      public void placeShip(Ship aShip) {
            // your implementation
      }
 
      /**
       * This method processes a shot fired at cell x,y. It returns the status
       * of the targeted cell in the form of one of the status constant values
       * defined above. If the targeted cell has already been hit, then this
       * method simply returns SAME_TARGET. If the cell has not previously been
       * hit then the following steps are taken. First the cell is marked as hit
       * using the setIsHit method of the Cell class. Second, if there is not a
       * ship on the targeted cell, then the method returns the status MISSED.
       * Otherwise the ship occupying the targeted cell is marked as hit (by a
       * call to the ship's hit method); then if the targeted ship is sunk (to
       * be determined by a call to the ship's isSunk method) then this method
       * returns SHIP_SUNK status, otherwise it returns SHIP_HIT status.
       *
       * @param x the x coordinate of the target.
       * @param y the y coordinate of the target.
       **/
      public int processShot(int x, int y) {
            // your implementation
      }
 
      /**
       * Prints the ship list by calling the print method of the ship list.
       */
      public void printShipList() {
            // your implementation
      }
           
}

BoardTest class, Revisited

Your main() method in the BoardTest.java file should test all of the methods that you implement.  In previous homeworks, you did this for various classes.  This homework requires the same, but for the Board class.  It will be much easier if you implement a method at a time, then write the test code in Game.java to ensure that it works correctly!  You

Good Programming Practices

All the good programming practices from HW J1 should be included here as well.  Since you are providing code in the main() method of the BoardTest class to test your other code, you don't need to include execution runs as comments.

Submission

When you are finished, submit Board.java and BoardTest.java.  Since Game.java was not modified, it does not need to be submitted.

Congratulations on finishing the last programming assignment of CS 101!  Have a great holiday break!

spacer.gif
spacer.gif footer-middle.gif spacer.gif