Objectives
The goal of this lab is to learn the following concepts:
- How to create and use a class hierarchical system
- How a class inherits actions from another
- How accessor actions and access modifiers work
- How classes work
Overview
In this lab, you will explore the notion of inheritance. Inheritance allows one class to be used as a basis for defining another related class. When a class that is derived from another class it inherits the data and actions of that class. This way, you are able to reuse code and build more compact and maintainable programs. You will create two classes, Circle and Sphere, and observe how the actions declared in one can be accessed and used in another. You will start with creating a radius class variable, then will work up to using that radius class variable in multiple classes and in multiple actions. By the end of the lab, you will see how the Sphere class can use the actions defined in the Circle class to calculate various properties of a sphere.
Task 1: Getting Started
Start Sodbeans. Create a new “Quorum Application” project, and name it Lab6_1. In the Main.quorum file, it should contain a Main class and Main action.
You will create two additional classes in this project. In a New File dialog, create a new file by selecting “Quorum” and “Quorum Class” in the Categories and File Types windows, respectively. You can also access the New File dialog with the keyboard shortcut Ctrl + N. Then, name the new file Circle in a New Quorum Class dialog. Repeat the above steps to create a Class named Sphere.
You will specify code for Circle, Sphere, and Main classes in the next three tasks.
Task 2: Creating Class Variables
Now create a class variable and experiment with how inheritance works.
In the Circle class, add the class variable radius of type number. Recall, type number will allow us more precision than integer, so you can have a radius of 1.5 instead of 1 or 2. Set its default value to zero.
Now add a GetRadius and SetRadius action to help get a value for radius. Just as you learned in chapter 5, create an access action and access modifier for radius. Creating actions such as these to obtain values can help make your programs easy to modify and resilient to user errors. In task 3, you will be using inheritance to access these actions in class Sphere. Thanks to inheritance, you only need to write the actions once, and can then use them in both classes to obtain a value for radius.
Task 3: Inheriting Actions From Another Class
Now that you have your getter and setter for radius, you need to inherit those methods in class Sphere. To inherit the actions of another class, tell Sodbeans that "class A" is a "B". The keywords here are "is" which is used to inherit one class into another. Lets rewrite class Sphere so that Sodbeans knows class Sphere is inheriting the actions of class Circle. Your code should look similar to the following:
class Sphere is Circle
end
Great! Now use the SetRadius method you declared in class Circle to create a radius for Sphere. This time, you will get user input for the radius, and use that input as an argument to SetRadius:
class Sphere is Circle //creates a hierarchy, where sphere is inheriting from Circle
action SphereSetRadius
SetRadius(cast(number, input("Enter a radius"))) //this is a method declared in our Circle class
end
end
Now use the different actions to create diameters for Circle and Sphere. In Main, create Circle and Sphere objects. Next, create number variables circleRadius and sphereRadius. Using the set and get actions in class Circle, assign a radius value to circleRadius. Recall that in action SphereSetRadius, you've already set the value of radius to whatever the user entered, so now you only need to get that value. Using the GetRadius method from class Sphere, assign the sphereRadius variable a value. Next, multiply both values by 2 to get the circle and sphere diameters, then output or say their respective values.
Task 4: Using Inherited Actions
You'll now create actions that will calculate various circle and sphere properties. The sphere calculations will require a radius, which can only be obtained through inheritance from class Circle
- action CalculateVolume(number radius) returns number CalculateVolume requires a number type parameter and returns a number. As the name indicates, this action will calculate the volume of a sphere given its radius. The equation for sphere volume is: (4/3)*pi*radius cubed.
- action CalculateSurfaceArea(number radius) returns number CalculateSurfaceArea requires a number type parameter and returns a number. This action calculates the surface area of a sphere given its radius. The equation for sphere surface area is: 4*pi*radius squared.
- Now it's time to use these actions. In Main, call the actions you created above, using the radius you obtained from the Sphere class as the arguments. Create a say statement that tells the user what the radius is that was used, and what the calculated number is. Next you'll create some actions to calculate the properties of a circle.
- action CalculateArea(number radius) returns number CalculateArea uses a number type parameter and returns a number. This action will calculate the area of a circle given its radius. The equation for circle area is: pi*radius squared.
- action CalculateCircumference(number radius) returns number CalculateCircumference uses a number type parameter and returns a number. This action will calculate the circumference of a circle given its radius. The equation for circumference is 2*pi*radius
Now, just as you did with the sphere actions, call the actions you created above in Main, using the radius obtained from the Circle actions. Create a say statement that tells the user what the radius is that was used, and what the calculated number is.
Sample Output
Run the program and notice that a different radius is used for the circle and sphere calculations. This is because you are able to set different values using the same method. With inheritance, you were able to use the get and set actions created in class Circle to create a unique radius for the sphere calculations. At the same time, you could still use those same actions to create a unique radius for the circle calculations. In the case of class Sphere, inheritance allowed you to reuse code that had already been created. When run, your program should look similar to this:
Circle diameter is 10.
Area of a circle with a radius of 5 is 78.537
Circumference of a circle with a radius of 5 is 31.415
Sphere diameter is 25
Sphere volume with a radius of 12.5 is 8181.223
Sphere surface area with a radius of 12.5 is 1963.493
When you're done with this lab, show your instructor your code.
Next Tutorial
In the next tutorial, we will discuss Lab 6.2, which describes an introduction to blueprint actions..