In: Computer Science
You have been provided with 2 classes Stock and Portfolio.
The Stock class describes the template for stocks. Stocks have 3 fields/properties String stockName, int numberOfShares, double priceOfShare The constructor of Stock takes 3 parameters corresponding to the fields/properties of the Stock class. There are a variety of accessor/getter methods and mutator/setter methods for Stock.
The Portfolio class does not have any fields/properties and has a main method. Within the main method of Portfolio, a double variable portfolioValue is declared and set to 0.0. There are also 4 Stock objects made corresponding to 4 companies.
Your Tasks:
a. Create a new Java project
b. Import the 2 provided class files into the project by copying and pasting the code.
c. Work within the Stock class and accomplish the following
c.1 Create a method
public double calculateStockValue() {. }
Work in this method and calculate the value of the Stock (numberOfShares * priceOfShare) and return it.
c.2 Create a method
public void printInfo() { }
Work in this method to print out information of the Stock including stockName, numberOfShares and priceofShare.
Employ the provided output as a template for how the result of this method should look.
Stock Name : DKNY Stock Quantity : 10 Stock Price : 50.0
d. Work within the main method of the Portfolio class after the existing code and accomplish the following
d.1 Declare and initialize 2 new Stock objects for Facebook and LinkedIn respectively with the following parameters
stockName: Facebook numberOfShares:300, priceOfShare: 75
stockName: LinkedIn numberOfShares:500, priceOfShare: 100
d.2 Print out information about the complete portfolio, i.e. the information about all the stocks owned in the following order Amazon, Apple, Facebook, GE, GM and LinkedIn
d.3 Calculate and print out the dollar value of stocks in the entire portfolio i.e sum up the value of each of the stocks owned in the portfolio using the work done in c1.
----Portfolio.java----------
public class Portfolio {
public static void main(String[] args) {
double portfolioValue = 0.0;
Stock appl = new Stock("Apple", 1000, 50.0);
Stock ge = new Stock("General Electric", 1001,
51.0);
Stock gm = new Stock("General Motors", 2001,
25.0);
Stock amzn = new Stock("Amazon", 10000, 500.0);
/*
* CODE HERE
*/
}
}
------- Stock.java------
public class Stock {
String stockName;
int numberOfShares;
double priceOfShare;
public Stock(String name, int shares, double price) {
stockName = name;
numberOfShares = shares;
priceOfShare = price;
}
/**
* @return the stockName
*/
public String getStockName() {
return stockName;
}
/**
* @param stockName
*
the stockName to set
*/
public void setStockName(String pStockName) {
stockName = pStockName;
}
/**
* @return the numberOfShares
*/
public int getNumberOfShares() {
return numberOfShares;
}
/**
* @param numberOfShares
*
the numberOfShares to set
*/
public void setNumberOfShares(int pNumberOfShares) {
numberOfShares = pNumberOfShares;
}
/**
* @return the priceOfShare
*/
public double getPriceOfShare() {
return priceOfShare;
}
/**
* @param priceOfShare
*
the priceOfShare to set
*/
public void setPriceOfShare(double pPriceOfShare) {
priceOfShare = pPriceOfShare;
}
// CODE PAST THIS POINT
}
// Stock.java
public class Stock {
String stockName;
int numberOfShares;
double priceOfShare;
public Stock(String name, int shares, double price)
{
stockName = name;
numberOfShares = shares;
priceOfShare = price;
}
/**
* @return the stockName
*/
public String getStockName() {
return stockName;
}
/**
* @param stockName
* the stockName to set
*/
public void setStockName(String pStockName) {
stockName = pStockName;
}
/**
* @return the numberOfShares
*/
public int getNumberOfShares() {
return numberOfShares;
}
/**
* @param numberOfShares
* the numberOfShares to set
*/
public void setNumberOfShares(int pNumberOfShares)
{
numberOfShares =
pNumberOfShares;
}
/**
* @return the priceOfShare
*/
public double getPriceOfShare() {
return priceOfShare;
}
/**
* @param priceOfShare
* the priceOfShare to set
*/
public void setPriceOfShare(double pPriceOfShare)
{
priceOfShare = pPriceOfShare;
}
// CODE PAST THIS POINT
public double calculateStockValue() {
return numberOfShares *
priceOfShare;
}
public void printInfo() {
System.out.println("Stock Name : "
+ stockName);
System.out.println("Stock Quantity
: " + numberOfShares);
System.out.println("Stock Price : "
+ priceOfShare);
}
}
__________________________
// Portfolio.java
public class Portfolio {
public static void main(String[] args) {
double portfolioValue = 0.0;
Stock appl = new Stock("Apple",
1000, 50.0);
Stock ge = new Stock("General
Electric", 1001, 51.0);
Stock gm = new Stock("General
Motors", 2001, 25.0);
Stock amzn = new Stock("Amazon",
10000, 500.0);
Stock linkedIn=new
Stock("LinkedIn",500,100);
Stock faceBk=new
Stock("Facebook",300,75);
amzn.printInfo();
appl.printInfo();
faceBk.printInfo();
ge.printInfo();
gm.printInfo();
linkedIn.printInfo();
portfolioValue=appl.calculateStockValue()+ge.calculateStockValue()+gm.calculateStockValue()+amzn.calculateStockValue()+
linkedIn.calculateStockValue()+faceBk.calculateStockValue();
System.out.println("Total cost of
all Stocks :$"+portfolioValue);
}
}
______________________________
Output:
Stock Name : Amazon
Stock Quantity : 10000
Stock Price : 500.0
Stock Name : Apple
Stock Quantity : 1000
Stock Price : 50.0
Stock Name : Facebook
Stock Quantity : 300
Stock Price : 75.0
Stock Name : General Electric
Stock Quantity : 1001
Stock Price : 51.0
Stock Name : General Motors
Stock Quantity : 2001
Stock Price : 25.0
Stock Name : LinkedIn
Stock Quantity : 500
Stock Price : 100.0
Total cost of all Stocks :$5223576.0