CS 1321L: Programming and Problem Solving I Lab Lab 10 Classes

$30.00

Category: You will Instantly receive a download link for .zip solution file upon Payment || To Order Original Work Click Custom Order?

Description

5/5 - (4 votes)

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

 

  1. Twoclass (changed from private)variablesoftypedouble namedheightandwidthtorepresenttheheightand width of the rectangle. Set their default values to1in the default constructor. (Added clarification)
  2. A non-argument constructor method to create a (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 andwidth.
  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:     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:

 

  1. Aclass (Changed from private)variableof type Stringnamed Symbolfor the stock’ssymbol.
  2. Aclass (Changed from private)variableoftypeStringnamed Namefor the stock’sname.
  3. Aclass (Changed from private)variableoftype doublenamedpreviousClosingPricetostorethelastclosing price.
  4. Aclass (Changed from private)variableoftypedoublenamedcurrentPricetostorethe currentprice.
  5. A constructor method to create a stock with user-specified name andsymbol.
  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.
  • Method getChangePercent()that returns the percentage changed from

previousClosingPriceto currentPrice. Using the formula:

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

 

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

 

  1. Programs must be workingcorrectly.
  2. Programs must be completed and checked before workingassignment.
  3. Programs must be checked by the end of the designated labsession.