Question

In: Computer Science

Needs to be done in PYTHON A. Create a Dollar currency class with two integer attributes...

Needs to be done in PYTHON

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 factor can be defaulted in the class definition based on 1 USD = 1.36 C2D or 1 C2D = 0.74 USD.
  • Also, 1000 of C2D fractional parts equals 1 C2D whole part.

C. In your two currency classes, add public methods for the following:

  • Default Construction (i.e. no parameters passed)
  • Construction based on parameters for all attributes
  • Copy Constructor and/or Assignment, as applicable to your programming language of choice
  • Destructor, as applicable to your programming language of choice
  • Setters and Getters for all attributes
  • Adding two objects of the same currency
  • Subtracting one object from another object of the same currency
  • Comparing two objects of the same currency for equality/inequality
  • Comparing two objects of the same currency to identify which object is larger or smaller
  • Print method to print details of a currency object
  • In your derived class only, methods to convert USD objects to C2D and vice versa

D. Create a Wallet class with one attribute - an array of two Dollar references / pointers and the following methods to demonstrate polymorphism of the currencies:

  • A default Constructor which sets
    • the first element of the array to a zero value Dollar object
    • the second element of the array to a zero value CIS22C Dollar object
  • A Destructor, as applicable to your programming language of choice
  • Methods to add or subtract
    • USD objects to/from the first element only and
    • C2D objects to/from the second element only
  • Methods to compare if the value of either element is greater or smaller than an input value
  • A method to Print the values of the two elements in the Wallet

E. In your main:

  • Create a Wallet object
  • Provide the user a main menu to add/ subtract/ compare the Dollar and CIS22C Dollar values in the Wallet as well as print the contents of the Wallet
  • You can use a second level menu choice to allow the user to select currency type
  • Based on user choice, create either UD or C2d objects as needed to perform the desired operations.
  • The main menu should be run in a loop until the user selects the Exit option
  • There is no sample output - you are allowed to provide user interactivity as you see fit and programs will be graded for clarity of interaction

Needs to be done in PYTHON, do the best with the information you have no other info there.

This assignment was to be done in C++ but our teacher allows us to do in Python. Please to the best of your ability to try fufill as many requirements as possible in Python. I am aware this is in C++ but try the best to modify to make it work in Python. Thank you.

Solutions

Expert Solution

# 100 fract = 1 whole
class Dollar:
  
# Constructors
def _init_(self):
__whole = 0
__fract = 0
__name = ''
  
def _init_(self, whole, fract, name):
self.__whole = whole
self.__fract = fract
self.__name = name
  
# Setters and Getters
def set_whole(self, whole):
self.__whole = whole
  
def set_fractional(self, fractional):
self.__fract = fractional
  
def set_name(self, name):
self.__name = name
  
def get_whole(self):
return self.__whole
  
def get_fractional(self):
return self.__fract
  
def get_name(self):
return self.__name

# Destructor
def _del_():
print("Destructor called!")

# Adding same currency
def add_sameCurrency(self, node):
fract = self._fract + node._fract
if fract > 99:
wholePart = fract // 100
fract = fract % 100
whole = self._whole + node._whole + wholePart
  
return whole, fract
  
# Subtraction same currency
def sub_sameCurrency(self, node):
fract = 0
whole = 0
if self._fract >= node._fract:
fract = self._fract - node._fract
else:
fract = self._fract - node._fract + 100
whole = self.__whole - 1
whole += self._whole - node._whole
return whole, fract
  
# Checking equality/inequality
def is_equal(self, node):
if self._name == node._name:
if self.__whole == node.whole:
if self._fract == node._fract:
return True
else:
return False
else:
return False
else:
return False
  
# comapring same currency
def is_greater(self, node):
if self._whole > node._whole:
return True
elif self._whole == node._whole:
if self._fract > node._fract:
return True
else:
return False
else:
return False
  
def show(self):
print('The amount is: ', self._whole, '.', self.fract, ' ', self._name)
  
def C2D2USD(self):
self._fract = 0.74 * self_fract
self._whole = 0.74 * self._whole
  
if self.__fract > 99:
self._fract = self._fract % 100
self._whole += self._fract // 100
  
self.__name = 'USD'
return self

# 1000 fract = 1 whole
# 1 USD = 1.36 C2D
# 1 C2D = 0.74 USD
class CIS22C:
# Constructors
def _init_(self):
__whole = 0
__fract = 0
__name = ''
  
def _init_(self,whole, fract, name):
self.__whole = whole
self.__fract = fract
self.__name = name
  
# Setters and Getters
def set_whole(self, whole):
self.__whole = whole
  
def set_fractional(self, fractional):
self.__fract = fractional
  
def set_name(self, name):
self.__name = name
  
def get_whole(self):
return self.__whole
  
def get_fractional(self):
return self.__fract
  
def get_name(self):
return self.__name

# Destructor
def _del_():
print("Destructor called!")

# Adding same currency
def add_sameCurrency(self, node):
fract = self._fract + node._fract
if fract > 99:
wholePart = fract // 1000
fract = fract % 1000
whole = self._whole + node._whole + wholePart
  
return whole, fract
  
# Subtraction same currency
def sub_sameCurrency(self, node):
fract = 0
whole = 0
if self._fract >= node._fract:
fract = self._fract - node._fract
else:
fract = self._fract - node._fract + 1000
whole = self.__whole - 1
whole += self._whole - node._whole
return whole, fract
  
# Checking equality/inequality
def is_equal(self, node):
if self._name == node._name:
if self.__whole == node.whole:
if self._fract == node._fract:
return True
else:
return False
else:
return False
else:
return False
  
# comapring same currency
def is_greater(self, node):
if self._whole > node._whole:
return True
elif self._whole == node._whole:
if self._fract > node._fract:
return True
else:
return False
else:
return False
  
def show(self):
print('The amount is: ', self._whole, '.', self.fract, ' ', self._name)
  
def USD2C2D(self):
self._fract = 1.36 * self_fract
self._whole = 1.36 * self._whole
  
if self.__fract > 999:
self._fract = self._fract % 1000
self._whole += self._fract // 1000
  
self.__name = 'C2D'
return self

class Wallet:
  
def _init_(self):
array = [0, 0]
dol = Dollar(0, 0, 'USD')
array[0] = dol
c2d = CIS22C(0, 0, 'C2D')
array[1]= c2d
  
def _init_(self, whole, fract, name):
array = [0, 0]
if name == 'USD':
dol = Dollar(whole, fract, name)
array[0] = dol
else:
c2d = CIS22C(whole, fract, name)
array[1] = c2d
  
def _del_(self):
print('The object is deleted!')
  
def addUSD(self, node):
self[0] = self[0].add_sameCurrency(node)
  
def addC2D(self, node):
self[1] = self[1].add_sameCurrency(node)
  
def subUSD(self, node):
self[0] = self[0].sub_sameCurrency(node)
  
def subC2D(self, node):
self[1] = self[1].sub_sameCurrency(node)
  
def compareUSD(self, node):
if not self[0].is_equal(node):
if self[0].is_greater(node):
print("Greater")
else:
print("smaller")
else:
print('Equal')
  
def compareC2D(self, node):
if not self[1].is_equal(node):
if self[1].is_greater(node):
print("Greater")
else:
print("smaller")
else:
print('Equal')

if '_name' == __main_: // This is the main function
wallet = Wallet()
While True:
print("Press 1 to add")
print("Press 2 to subtract")
print("Press 3 to compare")
print("Press 0 to exit")
  
i = int(input())
if i == 0:
break
if i == 1:
while True:
print("Press 1 for USD")
print("Press 2 to C2D")
print("Press 0 to exit")
  
k = int(input())
if k == 0:
break
if k == 1:
print("Enter value")
value = input()
value = value.strip().split('.')
value = Dollar(value[0], value[1], 'USD')
wallet.addUSD(value)
if k == 2:
print("Enter value")
value = input()
value = value.strip().split('.')
value = Dollar(value[0], value[1], 'C2D')
wallet.addC2D(value)
if i == 2:
while True:
print("Press 1 for USD")
print("Press 2 to C2D")
print("Press 0 to exit")
  
k = int(input())
if k == 0:
break
if k == 1:
print("Enter value")
value = input()
value = value.strip().split('.')
value = Dollar(value[0], value[1], 'USD')
wallet.subUSD(value)
if k == 2:
print("Enter value")
value = input()
value = value.strip().split('.')
value = Dollar(value[0], value[1], 'C2D')
wallet.subC2d(value)
if i == 3:
while True:
print("Press 1 for USD")
print("Press 2 to C2D")
print("Press 0 to exit")
  
k = int(input())
if k == 0:
break
if k == 1:
print("Enter value")
value = input()
value = value.strip().split('.')
value = Dollar(value[0], value[1], 'USD')
wallet.compareUSD(value)
if k == 2:
print("Enter value")
value = input()
value = value.strip().split('.')
value = Dollar(value[0], value[1], 'C2D')
wallet.compareC2d(value)


Related Solutions

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...
In python- Create a class defined for Regression. Class attributes are data points for x, y,...
In python- Create a class defined for Regression. Class attributes are data points for x, y, the slope and the intercept for the regression line. Define an instance method to find the regression line parameters (slope and intercept). Plot all data points on the graph. Plot the regression line on the same plot.
This is python #Create a class called Rectangle. Rectangle should #have two attributes (instance variables): length...
This is python #Create a class called Rectangle. Rectangle should #have two attributes (instance variables): length and #width. Make sure the variable names match those words. #Both will be floats. # #Rectangle should have a constructor with two required #parameters, one for each of those attributes (length and #width, in that order). # #Rectangle should also have a method called #find_perimeter. find_perimeter should calculate the #perimeter of the rectangle based on the current values for #length and width. # #perimeter...
Challenge: Documents Description: Create a class in Python 3 named Document that has specified attributes and...
Challenge: Documents Description: Create a class in Python 3 named Document that has specified attributes and methods for holding the information for a document and write a program to test the class. Purpose: The purpose of this challenge is to provide experience creating a class and working with OO concepts in Python 3. Requirements: Write a class in Python 3 named Document that has the following attributes and methods and is saved in the file Document.py. Attributes __title is a...
Create a Class to contain a customer order Create attributes of that class to store Company...
Create a Class to contain a customer order Create attributes of that class to store Company Name, Address and Sales Tax. Create a public property for each of these attributes. Create a class constructor without parameters that initializes the attributes to default values. Create a class constructor with parameters that initializes the attributes to the passed in parameter values. Create a behavior of that class to generate a welcome message that includes the company name. Create a Class to contain...
This needs to be in Python and each code needs to be separated. Design a class...
This needs to be in Python and each code needs to be separated. Design a class for a fast food restaurant meals that should have 3 attributes; Meal Type (e.g., Burger, Salad, Taco, Pizza); Meal size (e.g., small, medium, large); Meal Drink (e.g., Coke, Dr. Pepper, Fanta); and 2 methods to set and get the attributes. Write a program that ask the user to input the meal type, meal size, and meal drinks. This data should be stored as the...
Using python class: Design a class called Account CLASS NAME:    Account ATTRIBUTES: -nextAcctID: int    #NOTE-...
Using python class: Design a class called Account CLASS NAME:    Account ATTRIBUTES: -nextAcctID: int    #NOTE- class-level attribute initialized to 1000 -acctID: int -bank: String -acctType: String (ex: checking, savings) -balance: Float METHODS: <<Constructor>>Account(id:int, bank: String, type:String, bal:float) +getAcctID(void): int                        NOTE: retrieving a CLASS-LEVEL attribute! -setAcctID(newID: int): void           NOTE: PRIVATE method +getBank(void): String +setBank(newBank: String): void +getBalance(void): float +setBalance(newBal: float): void +str(void): String NOTE: Description: prints the information for the account one item per line. For example: Account #:        ...
Details: Create a class called CompareArrays that determines if two specified integer arrays are equal. The...
Details: Create a class called CompareArrays that determines if two specified integer arrays are equal. The class should have one static methods: public static boolean compare(int[] arrayOne, int[] arrayTwo) – Which compares the two arrays for equality. The two arrays are considered equal if they are the same size, and contain the same elements in the same order. If they are equal, the method should return true. Otherwise, the method should return false. NOTE: You are not allowed to use...
Create a car class with three attributes: year, mpg, speed and list of owners. The class...
Create a car class with three attributes: year, mpg, speed and list of owners. The class also should have 2 methods called accelerate and brake. Whenever the car accelerates, its speed is increased by 30 and whenever the car brakes, its speed is reduced by 60. add a constructor to the class, which takes year, mpg, and speed as input implement a magic method that prints the car information including year, speed and mpg. That is, for a car object,...
In python please Problem Create two server objects using the class create a function, biggest_server_object_sum, outside...
In python please Problem Create two server objects using the class create a function, biggest_server_object_sum, outside of the ServerClass class, that: 1. Takes the IP 2. Sums the octets of the IP together (example 127.0.0.1 = 127+0+0+1 = 128) 3. Returns the Server object with the larger sum Example: server_one = ServerClass("127.0.0.1") server_two = ServerClass("192.168.0.1") result = biggest_ip_sum(server_one, server_two) print(result) Hint Modify get_server_ip in the previous problem. -------------------- Please use this code to start class ServerClass: """ Server class for...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT