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:
_____________________________________________________________________________
Screenshot of the code:
=============================================================================================
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:
___________________________________________________________________
Screenshot of the code:
============================================================================================
python3 d. Make a class called "bad_math' that has 2 variables, a and b. Include all...
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 - 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 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 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 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, 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, 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 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", 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 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...