This class exercise is designed to get you acquainted with Interfaces in Java. When a seasoned programmer or potential employer asks you if you know Object-Oriented Programming (OOP), they do not mean, "do you know classes and objects?" Instead, they mean, "do you know the pillars of OOP?" The pillars of OOP are interfaces, inheritance, and polymorphism. In this exercise, we continue the exploration of interfaces and interface-based polymorphism.
- A basic understanding of creating and implementing interfaces in Java.
- CSCI 1302 Interfaces Tutorial
- LO2.e: (Partial) Utilize existing generic methods, interfaces, and classes in a software solution.
- LO3.b: Create class, interface, method, and inline documentation that satisfies a set of requirements.
- LO3.c: Generate user-facing API documentation for a software solution.
- L04.b: Utilize interface-based polymorphism in a software solution.
In your notes, clearly answer the questions in the following exercise steps. All instructions assume that you are logged into the Odin server.
NOTE: For each step, please provide in your notes the full command that you typed to make the related action happen along with an explanation of why that command worked. Some commands require multiple options. It is important to not only recall what you typed but also why you typed each of them. If context is necessary (e.g., the command depends on your present working directory), then please note that context as well. You won't need to submit your notes in your final submission. However, if done properly, your exercise notes will serve as a helpful study guide for the exam.
-
Use Git to clone the repository for this exercise onto Odin into a subdirectory called
cs1302-hw03
:$ git clone https://github.com/cs1302uga/cs1302-hw03.git
-
Change into the
cs1302-hw03
directory that was just created and look around. There should be multiple Java files contained within the directory structure. To see a listing of all of the files under thesrc
subdirectory, use thetree
command as follows:$ tree src
-
Generate the API documentation website for all of the code in the
cs1302
package. Host the documentation on Odin usingcs1302-hw03-doc
as the name for your symbolic link. What is the URL to your hosted website? -
Using the API documentation website you generated in the last step, look at the documentation for the three class files provided in the starter code. For each Java file, fill out a row in a table similar to the following in your notes:
Note: If a class is not an interface and does not implement an interface, write
NA
in the second column. Also, the "Depends On" column should list any Java types in thecs1302.hw03
package that the file is dependent upon.Name of the Java file Interface or Implementing Class? Fully Qualified Name (FQN) Depends On ----------------------- ---------------------------------- ---------------------------- ------------ ----------------------- ---------------------------------- ---------------------------- ------------ ----------------------- ---------------------------------- ---------------------------- ------------ -
Read through the Javadoc comments for the
Drivable
interface on the API website you generated or in the source code. Make a note of the methods contained in the interface. You may find it helpful to write them in your notes. -
Look at the
speedUp
method in theCar.java
file. Compare the Javadoc comment for the method in that file to the documentation listed for that method in the API documentation website. Notice that there is no Javadoc comment in the source code. However, a comment still shows up on the website. How is this possible? -
Run the given code through
checkstyle
using thecheck1302
command and fix any errors that it gives. Remember to use the 1302 Style Guide as a reference for the various types of errors that may pop up. -
Before moving on, we highly recommend drawing out the UML diagram for these classes and creating a compile script for quick and easy compilation.
-
Make sure that you are still in the
cs1302-hw03
directory. CompileDrivable.java
and place the compiled code under the preexistingbin
directory. To see a listing of all of the files under thebin
subdirectory, use thetree
command as follows:$ tree bin
Notice that interfaces are compiled the same way as classes. Your output should be similar to the following:
bin └── cs1302 └── hw03 └── contract └── Drivable.class
-
Compile
Car.java
. Remember to set the classpath appropriately sojavac
can locate dependencies.
-
Compile and execute
Driver.java
. It should run properly onceCar.java
is compiled. For now, we only have a single class (Car.java
) that implementsDrivable
. Notice that thetest
method inDriver.java
takes an argument of typeCar
. For now this is okay--just observe what is going on. -
Think of something else in real life that is capable of being driven (other than a car). This can be anything - have fun with it. :)
-
Now, add a class to the
cs1302.hw03.impl
package that represents your new drivable type. That type should be something that is capable of being driven (speeding up and slowing down) and therefore a perfect candidate for a class that implements the actions in theDrivable
interface. Make sure that your class properly implements the interface. Also make sure that your class compiles. Here is a small list of things you should do:- Properly implement the interface.
- Add appropriate Javadoc comments and update your API documentation website.
- Compile your class.
- Run your program through
check1302
to make sure it follows the style guidelines.
What is the command that you used to compile your class?
-
Tricky (hint below): Update
Driver.java
to test your new class. Modify the existingtest
method (don't overload the method by creating a separate method with the same name) so it can be used to testCar
objects and objects of the new type that you just created (what doCar
objects and objects of your new type have in common?). You should not change the name of the method, its return type, or the number of parameters. However, you may modify other parts of the method signature as well as the method's body, as needed. Be sure to update the Javadoc comments in theDriver
class where necessary. In your notes, summarize the changes you made.Hint: If you're stuck on this step, it is similar to the
gatherTotalDonations
method in the video on Interfaces provided in the 1302 Interfaces Tutorial. ThegatherTotalDonations
method used an array ofDonator
references. Your method will just need a singleDrivable
reference. -
Regenerate the API documentation website for all of the code in the
cs1302
package using the Javadoc tool. You should not need to recreate the symbolic link, assuming it still exists from a prior step.
-
Add an abstract method
stop
to theDrivable
interface, including appropriate Javadoc comments for what the method should do. Recompile the interface as well as any code that depends on the interface. Did any compilation problems occur? If so, where and why? -
Without further modifying the code in the interface, fix the errors observed in the previous step. If you find yourself writing additional methods in any of the other Java source code files, then be sure to include appropriate Javadoc comments. For each affected file, list the commands you used to verify that you fixed the errors. Repeat this step, as needed, until the code compiles.
-
Regenerate the API documentation website for all of the code in the
cs1302
package. -
Run the 1302
checkstyle
program using thecheck1302
command on all.java
files. If errors are reported, look up each error message in the Style Guide, fix the error, and repeat until no style errors remain. -
Take a minute to notice your
stop
method in the various places that it appears in the API documentation website that you regenerated in the previous step. -
Do not type the following command:
$ nc towel.blinkenlights.nl 23
The
nc
command provided above is not related to the exercise. If you type it in, then you may end up wasting time. The command connects you to a highly addictive form of entertainment provided Sten S. Stans, an elite Dutch Unix engineer.
Each student needs to individually submit their own work.
-
Create a plain text file called
SUBMISSION.md
directly inside thecs1302-hw03
directory with the following information.- Your name and UGA ID number; and
- The full link to the website generated in this exercise.
Here is an example of the contents of
SUBMISSION.md
.Sally Smith (811-000-999) https://webwork.cs.uga.edu/~user/cs1302-hw03-doc
-
Change directories to the parent of
cs1302-hw03
(e.g.,cd ..
fromcs1302-hw03
). If you would like to make a backup tar file, the instructions are in the submissions steps for hw01. We won't repeat those steps here and you can view them as optional. -
Use the
submit
command to submit this exercise tocsci-1302
:$ submit cs1302-hw03 csci-1302
Read the output of the submit command very carefully. If there is an error while submitting, then it will displayed in that output. Additionally, if successful, the submit command creates a new receipt file in the directory you submitted. The receipt file begins with rec and contains a detailed list of all files that were successfully submitted. Look through the contents of the rec file and always remember to keep that file in case there is an issue with your submission.
Note: You must be on Odin to submit.
Copyright © Michael E. Cotterell, Bradley J. Barnes, and the University of Georgia. This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License to students and the public and licensed under a Creative Commons Attribution-NonCommercial 4.0 International License to instructors at institutions of higher education. The content and opinions expressed on this Web page do not necessarily reflect the views of nor are they endorsed by the University of Georgia or the University System of Georgia.