Programming with objects

From Computer Science Wiki
Revision as of 10:01, 4 January 2023 by Bmackenty (talk | contribs) (Created page with "=== Students must be able to create a UML diagram for a system that is being modelled and identify and include existing libraries which will be used. === To create a UML dia...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Students must be able to create a UML diagram for a system that is being modelled and identify and include existing libraries which will be used.[edit]

To create a UML diagram for a system that is being modeled and identify and include existing libraries, you will need to follow these steps:

Identify the purpose of the UML diagram. Is it to model the static structure of the system, the interactions between objects, the flow of activities, or some other aspect of the system?

Identify the scope of the UML diagram. What parts of the system will be included in the diagram?

Identify the objects that will be represented in the diagram. These may be classes, interfaces, objects, components, or some other type of element.

Identify the relationships between the objects. These may include inheritance, associations, dependencies, and aggregations.

Use the standard UML symbols to represent the objects and relationships. These symbols include rectangles for classes, circles for objects, arrows for associations, and diamonds for inheritance relationships, among others.

Identify the existing libraries that will be used in the system. These may include external libraries, frameworks, or other pre-existing code that will be incorporated into the system.

Represent the existing libraries in the UML diagram by including them as external objects or components.

Use labels and other annotations to provide additional detail and clarity about the objects, relationships, and libraries in the diagram.

Use layout and formatting to make the diagram easy to read and understand.

It may also be helpful to refer to the UML specification or other resources that provide more detailed information about the rules and conventions for creating UML diagrams.

Students must be able to explain the difference between an object/class and an instance of an object. Students must be able to construct entire classes including instance variables, constructors, and accessor, mutator and helper methods using the four principles of OOP.[edit]

In object-oriented programming (OOP), a class is a blueprint or template for creating objects. It defines the properties and behaviors that will be shared by all objects of that type. An object, also known as an instance of a class, is a specific realization of a class. It is a unique instance of the class that has its own state and behavior.

For example, consider a class called "Dog" that represents dogs in a computer program. The class would define characteristics such as breed, color, and size, as well as behaviors such as barking and wagging its tail. An object of the class "Dog" would be a specific dog, such as a golden retriever named "Buddy." It would have its own specific values for the characteristics defined in the class, such as "golden retriever" for breed and "golden" for color.

The four principles of OOP are:

Encapsulation: This refers to the idea of bundling data and methods that operate on that data within a single unit, or object. Encapsulation helps to reduce complexity by providing a clear separation between the internal workings of an object and the external interface.

Abstraction: This refers to the idea of exposing only the necessary details and hiding the implementation details. Abstraction allows the user of an object to focus on what the object does, rather than how it does it.

Inheritance: This refers to the ability of a class to inherit properties and behaviors from a parent class. Inheritance allows developers to create new classes that are derived from existing ones, and to reuse code and customize the behavior of the new class as needed.

Polymorphism: This refers to the ability of different objects to respond to the same message in different ways. Polymorphism allows developers to write code that is more flexible and easier to maintain by allowing objects to behave differently based on their specific type or implementation.

To construct a class in OOP, you will need to define the instance variables, constructors, and methods that make up the class. An instance variable is a property of an object that stores the state of the object. A constructor is a special method that is called when an object is created, and is used to initialize the object's state. Accessor methods are used to retrieve the values of instance variables, while mutator methods are used to change the values of instance variables. Helper methods are methods that perform a specific task that is related to the behavior of the object, but is not part of the object's external interface.


Here is an example of a class definition in Python that includes instance variables, constructors, and methods:

class Dog:
  # instance variables
  def __init__(self, breed, color, size):
    self.breed = breed
    self.color = color
    self.size = size

  # accessor methods
  def get_breed(self):
    return self.breed

  def get_color(self):
    return self.color

  def get_size(self):
    return self.size

  # mutator methods
  def set_breed(self, breed):
    self.breed = breed

  def set_color(self, color):
    self.color = color

  def set_size(self, size):
    self.size = size

  # helper method
  def bark(self):
    print("Woof!")


To create an object of this class, or an instance of the class, you would use the following syntax:

dog = Dog("Golden Retriever", "Golden", "Large")


You can then access the instance variables and call the methods of the object using dot notation, like this:

breed = dog.get_breed()
dog.bark()



Students must be able to construct entire classes using the principles of abstraction and encapsulation.[edit]

Students must be able to construct entire classes using the principles of abstraction and encapsulation.[edit]

Students must be able to construct entire classes using the principles of inheritance and polymorphism.[edit]