Unit 4A - More Code Practice with Arrays
From CS Awesome 2CC BY-NC-SA 4.0Original page
Java1 class period4 exercises8 graded tests
Files
Unit 4A - More Code Practice with Arrays
Each coding exercise has its own folder. Open its .java file and press Run to try it. Run Tests checks every graded exercise in this lesson at once.
Exercise 1
Create a method sum13(nums) that takes an array of integers, nums and returns the sum of the numbers in the array. However, the number 13 is very unlucky, so do not add it or the number that comes immediately after a 13 to the sum. Return 0 if nums is an empty array.
| Example Input | Expected Output |
|---|---|
sum13({13, 1, 2}) | 2 |
sum13({1,13}) | 1 |
sum13({4, 13, 8}) | 4 |
sum13({13, 1, 13, 3, 2}) | 2 |
sum13({}) | 0 |
Open the
01-sum13-writtenfolder and editSum13.java. Press Run to try it; Run Tests checks it (2 tests).
Exercise 2
Create a method has22(nums) that takes an array of integers, nums and returns true if there are at least two items in the list that are adjacent and both equal to 2, otherwise return false.
| Example Input | Expected Output |
|---|---|
has22({1, 2, 2}) | true |
has22({2, 1, 2}) | false |
has22({2, 2, 8}) | true |
has22({3, 3, 5}) | false |
Open the
02-has22-written2folder and editHas22.java. Press Run to try it; Run Tests checks it (2 tests).
Exercise 3
Create the total89(nums) method below that takes an array of integers, nums, and returns the total of the numbers in nums except for all numbers in the array between an 8 and a 9 (inclusive).
| Example Input | Expected Output |
|---|---|
total89({1, 2}) | 3 |
total89({2, 8, 3, 9, 2}) | 4 |
total89({8, 3, 5, 9, 6}) | 6 |
Open the
03-total89-writtenfolder and editTotal89.java. Press Run to try it; Run Tests checks it (2 tests).
Exercise 4
Create a method twoSum(nums, target) that takes an array of integers nums and an integer target and returns an array with the indices of two numbers such that they add up to target. If no two numbers add up to target, it returns an empty array. Assume that each input has exactly one solution, and you may not use the same element twice.
| Example Input | Expected Output |
|---|---|
twoSum({2,7,11,15}, 9) | {0, 1} |
twoSum({2,7,11,15}, 13) | {0, 2} |
twoSum({2,7,11,15}, 5) | {} |
Open the
04-twoSum-writtenfolder and editTwoSum.java. Press Run to try it; Run Tests checks it (2 tests).
For teachers
Unit 4A - More Code Practice with Arrays
Students read CSAwesome2's “Unit 4A - More Code Practice with Arrays” in CS Room and work through its 4 Java coding exercises.
- Time: 1 class period
- Tests: 8 in 4 graded exercises, set up for you when you import the lesson
Goals
- Practice for Unit 4: Data Collections.
Before class
- Import the lesson into your class from the Lessons page, or with New assignment → Use a lesson. It arrives as a draft with its tests already set up.
- Publish it.
- The original page is on CS Awesome: https://runestone.academy/ns/books/published/csawesome2/MoreArrayPractice.html
- CS Awesome's own teacher materials: the CS Awesome teacher site
In class
- Students read
README.md. - Students do exercises 01–04, each in its own folder, pressing Run to try their code.
- Run Tests checks every graded exercise at once; scores appear in the Gradebook. Grading uses your copy of the tests, so a student who edits theirs does not change the grade.
Exercises
| Folder | File | Grading | Tests |
|---|---|---|---|
01-sum13-written | Sum13.java | Autograded | 2 |
02-has22-written2 | Has22.java | Autograded | 2 |
03-total89-written | Total89.java | Autograded | 2 |
04-twoSum-written | TwoSum.java | Autograded | 2 |
Teachers can download a reference solution after signing in.