DAT - Designing and Implementing a Custom Class (Lesson)
Designing and Implementing a Custom Class
Introduction
Setup
In order to complete the activities for this lab you will need to be running Java 1.8 or a later Java Software development kit. You can check your compiler by looking at the bottom right of the compiler output screen when you have a Dr. Java window open. You should see something like this:
If you do not see a JDK listed contact your teacher.
Quick Install
After you have the correct JDK you will need to download the Sinbad library.
- Click the down arrow to right to download the latest sinbad.jar zip file to your computer Download Click the down arrow to right to download the latest sinbad.jar zip file to your computer.
Add the sinbad.jar file to the class path for Dr. Java using the following instructions:
- From the “Edit” menu, choose “Preferences”. Select the “Resource Locations” tab.
- In the “Extra Classpath” section, click “Add”. Locate the sinbad.jar file that you downloaded and click “Select”/”OK”.
You should be all set! You can use the Sinbad library with any program you write in Dr. Java without having to repeat these steps (if you don’t move the sinbad.jar file.)
Designing and Implementing a Custom Class
Throughout this course you have created classes from given descriptions or specifications several times. This is an important skill, but equally important is the ability to determine essential information to include when creating a class. What is “essential” can vary based on perspective or can be determined by a question that is being asked or a problem that is attempting to be solved. This activity will give you an opportunity to practice making this type of determination. Consider the following selection.
This lab content is from the CollegeBoard.
Designing a Class
Click below to begin the Designing a Cereal Class learning activity.
Designing a Class Lab Activity
You will design a class to describe an object and answer the following question:
What cereal(s) contain more protein than sugar?
When creating a class, there is a tradeoff between the complexity of the object created (the more data that is stored the more complex the class) and the questions that can be answered. More questions will be able to be answered if there is more data to examine. Think about what data you want to store and how it could limit the questions that you can answer with the data.
Record the answers to the following questions on a sheet of paper. You will need the answers to complete the Lab Check Quiz.
1. Each row of the table represents an instance of an object. What is the best name for that object?
2. Write the class header.
3. How many instance variables will you create?
4. List the data types and names you will use for the instance variables.
In Dr. Java, create a new Java file named Cereal.java and implement the class described above. Your class should contain all necessary instance variables, constructors, accessor methods, and a toString method.
Write a main method to test your Cereal class by implementing multiple instances of Cereal objects. This program should include lines instantiating Cereal objects.
5. Given your class design, determine one additional question that you can answer without making any changes to Cereal.java.
6. Identify one additional question that can be answered from the given data that you are not able to answer based on your implementation of Cereal.java.
7. What modification could you make in order to answer this question?
IMAGES CREATED BY GAVS