====== List of CS-175 Assignments ======
Most solutions will be posted on Piazza, under the Resources page:
* CS-175/501a: https://piazza.com/monmouth/fall2014/cs175501a/resources
* CS-501a-51: https://piazza.com/monmouth/fall2014/cs501a51/resources
----
===== Assignment 1 - Algorithms, Robocode I =====
Deadline: Fri 9/12, 5pm.
==== 1. Write an algorithm ====
In a plain text file called //largestOfFive.txt//, write an algorithm to find the largest of 5 numbers that are in a container. The values of the numbers are unknown when you start. Also, you must take the numbers out of the container one at a time. Use the CS-175/501a ecampus dropbox to turn in //largestOfFive.txt//. See http://en.wikipedia.org/wiki/List_of_text_editors to see a list of plain text file editors.
==== 2. FigureEightBot ====
Modify the default robocode robot you started in lab to run in a [[http://rockhopper.monmouth.edu/~jchung/cs175/fa14/src/fig8.gif|figure eight pattern]].
Turn in your robot's java code file, e.g. //RobotName.java//, using the CS-175/501a ecampus dropbox.
----
===== Assignment 2 - Robocode II =====
Deadline: Tue, 9/23, noon.
* Use the ecampus dropbox for CS-175/501a.
* Comment your robot code.
* Include your name and date in comments.
* Properly indent and format your robot code.
* You are encouraged to work together with classmate(s) to work on this assignment and share ideas. However, all code must be physically typed in by you. If you are adapting code from another classmate or sample robot, please attribute it in comments.
* Use the Robot class API documentation (http://robocode.sourceforge.net/docs/robocode/robocode/Robot.html) and other sources.
==== 1. ChaseBot1 ====
In Robocode create ''ChaseBot1''.
''ChaseBot1'' scans an enemy robot and follows that enemy robot by moving toward it.
Test your ''ChaseBot1'' against the ''Crazy'' sample robot.
Customize your ''ChaseBot1's'' colors.
Dropbox ''ChaseBot1.java''.
==== 2. SpiralBot ====
Start a new Robocode robot named ''SpiralBot''.
Define the ''SpiralBot'' robot's movement as follows:
For each turn, move forward a total of N pixels per turn, then turn right 90 degrees. Before the robot starts any movements, the variable N must be initialized to 15, and increase by 15 pixels per turn. N must be an //instance variable// of ''SpiralBot''.
Customize your ''SpiralBot's'' colors.
Dropbox ''SpiralBot.java''.
== Run path of SpiralBot ==
{{:cs175:spiralbotpath.jpg?200|}}
----
===== Assignment 3 - Fundamental Data Types, Robocode III =====
Deadline: Thurs, 10/2, noon.
* Use the ecampus dropbox for CS-175/501a.
* Comment your program code.
* Include your name and date in comments.
* Properly indent and format your program code.
* You are encouraged to work together with classmate(s) to work on this assignment and share ideas. However, all code must be physically typed in by you. If you are adapting code from another classmate, please attribute it in comments.
==== 1. BookOrder ====
Do programming project "Business P4.5" on p. 172 of the book. Dropbox the program as //BookOrder.java//.
==== 2. DollarsCents ====
Do programming project "Business P4.7" on p. 173 of the book. Dropbox the program as //DollarsCents.java//.
==== 3. NearestWallBot1 (Algorithm) ====
In //NearestWallBot1.txt// (plain text file), write an algorithm to move a robocode robot to the nearest wall.
----
===== Assignment 4 - Decisions, Robocode IV =====
Deadline: Wed, 10/15, 5pm
* Use the ecampus dropbox for CS-175/501a.
* Comment your program code.
* Include your name and date in comments.
* Properly indent and format your program code.
* You are encouraged to work together with classmate(s) to work on this assignment and share ideas. However, all code must be physically typed in by you. If you are adapting code from another classmate, please attribute it in comments.
==== 1. AdjustFireBot2 ====
Start a new Robocode robot named //AdjustFireBot2//. Change the //AdjustFireBot2// robot's //onScannedRobot// method to fire the cannon with a power level that is inversely proportional to the distance between your robot and the scanned enemy robot. The idea is similar to [[cs-175_sp13_robocode_select_fire_with_variables|what we did earlier to adjust the cannon fire power]]. But instead of using discrete fire power levels like 1.0, 2.0, and 3.0, we will compute the cannon power level based on distance.
The minimum fire power level is 0.1, and the maximum fire power level is 3.0 (See Robocode [[http://robocode.sourceforge.net/docs/robocode/robocode/Rules.html#MIN_BULLET_POWER|API]]). At maximum distance apart, your robot should fire with power=0.1. At minimum distance apart, your robot should fire with power=3.0.
Dropbox //AdjustFireBot2.java//.
== Algorithm ==
* Use the //[[http://robocode.sourceforge.net/docs/robocode/robocode/Robot.html|getBattleFieldHeight and getBattleFieldWidth]]// methods to get the dimensions of the field.
* Compute maximum distance as the length of the diagonal distance from corner to corner of the field (compute hypotenuse of the right triangle)
* //Math.sqrt//( height * height + width * width )
* Fire power (P) should be related to the distance to the other robot (D) like this (compute P from this formula):
P * (maximum distance) = ((maximum distance) - D ) * 3.0
(Re-arrange and solve for P; assumes you passed algebra.)
* No //if// statements are necessary for AdjustFireBot2.
== Template ==
See the [[AdjustFireBot template|AdjustFireBot2 template]].
==== 2. CenterBot1 ====
Create //CenterBot1//. //CenterBot1// moves to the center of the arena, spins around in a circle, and stops.
Dropbox //CenterBot1.java//.
== Algorithm ==
* To get the x,y coordinates of the center of arena, use the //getBattleFieldHeight// and //getBattleFieldWidth// methods again. Then, use **ONE** of the approaches listed below.
* (Approach #1) Get robot's current heading. Turn robot to point north (heading=0). Move robot ahead or back to get to middle of screen vertically. Turn robot to point east (heading=90). Move robot ahead or back to get to middle of screen horizontally.
* (Approach #2) Go ahead until you hit a wall. Use onHitWall method to find angle at which you hit wall. Turn to go into a corner. Go halfway to next corner. Go to center.
* (Approach #3) Once you know the x,y coordinates of the center of the arena, point robot in direction of center, compute distance to center, move ahead to center.
* __+20% extra credit if you use approach #3.__
* Use robot getX() and getY() methods to get current x,y of robot.
* Requires [[CenterBot3 Approach #3 trigonometry functions|trigonometry functions]]
== Template ==
See the [[CenterBot template]] for Approach #1.
----
===== Assignment 5 - Decisions =====
Deadline: Thurs, 11/6, noon
* Use the ecampus dropbox for CS-175/501a.
Download the [[https://piazza.com/class_profile/get_resource/hzkswvpxv2r2nu/i1vdtt1euoc61e|Assignment 5 file]].
----
===== Assignment 6 - Decisions, Methods =====
Deadline: Tues, 11/18, noon
* Use the ecampus dropbox for CS-175/501a.
==== 1. Improved randbetween method ====
We worked on a convenient [[http://rockhopper.monmouth.edu/cs/jchung/cs175/methods_lab_-_tryrandom3_using_randbetween_method|randbetween method]] before. Improve the //randbetween// method in the following way:
* Currently, the randbetween method must be called with the low value first, and then the high value, e.g., randbetween(1, 6). If the high value is given first, e.g., randbetween(6, 1), the method will return invalid values. Improve randbetween to allow the randbetween method to be called with low and high values in any order:
// Simulate rolling a 6-sided die (1-6):
System.out.println( randbetween(1, 6) );
System.out.println( randbetween(6, 1) ); // also works
// Simulate a coin toss (0-1):
int randnum = randbetween(0, 1);
System.out.println(randnum);
randnum = randbetween(1, 0); // also works
System.out.println(randnum);
Test the improved //randbetween// method in a program called //TryRandom4.java//. Dropbox //TryRandom4.java//.
==== 2. getBMI method ====
We worked on a [[http://rockhopper.monmouth.edu/cs/jchung/cs175/data_types_and_input_lab_1|program to compute the body mass index]] (BMI). Complete the following method to compute and return the BMI value for a given mass in lbs., and height in feet & inches:
public static double getBMI( double mass, int feet, int inches )
{
double bmi; // compute from mass, feet and inches
//
// Your code goes here.
// Use no System.out.println statements.
// Do no Scanner input.
//
return bmi;
}
Dropbox //getBMI.java//. Make sure //getBMI.java// contains only the //getBMI// method code.
==== 3. ValidTriangles ====
Write and dropbox a program //ValidTriangles.java// that reads three edges for a triangle and determines whether the input is valid. The input is valid if the sum of any two edges is greater than the third edge. Here are two sample user dialogs from running this program:
Enter the three integer edges of a triangle: 1 2 1
Can edges 1, 2, and 1 form a triangle? NO
Enter the three integer edges of a triangle: 2 2 1
Can edges 2, 2, and 1 form a triangle? YES
Use a //while(true)// loop in the program to read triangle edges and determine valid triangles continuously.
----
===== Assignment 7 - Decisions, Loops, Methods =====
Deadline: Wed, 11/26, 7pm
* Use the ecampus dropbox for CS-175/501a.
==== 1. validTriangle method ====
This is related to the //ValidTriangles// program from Assignment 6. (See the Resources page, Resources tab on Piazza for all assignment solutions.) Complete the following method to return true if a triangle with given side1, side2 and side3 is a valid triangle.
public static boolean validTriangle( int side1, int side2, int side3 )
{
boolean valid = false; // assume false
//
// Your code goes here.
// Use no System.out.println statements.
// Do no Scanner input.
//
return valid;
}
Put the //validTriangle// method in the //ValidTriangles2.java// program, along with the //main// method. In the //main// method of //ValidTriangles2.java// read three edges for a triangle via Scanner, call the //validTriangle// method with the three edges as input parameters, and print whether the input is valid. Here are some sample user dialogs from running //ValidTriangles2.java//:
Enter the three integer edges of a triangle (-1 to quit): 1 2 1
Can edges 1, 2, and 1 form a triangle? NO
Enter the three integer edges of a triangle (-1 to quit): 2 2 1
Can edges 2, 2, and 1 form a triangle? YES
Enter the three integer edges of a triangle (-1 to quit): -1
Use a //while(true)// loop in the //main// method to read triangle edges and determine valid triangles continuously. Allow the user to enter -1 to quit.
Dropbox //ValidTriangles2.java//.
==== 2. GuessNumber ====
Complete the //[[loops_lab_-_number_guessing_game|GuessNumber.java]]// program. Your program must include the 3 possible improvements.
Dropbox //GuessNumber.java//.
==== 3. Review exercises ====
From the book, do review exercises R6.2 (a only), R6.15, R6.19 (a, b, d), R6.27. Write your answers in //assign7revex.txt// (plain text) and dropbox it.
----
===== Assignment 8 - Loops =====
Deadline: Tues, 12/9, noon
* Use the ecampus dropbox for CS-175/501a.
==== 1. FactorialProg1.java ====
**(CS-175/501a-50 only)**
Complete [[https://piazza.com/class_profile/get_resource/hzkswvpxv2r2nu/i398cwa1cqm56s|problem #11 of Quiz 2]]. Dropbox //FactorialProg1.java//.
==== 2. EquivalentFor.java ====
Rewrite the following program using a 'for' loop instead of a 'while' loop. The rewritten program must function in the same way as the original program below. Dropbox the rewritten program as //EquivalentFor.java//.
public class EquivalentFor
{
public static void main( String[] args )
{
int i = 0;
int j = 0;
while ( i <= 20 ) {
j = i * i * i;
i = i + 1;
}
System.out.println( "final values: i = " + i + ", j = " + j );
}
}
==== 3. Sample lab test programs ====
See [[https://piazza.com/class_profile/get_resource/hzkswvpxv2r2nu/i3980kcrxoog3|assign8sample2.txt]]. Dropbox //Prog1.java, Prog2.java and Prog3.java//.
----