Coding Practice 1b (1.7-1.15)
From CS Awesome 2CC BY-NC-SA 4.0Original page
Java2 class periods13 exercises23 graded tests
Files
Coding Practice 1b (1.7-1.15)
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
Write the code to print a random number from 1 to 100. You can use Math.random() to get a value between 0 and not quite 1.
Open the
01-exRandom100folder and editTest1.java. Press Run to try it; Run Tests checks it (3 tests). Its tests check your code, not what it prints: “random number up to 100”, “random number starting at 1”, “casting to int”.
Exercise 2
Given the Circle class, use its constructor to create a circle of radius 5.0 and then print out its area using its getArea method.
Open the
02-circleMethodsfolder and editCircle.java. Press Run to try it; Run Tests checks it (3 tests).
Exercise 3
Given the Television class, use its constructor to create a Television object with the brand Sony and price 499.99. Then print out its brand and price. Finally, set the number of pixels to 1080 and print it out.
Open the
03-tvMethodsfolder and editTestTelevision.java. Press Run to try it; Run Tests checks it (5 tests).
Exercise 4
The code should get the first letter of the first name, middle name, and last name and append (concatenate) them together and then return them all in lowercase. However, the code has errors. Fix the code so that it compiles and runs correctly.
Open the
04-exFirstLetterfolder and editTest1.java. Press Run to try it; Run Tests checks it (2 tests).
Exercise 5
The code should print the first 3 letters of the string message all in lowercase letters. However, the code has errors. Fix the errors so that the code runs as intended.
Open the
05-exFirst3Lowerfolder and editTest1.java. Press Run to try it; Run Tests checks it (2 tests).
Exercise 6
The code starts with String name1 = "ALEX"; and should print Alex. Use the toLowerCase and substring methods to do this task.
Open the
06-exLowerfolder and editTest1.java. Press Run to try it; Run Tests checks it (2 tests).
Exercise 7
The code should remove the word “very ” (and following space) from the message and print the new message. You can use indexOf to find the position of a substring in your string. You can use substring to create a new string removing the word.
Open the
07-exRemoveVeryfolder and editTest1.java. Press Run to try it; Run Tests checks it (3 tests).
Exercise 8
The code should replace lol in the message with laugh out loud and print the new message using indexOf and substring.
Open the
08-exReplaceLolfolder and editTest1.java. Press Run to try it; Run Tests checks it (3 tests).
More practice
For more practice with Strings see problems at http://codingbat.com/java/String-1.
- http://codingbat.com/prob/p161056
- http://codingbat.com/prob/p147483
- http://codingbat.com/prob/p108853
- http://codingbat.com/prob/p130896
- http://codingbat.com/prob/p130781
Here are some practice coding problems for Turtles.
Exercise 9
Finish the code to have t1 draw a triangle where all of the sides are length 50.
Open the
09-Turtle-eoc-triangle-acfolder and editTurtleTest.java. This one is not graded, so check it yourself. It draws pictures: Run saves the drawing as an image in its folder.
Exercise 10
Finish the code to have t1 draw a rectangle. The vertical sides should be length 50 and the horizontal length 100.
Open the
10-Turtle-eoc-rect-acfolder and editTurtleTest.java. This one is not graded, so check it yourself. It draws pictures: Run saves the drawing as an image in its folder.
Exercise 11
Finish the code to have t1 draw the number seven.
Open the
11-Turtle-eoc-draw-seven-acfolder and editTurtleTest.java. This one is not graded, so check it yourself. It draws pictures: Run saves the drawing as an image in its folder.
Exercise 12
Finish the code to have t1 draw the number four.
Open the
12-Turtle-eoc-draw-four-acfolder and editTurtleTest.java. This one is not graded, so check it yourself. It draws pictures: Run saves the drawing as an image in its folder.
Exercise 13
Finish the code to have t1 draw something interesting.
Open the
13-Turtle-eoc-draw-interesting-acfolder and editTurtleTest.java. This one is not graded, so check it yourself. It draws pictures: Run saves the drawing as an image in its folder.
For teachers
Coding Practice 1b (1.7-1.15)
Students read CSAwesome2's “Coding Practice 1b (1.7-1.15)” in CS Room and work through its 13 Java coding exercises (8 graded, 5 ungraded).
- Time: 2 class periods
- Tests: 23 in 8 graded exercises, set up for you when you import the lesson
Goals
- Practice for topics 1.7–1.15: 1.7 APIs and Libraries, 1.8 Documentation with Comments and Preconditions, 1.9 Method Signatures, 1.10 Calling Class Methods, 1.11 Using the Math Class, 1.12 Objects - Instances of Classes, 1.13 Creating and Initializing Objects: Constructors, 1.14 Calling Instance Methods, 1.15 Strings.
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/unit1b-practice-coding.html
- CS Awesome's own teacher materials: the CS Awesome teacher site
In class
- Students read
README.md. - Students do exercises 01–13, 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-exRandom100 | Test1.java | Checks code text only | 3 |
02-circleMethods | Circle.java | Autograded | 3 |
03-tvMethods | TestTelevision.java | Autograded | 5 |
04-exFirstLetter | Test1.java | Autograded | 2 |
05-exFirst3Lower | Test1.java | Autograded | 2 |
06-exLower | Test1.java | Autograded | 2 |
07-exRemoveVery | Test1.java | Autograded | 3 |
08-exReplaceLol | Test1.java | Autograded | 3 |
09-Turtle-eoc-triangle-ac | TurtleTest.java | Not graded; students check it themselves (draws pictures) | 0 |
10-Turtle-eoc-rect-ac | TurtleTest.java | Not graded; students check it themselves (draws pictures) | 0 |
11-Turtle-eoc-draw-seven-ac | TurtleTest.java | Not graded; students check it themselves (draws pictures) | 0 |
12-Turtle-eoc-draw-four-ac | TurtleTest.java | Not graded; students check it themselves (draws pictures) | 0 |
13-Turtle-eoc-draw-interesting-ac | TurtleTest.java | Not graded; students check it themselves (draws pictures) | 0 |
Teachers can download a reference solution after signing in.