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. "]...
Python Please Define a class that will represent soccer players as objects. A soccer player will...
Python Please Define a class that will represent soccer players as objects. A soccer player will have as attributes, name, age, gender, team name, play position on the field, total career goals scored. The class should have the following methods: 1. initializer method that will values of data attributes arguments. Use 0 as default for career goals scored. 2. str method to return all data attributes as combined string object. 3. addToGoals that will accept an argument of int and...
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 please Questions #6 and # 7 please A string is one of most powerful data...
Python please Questions #6 and # 7 please A string is one of most powerful data types in programming. A string object is a sequence of characters and because it is a sequence, it is indexable, using index numbers starting with 0. Similar to a list object, a string object allows for the use of negative index with -1 representing the index of the last character in the sequence. Accessing a string object with an invalid index will result in...
Define the class HotelRoom. The class has the following private data members: the room number (an...
Define the class HotelRoom. The class has the following private data members: the room number (an integer) and daily rate (a double). Include a default constructor as well as a constructor with two parameters to initialize the room number and the room’s daily rate. The class should have get/set functions for all its private data members [20pts]. The constructors and the get/set functions must throw an invalid_argument exception if either one of the parameter values are negative. The exception handler...
Define the class HotelRoom. The class has the following private data members: the room number (an...
Define the class HotelRoom. The class has the following private data members: the room number (an integer) and daily rate (a double). Include a default constructor as well as a constructor with two parameters to initialize the room number and the room’s daily rate. The class should have get/set functions for all its private data members [20pts]. The constructors and the get/set functions must throw an invalid_argument exception if either one of the parameter values are negative. The exception handler...
Implement a program as an object using a class (abstract data type) in C++ that does...
Implement a program as an object using a class (abstract data type) in C++ that does the following: 1) reads the firstName, lastName and 3 test scores of at least five students. 2) calculate student test score totals, average, letter grade for each student. 3) Display the results in a table format showing firstName, lastName, test1, test2, test3, total, average, letterGrade, of all the students. 3 files .h, .cpp, main.cpp create an object that can hold records. must get records...
Define a class template called genericStack for storing any data type in a static stack. Your...
Define a class template called genericStack for storing any data type in a static stack. Your class template must include a constructor, a destructor, push, pop, peek, isFull, and isEmpty functions (no display function is required). Write a simple main function in your program that demonstrates the class template with a stack of strings.
#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() ?
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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT