In: Computer Science
PUT IN JAVA PROGRAMMING
The Stock class: Design a class named Stock that contains:
• A string data field named symbol1 for the stock’s symbol.
• A string data field named name for the stock’s name.
• A double data field named previousClosingPrice that stores the
stock price for the previous day.
• A double data field named currentPrice that 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 previousClosingPrice to 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 current price to 34.35 and display the
price-change percentage.
Below is your code:
Stock.java
public class Stock {
// • A string data field named symbol1 for the stock’s symbol.
private String symbol1;
// • A string data field named name for the stock’s name.
private String name;
// • A double data field named previousClosingPrice that stores the stock
// price for the previous day.
private double previousClosingPrice;
// • A double data field named currentPrice that stores the stock price for
// the current time.
private double currentPrice;
// • A constructor that creates a stock with the specified symbol and name.
public Stock(String symbol1, String name) {
this.symbol1 = symbol1;
this.name = name;
}
// A method named getChangePercent() that returns the percentage changed
// from previousClosingPrice to currentPrice.
public double getChangePercent() {
// calculate change
double change = this.currentPrice - this.previousClosingPrice;
// calculate percent by dividing change with current price and
// multiplying with 100
double percentage = (change / this.currentPrice) * 100;
return percentage;
}
// getter and setters for instance variables
public String getSymbol1() {
return symbol1;
}
public void setSymbol1(String symbol1) {
this.symbol1 = symbol1;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPreviousClosingPrice() {
return previousClosingPrice;
}
public void setPreviousClosingPrice(double previousClosingPrice) {
this.previousClosingPrice = previousClosingPrice;
}
public double getCurrentPrice() {
return currentPrice;
}
public void setCurrentPrice(double currentPrice) {
this.currentPrice = currentPrice;
}
}
StockDriver.java
public class StockDriver {
// main method declaration
public static void main(String[] args) {
// create stock object
Stock stock = new Stock("ORCL", "Oracle Corporation");
// set previous closing price
stock.setPreviousClosingPrice(34.5);
// set current price
stock.setCurrentPrice(34.35);
// get percentage
double percentage = stock.getChangePercent();
// print result
if (percentage < 0) {
System.out.println("Price decreased by " + (-percentage) + "%.");
} else {
System.out.println("Price increased by " + (percentage) + "%.");
}
}
}
Output
Price decreased by 0.4366812227074194%.