Description
Exercise #1:Design and implement class Rectangleto represent a rectangle object. The class defines the following attributes (variables) and methods:
- Twoclass (changed from private)variablesoftypedouble namedheightandwidthtorepresenttheheightand width of the rectangle. Set their default values to1in the default constructor. (Added clarification)
- A non-argument constructor method to create a (Added clarification)
- Another constructor method(Python only: using classmethod decorator) (Changed since Python does not support method overloading) to create a rectangle with user-specified height andwidth.
- 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: 1unit
Width: 1unit
Area: 1unit
Perimeter: 4units
Second object:
Height: 5unit
Width: 6unit
Area: 30units
Perimeter: 22units
Exercise #2:Design and implement class Stockto represent a company’s stock. The class defines the following attributes (variables) and methods:
- Aclass (Changed from private)variableof type Stringnamed Symbolfor the stock’ssymbol.
- Aclass (Changed from private)variableoftypeStringnamed Namefor the stock’sname.
- Aclass (Changed from private)variableoftype doublenamedpreviousClosingPricetostorethelastclosing price.
- Aclass (Changed from private)variableoftypedoublenamedcurrentPricetostorethe currentprice.
- A constructor method to create a stock with user-specified name andsymbol.
- 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
previousClosingPriceto currentPrice. Using the formula:
-(previousClosingPrice – currentPrice) / previousClosingPrice * 100 (Added a formula here)
- Method either named toString(), in Java and C#, or __str__() in Python (Changed since Python calls this method a different name) to printout a meaningful description of a stock object when passingthe object name to the printstatement.
The statement PRINT yahooStockwould print the string:
Yahoo stock’s closing price is $234.54 (Changed to be grammatically correct and to use pseudo code instead of Java for no reason, also removed the Java code after this)
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 pricesaccording to the information below. Next, test the class methods on each object to print the information in a similar manner to the one shown below. (Added clarification)
Sample run:
Google stock:
Symbol: GOG
Closingprice: 134.67
Currentprice: 131.98
Changepercent: – 2%
Google stock closing price is $131.98
Microsoft stock:
Symbol: MSF
Closingprice: 156.52
Currentprice: 161.22
Changepercent: 3%
Microsoft stock closing price is $161.22
Instructions:
- Programs must be workingcorrectly.
- Programs must be completed and checked before workingassignment.
- Programs must be checked by the end of the designated labsession.