Unit 3 Write Code Practice for Classes
From CS Awesome 2CC BY-NC-SA 4.0Original page
Java2 class periods10 exercises25 graded tests
Files
Unit 3 Write Code Practice for Classes
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.
After doing the mixed-up code problems on CS Awesome, try doing similar write code problems below.
Exercise 1
Fix the code so that the Dog class has a constructor that takes in a String argument name and assigns that value to the instance variable name.
Open the
01-u3-muc-wc1folder and editDog.java. Press Run to try it; Run Tests checks it (2 tests).
Exercise 2
Create a constructor for the Dog class that takes two parameters, name (a String) and age (an integer), and saves them in the correspondingly-named instance variables.
Open the
02-u3-muc-wc2folder and editDog.java. Press Run to try it; Run Tests checks it (2 tests).
Exercise 3
Fix the errors (commented as “TODO”) in the code so that it defines the Cat class correctly. There should be a makeSound method that prints "meow" and returns nothing. There should also be a toString method that returns "Name: name, Age: age" (such that Cat("Lucky", 10)’s toString method would return "Name: Lucky, Age: 10").
Open the
03-u3-muc-wc3folder and editCat.java. Press Run to try it; Run Tests checks it (4 tests).
Exercise 4
Write code that completes the Square class. It needs two variables: length and numberOfSquares. length is an instance variable, while numberOfSquares is a class variable that tracks how many squares have been made. getArea() also needs to be completed, which will return the area of the square. Finally, there needs to be a completed toString() method that returns "Length: length" (such that Square(5)’s toString method would return "Length: 5").
Open the
04-u3-muc-wc4folder and editSquare.java. Press Run to try it; Run Tests checks it (3 tests).
Exercise 5
Write code that completes the Rectangle class. It should have constructors that can take zero or two integer parameters. With zero arguments passed, the Rectangle should be initialized with a length of 10 and a width of 10. With two integers passed, the Rectangle should have a length equal to the first argument and a width equal to the second argument. There should also be a getArea method that returns the area length times width.
Open the
05-u3-muc-wc5folder and editRectangle.java. Press Run to try it; Run Tests checks it (1 test).
Exercise 6
Write code that completes the CelestialBody class. Each CelestialBody instance has an integer orbitLength (in days) and a integer daysSinceDiscovered attribute (which is initially 0). Using these, write the orbit(int numberOfTimes) method that adds numberOfTimes * orbitLength to daysSinceDiscovered (e.g., if Planet X has done two orbits with an orbit length of 12 days, it was discovered 24 days ago. If it then orbits another three times, it was discovered 60 days ago). Also, fix the two errors in the class.
Open the
06-u3-muc-wc6folder and editCelestialBody.java. Press Run to try it; Run Tests checks it (2 tests).
Exercise 7
Write code to create a Person class. Each Person instance should have a String name attribute and a integer age attribute. There should also be getName and setName methods. Finally, there should to be a toString method that returns “{name} is {age} years old” (e.g., Person("Carol", 12)’s toString method would return "Carol is 12 years old").
Open the
07-u3-muc-wc7folder and editPerson.java. Press Run to try it; Run Tests checks it (3 tests).
Exercise 8
Write code to create a Point class. Each Point instance should have integer x and y attributes (and the constructor should take those in that order). There should be getter methods for each: getX and getY. There should be a getDistance method that takes in another Point object as an argument and calculates the euclidean distance from this object to that one (which would be sqrt((this.x - other.x) ^ 2 + (this.y - other.y) ^ 2)). Finally, there should to be a toString method that returns “(Point.x, Point.y)” (e.g., Point(3, 4)’s toString method would return "(3, 4)").
Open the
08-u3-muc-wc8folder and editPoint.java. Press Run to try it; Run Tests checks it (4 tests).
Exercise 9
Write code to create an Account class. Each Account instance should have integer balance and String owner attributes (and the constructor should take those in that order). To increase balance, there should be a deposit method that takes in an integer argument and adds that to balance. To decrease balance, there should be a withdraw method that takes in an integer argument and subtracts that from balance. However, if balance would end as a negative number, it should just be set to zero. Finally, there should be a toString method that returns "Account.owner: $Account.balance" (so for Account(5, "Tom") it should return "Tom: $5").
Open the
09-u3-muc-wc9folder and editAccount.java. Press Run to try it; Run Tests checks it (2 tests).
Exercise 10
Write code that completes the Character class. Each Character instance has an integer healthPoints (abbreviated HP) attribute and a String name attribute. They also have the associated getHP, setHP, and getName methods. Using these, write code that finishes the fight(Character other) method that lets a character fight another. If the character’s healthPoints are the same or more than other’s, other’s HP should be set to zero, the current character’s HP should be set to the difference, and the program should print “{the character’s name} wins”. That entire section is already completed. On the other hand, if other’s HP is greater, the current character’s HP should be set to zero, other’s HP should be set to the difference, and the program should print “{other’s name} wins”.
Open the
10-u3-muc-wc10folder and editCharacter.java. Press Run to try it; Run Tests checks it (2 tests).
For teachers
Unit 3 Write Code Practice for Classes
Students read CSAwesome2's “Unit 3 Write Code Practice for Classes” in CS Room and work through its 10 Java coding exercises.
- Time: 2 class periods
- Tests: 25 in 10 graded exercises, set up for you when you import the lesson
Goals
- Practice for Unit 3: Class Creation.
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/classes-write-code.html
- CS Awesome's own teacher materials: the CS Awesome teacher site
In class
- Students read
README.md. - Students do exercises 01–10, 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-u3-muc-wc1 | Dog.java | Autograded | 2 |
02-u3-muc-wc2 | Dog.java | Autograded | 2 |
03-u3-muc-wc3 | Cat.java | Autograded | 4 |
04-u3-muc-wc4 | Square.java | Autograded | 3 |
05-u3-muc-wc5 | Rectangle.java | Autograded | 1 |
06-u3-muc-wc6 | CelestialBody.java | Autograded | 2 |
07-u3-muc-wc7 | Person.java | Autograded | 3 |
08-u3-muc-wc8 | Point.java | Autograded | 4 |
09-u3-muc-wc9 | Account.java | Autograded | 2 |
10-u3-muc-wc10 | Character.java | Autograded | 2 |
Teachers can download a reference solution after signing in.