Time estimate: 90 min.

5.20. groupwork Design a Class for your Community Project

In lessons 5.1, 5.2, and 5.6, 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.

  1. 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.

  2. 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).

5.20.1. Code your Class

  1. Create your class and its instance variables below.

  2. Add a constructor with 3 parameters to set all of the instance variables to the given parameters.

  3. Write a print() method that uses System.out.println to print out all the instance variables.

  4. Create accessor (get) methods for each of the instance variables.

  5. Create mutator (set) methods for each of the instance variables.

  6. Create a toString method that returns all the information in the instance variables.

  7. 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 (|).

  8. 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.

Design your class for your community below.

You will continue this project in Unit 6 in lessons 6.1-6.3 to create an array of objects using your class.

5.20.2. 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). Here’s a Java Swing Example on repl and below that sets up a JFrame with JTextfields, JLabels, and a JButton. It calls a special method called addActionListener where you can put the code to be executed when you click on the button. This program prints out the entered data into a file, although more advanced systems would use a database. You can fork this project in replit or copy the code to your local computer to design an input form for your class. You can uncomment the code in the actionlistener that prints the entered data into a file. To learn more about Java Swing, click on the different Swing components in the left navigation column of https://www.javatpoint.com/java-swing.

You have attempted of activities on this page