Exercise #1: Design and implement class Rectangle to represent a rectangle object. The class defines the following attributes (variables) and methods:
- Two class (changed from private) variables of type double named height and width to represent the height and width of the rectangle. Set their default values to 1 in the default constructor. (Added clarification)
- A non-argument constructor method to create a default (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 and
- 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
Exercise #2: Design and implement class Stock to represent a companys stock. The class defines the following attributes (variables) and methods:
- A class (Changed from private) variable of type String named Symbol for the stocks
- A class (Changed from private) variable of type String named Name for the stocks
- A class (Changed from private) variable of type double named previousClosingPrice to store the last closing price.
- A class (Changed from private) variable of type double named currentPrice to store the current
- A constructor method to create a stock with user-specified name and
- Method getName() that returns the stocks name.
- Method getSymbol() that returns the stocks 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. 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 passing the object name to the print
The statement PRINT yahooStock would print the string:
Yahoo stocks 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 prices according 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
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
Reviews
There are no reviews yet.