Upgrade to remove ads
chapter 13 review q's
Terms in this set (13)
List attributes and behaviors for the following classes:
title, author, publication year, num of pages, publics, topic
make, model, color, year, type, engine size, number of cylinders, number of doors
balance, transactions, type, overdraft limit, interest rate
name, address, student ID, classes taken, major, grades received, class, graduation year
name, address, employee ID, department, title, year employed.
Distinguish between classes and objects
classes are like the blueprint that can be used to create many instances (objects) that have the same properties.
objects are just instances of the class.
You can think of a class as the blueprint used to describe how to build a house, and each specific house built based on that blueprint is an object.
Imagine a thermostat class that allows for a temperature to be set to specific values within minimum and maximum bounds. Discuss how encapsulation can be helpful for this class, as well as what problems might arise if data in the class were not encapsulated.
The temperature could be set internally to Celsius or Fahrenheit, and then conversions could expose the other type without problems.
if the thermostat is envisioned to allow a user to increase or decrease the temperature, methods could be provided for increase or decrease but leaving the temperature itself as read-only.
If the temperature property wasn't encapsulated, it would allow for it to be set to values outside the maximum and minimum limits, or to be set to a value that doesn't make sense according to the unit being used.
Encapsulation can also help ensure that max is always higher than min.
UML diagram for Thermostat Class
Advantages of automatic properties
Less boilerplate code
Faster to write code
code that is easier to read
Disadvantages of automatic properties
you have to know what the syntax means, as it isn't immediately intuitive.
this only works for read/write properties
changing a property to read-only means having to add both a backing instance variable and changing the property, as well as anywhere in the class the property is being written to.
you cannot have any conversion or calculation code in an automatic property.
Why should fields always be marked as private?
this allows for a private implementation that allows for the implementation to be changed later.
For instance, it may allow for conversion of a variable before being accessed. While any instance variable may not need to be hidden, it is good practice to always make them private to allow future protection.
Why did the C# language designers choose to introduce properties instead of getters and setters like in Java?
the designers of c# chose to make it a language feature, as it made it clearer what the correct way to deal with properties is. This also allowed the compiler to use databinding with properties. Lastly, it allowed for automatic properties that make the code a lot faster to write for many of the properties.
YOU MIGHT ALSO LIKE...
CIST2351-PHP Programming I - Ch 14
OTHER SETS BY THIS CREATOR
IT Chapter 6 review questions
IT Chapter 5 review questions
Chapter 5, 6, 7, 9 Review Questions
5,7,9 review Qs