This homework assignment is designed to provide further practice with command-line text editors as well as compiling Java code organized into packages on a Unix system.
- LO1.a: Navigate and modify files, directories, and permissions in a multi-user Unix-like environment.
- LO1.c: Create and modify text files and source code using a powerful terminal-based text editor such as Emacs or Vi.
- LO1.d: (Partial) Use shell commands to compile new and existing software solutions that are organized into multi-level packages and have external dependencies.
In your notes, 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. 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.
-
In your home directory on Odin create the subdirectory structure seen below. What single command can be used to create all of these directories at once?
homework1 |--- src |--- cs1302 |--- example -
Navigate to the
srcdirectory that you just created. In this example,srcis the default package directory for source code. Inside thesrcdirectory, create a file calledHello.javausing either thetouchcommand or using Emacs. Within this file, write a Java program to accomplish the following tasks:- prompt the user for their name
- read in the user's full name
- output
Hello, <user>with their name instead of<user>.
Remember: Don't forget the class declaration and the
mainmethod! On Odin, you have to type these out manually. -
Compile and run your code directly from the default package for source code (the
srcdirectory in this example). Don't use the-doption forjavacin this step. In which directory is the compiled code contained? -
Execute the
treecommand from inside of yoursrcdirectory. You should see output similar to this:. ├── cs1302 │ └── example ├── Hello.class └── Hello.java -
This example demonstrates compiling without using a Java package. In the next step, we will use packages to further organize our code. Once you are confident that it is working, delete the compiled (byte) code (the class file - not your source code).
-
Move the
Hello.javafile (source code) into thecs1302.examplepackage. What two things must be done to accomplish this? Hint: Named Packages -
For better organization, let's separate the source code from the compiled code. Directly inside the
homework1directory, add a subdirectory calledbin. This directory will be the default package for our compiled code. From withinhomework1, what is the single command to compileHello.javaand place the compiled code into thebindirectory? Remember to use tab completion when typing in Unix to avoid mistakes and save you time! -
From the
homework1directory, what is the single command to run theHelloprogram? -
From your home directory (not
homework1), what is the single command to run theHelloprogram? -
Execute the
treecommand from directly within yourhomework1directory. If the previous steps were executed correctly, you should see the following output:Note: If you see any tilda (~) files, those are just backup copies of older versions of your files. You can ignore those.
. ├── bin │ └── cs1302 │ └── example │ └── Hello.class └── src └── cs1302 └── example └── Hello.java
-
Navigate to the
homework1folder and add acs1302.utilitypackage directory to your hierarchy. Note: Theutilitydirectory will need to be inside of thecs1302folder. In your notes, write the command to create theutilityfolder in the proper location without changing out of thehomework1directory. -
Create a class called
MyMethodsin thecs1302.utilitypackage. Make sure to type the class declaration. This class won't need amainmethod because it won't be run directly. -
Add a single, static method called
maxto theMyMethodsclass with the following signature:/** * Returns the larger of the two input parameters. If the parameters * are equal, either value can be returned. * * @param num1 the first number to check. * @param num2 the second number to check. * @return the larger of the two parameters. */ public static int max(int num1, int num2) { // Your code here } // max
-
Implement the body of the
maxmethod. The method should determine and return the bigger of twointvalues supplied as parameters to the method. These values will be supplied when you call the method in a later step. Note: You should not use theScannerclass inside of theMyMethodsclass.PROTIP: Unless it is specifically stated, it is best to always work from the main project directory. For example, while working on the source code for this exercise, you can modify all of the files without leaving the
homework1directory by providing the relative path (using tab completion) to the file from thehomework1directory. This means you would rarely use thecdcommand while working on an assignment. When Unix beginners overuse thecdcommand, they often find themselves lost in the directory structure which can lead to mistakes and, in rare cases, frustration! -
Assuming your present working directory is still
homework1, what is the command to compileMyMethods.javaand place the byte code in thebindirectory? Remember, there are no dependencies when compilingMyMethods.javaas it does not depend on any other source code.Now that you've compiled both Hello.java and MyMethods.java, look in the
bindirectory. Notice the directory hierarchy that was automatically created. -
Now, modify the
Helloclass in theHello.javafile. Have it print out the maximum of two values input by the user. To accomplish this, in themainmethod of yourHelloclass, you should read two integers from a validScannerobject and pass those values into themaxmethod you created in theMyMethodsutility class. In other words, you should call themaxmethod inMyMethodsfrom themainmethod in yourHelloclass and print the result.Note: You must import
MyMethodsinHello.java. -
What is the command to compile the
Helloclass from thehomework1directory and place the compiled code intobin? Note: there is now a dependency inHello.java. It relies on the code fromMyMethods, so the compiler needs to know where to find that class. -
Execute the
treecommand from directly within yourhomework1directory. If the previous steps were executed correctly, you should see the following output:Note: If you see any tilde (~) files, those are just backup copies of older versions of your files. You can ignore those.
. ├── bin │ └── cs1302 │ ├── example │ │ └── Hello.class │ └── utility │ └── MyMethods.class └── src └── cs1302 ├── example │ └── Hello.java └── utility └── MyMethods.java
Each student needs to individually submit their own work.
-
Create a plain text file called
SUBMISSION.mddirectly inside thehomework1directory with your name and UGA ID number.Here is an example of the contents of
SUBMISSION.md.Sally Smith (811-000-999) -
From the parent directory of the
homework1folder, use thesubmitcommand to submit this assignment tocsci-1302:$ submit homework1 csci-1302Read 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
submitcommand 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 receipt 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.
If you would like to make a copy of your homework assignment into a single, zipped file, follow the steps below (not required)
-
Change directories to the parent of
homework1(e.g.,cd ..fromhomework1). We will use thetarcommand to combine our directory hierarchy into a single file for backup purposes. To do this, execute the command:$ tar -cf homework1.tar homework1/Read the manual page for
tarin section 1 of the manual to learn more abouttarand its various options. -
List the contents of your directory and make sure you see
homework1.tar. Now, to make sure this tar file contains all of the work you just did, list all of its contents with the command:$ tar -tf homework1.tar -
Now, to make this file smaller, we will compress it with
gzip. Execute the command:$ gzip homework1.tarRead the manual page for
gzipin section 1 of the manual to learn more aboutgzipand its various options. -
List the contents of your directory and make sure you see
homework1.tar.gzinstead ofhomework1.tar. Now, you have a compressed backup of your directory saved in the.tar.gzfile.
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.