Question

In: Computer Science

You have been providedwith 2 classes Stock and Portfolio.The Stock classdescribes the template...

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

}


Solutions

Expert Solution

// 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


Related Solutions

You have been providedwith 2 classes TicketPrinter and TicketMachine.The classTicketMachine describes a TicketMachine...
You have been provided with 2 classes TicketPrinter and TicketMachine.The class TicketMachine describes a TicketMachine that has 2 destinations (destination1 and destination2) and 2 corresponding prices (price1 and price2) as attributes. It also has moneyCollections and balance where money collections is the money collected based on ticket purchases and balance is the amount of money the customer has inserted in the ticket machine.The TicketMachine class also has a constructor that is used to make the ticket machine object. Please inspect...
You have been retained by Hayek & Company to recommend a distribution system template for projected...
You have been retained by Hayek & Company to recommend a distribution system template for projected free cash flow of +79.0, -38.8, +188.7, -5.4, and 6.7 over the 2016–2020-time period. What 10 questions do you need to pose to the C-Suiteand the board to make a free cash flow distribution decision that is satisfactory to all involved? Be as specific as needed.
You have been retained by Murphy & Company to recommend a distribution system template for projected...
You have been retained by Murphy & Company to recommend a distribution system template for projected free cash flow of +79.0, -38.8, +188.7, -5.4, and 6.7 over the 2016–2020-time period. What 10 questions do you need to pose to the C-Suite and the board to make a free cash flow distribution decision that is satisfactory to all involved? Be as specific as needed.
*JAVA* For this assignment you have been given two classes, a Main.java and a Coin.java. The...
*JAVA* For this assignment you have been given two classes, a Main.java and a Coin.java. The coin class represents a coin. Any object made from it will have a 1) name, 2) weight and 3) value. As of now, the instance variables in Coin.java are all public, and the main function is calling these variables directly for the one coin made in it. Your goal is to enforce information hiding principles in this project. Take Coin.java, make all instance variables...
2. You have been given this probability distribution of the holding period return for KMP stock:...
2. You have been given this probability distribution of the holding period return for KMP stock: State Probability Return 1 30% 20% 2 60% 12% 3 10% -5% What are the expected return and standard deviation for KMP stock?
Why would firms have two (or more) classes of common stock?
Why would firms have two (or more) classes of common stock?
Think about different groups you have been a part of (classes, family gatherings, office meetings). Discuss...
Think about different groups you have been a part of (classes, family gatherings, office meetings). Discuss with the class how the process and content of each type of group differ. Which concept was more important in which group? Why?
In this question, use your new GUI classes to create a small application. You have 2...
In this question, use your new GUI classes to create a small application. You have 2 options: Write any small application of your choice, as long as it follows the rules that follow. Write the small UnitConversion application, which will be described for you. Option 1: Write your own application You can write any small application that you like, as long as: It uses your GUI classes, including: at least one checkbox, at least one group of 3 radio buttons,...
PDCA template and how to do it for financial. have to make a template of our...
PDCA template and how to do it for financial. have to make a template of our own thing. i was gonna do financial.
Some companies, such as Google, have created classes of stock with little or no voting rights...
Some companies, such as Google, have created classes of stock with little or no voting rights at all. Should this be allowed? Why would investors buy such stock? Explain in detail please.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT