Starting from:

$25

CSE1321-Lab 10 Classes Solved

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

 

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

2.        A non-argument constructor method to create a default rectangle. (Added clarification)

3.        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 width.

4.        Method getArea() that returns the area.

5.        Method getPerimeter() that returns the perimeter.

6.        Method getHeight() that returns the height.

7.        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 company’s stock. The class defines the following attributes (variables) and methods:

 

1.        A class (Changed from private) variable of type String named Symbol for the stock’s symbol.

2.        A class (Changed from private) variable of type String named Name for the stock’s name.

3.                       A class (Changed from private) variable of type double named previousClosingPrice to store the last closing price.

4.        A class (Changed from private) variable of type double named currentPrice to store the current price.

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

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

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

8.        Method setClosingPrice() that sets the previous closing price.

9.        Method setCurrentPrice() that sets the current price.

10.   Method getChangePercent() that returns the percentage changed from

previousClosingPrice to currentPrice. Using the formula: 

-(previousClosingPrice - currentPrice) / previousClosingPrice * 100 (Added a formula here)

 

11.                  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 statement.

 

The statement PRINT yahooStock would 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 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

More products