Design a Class for your Community Project
From CS Awesome 2CC BY-NC-SA 4.0Original page
Java2 class periods1 exercise7 graded tests
Files
Design a Class for your Community Project
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.
In this unit’s challenges, you were asked to design a class of your own choice that is relevant to your community. If you would like to do this activity as 1 lab project rather than a piece at a time at the end of each lesson, you can create your complete class here. Or you may choose to create a different class of your own here. You can work in pairs on this project.
- Make your own copy of this worksheet from the File menu. For question 1, brainstorm and ask people in your community what would be important or useful for them to track in a community organization or activity or to help your community. For example, you could create a Java class to keep track of community events, club activities, athletic games or statistics, community leaders, performers or performances, health tracking, or another subject of your choice.
- Come up with your class name for your community data-tracking class (it should be a category of people or things, not a specific person or thing). Come up with at least 3 instance variables that are attributes of things in that class. Think about what data type each variable should be. You can use the worksheet to design your class. Optionally, you may want to draw a UML class diagram for your class on paper or using app.diagrams.net or Creately.com (see tutorial on class diagrams).
Code your Class
- Create your class and its instance variables in the exercise folder.
- Add a constructor with 3 parameters to set all of the instance variables to the given parameters.
- Write a print() method that uses System.out.println to print out all the instance variables.
- Create accessor (get) methods for each of the instance variables.
- Create mutator (set) methods for each of the instance variables.
- Create a
toStringmethod that returns all the information in the instance variables. - Write an additional method for your class that takes a parameter. For example, there could be a print method with arguments that indicate how you want to print out the information, e.g. print(format) could print the data according to an argument that is “plain” or “table” where the data is printed in a table drawn with dashes and lines (|).
- Write a main method that constructs at least 2 objects of your class using the constructor and then calls all of the methods that you created above to test them.
Exercise 1
Design your class for your community.
Open the
01-community-challenge-complete-projectfolder and editMain.java. Press Run to try it; Run Tests checks it (7 tests). Name the class, then rename the file to match.
You will continue this project in Unit 4 to create an array of objects using your class.
Optional Swing GUI
An optional additional project is to build a GUI (graphical user interface) for your class (GUIs are not on the AP exam). To learn more about Java Swing, click on the different Swing components in the left navigation column of https://www.javatpoint.com/java-swing.
For teachers
Design a Class for your Community Project
Students read CSAwesome2's “Design a Class for your Community Project” in CS Room and work through its 1 Java coding exercise.
- Time: 2 class periods
- Tests: 7 in 1 graded exercise, 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/community-challenge.html
- CS Awesome's own teacher materials: the CS Awesome teacher site
In class
- Students read
README.md. - Students do exercise 01 in its 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-community-challenge-complete-project | Main.java | Autograded | 7 |
Teachers can download a reference solution after signing in.