ISF DP Computer Science

Static Methods #

In this lab you will learn about static methods through a Currency Conversion System.


Syllabus Topics [SL] #

  • B3.1.3 Distinguish between static and non-static variables and methods.

Key Vocabulary #

WordDefinition
Static MethodA method that belong to the class, not the individual objects.
@staticmethod decoratorInforms Python that the method is static.

[0] Class Overview #

In this lab, you will experience a class that only has static methods and static attributes: CurrencyConverter.

CurrencyConversion has only static methods and static attributes. It’s helpful to have the functions grouped together in a class, but it does not require objects with unique attributes or methods.

Static methods can be called without can object, simply on the class itself.

class CurrencyConverter:
    
    __CURRENCIES = ["HKD", "USD", "JPY", "EUR", "GBP", "THB", "KRW"]
    __RATES = [1.0, 0.13, 20.32, 0.11, 0.096, 4.02, 188.68]

    @staticmethod
    def get_rate(currency):
        """Returns the rate for the currency"""

        index = CurrencyConverter.__CURRENCIES.index(currency)

        return CurrencyConverter.__RATES[index]

CurrencyConverter.get_rate("HKD")

๐Ÿ“– Here is the UML diagram for the class. The underline of an attribute or method denotes it is static.


classDiagram
    
    class CurrencyConverter {
        - CURRENCIES : string[]$ 
        - RATES : float[]$
        - SYMBOLS: string[]$
        + get_rate(string) float $ 
        + set_rate(string, float) None$
        + convert(float, string, string) float$
        + format(float, string) string$
        + parse_and_convert(string) string$
    }


[1] Set up #

๐Ÿ’ป Clone your repo in the correct folder. Be sure to replace yourgithubusername with your actual username.

cd ~/desktop/dpcs/unit03_oop
git clone https://github.com/isf-dp-cs/lab_static_methods_yourgithubusername
๐Ÿ’ป In the Terminal, type the following command to open the lab folder.
cd lab_static_methods_yourgithubusername

๐Ÿ’ป Enter the Poetry Shell to start the lab. As a reminder, we will run this command at the start of each lab, but only when we are inside a lab folder.

poetry shell
๐Ÿ‘พ ๐Ÿ’ฌ Exiting the poetry shell

When you want to exit the shell, you can type exit or ^D


[2] Implement the methods #

๐Ÿ’ป Firstly, test using the existing static attributes and methods in the CurrencyConverter class.

๐Ÿ’ป Secondly, implement the following methods as outlined in the DOCSTRINGS

  • convert(amount, from_currency, to_currency)
  • format(amount, currency)
  • parse_and_convert(user_input)

๐Ÿ’ธ Currency Conversion Tips

  • HKD to another currency: multiply by the rate
  • another currency to another currency: convert the first currency to HKD then to the other currency
# HKD $10 -> USD $1.3
10 * 0.13 = 1.3

# JYP ยฅ1000  -> GDP ยฃ8.64
1000/20.32 -> 49.20     #1) convert JYP to HKD
4.92*0.096 = 4.72       # 2) convert HKD to GDP

[3] Deliverables #

โšกโœจ Once you finish the lab, be sure to complete these two steps:

๐Ÿ“‹ Update Syllabus Checklist: Go to your Syllabus Content Checklist in your Google Drive and update it accordingly.

๐Ÿ’ป Push your work to Github

  • git status
  • git add -A
  • git status
  • git commit -m “describe your code here”
  • git push
  • remote