COSC 1010 Introduction to Software Problem Solving

Fall 2010

Homework Assignment #5

"Talent hits a target no one else can hit;
Genius hits a target no one else can see."
-- Arthur Schopenhauer
Due: Wednesday, October 13th, 11:00am CDT
Submit: Turn in all four of your Java source files using the D2L Dropbox:
Work may be completed in teams of two. The names of both partners must appear in a comment block at the top of your files for credit to be given.

Target Practice

For this project we're going to build a small game, based on some of the pieces we've used in previous projects. The user will define a rectangular target area (using the X and Y coordinates of two opposite corner points,) and then fire projectiles at the target area using a cannon at the origin. The cannon fires shells at a fixed velocity, but the user controls the bearing and declination of the shot.

The goal of this project is primarily to practice breaking a problem down into a set of simpler pieces using classes and methods. To assist in this, we provide the following "skeleton" that should be used as a starting point:

Point class

Use the new and improved to represent cartesian coordinates.

Target class

The Target class will represent the target areas that the Cannon is attempting to hit. A Target can be defined by two points in the X-Y plane, and is assumed to be the rectangle with corners at those two locations. Your Target class should contain implementations of the following methods:

  • public Target(Point p1, Point p2). A constructor for initializing a target region.
  • public String toString(). A helper method for returning a String that describes the target.
  • public boolean isHit(Point p3). Given an impact point as a parameter, this method determines whether or not the shot was a successful "hit".
  • Cannon class

    The Cannon class represents a cannon that fires simple projectiles at the target(s). It should have instance variables that represent the number of projectiles remaining, and a Point where the cannon is located.

    Two fixed constants are used in this project, and it makes the most sense for them to belong in this class:

  • public static final int VELOCITY = 50; // projectile velocity, in meters per second.
  • public static final double GRAVITY = -9.81; // gravity acceleration, in meters per second per second.
  • Your Cannon class should contain definitions of the following methods:

  • public Cannon(Point location). A constructor that initializes the location of a cannon.
  • public void load(int shots). A method to set the number of shots the cannon has.
  • public boolean hasShots(). A "getter" method for checking whether a cannon has more ammunition.
  • public Point fire(int bearing, int declination). This method performs the ballistic calculations, given a bearing and a declination, to determine where a shot will impact. It returns a new Point object representing the location of impact.
  • public String toString(). A helper method for printing out info about a cannon.
  • Project5 class

    This class pulls together all of the pieces to run the game. It should take care of all of the input required from the player, and encode the rules for the game.

  • public static void main(String[] args). The main program. Reads in the target coordinates and creates a Target. Initializes the Cannon to be at the origin, and loads it with 5 rounds. Enters a game loop that requests bearings and declinations from the player (and reports results) until the player hits the target or runs out of shots.
  • For each of the classes listed above, you may create as many additional helper methods as you need to solve the problem.

    Example Run

    In the examples below, I use text in blue to distinguish the output of the program from the input I typed. This is for purpose of clarity only; your program will not print text in different colors.

    Project 5 - Target Practice
    Enter Target 1.
    Enter first point for target:
    Y? 100
    Enter second point for target:
    Y? 200
    Target 1 at [(100,100,0):(200,200,0)]
    Cannon at (0,0,0), 5 shots remaining.

    Firing cannon:
    Declination? 45
    Impact at (0,254,0), target missed.

    Firing cannon:
    Declination? 45
    Impact at (254,0,0), target missed.

    Firing cannon:
    Declination? 45
    Impact at (180,180,0), target hit! Well done.


  • We will expand upon these classes in later projects, so it is particularly important that your team understand how to build this solution.
  • The Java math library contains methods Math.sqrt(), Math.sin(), and Math.cos to calculate square roots for the distance function, and trigonometric identities to decompose the coordinates. You can read further details of these methods on the Java API website online, or in your textbook.
  • The Java trig functions expect arguments in radians instead of degrees. The Math.toRadians() method will convert degrees to radians for you.
  • This assignment uses compass bearings like pilots or map makers -- from starting position, 0 degrees is due north, 90 degrees is to the east, etc. Assuming standard orientation, due north would be along the Y axis of the Cartesian coordinate system, and 90 degrees would be along the positive X axis. The Z axis is height. The Java trigonometry functions use mathematicians angles -- 0 degrees is along the positive X axis, and 90 degrees is along the positive Y axis. The conversion between compass bearings and mathematic angles is to subtract the bearing from 90 degrees.
  • The Professor has provided a reference implementation for you to compare against. Change the directory on Morbius: cd ~brylow/cosc1010/Demos/Project5, and run java Project5.

  • Back
    [Revised 2010 Oct 04 20:08 DWB]