L03

Due by the end of Tuesday, 26 January 2021


SUBMIT   /   CHECK

This Week's Activities

Activity 1
Download the program file ArrowTest.java. It uses a data type Arrow. Create that data type and all the methods it needs so that the program runs and produces this output:
> -> --> ---> ----> -----> ------> -------> --------> ---------> The last arrow had a length of 9 (not including the arrow head).
NOTE: the loop in the program ends the lines; draw just draws the arrow.
Programming Activity 2
Add a toString method to the Arrow class. Activate the Activity 2 code in ArrowTest and confirm that the output above is followed by this:
> -> --> ---> ----> The last arrow had a length of 4 (not including the arrow head).
HINT: You can add more to a String in Java by using the += operator. To get the String you need for toString, you just need to add the correct number of "-" characters.

Alternatively, there is a way to create the required String using a char[], Arrays.fill and String.valueOf.

Programming Activity 3
Overload the constructor so that the client can choose which side of the arrow the arrow-head is on. The options are: Declare int values for those three constants.
I recommend using either 1, 2 and 3 as the three values, or -1, 0 and 1. When you're assigning values, try to think about which values would make your later programming easier.

Make sure that the output from Activity 1 is not changed! (That is, if the client doesn't say which end the arrow head is on, then it's on the RIGHT.)

You will need to update the draw and toString methods so that the arrows are drawn properly.

Add two more methods, pointsLeft and pointsRight, so the client can ask an arrow whether it points in that direction.

Here is where your earlier choices on constants might make your life a little easier (or harder).

Test your changes by activating the Activity 3 code in ArrowTest. Ensure that the output above is followed by:

This arrow points HERE<---------- This arrow points HERE<----->HERE, too! This arrow points ------->HERE -------><-----><---------- ---->.pointsLeft() is false <----------.pointsLeft() is true <----->.pointsLeft() is true ------->.pointsLeft() is false ---->.pointsRight() is true <----------.pointsRight() is false <----->.pointsRight() is true ------->.pointsRight() is true
Programming Activity 4
Modify your constructor (if you haven't done this already) to throw an IllegalArgumentException when the requested length is negative, or when the requested direction (LEFT, RIGHT, BOTH) is invalid.

Activate the code for Activity 4 and confirm that the output above is followed by:

Illegal length: -5 Illegal length: -3 Illegal direction: -7 Illegal direction: 7
NOTE: we will learn about try-catch blocks later in the term. You don't need to understand them right now.

Submit this/these files:

You will be graded on the following:

  1. Arrow contains an appropriate IV for its length
  2. ... and appropriate IV(s) for its direction(s)
  3. ... and the required constants
  4. It has a 2-arg constructor that sets length and direction(s) correctly`
  5. It has a 1-arg constructor that sets the length correctly
  6. ... and the direction(s) correctly
  7. ... by calling the 2-arg constructor
  8. Exceptions are thrown as required
  9. draw behaves correctly for all arrows
  10. toString behaves correctly for RIGHT arrows
  11. ... and also for LEFT and BOTH arrows
  12. The points methods behave correctly

SUBMIT   /   CHECK