Lab 5.1: Practicing Classes
An Introduction to Classes and ObjectsGoals
The goals of this lab are to learn the following:
- How to create a class
- How to write actions for an existing class
- How to understand class & action syntax
Computer Science Principles Curriculum
- Big Idea: Programming: EK 5.1.2B, EK 5.1.2E, EK 5.2.1F, EK 5.1.2J, EK 5.2.1C, EK 5.3.1D, EK 5.3.1I, EK 5.4.1
Common Core Standards
- English Language Arts Standards ยป Science & Technical Subjects: CCSS.ELA-Literacy.RST.9-10.2, CCSS.ELA-Literacy.RST.9-10.3, CCSS.ELA-Literacy.RST.9-10.4, CCSS.ELA-Literacy.RST.9-10.5, CCSS.ELA-Literacy.RST.9-10.6, CCSS.ELA-Literacy.RST.11-12.2, CCSS.ELA-Literacy.RST.11-12.3
- Standards For Mathmatical Practice: CCSS.Math.Practice.MP1, CCSS.Math.Practice.MP2, CCSS.Math.Practice.MP5, CCSS.Math.Practice.MP6, CCSS.Math.Practice.MP7, CCSS.Math.Practice.MP8
Overview
In this lab you will learn about classes and how they can be used. You will start by looking at the code for a basic class named Animal. You will then code a few simple lines for the class and can then observe how you're able to change the class's attributes. The goal of this lab is to give you a foundation in classes needed for moving onto the next labs and subsequent programming assignments.
Task 1: Getting started
First, start Sodbeans. You will create a new project; however, you will not be creating a blank application this time. Instead, in the New Project dialog, select "Quorum Application." You must select this option when using actions due to the way Quorum understands your programs. (We will discuss this later in Chapter 5). Name the project "FunWithAnimals".
When your project opens in Sodbeans, main.quorum will contain the code listed below. You will be typing your code in two different locations--between action Main and the first end, and between class Main and the second end.
class Main
action Main
end
end
Task 2: Creating Basic Actions
First, you will create the foundation of your code.
class Animal
action Main
end
end
This sets your class name to Animal. Now you will code a few characteristics of animals. You will start with common things like number of arms, and the animal's color.
class Animal
integer arms = 2
text color = "brown"
action Main
end
end
So now your animal has two arms and is brown. From this point you want to output the animal's existing characteristics, so you are going to put an output line in Main that has some text in it, as well as the variables you just coded. We have coded the output statement to show the number of arms the animal has, but it's your job to code the output statement for it's color.
class Animal
integer arms = 2
text color = "brown"
action Main
output "The animal has " + arms + " arms."
end
end
So now you're telling the user a few things about your animal. Add legs and adjust your output statement as well.
But what if the user would like to change the animal?
Next you're going to code a few actions for your class that will change the variables. These are called methods when they're actions for a class, and they're an integral part of class development.
Reusability is a fundamental part of programming, so you're going to move the output statements into their own method (called actions in Quorum), called Display. The skeleton code is provided for you. Don't forget to call the Display method once you're finished.
class Animal
integer arms = 2
text color = "brown"
action Main
output "The animal has " + arms + " arms and " + legs + " legs."
end
action Display
end
end
So now your program has the same functionality as before, but it's been decomposed into a method. One great benefit of this is that you're able to reuse that method now, without having to type it all out again. Reusability is a big time-saver and really simplifies your program.
The next thing that you're going to do is create three more methods that change the characteristics of the animal. This way the user can create their own animal with any number of legs, arms, and color.
So you're going to name the actions "Change(NameOfAttribute)"...ChangeArms for example. Inside these actions you're going to ask the user to enter a number or text. Then you're going to reassign the already created variables to what the user entered. Before you can do that though you have to cast the user's entry from a text to an integer.
You also need to be sure that you call those actions from within the Main.
One action is provided for you. It's your job to write the other actions that will allow the user to change the animal's color and legs.
Once you're finished with writing those actions you should call the Display method again so the user can see that the changes made have been saved.
class Animal
integer arms = 2
text color = "brown"
action Main
end
action Display
end
action ChangeArms
output "How many arms would you like your animal to have?"
text result = input("Number of Arms")
integer v = cast(integer, result)
arms = v
end
action ChangeColor
end
end
From this foundation you should be able to code a few more characteristics, and a few more actions. Most animals have legs, so let's add a legs variable similar to your arm variable if you haven't already. Let's also add a variable so you can give your animal a name like "Fido." Don't forget to add actions that allow the user to change those variables as well.
So by the time you're finished you should have 4 variables: arms, legs, name, and color. It should also have these 5 actions: "Display", "ChangeArms", "ChangeLegs", "ChangeColor", and "ChangeName".
Next Tutorial
In the next tutorial, we will discuss Lab 5.2, which describes an introduction to class variables and access modifiers..