Question

PLEASE DO BOTH OF THESE IN PYTHON 1) Exercise #1: Design and implement class Rectangle to...

PLEASE DO BOTH OF THESE IN PYTHON

1) Exercise #1: Design and implement class Rectangle to represent a rectangle object. The class defines the following attributes (variables) and methods:

Two Class variables of type double named height and width to represent the height and width of the rectangle. Set their default values to 1.0 in the default constructor.

A non-argument constructor method to create a default rectangle.

Another constructor method to create a rectangle with user-specified height and width.

Python only: use the classmethod decorator

Method getArea() that returns the area.

Method getPerimeter() that returns the perimeter.

Method getHeight() that returns the height.

Method getWidth() that returns the width.

Now design and implement a test program to create two rectangle objects: one with default height and width, and the second is 5 units high and 6 units wide. Next, test the class methods on each object to print the information as shown below.

Sample run:

First object:

Height:     1 unit

Width:      1 unit

Area:       1 unit

Perimeter: 4 units

Second object:

Height:     5 unit

Width:      6 unit

Area:       30 units

Perimeter: 22 units

2) Exercise #2: Design and implement class Stock to represent a company’s stock. The class defines the following attributes (variables) and methods:

A class variable of type String named Symbol for the stock’s symbol.

A class variable of type String named Name for the stock’s name.

A class variable of type double named previousClosingPrice to store the last closing price.

A class variable of type double named currentPrice to store the current price.

A constructor method to create a stock with user-specified name and symbol.

Method getName() that returns the stock’s name.

Method getSymbol() that returns the stock’s symbol.

Method setClosingPrice() that sets the previous closing price.

Method setCurrentPrice() that sets the current price.

Method getChangePercent() that returns the percentage changed from

previousClosingPrice to currentPrice.

Use the formula: (currentPrice - previousClosingPrice)/ currentPrice * 100

Method named toString() (Java & C#) or ___str___() (Python) to printout a meaningful description of a stock object when passing the object name to the print statement.

e.g.: The statement PRINT yahooStock would print the string:

Yahoo stock’s closing price is $234.54.

Now design and implement a test program to create two stock objects: one for Google with symbol GOG and the second is for Microsoft with symbol MSF. Set their closing and current prices according to the information below. Next, test the class methods on each object to print in the information in a similar manner to the one shown below:

Sample run:

Google stock:

Symbol: GOG     

Closing price: 134.67

Current price: 131.98

Change percent: - 2%

Google stock closing price is $131.98

Microsoft stock:

Symbol: MSF     

Closing price: 156.52

Current price: 161.22

Change percent: 3%

Microsoft stock closing price is $161.22

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

Here is the first part

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

class Rectangle():

    def __init__(self,height=1.0,width=1.0):
        self.height=height
        self.width=width

    def getArea(self):
        return self.height*self.width
    def getPerimeter(self):
        return 2*(self.width+self.height)
    def getHeight(self):
        return self.height
    def getWidth(self):
        return self.width


def main():
    first_rectangle=Rectangle()
    second_rectangle=Rectangle(5,6)
    print('First Object')
    print('Height:',first_rectangle.getHeight(),'unit')
    print('Width:',first_rectangle.getWidth(),'unit')
    print('Area:',first_rectangle.getArea(),'unit')
    print('Perimeter:',first_rectangle.getPerimeter(),'units')

    print('Second Object')
    print('Height:',second_rectangle.getHeight(),'unit')
    print('Width:',second_rectangle.getWidth(),'unit')
    print('Area:',second_rectangle.getArea(),'units')
    print('Perimeter:',second_rectangle.getPerimeter(),'units')

main()

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

Here is Part 2

class Stock():
    def __init__(self,sym,name):
        self.symbol=sym
        self.name=name
        self.previousClosingPrice=0.0
        self.currentPrice=0.0

    def getName(self):
        return self.name
    def getSymbol(self):
        return self.symbol
    def setClosingPrice(self,price):
        self.previousClosingPrice=price
    def setCurrentPrice(self,price):
        self.currentPrice=price
    def getChangePercent(self):
        return (self.currentPrice-self.previousClosingPrice)/(0.01*self.currentPrice)

    def __str__(self):
        return '{0} stock Symbol: {1}, Closing Price: {2}, Current Price: {3}, Change Percent: {4}% '.format(\
            self.name,self.symbol,self.previousClosingPrice,self.currentPrice,(int)(self.getChangePercent()))


def main():
    microsoft=Stock("MSF","Microsoft")
    microsoft.setClosingPrice(156.52)
    microsoft.setCurrentPrice(162.22)
    print(microsoft)

    google=Stock("GOG","Google")
    google.setClosingPrice(134.67)
    google.setCurrentPrice(131.98)
    print(google)

main()

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

Add a comment
Know the answer?
Add Answer to:
PLEASE DO BOTH OF THESE IN PYTHON 1) Exercise #1: Design and implement class Rectangle to...
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
  • PLEASE DO IN C# AND MAKE SURE I CAN COPY CODE IN VISUAL STUDIO Exercise #1:...

    PLEASE DO IN C# AND MAKE SURE I CAN COPY CODE IN VISUAL STUDIO Exercise #1: Design and implement class Rectangle to represent a rectangle object. The class defines the following attributes (variables) and methods: 1. Two Class variables of type double named height and width to represent the height and width of the rectangle. Set their default values to 1.0 in the default constructor. 2. A non-argument constructor method to create a default rectangle. 3. Another constructor method to...

  • NEEDS TO BE IN PYTHON: (The Rectangle class) Following the example of the Circle class in...

    NEEDS TO BE IN PYTHON: (The Rectangle class) Following the example of the Circle class in Section 9.2, design a class named Rectangle to represent a rectangle. The class contains: - Two data fields named width and height. - A constructor that creates a rectangle with the specified width and height. The default values are 1 and 2 for the width and height, respectively. - A method named getArea() that returns the area of this rectangle. - A method named...

  • SOLVE IN PYTHON: Exercise #1: Design and implement class Circle to represent a circle object. The...

    SOLVE IN PYTHON: Exercise #1: Design and implement class Circle to represent a circle object. The class defines the following attributes (variables) and methods: A private variable of type double named radius to represent the radius. Set to 1. Constructor Methods: Python : An overloaded constructor method to create a default circle. C# & Java: Default Constructor with no arguments. C# & Java: Constructor method that creates a circle with user-specified radius. Method getRadius() that returns the radius. Method setRadius()...

  • Following the example of the Circle class in Section 8.2, design a class named Rectangle to...

    Following the example of the Circle class in Section 8.2, design a class named Rectangle to represent a rectangle. The class contains: ■ Two double data fields named width and height that specify the width and height of the rectangle. The default values are 1 for both width and height. ■ A no-arg constructor that creates a default rectangle. ■ A constructor that creates a rectangle with the specified width and height. ■ A method named getArea() that returns the...

  • JAVA design a class named Rectangle to represent a rectangle. The class contains: A method named getPerimeter() that returns the perimeter. Two double data fields named width and height that specify...

    JAVA design a class named Rectangle to represent a rectangle. The class contains: A method named getPerimeter() that returns the perimeter. Two double data fields named width and height that specify the width and height of the rectangle. The default values are 1 for both width and height. A no-arg constructor that creates a default rectangle. A constructor that creates a rectangle with the specified width and height. A method named getArea() that returns the area of this rectangle. design...

  • Within NetBeans, write a Java program for EACH of the following problems. (The Rectangle class) Following...

    Within NetBeans, write a Java program for EACH of the following problems. (The Rectangle class) Following the example of the Circle class in Section 9.2, design a class named Rectangle to represent a rectangle. The class contains: · Two double data fields named width and height that specify the width and height of the rectangle. The default values are 1 for both width and height. · A no-arg constructor that creates a default rectangle. · A constructor that creates a...

  • Written in python using puTTy!! i'm having a lot of trouble with this, will upvote! also...

    Written in python using puTTy!! i'm having a lot of trouble with this, will upvote! also here is the address.csv file Name,Phone,Email,Year_of_Birth Elizi Moe,5208534566,[email protected],1978 Ma Ta,4345667345,[email protected],1988 Diana Cheng,5203456789,[email protected],1970 ACTIVITY I Implement in Python the Subscriber class modeled by the UML diagram provided below. Save in a file called MyClasses.py Subscriber name: string yearOfBirth: int phone: string email: string getName() getAge() getPhone() getEmail() Write a test program that does the following: Read the file addresses.csv. For each record, create an object...

  • Code a rectangle class extending the geometric obi.

    rectangle class contains tge following; -three data fields name recname,  height and width with default values"Ricky" for recName, 5 for the height and 15 for the width.-default constructor with no arguments -constructor that accept width  and height to create  a rectangle  -the accessor of all data feilds-methods that returns the area of the rectangle called getArea()-methods that returns the perimeter of thr rectangle called getPerimeter()-methods that returns the spring description of the rectangle called toString()    Note: the toString() method is...

  • Design and implement class Circle to represent a circle object. The class defines the following attributes...

    Design and implement class Circle to represent a circle object. The class defines the following attributes (variables) and methods: 1.      A private variable of type double named radius to represent the radius. Set to 1. 2.      Constructor Methods: •        Python : An overloaded constructor method to create a default circle. •        C# & Java: Default Constructor with no arguments. •        C# & Java: Constructor method that creates a circle with user-specified radius. 3.      Method getRadius() that returns the radius. 4.     ...

  • using c++ E2b:Design a class named Rectangle to represent a rectangle . The class contains: (1)...

    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()...

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