Question

In: Computer Science

python3 Design a class named Histogram to display a histogram of data. The Histogram class contains...

python3

Design a class named Histogram to display a histogram of data. The Histogram class contains the following

  • A number of instance variables/fields to store a histogram of data. You can design them by your own.
  • A constructor that creates a histogram with the following
    • a list of data. It may have more than 2 columns.
    • x_width: the width of the 1st column in the horizontal histogram. The default value is 5.
    • y_width: the width of the 2nd column in the horizontal histogram. The default value is 10.
  • A __str__ method that returns a nicely formatted string representation of the object.

and draw_vertical_histogram() method in the Histogram class. This method takes 3 parameters:

  • x_index: The draw_vertical_histogram() function takes data according to this index to plot the x-axis of the histogram. The default value is 0.
  • y_index: The draw_vertical_histogram() function takes data according to this index to plot the y-axis of the histogram.The default value is 1.

For example, consider the following code fragment:

h2 = Histogram([['May', 3], ['Bob', 7], ['Mike', 2]])
print(h2)
h2.draw_vertical_histogram()

produces:

x_width=5 y_width=10, data=[['May', 3], ['Bob', 7], ['Mike', 2]]
     *         
     *         
     *         
     *         
*    *         
*    *    *    
*    *    *    
---------------
May  Bob  Mike 

Note: column width is specified by the value of x_width

For example:

Test Result
h2 = Histogram([['May', 3], ['Bob', 7], ['Mike', 2]])
print(h2)
h2.draw_vertical_histogram()
x_width=5 y_width=10, data=[['May', 3], ['Bob', 7], ['Mike', 2]]
     *         
     *         
     *         
     *         
*    *         
*    *    *    
*    *    *    
---------------
May  Bob  Mike 
h2 = Histogram([['a', 2, 7], ['b', 1, 4], ['c', 8, 3]])
h2.draw_vertical_histogram(y_index=2)
*              
*              
*              
*    *         
*    *    *    
*    *    *    
*    *    *    
---------------
a    b    c    

Solutions

Expert Solution

If you have any doubts, please give me comment...

class Histogram:

    def __init__(self, data, x_width=5, y_width=10):

        self.data = data

        self.x_width = x_width

        self.y_width = y_width

    

    def __str__(self):

        return "x_width="+str(self.x_width)+", y_width="+str(self.y_width)+", data="+str(self.data)

    

    def draw_vertical_histogram(self, x_index=0, y_index=1):

        maximum = 0

        for d in self.data:

            if d[y_index]>maximum:

                maximum = d[y_index]

        for i in range(maximum-1, -1, -1):

            for d in self.data:

                if i<d[y_index]:

                    print('%-*s'%(self.x_width, "*"), end='')

                else:

                    print('%-*s'%(self.x_width, " "), end='')

            print()

        print('-'*(self.x_width*len(self.data)))

        for d in self.data:

            print("%-*s"%(self.x_width, d[x_index]), end='')

        print()

h2 = Histogram([['May', 3], ['Bob', 7], ['Mike', 2]])

print(h2)

h2.draw_vertical_histogram()

h2 = Histogram([['a', 2, 7], ['b', 1, 4], ['c', 8, 3]])

h2.draw_vertical_histogram(y_index=2)


Related Solutions

In Java, design a class named MyInteger. The class contains: An int data field named value...
In Java, design a class named MyInteger. The class contains: An int data field named value that stores the int value represented by this object. A constructor that creates a MyInteger object for the specified int A get method that returns the int Methods isEven(), isOdd(), and isPrime() that return true if the value is even, odd, or prime, respectively. Static methods isEven(int), isOdd(int), and isPrime(int) that return true if the specified value is even, odd, or prime, respectively. Static...
Design a class named Account that contains: A private int data field named id for the...
Design a class named Account that contains: A private int data field named id for the account. A private double data field named balance for the account. A private double data field named annualInterestRate that stores the current interest rate. A no-arg constructor that creates a default account with id 0, balance 0, and annualInterestRate 0. The accessor and mutator methods for id, balance, and annualInterestRate. A method named getMonthlyInterestRate() that returns the monthly interest rate. A method named withdraw(amount)...
Design a class named Account that contains: A private String data field named accountNumber for the...
Design a class named Account that contains: A private String data field named accountNumber for the account (default AC000). A private double data field named balance for the account (default 0). A private double data field named annualIntRate that stores the current interest rate (default 0). Assume all accounts have the same interest rate. A private Date data field named dateCreated that stores the date when the account was created. A no-arg constructor that creates a default account. A constructor...
7.3 (The Account class) Design a class named Account that contains: ■ A private int data...
7.3 (The Account class) Design a class named Account that contains: ■ A private int data field named id for the account. ■ A private float data field named balance for the account. ■ A private float data field named annualInterestRate that stores the current interest rate. ■ A constructor that creates an account with the specified id (default 0), initial balance (default 100), and annual interest rate (default 0). ■ The accessor and mutator methods for id, balance, and...
Java - Design a class named Account that contains: A private String data field named accountNumber...
Java - Design a class named Account that contains: A private String data field named accountNumber for the account (default AC000). A private double data field named balance for the account (default 0). A private double data field named annualIntRate that stores the current interest rate (default 0). Assume all accounts have the same interest rate. A private Date data field named dateCreated that stores the date when the account was created. A no-arg constructor that creates a default account....
In c++ Design a class named Account that contains: a.An int data field named id for...
In c++ Design a class named Account that contains: a.An int data field named id for the account. b.A double data field named balancefor the account. c.A double data field named annualInterestRate that stores the current interestrate. d.A no-arg constructor that creates a default account with id 0, balance 0, andannualInterestRate 0. e.The set and get functions for id,balance, and annualInterestRate. f.A functionearnedAmount()that calculates and returns the amount of dollars earned after one year. g.A function named printAccountInfo() that print...
Design a class named Fan to represent a fan. The class contains: ■ Three constants named...
Design a class named Fan to represent a fan. The class contains: ■ Three constants named SLOW, MEDIUM, and FAST with the values 1, 2, and 3 to denote the fan speed. ■ A private int data field named speed that specifies the speed of the fan (the default is SLOW). ■ A private boolean data field named on that specifies whether the fan is on (the default is false). ■ A private double data field named radius that specifies...
THIS IS JAVA PROGRAMMING Design a class named Account (that contains 1. A private String data...
THIS IS JAVA PROGRAMMING Design a class named Account (that contains 1. A private String data field named id for the account (default 0). 2. A private double data field named balance for the account (default 0). 3. A private double data field named annualInterestRate that stores the current interest rate (default 0). 4. A private Date data field named dateCreated that stores the date when the account was created. 5. A no-arg constructor that creates a default account. 6....
c++ E2b: Design a class named Rectangle to represent a rectangle. The class contains:
using c++E2b: Design a class named Rectangle to represent a rectangle. The class contains:(1) Two double data members named width and height which specifies the width and height of the rectangle .(2) A no-arg constructor that creates a rectangle with width 1 and height 1.(3) A constructor that creates a rectangle with the specified width and height .(4) A function named getArea() that returns the area of this rectangle .(5) A function named getPerimeter() that returns the perimeter of this...
Put In Java Programming The TicketMachine class: Design a class named TicketMachine that contains: • A...
Put In Java Programming The TicketMachine class: Design a class named TicketMachine that contains: • A double data field named price (for the price of a ticket from this machine). • A double data field named balance (for the amount of money entered by a customer). • A double data field named total (for total amount of money collected by the machine). • A constructor that creates a TicketMachine with all the three fields initialized to some values. • A...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT