Question

In: Computer Science

For python Write a new method for the Fraction class called mixed() which returns a string...

For python

Write a new method for the Fraction class called mixed() which returns a string that writes out the Fraction in mixed number form. Here are some examples: f1 = Fraction(1, 2) print(f1.mixed()) # should print "1/2" f2 = Fraction(3, 2) print(f2.mixed()) # should return "1 and 1/2" f3 = Fraction(5, 1) print(f3.mixed()) # should return "5" def gcd(m, n): while m % n != 0: oldm = m oldn = n m = oldn n = oldm % oldn return n class Fraction: def __init__(self, top, bottom): self.num = top # the numerator is on top self.den = bottom # the denominator is on the bottom def __str__(self): return str(self.num) + "/" + str(self.den) def simplify(self): common = gcd(self.num, self.den) self.num = self.num // common self.den = self.den // common def __add__(self,otherfraction): newnum = self.num*otherfraction.den + self.den*otherfraction.num newden = self.den * otherfraction.den f = Fraction(newnum, newden) f.simplify() return( f ) # define the mixed() method below def main(): if __name__ == "__main__": main()

Solutions

Expert Solution

Short Summary:

Implemented the program as per requirement

Attached source code and sample output

**************Please do upvote to appreciate our time. Thank you!******************

Source Code:

def gcd(m, n):
while m % n != 0:
oldm = m
oldn = n
m = oldn
n = oldm % oldn
return n
from math import floor

class Fraction:
def __init__(self, top, bottom):
self.num = top
self.den = bottom
def __str__(self):
return str(self.num) + "/" + str(self.den)
def simplify(self):
common = gcd(self.num, self.den)
self.num = self.num
self.den = self.den
def __add__(self,otherfraction):
newnum = self.num*otherfraction.den + self.den*otherfraction.num
newden = self.den * otherfraction.den
f = Fraction(newnum, newden)
f.simplify()
return( f )
  
def mixed(self):
s= self.num/self.den
a=str(s)
b=a.split(".")
if int(b[1]) is not 0:
  
number=float("." + b[1])
# Fetch integral value of the decimal
intVal = floor(number)
  
# Fetch fractional part of the decimal
fVal = number - intVal
  
# Consider precision value to
# convert fractional part to
# integral equivalent
pVal = 1000000000
  
# Calculate GCD of integral
# equivalent of fractional
# part and precision value
gcdVal = gcd(round(fVal * pVal), pVal)
  
# Calculate num and deno
num= round(fVal * pVal) // gcdVal
deno = pVal // gcdVal
  
# Print the fraction
if int(b[0])>0:
print("The value is ",b[0]," and ",(intVal * deno) + num, "/", deno)
else:
print("The value is ",self.num, "/", self.den)
else:
print("The value is ",b[0])

  
  
def main():
f1 = Fraction(3,2)
  
f1.mixed()

if __name__ == "__main__":
main()

Output:

For 3,2,

For 5,1,

For 1,2,


**************Please do upvote to appreciate our time. Thank you!******************


Related Solutions

Python program. Write a function called cleanLowerWord that receives a string as a parameter and returns...
Python program. Write a function called cleanLowerWord that receives a string as a parameter and returns a new string that is a copy of the parameter where all the lowercase letters are kept as such, uppercase letters are converted to lowercase, and everything else is deleted. For example, the function call cleanLowerWord("Hello, User 15!") should return the string "hellouser". For this, you can start by copying the following functions discussed in class into your file: # Checks if ch is...
Given a string, write a method called removeRepthat returns another string where adjacent characters that are...
Given a string, write a method called removeRepthat returns another string where adjacent characters that are the same have been reduced to a single character. Test the program by calling the method from the main method. For example: removeRep(“yyzzza”) à “yza” removeRep(“aabbbccd”) à “abcd” removeRep(“122333”) à “123”
Write Python class that takes a string and returns with a valid phone number. Number format...
Write Python class that takes a string and returns with a valid phone number. Number format is ten-digit numbers consisting of a three-digit area code and a seven-digit number. Clean up different telephone numbers by removing punctuation, and removing incorrect format and the country code (1). You should throw a ValueError with a string if there are too many or too few digits, or the wrong digits. For example, the strings: +1 (617) 111-0000, 617-111-0000, 1 617 111 0000, 617.111.0000...
- Class DiscountPolicy is an abstract class with a method called computeDiscount, which returns the discount...
- Class DiscountPolicy is an abstract class with a method called computeDiscount, which returns the discount for the purchase of items. DiscountPolicy knows the name of the item and its cost as well as the number of items being purchased. - Class BulkDiscount, derived from DiscountPolicy, has two fields, minimum and percentage. computeDiscount method will return the discount based on the percentage applied, if the quantity of items purchased is more than minimum. For example: if minimum is 3 and...
JAVA Arrays 4 Write a method called isPalindrome that takes a String as input and returns...
JAVA Arrays 4 Write a method called isPalindrome that takes a String as input and returns true if the String is a palindrome.
Using python. Produce a method for a linked list that is called FIND , which returns...
Using python. Produce a method for a linked list that is called FIND , which returns the index of a lookup value within the linked list
Write the definition of a Point class method called clone that takes NO arguments and returns...
Write the definition of a Point class method called clone that takes NO arguments and returns a new Point whose x and y coordinates are the same as the Point’s coordinates.
Create a class named RemoveDuplicates and write code: a. for a method that returns a new...
Create a class named RemoveDuplicates and write code: a. for a method that returns a new ArrayList, which contains the nonduplicate elements from the original list public static ArrayList removeDuplicates(ArrayList list) b. for a sentinel-controlled loop to input a varying amount of integers into the original array (input ends when user enters 0) c. to output the original array that displays all integers entered d. to output the new array that displays the list with duplicates removed Use this TEST...
Write in Java: Write a method called: public static String[] noIdenticalCombine(String[] array1, String[] array2) { //...
Write in Java: Write a method called: public static String[] noIdenticalCombine(String[] array1, String[] array2) { // instructions: returns an array that contains all the Strings in array1 and array2 but without repetition. order does not matter, but it will return array1's elements and then array2's element that are not in array1. Assume there are no duplicates are in array1 and array2. Could use count which is how many str there are in array2, where !contains(array1, str). May an array of...
Code in Java Write a recursive method, reverseString, that accepts a String and returns the String...
Code in Java Write a recursive method, reverseString, that accepts a String and returns the String reversed. Write a recursive method, reverseArrayList, that accepts an ArrayList of Strings and returns the ArrayList in reserve order in reserve order of the input ArrayList. Write a main method that asks the user for a series of Strings, until the user enters “Done” and puts them in an ArrayList. Main should make use to reverseArrayList and reverseString to reverse each String in the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT