CSCD 211 Lab 2 Chapter 9- OOP, Classes, and Objects

$35.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 - (2 votes)

(2) Two programming challenges each worth 10 points

9.1 (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 doubledata fields named widthand 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 widthand height.
  • A method named getArea()that returns the area of this rectangle.
  • A method named getPerimeter()that returns the perimeter.

Write a test program that creates two Rectangle objects—one with width 4 and height 40, and the other with width 3.5 and height 35.9. Display the width, height, area, and perimeter of each rectangle in this order.

 

 

 

 

 

 

 

 

  1. 2 (THE STOCKCLASS) Following the example of the Circle class in Section 9.2, design a class named Stock that contains:
    • A string data field named symbolfor the stock’s symbol.
    • A string data field named namefor the stock’s name.
    • doubledata field named previousClosingPricethat stores the stock price for the previous day.
    • doubledata field named currentPricethat stores the stock price for the current time.
    • A constructor that creates a stock with the specified symbol and name.
    • A method named getChangePercent()that returns the percentage changed from previousClosingPriceto currentPrice.

Write a test program that creates a Stock object with the stock symbol ORCL, the name Oracle Corporation, and the previous closing price of 34.5. Set a new Set a new currentprice to 34.35 and display the price-change percentage.