Question

In: Computer Science

Description: You will build a portfolio containing the stock prices of 20 companies. The portfolio will...

Description:

You will build a portfolio containing the stock prices of 20 companies. The portfolio will be built by flipping a coin and adding a Technology company or a Manufacturing company to the portfolio based on the outcome of the coin flip. Your program will use four classes to demonstrate inheritance: Company, Technology, Manufacturer and Portfolio (the driver class). The details of these classes are outlined below.

Learning Objectives:

• Inheritance

• Method overriding

• Invoking superclass constructors

You must write at least the following four classes!!

Requirements for the Company Class:

Fields: – stock price (as a double)

Be sure to use the appropriate access modifier.

Methods:

You must use the following constructors and methods:

1. One-argument constructor – This class needs only one constructor. This constructor takes a double as an argument and uses it to set the company’s stock price. Use best practices in software engineering!

2. Getter & setter methods – Provide a getter and setter for each field as needed. Determine if the getter or setter needs to exist, and, if so, whether it should be public, protected, or private. Use the most restrictive access modifier that is appropriate.

3. toString method – This class implements an override of the toString method. (Remember that all classes are implicit subclasses of the class Object, which is where the toString method is defined.)

The toString method DOES NOT PRINT ANYTHING. It simply builds a String which it then RETURNS.

The String built in this method is the stock price formatted to 2 decimal places, with a dollar sign. You can use String concatenation, or the format() method of the String class for this. The format() method builds a String using the same style of syntax as the printf() method.

Requirements for the Technology Class:

The Technology class is a subclass of the Company class.

Fields – none Methods:

1. One-argument constructor – This class needs only one constructor. This constructor takes a double as an argument. It invokes the constructor of its superclass, passing along the argument.

2. toString() method – This class implements an override of the toString method of its superclass.

Again, this toString method DOES NOT PRINT ANYTHING. It simply builds a String which it then RETURNS.The string that it builds includes a portion specific to this class (the name of the class), and a portion that it gets from invoking the toString method of its superclass.

Requirements for the Manufacturer Class:

The Manufacturer class is a subclass of the Company class.

Fields – none Methods:

3. One-argument constructor – This class needs only one constructor. This constructor takes a double as an argument. It invokes the constructor of its superclass, passing along the argument.

4. toString() method – This class implements an override of the toString method of its superclass. Again, this toString method DOES NOT PRINT ANYTHING. It simply builds a String which it then RETURNS. The string that it builds includes a portion specific to this class (the name of the class), and a portion that it gets from invoking the toString method of its superclass.

Requirements for the Portfolio Class:

Fields – none

Methods: A main method is required. You may include other methods if you prefer. Main builds the portfolio of companies and generates a summary report to the screen.

Main Method Details:

Use a counter-controlled loop in main to create 20 companies. Each company will be an instance of either the Technology or the Manufacturing class.

You do not need to use an array to store each object. Just create and print.

For each object to be created:

1. Generate a random integer to simulate flipping a coin.

2. Create the appropriate object (Technology or Manufacturing) depending on the result of the coin flip. Use this formula to generate the stock price to pass to the constructor of the object: current loop index * 0.11 + 1. 3.

3. Print each Company’s details. Remember, the toString method is automatically invoked when you use the object’s name in a situation where a String is needed. Do not explicitly call toString here.

Random number generation class:

Fields – none

Methods:

1. Static method to generate random numbers Since this project requires generating many random integers, it might make sense to create a separate class with a static method for this purpose, similar to the class Math, which contains helpful static methods for performing math calculations. This method should use two integer arguments for the shift and scale factors, and it should return an integer.

Solutions

Expert Solution


// Defines class Random
class Randomm
{
   // Static method to generate random number between shift and scale
   // and returns the random number
   static int getRandomNumber(int shift, int scale)
   {
       return (int)(Math.random() * ((shift - scale) + 1)) + scale;
   }
}// End of class Random

// Defines super class Company
class Company
{
   // Instance variable to store price
   private double stockPrice;
  
   // Parameterized constructor to assign stock price
   public Company(double stockPrice)
   {
       this.stockPrice = stockPrice;
   }
  
   // Getter method to return stock price
   protected double getStockPrice()
   {
       return stockPrice;
   }
  
   // Setter method to change stock price
   protected void setStockPrice(double stockPrice)
   {
       this.stockPrice = stockPrice;
   }
  
   // Overrides toString() method to return stock price
   public String toString()
   {
       // Uses String.format for 2 decimal places
       return " Stock Price $" + String.format("%.2f", stockPrice);
   }
}// End of class Company

// Defines derived class Technology extends super class Company
class Technology extends Company
{
   // Parameterized constructor
   public Technology(double price)
   {
       // Calls super class parameterized constructor
       super(price);
   }
  
   // Overrides toString() method to return technology information
   public String toString()
   {
       // Calls super class toString() method and concatenates
       // return result to class name
       return "\n Technology: " + super.toString();
   }
}// End of class Technology

// Defines derived class Manufacturer extends super class Company
class Manufacturer extends Company
{
   // Parameterized constructor
   public Manufacturer(double price)
   {
       // Calls super class parameterized constructor
       super(price);
   }
  
   // Overrides toString() method to return technology information
   public String toString()
   {
       // Calls super class toString() method and concatenates
       // return result to class name
       return "\n Manufacturer: " + super.toString();
   }
}// End of class Manufacturer

// Driver class definition
public class Portfolio
{
   // main method definition
   public static void main(String []s)
   {
       // Creates an array of object of class Company of size 20
       Company company[] = new Company[20];
      
       // Loops till length of the company array
       for(int index = 0; index < company.length; index++)
       {
           // Calls the method to generate random number between 1 and 0
           int coin = Randomm.getRandomNumber(1, 0);
           // Calculate price
           double price = index * 0.11 + 1.3;
          
           // Checks if coin value is 1
           if(coin == 1)
               // Creates Technology class object using parameterized constructor
               // assigns at index position of company array of object
               company[index] = new Technology(price);
          
           // Otherwise coin value is 0
           else
               // Creates Manufacturer class object using parameterized constructor
               // assigns at index position of company array of object
               company[index] = new Manufacturer(price);
       }
      
       // Loops till length of the company array
       for(int index = 0; index < company.length; index++)
           // Displays current index position object information
           System.out.println(company[index]);
   }// End of main method
}// End of driver class

Sample Output:


Technology: Stock Price $1.30

Technology: Stock Price $1.41

Manufacturer: Stock Price $1.52

Manufacturer: Stock Price $1.63

Manufacturer: Stock Price $1.74

Manufacturer: Stock Price $1.85

Manufacturer: Stock Price $1.96

Manufacturer: Stock Price $2.07

Technology: Stock Price $2.18

Manufacturer: Stock Price $2.29

Manufacturer: Stock Price $2.40

Technology: Stock Price $2.51

Technology: Stock Price $2.62

Technology: Stock Price $2.73

Manufacturer: Stock Price $2.84

Manufacturer: Stock Price $2.95

Technology: Stock Price $3.06

Manufacturer: Stock Price $3.17

Technology: Stock Price $3.28

Manufacturer: Stock Price $3.39


Related Solutions

Suppose you are holding a stock portfolio worth $20 million at current market prices. The stock...
Suppose you are holding a stock portfolio worth $20 million at current market prices. The stock portfolio has a beta of 1.3. You are given the following data: • Today’s date: September 2020 • December NZSE10 futures expiration: Dec. 2020 (in exactly 3 months’ time from today) • Current NZSE10 index: 1,950.00 • December NZSE10 futures price (actual trade price): 1,989.00 • Current 3-month risk-free interest rate: 6 percent per annum (continuously compounded) • Dividend yield on NZSE10 index: 0.00...
Analyzing a Portfolio You have $100,000 to invest in a portfolio containing Stock X and Stock...
Analyzing a Portfolio You have $100,000 to invest in a portfolio containing Stock X and Stock Y. Your goal is to create a portfolio that has an expected return of 12.7 percent. If Stock X has an expected return of 11.4 percent and a beta of 1.25 and Stock Y has an expected return of 8.68 percent and a beta of .85, how much money will you invest in Stock Y? How do you interpret your answer? What is the...
You own a stock portfolio invested 20 percent in Stock Q, 20 percent in Stock R,...
You own a stock portfolio invested 20 percent in Stock Q, 20 percent in Stock R, 10 percent in Stock S, and 50 percent in Stock T. The betas for these four stocks are 1.12, 0.69, 1.16, and 1.69, respectively. What is the portfolio beta?
The average stock prices of 20 technology companies were compared to see whether they improved one...
The average stock prices of 20 technology companies were compared to see whether they improved one year after the 2008 stock market crash (2008 – 2009). The mean of the differences was computed to be –17.8 USD with a standard deviation of 4.81 USD. Calculate and interpret a 95% confidence interval for the average change in stock prices over the two years.
You own a stock portfolio invested 30 percent in stock Q, 20 percent in stock R,...
You own a stock portfolio invested 30 percent in stock Q, 20 percent in stock R, 35 percent in stock S, and 15 percent in stock T. The bestas for these four stocks are .85 1.18, 1.02, and 1.20, respectively. what is the portfolio beta?
You own a stock portfolio invested 25 percent in Stock Q, 20 percent in Stock R,...
You own a stock portfolio invested 25 percent in Stock Q, 20 percent in Stock R, 15 percent in Stock S, and 40 percent in Stock T. The betas for these four stocks are .9, 1.4, 1.1 and 1.8, respectively. What is the portfolio beta? You want to create a portfolio equally as risky as the market (i.e, a portfolio with beta equal to 1), and you have $1,000,000 to invest. Given this information, fill in the rest of the...
Create an application containing an array that stores 20 prices, such as 2.34, 7.89,1.34, and so...
Create an application containing an array that stores 20 prices, such as 2.34, 7.89,1.34, and so on. The application should (1) display the sum of all the prices, (2) display all values less than 5.00, (3) calculate the average of the prices, and (4) display all values that are higher than the calculated average value. Write a program in Java which performs the sort operation. The main method accepts ten numbers in an array and passes that to the method...
The stock prices of energy companies have tumbled (mainly because of a collapse in energy prices)...
The stock prices of energy companies have tumbled (mainly because of a collapse in energy prices) to lows we haven’t seen in years. As a result the dividend yields of these companies have skyrocketed. BP, as of today, is paying greater than a 10 percent dividend to its shareholders. Exxon and some other major oil companies have similar dividend yields. This can be lure to investors because of a very low interest rate environment, a risky stock market (at the...
Your client has $100,000 invested in stock A. She would like to build a two-stock portfolio...
Your client has $100,000 invested in stock A. She would like to build a two-stock portfolio by investing another $100,000 in either stock B or C. She wants a portfolio with an expected return of at least 13.5% and a low a risk as possible, the standard deviation must be no more than 30%. What do you advise her to do (Rationalize your answer), and what will be the expected return and deviation of the recommended portfolio? (Please Show Calculations)....
Your client has $96,000 invested in stock A. She would like to build a​ two-stock portfolio...
Your client has $96,000 invested in stock A. She would like to build a​ two-stock portfolio by investing another $96,000 in either stock B or C. She wants a portfolio with an expected return of at least 14.5% and as low a risk as​ possible, but the standard deviation must be no more than​ 40%. What do you advise her to​ do, and what will be the portfolio expected return and standard​ deviation? Expected Return Standard Deviation Correlation with A...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT