An introduction to the Java programming language and tools including Eclipse, Eclipse Git client, etc.
Followed by an introduction to using conditionals and loops in the Java programming language.
This is a lab used in Computer Science II (CSCE 156, CSCE 156H) in the Department of Computer Science & Engineering at the University of Nebraska-Lincoln.
Chris Bourke wrote this lab, revamped and combined with Lab 2 by Sarah Roscoe for Summer 2021 - 2024.
In each lab there may be pre-lab activities that you are required to complete prior to attending lab. Failure to do so may mean that you will not be given full credit for the lab.
Prior to lab you should read/review the following resources.
if-else tutorial: http://download.oracle.com/javase/tutorial/java/nutsandbolts/if.htmlforloop tutorial: http://download.oracle.com/javase/tutorial/java/nutsandbolts/for.htmlwhileloop tutorial: http://download.oracle.com/javase/tutorial/java/nutsandbolts/while.html
Following the lab, you should be able to:
- Clone projects from GitHub using Eclipse
- Open, compile, and execute a given Java program in Eclipse.
- Write a simple program in the Eclipse IDE, compile, and execute that program.
- Use
if-elsestatements to control the logical flow of the program. - Use
forandwhileloops to implement repetition statements in a program. - Write complex programs that require conditional logical statements and loops.
You will work on this lab with a partner One of you should submit your code to the corresponding Handin assignment, or both of you should submit identical code. Handin link: https://cse-apps.unl.edu/handin
You will work in a pair programming setup. At the start of each lab, you may be randomly paired up with another student by a lab instructor. One of you will be designated the driver and the other the navigator.
The navigator will be responsible for reading the instructions
and telling the driver what is to be done. The driver will be
in charge of the keyboard and workstation (on Zoom, this means the driver shares their screen). Both driver and
navigator are responsible for suggesting fixes and solutions
together. Neither the navigator nor the driver is "in charge."
Beyond your immediate pairing, you are encouraged to help and
interact and with other pairs in the lab.
Each week you should try to alternate: if you were a driver last week, be a navigator next, etc. Resolve any issues (you were both drivers last week) within your pair. Ask the lab instructor to resolve issues only when you cannot come to a consensus.
Because of the peer programming setup of labs, it is absolutely essential that you complete any pre-lab activities and familiarize yourself with the handouts prior to coming to lab. Failure to do so will negatively impact your ability to collaborate and work with others which may mean that you will not be able to complete the lab.
You may already have "Java" installed on your computer, but this is most likely the Java Virtual Machine (JVM) that allows you to run Java programs. To actually develop Java programs you need a Java Development Kit (JDK). There are several alternative JDKs and you are welcome to explore them, but for this course, we are recommending Oracle's JDK available for download at the following URL. Follow the instructions for downloading and installing.
https://www.oracle.com/java/technologies/javase-downloads.html
Eclipse is an Integrated Development Environment (IDE) for Java development. There are many other popular IDEs available and you are welcome (and encouraged) to try them out and use them if you wish. However, for this course, most instructions will assume the use of Eclipse and it is the IDE we will primarily use for this course. You can download and install Eclipse at the following URL.
Each lab will have some starter code and other artifacts
(data files, scripts, etc.) that will be provided for to you.
The code is hosted on Github (https://github.com) and you must
clone your own copy to work with it. You will not need to
know the details of using git nor be a registered Github user
to get access to the code necessary for your labs. However,
you are highly encouraged to learn this essential tool.
You may find it very useful to keep track of your own code
and to share code if you work in pairs or groups.
To check out the code for this lab, do the following. You may want to reference this step-by-step process in subsequent labs.
- First we need a Git perspective (a context in the Eclipse User Interface that will allow us to work with Git). To open the Git perspective, click on the "Open Perspective" tab in the upper right:
-
Select "Git" from the menu and click
OK -
Click the "Clone a Git repository" in the Git Repositories navigation menu:
- Copy/paste or type into the URI field, the URL: https://github.com/sroscoe2/CSCE156-Lab01/
- Click
Next; once Eclipse has grabbed the project, the "master" branch should be selected (checkbox); clickNextagain.
- Select the directory where you want your project to be saved.
Caution: the default option may not correspond to your default workspace. You will want to change it to your workspace. Mark the "Import all existing projects after clone finishes" checkbox option or you will need to manually import the cloned project into Eclipse.
- Switch back to your Java or JavaEE perspective and you can see your cloned project.
All students should complete this Java section, even if you are already familiar with Java, in order to familiarize yourself with how labs will work for the semester.
We will now familiarize you with Eclipse by editing an existing project's code.
-
Expand the
srcdirectory. Under this we have a package namedunl.cse. Java classes are organized in a hierarchy of packages. Packages correspond to actual directories in your file system. -
Expand the package and you'll find several classes. All Java code must be contained in a class. This is in contrast to other languages that may allow global variables or allow functions to exist without an object or a class.
-
Double click on the
StatisticsDemoclass to open it in the Eclipse editor. This class contains a main method,public static void main(String args[])In Java, classes are executable only if a main method is defined.
Classes without amainmethod can be used by other classes, but they cannot be run by themselves as an entry point for the Java Virtual Machine (JVM). -
Click on the "play" button as highlighted (click "Proceed" if prompted):
Though the program runs, it does not output correct answers. You will need to modify these classes to complete the program.
- Implement the
getMax()method in theStatisticsclass. Use thegetMin()method for directions on syntax. - Implement the
getSum()method in theStatisticsclass. Use the other methods for direction on syntax. - Rerun the program to verify that it now works.
The program you've completed is interactive in that it prompts the user for input. You will now change the program to instead use command line arguments to read in the list of numbers directly from the command line.
Command line arguments are available to your main method through
the args array of Strings. The size of this array
can be obtained by using args.length which is an
integer. Modify your code to iterate through this array and convert
the arguments to integers using the following snippet of code:
for(int i=0; i<args.length; i++) {
array[i] = Integer.parseInt(args[i]);
}The command line may not be apparent as you are using an IDE.
However, it is still available to you. Instead of clicking the "Play"
button to run your program, click the down arrow right next to it.
Then select "Run Configurations". This brings up a dialog box with
which you can run custom configurations. Click the Arguments tab and
enter a space-delimited list of numbers under "Program Arguments"
and click "Run".
In the next activities you'll get more familiar with using Eclipse and the convenient functionality IDEs provide.
No man is an island. Good code depends on selecting and (re)using standard libraries whenever possible so that you are not continually reinventing the wheel. This activity will familiarize you with how to import and use an external Java library. Java libraries are usually packaged into JAR Java ARchive files which contain a collection of compiled class files and other resources necessary to use the library.
- You'll notice that there are compilation errors in the
Birthday.javafile. This is because this class uses other classes that are not available in the standard Java Development Kit (JDK). It instead uses classes from the Joda-Time library; a library of useful classes and utilities for dealing with dates, times, intervals, durations, etc. - The JAR file,
joda-time-2.0.jarhas been included in the project in thelibfolder. External libraries are usually kept in a hierarchy of folders like this (you can create your own folders by right-clicking the project and selecting "New" then "Folder") - Right-click the JAR file and select "Build Path" then "Add to Build Path." The library is now included in your project and the compiler errors should go away.
Though the syntax errors should now be resolved, the code isn't pretty making it difficult to read and understand. Eclipse provides a built-in code formatter functionality. Typically if you write good code to begin with it will automatically provide consistent indentation and other stylistic features. It is best practice to get in the habit of writing good, clean code automatically. However, if you need to clean up a file in one shot you can do use the auto-formatter feature.
- On Windows: press
control-shift-fto reformat the code - On Mac: press
shift-command-fto reformat the code
Another issue with the code is that it is using lower_underscore_casing
for some of its variables. Change the variable names to the preferred
lowerCamelCasing convention in Java. You could do this manually but
a neat trick that most IDEs provide is as follows.
- Highlight the variable name (any instance will do)
- Right click and select
RefactorthenRename - Type the new variable name and hit enter and it will automatically be changed for all instances!
Finally, every non-trivial class and method should have documentation.
In Java, it is standard to use doc-style or "javadoc" comments. Look
at the Statistics.java file again to see the format for these style of comments.
Add documentation to this file to complete it.
Though the program should have no syntax errors, if you run it, no output will be displayed. You need to complete the program as follows.
- For the variables, name, month, date, and year, enter your own information (your name and your birthday)
- Add appropriate code (using
System.out.println()) which prints to the standard output a full line) a greeting similar to the following.Greetings, NAME. Today you are XX years, XX months, and XX days old.Of course, the placeholders should be replaced with variable values.
In Java, variable values can be concatenated with strings using the+(plus) operator. - Add a conditional statement that, if today is the user's birthday
will output
Happy Birthday. If it is not the user's birthday, outputYour friends have XX shopping days until your next birthdayagain with an appropriate variable value.
Java provides standard control structures for conditionals and
repetition. Specifically, Java provides the usual if-else
statements as well as for and while loops. The syntax for these
control structures should look familiar. Some examples:
if(x > 0) {
//DO SOMETHING
} else if(x < 0) {
//DO SOMETHING ELSE
} else {
//OTHERWISE
}
for(int i=0; i<n; i++) {
//DO SOMETHING
}
int i = 0;
while(i < n) {
//DO SOMETHING
i++;
}In addition, Java provides a foreach-loop, also referred to as an enhanced for-loop, for iterating over collections or elements in an array. This feature is mostly for convenience. The following example demonstrates how to use this loop.
String arr[] = new String[10];
...
for(String s : arr) {
System.out.println(s);
}An enhanced for-loop can be used for Set and List collections.
Natural numbers are the usual counting numbers; 1, 2, 3, ... In this
exercise you will write several loops to compute the sum of natural
numbers 1 thru n where n is read from the command line. You will
also write an enhanced for-loop to iterate over an array and process
data.
-
Open the
Natural.javasource file. The code to read innhas been provided for you. An array mapping integer values 1 thru 10 to text values has also been created for you. -
Write a
for-loop and awhile-loop in the relevant methods to compute the sum of natural numbers 1 thrunand return the result. In themainmethod call your methods and output the result. -
Write a an enhanced for-loop to iterate over the elements of the
zeroToTenarray. As you iterate over the elements, concatenate each string, delimited by a single space to a result string and print the result at the end of the loop. Your result should look something like the following:zero + one + two + three + four + five + six + seven + eight + nine + ten = 55
When filing for federal taxes, a credit is given to tax payers with dependent children according to the following rules. The first dependent child younger than 18 is worth a $1,000.00 credit. Each dependent child younger than 18 after the first child is worth a $500 tax credit each. You will complete a Java program to output a table of dependent children, how much each contributes to a tax credit, and a total child tax credit. Your table should look something like the following.
Child Amount
Tommy (14) $1000.00
Richard (12) $500.00
Harold (21) $0.00
Total Credit: $1500.00
-
Open the
Child.javaandChildCredit.javasource files -
The
Childclass has already been implemented for you. Note how theChildclass is used. Several instances of children have been created and placed into aList.Similar to an array, a
Liststores elements using 0-indexing. Each one can be accessed using thegetmethod. For example,kids.get(i)gets the i-th kid. You could also use an enhanced for loop to iterate over the elements in theList. -
Implement the
produceReportmethod to compute the child tax credit(s) and output a table similar to the one above. Your method should return the grand total of the credit.Each child is an instance of the
Childclass. Each instance has its own variables so that each child can be a different age. To get a particular child’s age, you can use a getter method. For example, to get the age oftomyou can use the syntax,tom.getAge()
Use the String.format() method to reformat the output of the Child Tax
Credit program to print every piece of data in its own column.
Every lab will come with a collection of test files that contain a suite of unit tests using the JUnit testing framework. Before you submit your lab, you should run these tests locally to verify that your code is correct.
- Open the
StatisiticsTests.javasource file in thesrc/test/javasource folder. This file contains several unit tests written using JUnit annotations. You are encouraged to explore how these tests are written and work and to even add your own tests but otherwise, the file is complete. - Run the test suite by clicking the usual "Play" button. A report will be presented in a JUnit tab detailing which test cases pass and which fail along with expected output and the actual output (for failed test cases).
- Address any issues or failing tests by debugging your code and rerun the test suite until all tests pass.
Many of your assignments will include a programming portion that will require you to hand in soft-copy source files for graders to compile and evaluate. To do this, you will use a web-based handin program. After handing your file(s) in, you can then grade them by using the web grader. To demonstrate, do the following.
- Open a browser to https://cse-apps.unl.edu/handin
- Login with your CSE credentials
- Click on Lab 1.0 and hand in the following files:
Natural.javaChildCredit.javaStatistics.javaBirthday.javaYou can either click the large "handin" area and select the file or you can drag-drop the files. You will be able to re-handin the same file as many times as you want up until the due date.
Now that the file has been handed in, you can "grade" yourself by using the webgrader
- Open a new tab/window and point your browser to https://cse-linux-01.unl.edu/~c-sroscoe2/CSE156/grade/ (depending on your section, this URL may be different).
- Fill the form with your CSE login and password, select the appropriate assignment and click "Grade"
- Observe the expected output and compare it to your output to be sure that your program is correct.
For labs, the grader script simply runs the provided JUnit test suite, but it may run additional or modified tests. In any case, be sure your code compiles, runs and passes all tests in the webgrader. Address any issues and resubmit as many times as you like up to the due date.





