Uploaded by yeahhboyyy

Learning Activity 8 Classes

advertisement
Learning Activity 8
We will learn more about C# classes. Classes are blueprints for how to create objects. Classes are special code that
are designed to hold lots of data and have behaviors while allowing us to resuse our code multiple times. An object is
an instance of a class. In C#, you can design a class (blueprint) and then create many objects using the blueprint
(called instances).
Learning Objectives: Students will be able to …
● Given a class diagram, code a class with name, fields, and methods
● Create an object
● Change object’s field values using dot-notation
● Call an object’s method using dot-notation
Task A.
This figure is called a class diagram. A class diagram communicates to the coder how to build a class in C# or any
object-oriented language. This class is for a bank to track customer bank accounts. It’s simplified on purpose as we
learn.
Account
customerName: string
balance: decimal
No methods yet
1. Log into repl.it. Create a repl workspace called Lesson8Bank
2. Create a new file called Account.cs
3. Type the code from Figure 1 into Account.cs. This code models the class diagram above. You have created
a class!
Figure 1. Type code into Account.cs
4. Type the code in Figure 2 into main.cs. This code creates two objects of class type Account. We say,
“account1 and account 2 are instances of class Account.” That means the objects account1 and account2
have the same fields defined by class Account.
Figure 2. Type code into main.cs to create objects of class Account
5. Run the code. You should see results as in Figure 3.
Figure 3. Results of code
Task B.
1. The bank has acquired new customers. Please add more objects for the following people. Add the necessary
code around line 16 in Figure 2.
Each new account should have a different variable names: account3, account4, and account5. Account3 belongs to
“Mary Kenneth Keller” and her balance is 4158.50. [Don’t forget the suffix m to force a type of money/decimal. See
examples line 9 and 14 in Figure 1]. Mary was a pioneer in computer information systems and the first of two people
to earn a doctorate in computers in the USA.
Account4 belongs to “Grace Hopper”. She was a Navy rear admiral, a CIS pioneer, and invented Linker (look it up).
Her account balance is 3344.01.
Account5 belongs to “Hedy Lammar.” She was an actress and inventor, who invented frequency-hopping spread
spectrum to prevent jamming of guided torpedoes. That technology is now in every CDMA cell phone. Her account
balance is 876543.00.
2. Edit the code on line 20 in Figure 2 to add all five account balances.
3. Run your code. You should see the results in Figure 4.
Figure 4. Results of code for Task B.
Task C:
The class diagram has been modified to include two methods: ChargeServiceFee() and CreditInterestEarned().
ChargeServiceFee() method charges a $20.00 monthly service fee. Because the bank offers interest bearing
accounts, CreditInterestEarned() credits or adds 1.5% of the balance to the account. .
Account
customerName: string
balance: decimal
ChargeServiceFee()
CreditInterestEarned()
1. Edit Account.cs to add the two methods. Type the code in Figure 5.
2. Edit main.cs to charge a service fee for each account and to record the interest earned by each account
holder. See Figure 6. Notice line 10 and line 11 in Figure 6 which call the methods. The term “Call the
method” means the code will execute ChargeServiceFee() and CreditInterestEarned(). Repeat the
ChargeServiceFee() and CreditInterestEarned() for each account.
3. Run your code. The results are in Figure 7. Is your bank asset balance the same as in Figure 7?
Figure 5. Add two methods to the Account class as per the class diagram.
Figure 6. Edit main.cs to charge fees and record interest earned by each account holder.
Figure 7. Results of code
Task D.
Watch CodeWithMosh “C# Classes Tutorial | Mosh” on Youtube. This is a free chapter in Mosh’s second e-book on
Intermediate C#. Here’s the link (14 mins).
https://www.youtube.com/watch?v=ZqDtPFrUonQ&list=PLTjRvDozrdlz3_FPXwb6lX_HoGXa09Yef&index=3
1. Create a repl.it workspace called Lesson8Person
2. Delete any existing default code in main.cs
3. Code with Mosh starting 9:05mins - 14:24 in the video above. What every he codes, you code too. Learn as
you code and listen to his explanation.
4. Your learning activity will be judged complete compared to Mosh’s final code.
5. Why? Practice. Practice. Coding use some different neurons in your brain that may not be strong yet, you
need to exercise those neural pathways to grow, you grow by actual coding. Your brain success will be
determined by the amount of time you spend on the keyboard coding. Plus hearing it from Dr Humpherys and
from Mosh let you hear the same content in different ways to help you understand better.
Task E. Plane class
You have been hired to create a computer flight control system for a plane. Below is a class diagram of a Plane.
Note. "double x" is called a parameter of the ThrottleEngine() method. The variable is x and data type is double. We
will pass data to ThrottleEngine() using the variable x. Why? Because ThrottleEngine() needs to know by how much
to increase or degree the engine thrust. You'll see code for how to implement this in Figure 8. You’ll learn more
about parameters in a future lesson.
Plane
emptyWeight: double
wingSpan: double
numberOfEngines: int
make: string
model: string
speed: double
ThrottleEngine(double x)
Pitch(double degrees)
Roll(double degrees)
1. Create a repl.it workspace called Lesson8Plane
2. Add a file called Plane.cs and type the code in Figure 8. This code creates the blueprint of a plane, called a
class.
3. Edit main.cs, delete any existing code and type the code from Figure 9. This code creates two objects of a
plane class. We manipulate each object separately (e.g., fly the planes in different directions). .
Figure 8. Type this code into Plane.cs
Figure 9. Code for main.cs
Task F. Blog Posts
Time for you to do this all on your own. You may help each other. IF you get really stuck, the answer is in the
Appendix at the end of this document. You must create a class to complete this learning activity. The correct results
are displayed in Figure 10. Note. If you need help, the answer is in Appendix A.
Blog
message: string
likes: int
Post()
You are a high-paid developer for a blog company. Read all these instructions before coding.
1. Create a repl.it workspace called Lesson8Blog
2. Add a file to the workspace called Blog.cs. Delete any default code in main.cs
3. Code a class called Blog() that follows the class diagram. Edit the Blog.cs file.
a. Message will store the text posted by the guest. When you declare the public variable message, set
the initial value of message to empty string “”
b. Like will represent how many time other guests liked this message. When you declare the public
variable likes, set the initial value to 0.
4. Use the namespace called Blogger in both Blog.cs and main.cs.
5. In main.cs, do the following:
a. Declare five objects of class Blog(). Name the objects blog1, blog2, blog3, blog4, and blog5. These
will represent five different blog posts by our guests.
b. In main.cs, set the message field of each blog object to the following messages (Hint: use dot-notation
as exampled in Figure 6 Line 8.):
Blog1’s message is "Who knows how to declare a variable for money in C#? "
Blog 2’s message is “I think you use double data type.”
Blog 3’s message is "No. you should use decimal data type because the precision is better without rounding errors."
Blog 4’s message is "I agree with decimal. Like this: var salary = 5000.00m; don't forget the m which means
money/decimal"
Blog 5’s message is "Thanks all. That helps."
c. Call the Post() method for each of the five blog objects. (Hint: Use dot-notation for each blog object as
exampled in Figure 6 Line 10.)
d. In main.cs, set the likes of blog1 to equal 2.
e. In main.cs, set the likes of blog4 to equal 100.
6. Run your code. The results should look like Figure 10.
Figure 10. Results of code
Congrats! You’ve learned a lot and worked hard with perspiration.
“Genius is 1% inspiration and 99% perspiration.” Thomas Edison
Deliverables:
Submit each workspace URL for Lessons8Bank, Lesson8Person, Lesson8Plane, and Lesson8Blog. Submit to
\\Wtclass\cidm2315\lessons\Week5\Turn in Learning Activity 8\. Copy each URL and append them into the
comments section.
Appendix A: Answers
Figure 11. These are the answers to Task F. Only look at them after you have tried your hardest and slept on the
problem overnight :-) Don’t rob yourself of the growth that comes through honest struggle. Feel free to help each
other without giving each other the answer.
Download