Question

In: Computer Science

Python Please Define a class that has one data attribute of type float. The initializer does...

Python Please

Define a class that has one data attribute of type float. The initializer does not accept an argument, however, it initializes the data attribute to zero. Include in class the following methods:

1. func that will accept as two arguments. The first argument a value of float type and the second argument if string. The function will perform the following depending on the string argument:

a. if string is '+', the first argument will be added to the data attribute.

b. if string is '-', the first argument will be subtracted from the data attribute.

c. if string is '*', the data attribute will be equal to the data attribute multiplied by first argument.

d. if string is '/', the data attribute will be equal to the data attribute divided by first argument.

e. if string is '^', the data attribute will be equal to the data attribute raised to the power of the first argument.

2. _ _init_ _ method

3. _ _str_ _ that will return the data attribute as a string

4. An accessor that will return the data attribute

Solutions

Expert Solution

class Operations:
    #constructor
    def __init__(self):
        self.data=0.0
    #func accepts 2 arguments
    def func(self,value,string):
        if string=='+':
            self.data+=value
        elif string=='-':
            self.data-=value
        elif string=='*':
            self.data*=value
        elif string=='/':
            self.data/=value
        elif string=='^':
            self.data**=value
    #accessor
    def getData(self):
        return self.data
    #str() prints string representation of object
    def __str__(self):
        return str(self.data)
#testing class
#create object for class
operation=Operations()
operation.func(4.5,"+")
operation.func(3.4,"*")
print(operation) #invokes str()


Output


Related Solutions

Python: High school assignment, please keep simple In python: Use the following initializer list to create...
Python: High school assignment, please keep simple In python: Use the following initializer list to create an array: twainQuotes = ["I have never let my schooling interfere with my education.", "Get your facts first, and then you can distort them as much as you please.", "If you tell the truth, you don't have to remember anything.", "The secret of getting ahead is getting started.", "Age is an issue of mind over matter. If you don't mind, it doesn't matter. "]...
Please show it with python class Node {     int data;     Node left, right;    ...
Please show it with python class Node {     int data;     Node left, right;     public Node(int item)     {         data = item;         left = right = null;     } } public class BinaryTree { // Root of the tree implemented in Node class Node root; Node findLowestCommonAncestor(int node1, int node2) {     return findLowestCommonAncestor(root, node1, node2); } // This function returns pointer to LCA of two given // values node1 and node2. This function assumes that...
#Python 3.7 "Has no attribute" error - def get():     import datetime     d = date_time_obj.date()...
#Python 3.7 "Has no attribute" error - def get():     import datetime     d = date_time_obj.date()     return(d) print(a["Date"]) print("3/14/2012".get()) How to write the "get" function (without any imput), to convery the format ""3/14/2012" to the format "2012-03-14", by simply using "3/14/2012".get() ?
Python Please (The Fan class) Design a class named Fan to represent a fan. The class...
Python Please (The Fan class) Design a class named Fan to represent a fan. The class contains: ■ Three constants named SLOW, MEDIUM, and FAST with the values 1, 2, and 3 to denote the fan speed. ■ A private int data field named speed that specifies the speed of the fan. ■ A private bool data field named on that specifies whether the fan is on (the default is False). ■ A private float data field named radius that...
What type of data is the number of robberies reported in your city? Attribute Discrete Qualitative...
What type of data is the number of robberies reported in your city? Attribute Discrete Qualitative Continuous
in java please: Create an ArrayListReview class with one data field of ArrayList and one with...
in java please: Create an ArrayListReview class with one data field of ArrayList and one with LinkedList with the generic type passed to the class. (2 point) Create a constructor that populate an array list and the LinkedList filled with the generic type through inserting new elements into the specified location index-i in the list. (2 points)
A. Create a Dollar currency class with two integer attributes and one string attribute, all of...
A. Create a Dollar currency class with two integer attributes and one string attribute, all of which are non-public. The int attributes will represent whole part (or currency note value) and fractional part (or currency coin value) such that 100 fractional parts equals 1 whole part. The string attribute will represent the currency name. B. Create a CIS22C Dollar derived/inherited class with one additional non-public double attribute to represent the conversion factor from/to US Dollar. The value of the conversion...
Objectives: Define the new class type: Queue using a singly linked list. Define the new class...
Objectives: Define the new class type: Queue using a singly linked list. Define the new class type: Jukebox which creates three objects of type Queue class. Practice enqueue-ing and dequeue-ing elements from the top of your singly linked list Queue class. Test the implementation of the class: MyTunes. The class files are here: https://drive.google.com/file/d/1yCCQeZCS-uLoL_CK0Et9dX-KCaokXQxR/view?usp=sharing class MyTunes Creates an object of type MyTunes class that partially simulate the digital jukebox TouchTunes, using a queue which holds playlist. Tests the implementation of...
***Define a class called Pizza that has member variables to track the type of pizza (either...
***Define a class called Pizza that has member variables to track the type of pizza (either deep dish, hand tossed, or pan) along with the size (either small, medium, or large) and the number of pepperoni or cheese toppings. You can use constants to represent the type and size. Include mutator and accessor functions for your class. Create a void function, outputDescription( ) , that outputs a textual description of the pizza object. Also include a function, computePrice( ) ,...
in python please ,, A SOON AS POSSIBLE PLEASE #Class invariant for SimpleQueue, a queue based...
in python please ,, A SOON AS POSSIBLE PLEASE #Class invariant for SimpleQueue, a queue based on a linked list # 1. The queue is a linked list of ListNode objects. # 2. A SimpleQueue object has instance variables self.head, a reference to the node at the front of the queue, self.tail, a reference # to the node at the end of the queue, and self.size, the number of nodes in the queue. # 3. If self.size = 0, then...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT