Coding Practice 1b (1.7-1.15)

From CS Awesome 2CC BY-NC-SA 4.0Original page

Java2 class periods13 exercises23 graded tests

Files

README.mdDownload

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-exRandom100 folder and edit Test1.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-circleMethods folder and edit Circle.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-tvMethods folder and edit TestTelevision.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-exFirstLetter folder and edit Test1.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-exFirst3Lower folder and edit Test1.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-exLower folder and edit Test1.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-exRemoveVery folder and edit Test1.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-exReplaceLol folder and edit Test1.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.

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-ac folder and edit TurtleTest.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-ac folder and edit TurtleTest.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-ac folder and edit TurtleTest.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-ac folder and edit TurtleTest.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-ac folder and edit TurtleTest.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

Before class

In class

  1. Students read README.md.
  2. Students do exercises 01–13, each in its own folder, pressing Run to try their code.
  3. 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

FolderFileGradingTests
01-exRandom100Test1.javaChecks code text only3
02-circleMethodsCircle.javaAutograded3
03-tvMethodsTestTelevision.javaAutograded5
04-exFirstLetterTest1.javaAutograded2
05-exFirst3LowerTest1.javaAutograded2
06-exLowerTest1.javaAutograded2
07-exRemoveVeryTest1.javaAutograded3
08-exReplaceLolTest1.javaAutograded3
09-Turtle-eoc-triangle-acTurtleTest.javaNot graded; students check it themselves (draws pictures)0
10-Turtle-eoc-rect-acTurtleTest.javaNot graded; students check it themselves (draws pictures)0
11-Turtle-eoc-draw-seven-acTurtleTest.javaNot graded; students check it themselves (draws pictures)0
12-Turtle-eoc-draw-four-acTurtleTest.javaNot graded; students check it themselves (draws pictures)0
13-Turtle-eoc-draw-interesting-acTurtleTest.javaNot graded; students check it themselves (draws pictures)0

Teachers can download a reference solution after signing in.