• Tidak ada hasil yang ditemukan

PDF Data structure and algorithm in Python - Object-Oriented Programming

N/A
N/A
Protected

Academic year: 2023

Membagikan "PDF Data structure and algorithm in Python - Object-Oriented Programming"

Copied!
103
0
0

Teks penuh

The class definition specifies instance variables (data members) that the object contains, as well as the methods (member functions) that the object can execute. A software is capable of handling unexpected inputs that are not explicitly defined for its application. If a program expects a positive integer and gets a negative integer instead, the program should be able to recover from this error gracefully.

This point was driven home in the late 1980s in the accidents involving the Therac-25, a radiation machine that severely overdosed six patients between 1985 and 1987, some of whom died from complications that were the result of their radiation overdose. Software must evolve over time in response to changing environmental conditions. Developing quality software can be an expensive enterprise, and its costs can be somewhat offset if the software is designed to be easily reused in future applications.

Object-Oriented Design Priciples

Robustness is significantly increased because it is easier to test and debug separate components before they are integrated into a larger software system. Furthermore, failures that persist in a complete system can be traced to a specific component, which can be corrected in relative isolation. An ADT is a mathematical model of a data structure that specifies the type of data stored, the operations supported on them, and the types of parameters for the operations.

We typically refer to the collective set of behaviors supported by an ADT as the public interface. As an interpreted and dynamically typed language, there is no "compile-time" check of data types in Python and no formal requirement for abstract base class declarations. It gives a programmer the freedom to implement the details of a component without worrying about other programmers writing code that depends intricately on those internal decisions.

The programmer of a component only needs to maintain the public interface for the component, since other programmers will write code that depends on that interface. It provides robustness and adaptability because it allows the implementation details of parts of a program to change without adversely affecting other parts, making it easier to fix bugs or add new functionality with relatively local changes to a component. to add.

Design Patterns

Software Development

Independence: Define work so that each class is as independent as possible from other classes. Behaviors: Define behaviors for each class carefully and precisely so that the consequences of any action taken by a class are well understood by other classes that interact with it. Divide responsibilities between classes so that each class has autonomy over certain aspects of the program.

Coding Style and Documentation

When multiple words are concatenated to form a class name, they must follow the so-called "CamelCase" convention in which the first letter of each word is capitalized (eg Credit Card). Sometimes we deviate from this rule when we use a single capital letter to indicate the name of a data structure (such as treeT). Identifiers in any context that begin with a single leading underscore (eg _secret). are meant to represent that they are for "internal" use only for a class or module, and not part of a public interface.

In Python, these are technically multi-line string literals, usually delimited by triple quotes, which have no effect in execution. Formally, any string literal that appears as the first statement in the body of a module, class, or function (including a member function of a class) will be treated as a document string.

Documentation

Debugging: the process of following the execution of a program and detecting errors in it. Testing and debugging are often the most time-consuming activities in software development.

Testing

In addition to special inputs to the program, we also need to consider special conditions for the structures used by the program. While using hand-crafted test suites is essential, running the program on a large collection of randomly generated input is also beneficial.

Debugging

Class Definitions

The class limits charges that would cause the card balance to exceed the spending limit, but does not charge interest or late payments. A construct begins with the keyword class, followed by the class name, a colon, and then an indented block of code that serves as the body of the class. These methods are defined as functions, but with a special parameter called self that serves to identify the particular instance on which the method is invoked.

Assume that a user of our class has a variable, my_card, that identifies an instance of the CreditCard class. When the user calls my_card.get_balance(), identifier itself, within the definition of theget_balancemethod, refers to the card known by the caller as my_card. The expression, self._balance refers to an instance variable, named_balance, stored as part of that particular credit card's state.

The Constructor

Ecapsulation

We can provide accessors, such as get_balance, to provide a user of our class with read-only access to a feature. If we want to allow the user to change the state, we can provide appropriate update methods. If we want to allow the user to change the state, we can provide appropriate update methods.

Additional Methods

Did not explicitly check the type parameters to load and pay, nor any of the parameters to the constructor. The code will crash when you try to add that parameter to the current balance, like asvisa.charge('candy'). Allowing a user to charge a negative price, such as visa.charge(-300), will serve to lower the customer's balance.

Testing the class I

Does not provide statement coverage as there are never cases where a charge is declined due to the credit limit. Does not provide statement coverage as there are never instances where a charge is declined due to the credit limit.

Operator Overloading and Python’s Special Methods

Non-Operator Overloads

The standard way to determine the size of a container type is by calling the top-level len function. However, in the case of a user-defined class, the top-level function relies on a call to the specially named __len__ method of that class.

Implied Methods

However, there are some operators that have default definitions provided by Python in the absence of special methods, and there are some operators whose definitions derive from others. For container types, the __len__ method is usually defined to return the size of the container. The difference between expression a is the band expression a == bis , where the former evaluates whether identifiers a and b are aliases for the same object, and the latter tests the notion of whether the two identifiers refer to equivalent values.

The notion of "equivalence" depends on the context of the class, and semantics is defined with the__cfg__. To demonstrate the use of operator overloading via special methods, we provide an implementation of aVectorclass, which the. While it might be tempting to use a Python list directly to represent those coordinates, a list does not provide an appropriate abstraction for a geometric vector.

Iterators

Our preferred approach is the use of the generator syntax, which automatically produces an iterator of yield values. Python also helps by providing an automatic iterator implementation for any class that defines both __len__and __getitem__. Here we develop our own implementation of a class that mimics Python's built-in range class.

Before Python 3 was released, range was implemented as a function and it returned a list instance with elements in the specified range. Before Python 3 was released, range was implemented as a function and it returned a list instance with elements in the specified range. Instead of creating a new list instance, range is a class that can effectively represent the desired range of elements without ever explicitly storing them in memory.

Referensi

Dokumen terkait

Investasi awal yang dibutuhkan oleh Luffa Indonesia Abadi adalah sebesar Rp 784.352.675,00,- yang akan digunakan sebagai cadangan kas, biaya peralatan, biaya