Montana Tech of The University of Montana
Computer Science & Software Engineering

CSCI 135
Fundamentals of Computer Science I
Fall 2013



ASSIGNMENT #2

In this assignment, you will gain experience reading from standard input, using arrays, and using Math methods. You will also learn how to design a brute-force algorithmic solution to a problem that would be tedious and error-prone for a human to calculate.


Part 1: Number Hunt
Create a game NumberHunt.java in which users try and guess a random number between 1 and 100. After each guess, the computer prints out a hint according to how close the guess was to the target value: If the user inputs a number that isn't between 1 and 100, you should print "Invalid input!". After correctly guessing the number, the game should print the total number of guesses (including invalid guesses). You can assume users will only enter integers and not floating-point numbers or text.

Your program should optionally take a single integer command line argument. If a command line argument is given, use this number instead of drawing a random number. This will help you test your program since you can start with a known number. You can assume the command line argument (if given) will always be between 1 and 100. Here are some example runs:
% java NumberHunt
Guess a number between 1-100? 50
Ice cold.
Guess a number between 1-100? 20
Getting warmer.
Guess a number between 1-100? 10
Hot.
Guess a number between 1-100? 5
Getting warmer.
Guess a number between 1-100? 15
Hot.
Guess a number between 1-100? 12
You nailed it!
It took you 6 guesses.

% java NumberHunt 42
Guess a number between 1-100? 10
Ice cold.
Guess a number between 1-100? 101
Invalid input!
Guess a number between 1-100? 30
Cold.
Guess a number between 1-100? 35
Getting warmer.
Guess a number between 1-100? 40
Hot.
Guess a number between 1-100? 41
On fire.
Guess a number between 1-100? 43
On fire.
Guess a number between 1-100? 42
You nailed it!
It took you 8 guesses.
How do I read in a number from the user? You need to use download the StdIn.java class and place it in the same directory as your NumberHunt.java program. When you want your program to read in an int from the user, call the method StdIn.readInt(). For an example program, see OrderProduct.java.

How do I check if a command line argument was given? You can use args.length to find out how many command line arguments were sent in. If args.length is zero, you know you need to draw a random number. If args.length is greater than zero, you can obtain the target value from args[0].

How do I draw a random number? You want to use the Math.random() method. Check out the TwoDice.java example.


Part 2: RadarContacts
You are a communications officer onboard an E-3 Sentry AWACS surveillance aircraft. Your radar equipment generates a file containing all radar contacts within your region. Each radar contact is given by its UTM coordinates. UTM coordinates consist of two numbers, an easting and a northing. The easting specifies how many meters the contact is to the east of a fixed grid reference location. Similarly the northing is how far the contact is north of the grid reference.

In addition to the location of every aircraft, your equipment also queries the transponder of all aircraft to obtain their call sign. Only friendly aircraft respond with a call sign. Other unknown or hostile aircraft are assigned a question mark as a call sign. Here is radar4.txt, a small example file showing four contacts, two friendly and two unknown:
4
34754 12029 EJ-475
38002 11193 CX-120
11899 28929 ?
39222 10028 ?
The first line of the example file specifies that there are four contacts in the data file. Each of the remaining lines gives the easting, northing and call sign (in that order). You can assume all eastings and northings are non-negative integers.

Your job is to write a program RadarContacts.java that first reports the number of friendly and non-friendly contacts in the region. The program then generates a list of warning messages for transmission to all friendly aircraft. The messages inform each friendly aircraft about the distance to any radar contacts that are too close. You program should take two command-line arguments radius and mode. The radius argument specifies how close (in kilometers) another contact must be before a warning is generated. A warning should be generated if the two-dimensional Euclidean distance between the friendly aircraft in question and the contact is less than or equal to radius kilometers. The radius argument can be any non-negative floating-point value (e.g. 1.5).

The mode command-line argument specifies the type of contacts that should be reported: The output report format first lists the call sign of the friendly aircraft followed by a colon. In mode 0 and mode 2, all non-friendly that are too close are listed (denoted by a question mark) followed by the bogey's distance (in kilometers) in parentheses. In mode 1, all friendly contacts are listed by their known call sign followed by the friendly's distance in parentheses. If a friendly contact does not have any contacts that are too close (base on the mode), it should not appear in the report. All distances should be reported in kilometers rounded to two decimal places. Contacts within the friendly and non-friendly sets for a given aircraft's report can appear in any order. Here are some example runs:
% java RadarContacts 1.0 2 < radar4.txt
Friendly aircraft: 2
Non-friendly aircraft: 2

% java RadarContacts 2.0 2 < radar4.txt
Friendly aircraft: 2
Non-friendly aircraft: 2
CX-120:
? (1.69)

% java RadarContacts 3.5 2 < radar4.txt
Friendly aircraft: 2
Non-friendly aircraft: 2
EJ-475:
CX-120 (3.35)

CX-120:
? (1.69)
EJ-475 (3.35)

% java RadarContacts 3.5 1 < radar4.txt
Friendly aircraft: 2
Non-friendly aircraft: 2
EJ-475:
CX-120 (3.35)

CX-120:
EJ-475 (3.35)

% java RadarContacts 3.5 0 < radar4.txt
Friendly aircraft: 2
Non-friendly aircraft: 2
CX-120:
? (1.69)
Here is some sample output from radar397.txt which contains many more contacts:
% java RadarContacts 0.5 0 < radar397.txt
Friendly aircraft: 262
Non-friendly aircraft: 135
EW-443:
? (0.47)

YN-457:
? (0.29)

RO-952:
? (0.12)

% java RadarContacts 0.75 0 < radar397.txt
Friendly aircraft: 262
Non-friendly aircraft: 135
EW-443:
? (0.47)

YN-457:
? (0.71)
? (0.29)

OT-689:
? (0.75)

TB-219:
? (0.70)

DT-583:
? (0.58)

RO-952:
? (0.12)

KG-168:
? (0.70)

% java RadarContacts 0.25 2 < radar397.txt
Friendly aircraft: 262
Non-friendly aircraft: 135
NE-655:
AR-490 (0.12)

RO-952:
? (0.12)

AR-490:
NE-655 (0.12)

How do I check a String variable to see if it is equal to something? You'll want to use the equals() method. So for example if foo is a String, foo.equals("bar") would return true only if the variable foo contains exactly the string "bar". DO NOT use == to compare String variables! It is not doing what you think and will cause trouble.

How can I check the length of String variable? If the variable foo is a String, you can call foo.length() to return the number of characters in the String. If the String is empty, its length() will be 0.

How do I put a return (line-feed) into a String variable? Put \n inside the double-quotes of a String literal.

How do I print a double variable rounded to two decimal places? You want to use the printf() method. Here is an example: System.out.printf("value = %.2f", foo);

How do I create a String variable that has a double rounded to two decimal places? You want to use the format() method. Here is an example: String bar = String.format("value = %.2f", foo);

How do I compute the distance between two points? You need to use the formula: In this formula p1 and p2 are the coordinates of one contact and q1 and q2 are the coordinates of another. You can take a square root in Java using Math.sqrt(). While there is a math method for taking powers, if you are just squaring a value, it is much more efficient just to use the normal multiplication operator.
Extra credit
Create a more advanced version of the program RadarContactsEC.java by first copying RadarContacts.java. This version should, in addition to the distance, report the bearing to all contacts. The bearing should be in decimal degrees with 0 degrees being due north, 90 due east, etc. Report the bearing to one decimal place. For non-friendly contacts, also report the total number of friendly aircraft that are within the radius distance of the bogey.
Submission. Submit your program NumberHunt.java and RadarContacts.java via Moodle. Be sure each submitted source file has the required header with your name, username, and a description of the program. If you did the extra-credit, submit a single zip file containing all the files required to run your creation to the extra-credit dropbox.

Page last updated: December 18, 2013