Question

need help with this python program NOTE: You are NOT permitted to use ANY global variables....

need help with this python program

NOTE: You are NOT permitted to use ANY global variables. The use of any global variables will result in a deduction of 20%.

NOTE: There is NO input or printing anywhere other than main! Significant points will be deducted if you violate this constraint!

The following UML diagrams specify three classes. The RC_Filter class has a composition relationship with the Resistor and Capacitor classes. Your job is to implement these three classes as specified below. None of these three classes do ANY input or printing operations. You can NOT add any other members to these three classes.

Resistor

- resistance: float

- tolerance: float

+ __init__(float, float) :

+ get_resistance( ) : float

+ set_resistance(float)

+ get_tolerance( ) : float

+ set_tolerance(float)

+ getMin( ) : float

+ getMax( ) : float

+ __repr__( ) : string

Capacitor

- capacitance: float

- tolerance: float

+ __init__(float, float):

+ get_ capacitance ( ) : float

+ set_ capacitance (float)

+ get_tolerance( ) : float

+ set_tolerance(float)

+ getMin( ) : float

+ getMax( ) : float

+ __repr__( ) : string

The __init__ methods of these two classes set the values of the resistance, capacitance, and tolerance according to the parameter values. The getMin methods return the value of the resistance or capacitance minus the tolerance amount. The tolerance is a percentage (ie. 5%), so the minimum is nominal * (1 – tolerance/100). The getMax methods work like the getMin methods except they return the maximum resistance or capacitance which is nominal * (1 + tolerance/100). The __repr__ method returns a formatted string containing the resistance and the tolerance values.

Note: the resistance, capacitance, and tolerance must be setup as properties with methods to set and get the property values. The set methods should raise a ValueError exception if the argument value is <= 0.

RC_Filter

+ resistor: Resistor

+ capacitor: Capacitor

+ __init__(Resistor, Capacitor) :

+ getNominalCutoff( ) : float

+ getMinCutoff( ) : float

+ getMaxCutoff( ) : float

+ __repr__( ) : string

The RC_Filter class __init__ method has the following parameters: a Resistor object and a Capacitor object. These are stored as the member variables of the class. The getNominalCutoff method returns the filter’s cutoff frequency using the nominal resistance and capacitance values. The formula for calculating cutoff frequency is:

1 / (2 * pi * resistance * capacitance)

The getMinCutoff method returns the minimum cutoff frequency which is calculated using the maximum resistance and capacitance values. The getMaxCutoff method returns the maximum cutoff frequency which is calculated using the minimum resistance and capacitance values. The __repr__ method returns a string containing the resistance and capacitance information.

The main function will ask the user to enter values for resistance, tolerance, and capacitance, tolerance. It will create Resistor and Capacitor objects, and an RC_Filter object. It will call the methods on the filter object to verify that the methods of all three classes are working correctly.

The following is sample output from running the program.

Enter the resistance value: 1000

Enter the resistor tolerance: 10

Enter the capacitance value: .000001

Enter the capacitor tolerance: 10

Resistance: 1000.0 ohms

Tolerance: 10.0%

Capacitance: 0.000001 farads

Tolerance: 10.0%

Nominal Cutoff Frequency is 159.15 Hz

Minimum Cutoff Frequency is 131.53 Hz

Maximum Cutoff Frequency is 196.49 Hz

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Solution:

import math


class Resistor:
    _resistance = 0.0
    _tolerance = 0.0

    def __init__(self, r, t):
        self._resistance = r
        self._tolerance = t

    def get_resistance(self):
        return self._resistance

    def set_resistance(self, r):
        if r <= 0:
            raise ValueError
        self._resistance = r

    def get_tolerance(self):
        return self._tolerance

    def set_tolerance(self, t):
        if t <= 0:
            raise ValueError
        self._tolerance = t

    def getMin(self):
        return (self._resistance) * (1 - (self._tolerance / 100))

    def getMax(self):
        return (self._resistance) * (1 + (self._tolerance / 100))

    def __repr__(self):
        return "Resistance:" + str(self._resistance) + " ohms\n" + "Tolerance:" + str(self._tolerance) + "%"


class Capacitor:
    _capacitance = 0.0
    _tolerance = 0.0

    def __init__(self, c, t):
        self._capacitance = c
        self._tolerance = t

    def get_capacitance(self):
        return self._capacitance

    def set_capacitance(self, c):
        if c <= 0:
            raise ValueError
        self._capacitance = c

    def get_tolerance(self):
        return self._tolerance

    def set_tolerance(self, t):
        if t <= 0:
            raise ValueError
        self._tolerance = t

    def getMin(self):
        return (self._capacitance) * (1 - (self._tolerance / 100))

    def getMax(self):
        return (self._capacitance) * (1 + (self._tolerance / 100))

    def __repr__(self):
        return "Capacitance:" + str(format(self._capacitance, '.6f')) + " farads\n" + "Tolerance:" + str(
            self._tolerance) + "%"


class RC_Filter:
    def __init__(self, r, c):
        self._resistor = r
        self._capacitor = c

    def getNominalCutoff(self):
        return 1 / (2 * math.pi * self._resistor.get_resistance() * self._capacitor.get_capacitance())

    def getMinCutoff(self):
        return 1 / (2 * math.pi * self._resistor.getMax() * self._capacitor.getMax())

    def getMaxCutoff(self):
        return 1 / (2 * math.pi * self._resistor.getMin() * self._capacitor.getMin())

    def __repr__(self):
        return "Nominal Cutoff Frequency is " + str(
            round(self.getNominalCutoff(), 2)) + " Hz\n" + "Minimum Cutoff Frequency is " + str(
            round(self.getMinCutoff(), 2)) + " Hz\n" + "Maximum Cutoff Frequency is " + str(
            round(self.getMaxCutoff(), 2)) + " Hz"


r = float(input('Enter the resistance value:'))
t = float(input('Enter the resistor tolerance:'))
R = Resistor(r, t)
c = float(input('Enter the capacitance value:'))
t = float(input('Enter the capacitor tolerance:'))
C = Capacitor(c, t)
print(R.__repr__())
print(C.__repr__())
RC = RC_Filter(R, C)
print(RC.__repr__())

Output:

Add a comment
Know the answer?
Add Answer to:
need help with this python program NOTE: You are NOT permitted to use ANY global variables....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • IN PYTHON Assignment Overview This assignment will give you experience on the use of classes. Understand...

    IN PYTHON Assignment Overview This assignment will give you experience on the use of classes. Understand the Application The assignment is to first create a class calledTripleString.TripleStringwill consist of threeinstance attribute strings as its basic data. It will also contain a few instance methods to support that data. Once defined, we will use it to instantiate TripleString objects that can be used in our main program. TripleString will contain three member strings as its main data: string1, string2, and string3....

  • Please help me with this question! It is due by midnight! I'm doing this in Visual...

    Please help me with this question! It is due by midnight! I'm doing this in Visual Studio 2019. But any others are fine as long as it can compile. I also wanted to see the results if it is compiled and QT comments as well. Please respond immediately! Description: For this homework we will use classes to model something in the real world. In this case a resistor. The colored bands on the top-most resistor shown in the photo below...

  • I need help writing my main method**** Computer Science 111 Introduction to Algorithms and Programming: Java...

    I need help writing my main method**** Computer Science 111 Introduction to Algorithms and Programming: Java Programming Project #4 – Classes and Objects (20 Points) You will create 3 new classes for this project, two will be chosen from the list below and one will be an entirely new class you invent.Here is the list: Shirt Shoe Wine Book Song Bicycle VideoGame Plant Car FootBall Boat Computer WebSite Movie Beer Pants TVShow MotorCycle Design First Create three (3) UML diagrams...

  • Hello. I need help writing the following Java Program. Thank you Develop a class encapsulating the...

    Hello. I need help writing the following Java Program. Thank you Develop a class encapsulating the concept of a college course, assuming that a course has following attributers: code (for instance COSC1337), a description, and a number of credits (for instance 3). Include a constructor, the accessors, mutators and methods ‘toString’, ‘equals’, and ‘finalize’. Write a client class to test the behavior of the class and its methods. The outline of the class is given as follows: public class Course...

  • In this assignment, you will add several methods to the Binary Search Tree. You should have compl...

    In this assignment, you will add several methods to the Binary Search Tree. You should have completed the following three methods in the lab: public void insert(Key key, Value value) public Value get(Key key) public void inorder(Node root) For this assignment, you will implement the following: public void remove(Node root, Key key) public Key getMin(Node n) public Key getMax(Node n) public int height(Node n) The main method contains the statements to check whether your implementation works. You need to change...

  • help please Program Requirements You are given 1 file: Lab1Tests.java. You need to complete 1 file:...

    help please Program Requirements You are given 1 file: Lab1Tests.java. You need to complete 1 file: UWECPerson.java uWECPerson.java UWECPerson uwecld: int firstName: String lastName : String +UWECPerson(uwecld: int, firstName : String, lastName: String) +getUwecid): int setUwecld(uwecld: int): void +getFirstName): String +setFirstName(firstName: String): void getLastName): String setLastName(lastName: String): void +toString): String +equals(other: Object): boolean The constructor, accessors, and mutators behave as expected. The equals method returns true only if the parameter is a UWECPerson and all instance variables are equal. The...

  • Using Python. You will create two classes and then use the provided test program to make sure your program works This m...

    Using Python. You will create two classes and then use the provided test program to make sure your program works This means that your class and methods must match the names used in the test program You should break your class implementation into multiple files. You should have a car.py that defines the car class and a list.py that defines a link class and the linked list class. When all is working, you should zip up your complete project and...

  • DataSetEmployee Can you please help me the JAVA program? Here is the requirement. Rewrite DataSetBook to...

    DataSetEmployee Can you please help me the JAVA program? Here is the requirement. Rewrite DataSetBook to be DataSetEmployee. DataSetEmployee should extend ArrayList<Employee> and have no instance variables. You'll have to decide what replaces the getPages concept for getMax/getMin. As a hint, what method of Employee returns a numeric value? Make a package com.acme.midmanager. This package should contain the Manager class. Make a package com.acme.personnel. This package should contain the DataSetEmployee class. Make a package com.acme.topmanagement. This package should contain the...

  • (The Triangle class) Design a class named Triangle that extends the GeometricObject class. The Triangle class...

    (The Triangle class) Design a class named Triangle that extends the GeometricObject class. The Triangle class contains: - Three float data fields named side1, side2, and side3 to denote the three sides of the triangle. - A constructor that creates a triangle with the specified side1, side2, and side3 with default values 1.0. - The accessor methods for all three data fields. - A method named getArea() that returns the area of this triangle. - A method named getPerimeter() that...

  • Using loops with the String and Character classes. You can also use the StringBuilder class to...

    Using loops with the String and Character classes. You can also use the StringBuilder class to concatenate all the error messages. Floating point literals can be expressed as digits with one decimal point or using scientific notation. 1 The only valid characters are digits (0 through 9) At most one decimal point. At most one occurrence of the letter 'E' At most two positive or negative signs. examples of valid expressions: 3.14159 -2.54 2.453E3 I 66.3E-5 Write a class definition...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT