十人十色 (Ten People, Ten Colors) --Japanese Idiom
This class exercise explores how to utilize inheritance in Java to create new classes that are directly based on existing ones, with an emphasis on code reuse and design implications. It also revisits how to commit and view changes to a local Git repository.
- 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.
- LO4.c: (Partial) Design, create and use inheritance relationships in a software solution.
- LO4.d: Utilize inheritance-based polymorphism in a software solution.
In your notes, answer the following questions. These 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-hw04:$ git clone --depth 1 https://github.com/cs1302uga/cs1302-hw04.git -
Set up your Git username and email on Odin by modifying and executing the commands below. When setting the
user.nameproperty, please provide your name as it appears on eLC and Athena. If you have a preferred name, then you may include it in parentheses. For theuser.emailproperty, please use your@uga.eduemail address:$ git config --global user.name "Mona Lisa (Liz)" $ git config --global user.email "email@uga.edu"You can verify that these properties were set correctly by observing the output of the following commands:
$ git config --global user.name $ git config --global user.email -
Change into the
cs1302-hw04directory 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 thesrcsubdirectory, use thefindcommand as follows:$ find srcYou should recognize this code from the inheritance lecture! Refer to your notes from class about any inter-dependencies and/or inheritance between the files.
Please Note: This homework assignment may be slightly different from what we did in class. Do not make any assumptions about the code based on what was discussed in the lecture.
-
In your notes, draw a complete, proper UML diagram for the three classes contained in the starter code. You might need to devote an entire page to this. We recommend using a pencil. Refer to the CSCI 1302 UML Chapter if needed. Specifically, each individual class diagram should contain:
- Class name;
- Variables;
- Constructors and methods;
- If needed, a solid generalization arrow (
extends) to a parent class; and - If needed, a dashed generalization arrow (
implements) to an interface.
In a class diagram, do not list inherited members (methods or attributes) unless they are explicitly overridden. Be sure to include visibility modifiers (e.g.,
+,#,~,-) and type/return type information where needed. Also, remember that generalization arrows have a triangle arrowhead, which differentiates them from dependency and aggregation associations.
-
Create and document a
Rectangleclass in thecs1302.shapespackage. It should extend theShapeclass. YourRectangleclass should include at least:- Relevant instance variables for describing a rectangle,
- A constructor,
- A set of overrides for the
getAreaandgetPerimetermethods, and - Getter methods for the instance variables.
-
Compile your
Rectangleclass. If you encounter any compilation errors:- Write the error down in your notes;
- Fix the error in your code;
- Recompile; then
- Note the fix in your notes.
- Repeat as needed.
-
Make sure that all Java files pass the
checkstyleaudit. -
Since we've added a new class to our project, it's a good idea to save our work using Git. In this step, you will do your first commit to your local git repository (on the Odin server) so that you (and the instructors/TAs) can see what you've done. This will create a snapshot of what you've done so far. We will use many additional features of git in future tutorials and exercises. These features include but are not limited to, creating multiple development branches, rolling back to a previous snapshot (version of the code), and collaborating with teammates.
Check the status of your local copy of the repository using the following command:
$ git statusYou should see a message similar to the following:
# On branch master # Untracked files: # (use "git add <file>..." to include in what will be committed) # # src/cs1302/shapes/Rectangle.java nothing added to commit but untracked files present (use "git add" to track)As the message suggests, your
Rectangle.javafile is untracked. This simply means thatRectangle.javais a new file that is not currently tracked by Git. To add the new file to our repository so we can save the changes, use thegit addcommand as described in the message. The full command would look something like:$ git add src/cs1302/shapes/Rectangle.javaNow Git is tracking the
Rectangle.javafile and we can add the changes to our next commit snapshot. Executegit statusagain to see the difference in how Git views this file after we added it. -
Now, use Git to commit the changes that you made to your source code to your local copy of the repository using the command below. Don't forget the string associated with the
-m(message) option to give a brief, one-sentence description of the changes you made to the source code. Feel free to change the message after-min the command below.$ git commit -m "Added a Rectangle class to the Shapes hierarchy" -
Generate the API documentation website for all of the code in the
cs1302package into thedocdirectory. You may need to create thedocdirectory if it does not already exist. Host the documentation on Odin usingcs1302-hw04-docas the name for your symbolic link. Write the full URL for theRectangleclass in your notes.
-
Create and document a
Squareclass in thecs1302.shapespackage. It should extend yourRectangleclass. You shouldn't need to introduce any new instance variables, but you should include a constructor that makes proper use of thesuperkeyword. -
Compile your
Squareclass. If you encounter any compilation errors:- Write the error down in your notes;
- Fix the error in your code;
- Recompile; then
- Note the fix in your notes.
- Repeat as needed.
-
Make sure that all Java files pass the
checkstyleaudit. -
You just utilized inheritance to reduce the amount of code needed to create a new class! Your
Squareclass has everything aRectanglehas. How much new code did you need to write? Usewcto check your code stats!- How many lines are in
Rectangle.java? - How many lines are in
Square.java?
Contrary to popular belief, fewer lines of code is usually better. Also, if you need to make a change to a
Rectanglemethod, then this change will propagate throughout all descendants in your hierarchy that don't explicitly perform an override of the relevant method. - How many lines are in
-
Tell Git to track changes made to your
Square.javafile using thegit addcommand, then commit the changes to your local copy of the repository. Be sure to include a good log message using the-moption to thegit commitcommand. -
Generate the API documentation website for all of the code in the
cs1302package into thedocdirectory. You may need to create thedocdirectory if it does not already exist. Host the documentation on Odin usingcs1302-hw04-docas the name for your symbolic link. Write the full URL for theSquareclass in your notes.
-
Create and document a
ShapeDriver(driver) class in thecs1302.shapespackage. Inside of themainmethod do the following:-
Create a
Shapearray, and populate it with two objects of each class (so, 8 total). Something like the following (will need to be modified):Shape[] shapes = new Shape[] { new Ellipse(1.1, 2.5), new Circle(1.5) };
-
Loop over the array. For each element in the array, print the name of the shape using the
getNamemethod as well as the return values of thegetAreaandgetPerimetermethods. Which classes do not explicitly definegetAreaandgetPerimeterin their source code?
-
-
Make sure that all Java files pass the
checkstyleaudit. -
Compile the
ShapeDriverclass and run it. Is the output what you expected? -
Tell Git to track changes made to your
ShapeDriver.javafile, then commit the changes to your local copy of the repository. Be sure to include a good log message. -
Regenerate the API documentation website for all of the code in the
cs1302package. What is the direct URL to the API documentation for the class that you wrote for this checkpoint? -
Wonder why we've been having you commit all of these changes to Git? Well, each time you save, you are taking a snapshot of your code. Think of it as a save point. Eventually, we will explore the idea of going back in time to an older version of the code. Imagine being able to save your code in various stages of development. Then, being able to make modifications and have a choice whether to incorporate those changes into your code or to discard them! That's just one of the benefits of using Git.
-
To view all of the commits you made in this homework assignment, run the following command:
$ git logFor more concise output, use the following:
$ git log --all --decorate --oneline --graphor just:
git adogfor short. Cool, huh? :)
You will not submit this assignment. However, this is important practice for the first exam. Make sure you understand the concepts from this homework assignment before the exam date.
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.