An introduction to the loops in the Java programming language.
This is a lab used in Computer Science II (CSCE 156) for Fall 2024 in the School of Computing at the University of Nebraska-Lincoln.
for
loop tutorial: http://download.oracle.com/javase/tutorial/java/nutsandbolts/for.htmlwhile
loop tutorial: http://download.oracle.com/javase/tutorial/java/nutsandbolts/while.html
Following the lab, you should be able to:
- Use
for
andwhile
loops to implement repetition statements in a program. - Write complex programs that require conditional logical statements and loops.
Note that the lab may involve some concepts, classes, or methods not covered (yet) in the class. You should be able to complete the lab without fully understanding them. If you have any questions about them, please feel free to ask our LAs.
At the start of each lab, you may find a team member by yourself or randomly paired up with another student by a lab instructor. One of you will be designated the driver and the other the navigator. If you prefer to work on this lab by yourself, that is fine too. Each week you should try to alternate: if you were a driver last week, be a navigator next, etc.
Note that each student must submit the code to GradeScope for grading.
Clone this project code for this lab from GitHub in Eclipse using the
URL: https://github.com/lisongxu/CSCE156-Lab-Java-Loop
. Refer to Lab 1.0 for
instructions on how to clone a project from GitHub.
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 through n
where n
is read from the command line. Refer to Lab 1.0 for
instructions on how to use the command line argument.
-
Open the
Natural.java
source file. The code to read inn
has been provided for you. The code to output the result has also been provided for you in themain
method. -
Write a
for
-loop and awhile
-loop in the relevant methods to compute the sum of natural numbers 1 throughn
and return the result.
When filing for federal taxes, a credit is given to taxpayers with dependent children according to the following rules.
- The first (not necessarily the oldest) 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.00 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.java
andChildCredit.java
source files -
The
Child
class has already been implemented for you. Note how theChild
class is used. Several instances of children have been created and placed into aList
in themain
method ofChildCredit
.Similar to an array, a
List
stores elements using 0-indexing. Each one can be accessed using theget
method. For example,Child c = kids.get(i)
gets the i-th kid and assigns it to variablec
. The total number of elements in aList
can be obtained using thesize()
method. -
Implement the
produceReport
method 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 that is an int value.Each child is an instance of the
Child
class. Each instance has its own variables so that each child can be a different age. For example, forChild c
,c.getAge()
returns the age, andc.getName()
returns the name.
-
Test your programs locally on your computer using the provided JUnit test suites. Fix any errors and completely debug your programs.
- Expand the
src/test/java
directory, then expand theunl.soc
package, and then double click on theChildCreditTests.java
file to open it. - Run the test suites by clicking the usual "Play" button.
- Fix any errors and completely debug your programs.
- Expand the
src/test/java
directory, then expand theunl.soc
package, and then double click on theNaturalTests.java
file to open it. - Run the test suites by clicking the usual "Play" button.
- Fix any errors and completely debug your programs.
- Expand the
-
Submit the following files to GradeScope. Do not upload any other files.
Natural.java
ChildCredit.java
-
Make sure that your programs pass all the tests on GradeScope. For this lab, as long as your programs pass all the tests on GradeScope, you will get full points for the lab.
-
What if the test on GradeScope fails? Please test your programs locally on your computer using the provided JUnit test suite, because the test on GradeScope is exactly the same as the provided JUnit test suite. Debugging your programs on GradeScope is not recommended because GradeScope gives very limited information.*