Description
(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.
- 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.
- A doubledata field named previousClosingPricethat stores the stock price for the previous day.
- A 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.