5.19. groupwork Design a Class for your Community Project

In lessons 5.1, 5.2, 5.4, 5.5, 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.

For this project, ask someone in your family or community what would be important for them to track. For example, you could create a class to keep track of community events, health tracking, athlete games or statistics, community leaders, performers or performances, 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) and 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 this 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.19.1. Code your Class

  1. Create your class and its instance variables below.

  2. Add 2 constructors: a constructor with no parameters that initializes the instance variables to default values and 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 2 different constructors 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.19.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