In: Computer Science
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.
// 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