Question
python3
d. Make a class called bad_math that has 2 variables, a and b. Include all the necessary functions, but also write a functi
0 0
Add a comment Improve this question Transcribed image text
Answer #1

d.

import random   

class bad_math:

def __init__(self,a,b):
self.a=a
self.b=b

def seta(self,a):
self.a=a

def geta(self):
return a

def setb(self,b):
self.b=b

def getb(self):
return b

def bad_addition(self):
add = self.a + self.b   
add = add - random.random()
print("a + b - random number = " + str(add))   

======================================================================================

e.

b = bad_math(5,12)
b.bad_addition()

=======================================================================================

complete program for question d and e:

import random # import random, to generate random numbers

class bad_math: # class bad_math

def __init__(self,a,b): # constructor. takes values of a and b as parameter and assigns to the class variables
self.a=a
self.b=b

def seta(self,a): # Mutators and accessord for variables a and b
self.a=a

def geta(self):
return a

def setb(self,b):
self.b=b

def getb(self):
return b

def bad_addition(self): # bad_addition()
add = self.a + self.b # adds a and b and stores the added number in add variable
add = add - random.random() # generate a random number and subtract from add
print("a + b - random number = " + str(add)) # print the result

b = bad_math(5,12) # create object of bad_math b
b.bad_addition() # call bad_addition()
  


____________________________________________________________________________

Output:

a + b - random number = 16.310302828677873

_____________________________________________________________________________

Screenshot of the code:

import random # import random, to generate random numbers class bad_math: # class bad_math # constructor. takes values of a a

=============================================================================================

f.

Please find the code below:

addition = 0 # declare variable addition to 0
while True: # create an infinite loop

num = input("Please enter a number: ") # Ask user to enter a number
addition = addition + int(num) # add the number to addition

if num=='0': # if the number is 0, come out of the loop
break

print("The sum of numbers is: " + str(addition)) # print the sum (value of addition variable)


  

_________________________________________________________________

Output:

Please enter a number: 5 Please enter a number: 6 Please enter a number: 7 Please enter a number: 4 Please enter a number: 10

___________________________________________________________________

Screenshot of the code:

addition = 0 while True: # declare variable addition to o # create an infinite loop num = input (Please enter a number: ) a

============================================================================================


  

Add a comment
Know the answer?
Add Answer to:
python3 d. Make a class called "bad_math' that has 2 variables, a and b. Include all...
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
  • Use PYTHON3 to create a program that asks the user for their name, and then prints...

    Use PYTHON3 to create a program that asks the user for their name, and then prints their initials. You must create a function called getInitials() that takes the name string and prints out the initials. It must be case insensitive and the initials must be printed in upper case. The program must contain a main() and a function called getInitials(), implemented as described in the function header comment given below. (You should include this function header comment in your own...

  • Write a class called Student. The specification for a Student is: Three Instance fields name -...

    Write a class called Student. The specification for a Student is: Three Instance fields name - a String of the student's full name totalQuizScore - double numQuizesTaken - int Constructor Default construtor that sets the instance fields to a default value Parameterized constructor that sets the name instance field to a parameter value and set the other instance fields to a default value. Methods setName - sets or changes the student name by taking in a parameter getName - returns...

  • Write code in static void main method class, which creates a boolean array called flags, size...

    Write code in static void main method class, which creates a boolean array called flags, size 10, and using a for loop sets each element to alternating values (true at index zero, false at index one Write code in a static void main method class, which creates a float array called sales, size 5, uses an initializer list to instantiate its elements with positive values having two decimal places (example 7.25), and using a for loop reads those values of...

  • Java Eclipse Coding question: Use data abstraction (real-world modeling) to come up with 3 instance variables...

    Java Eclipse Coding question: Use data abstraction (real-world modeling) to come up with 3 instance variables and 2 effectors for a new data type – the class HospitalPatient. You have the freedom to design the instance variables and effectors. instance variables effectors 1. 1. 2. 2. 3. Write the code for class HospitalPatient, according to the requirement above. Include the default constructors with no argument, and the constructor with all arguments. Provide a getter and setter for each individual instance...

  • Java - In NetBeans IDE: •Design an abstract class called BankAccount which stores the balance. –Implement...

    Java - In NetBeans IDE: •Design an abstract class called BankAccount which stores the balance. –Implement a constructor that takes the balance (float). –Provide abstract methods deposit(amount) and withdraw(amount) •Design a class called SavingsAccount which extends BankAccount. –Implement a constructor that takes the balance as input. –It should store a boolean flag active. If the balance at any point falls below $25, it sets the active flag to false. It should NOT allow the balance to fall below $0. –If...

  • FOR C++ PLEASE Create a class called "box" that has the following attributes (variables): length, width,...

    FOR C++ PLEASE Create a class called "box" that has the following attributes (variables): length, width, height (in inches), weight (in pounds), address (1 line), city, state, zip code. These variables must be private. Create setter and getter functions for each of these variables. Also create two constructors for the box class, one default constructor that takes no parameters and sets everything to default values, and one which takes parameters for all of the above. Create a calcShippingPrice function that...

  • In Java: Develop a simple class for a Student. Include class variables; StudentID (int), FirstName, LastName,...

    In Java: Develop a simple class for a Student. Include class variables; StudentID (int), FirstName, LastName, GPA (double), and Major. Extend your class for a Student to include classes for Graduate and Undergraduate. o Include a default constructor, a constructor that accepts the above information, and a method that prints out the contents FOR EACH LEVEL of the object, and a method that prints out the contents of the object. o Write a program that uses an array of Students...

  • Programming: Create a class called City with two public variables: a string to store the name...

    Programming: Create a class called City with two public variables: a string to store the name of the city and a float to store the average temperature in Fahrenheit. Create one object of the class that you create and ask the user to enter the city name and the average temperature in Fahrenheit. Store these in the object of the class that you create. Then display the contents of the class. Once that is working, make the variables private and...

  • Create a class called Pair that has two public integer member variables named "a" and "b",...

    Create a class called Pair that has two public integer member variables named "a" and "b", and a public member function named sum() that has no arguments but adds the two member variables together and returns their sum.

  • Write a class called Course_xxx that represents a course taken at a school. Represent each student...

    Write a class called Course_xxx that represents a course taken at a school. Represent each student using the Student class from the Chapter 7 source files, Student.java. Use an ArrayList in the Course to store the students taking that course. The constructor of the Course class should accept only the name of the course. Provide a method called addStudent that accepts one Student parameter. Provide a method called roll that prints all students in the course. Create a driver class...

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