Problem Background
Many times when programming, you will come across situations where youre trying to deal with data that doesnt come right away. Tasks like accessing a webpage over the internet or reading through a file system need some processing time in the background before they can be done. For tasks like these, we sometimes employ event-driven programming in order to make our programs fast and reactive when the data comes back.
In this homework, you will be creating an application that keeps track of changes in a stock market database and reports information out when relevant. In our sample stock market, data might be coming in and changing in real time. When values of stocks change, we want to build a system that can reevaluate the new values of these stocks and print out updated analysis.
Solution Description
For this assignment, create the following file: StockTracker.java.
You have also been provided with Stock.java, Market.java, and Tester.java. We have a few analysis questions that you must respond to as a comment!
Provided classes/interfaces
Stock.java:
An enum that holds instances of the Stocks that we might track. You should look at the JavaDoc comments for getCompanyName and getInitialValue inside this file.
Market.java:
An interface that specifies a contract for a stock markets with stocks that change values. You should look at the JavaDocs for registerHandler inside this file. You will NOT be implementing Market yourself but you will be using instances of classes that implement Market. These instances will be created for you and passed into the methods that you create inside of StockTracker.java.
Tester.java:
This class is provided for you so that you can test your code. It contains a main method which creates an instance of Market and calls the methods inside of StockTracker. It is setup to simulate asynchronously accessing information from the actual stock market. It may be useful to read through the main method but dont worry about the implementation details.
StockTracker Class:
Instance Variables:
- List<Stock> trackedStocks A List of the stocks that this tracker is tracking
- int cutoff The threshold for buying or selling a stock
- Market market The Market that this StockTracker is tracking
All fields should be private and non-static.
Constructor:
- StockTracker(Market market, Stock[] stocks, int cutoff) Takes in a an array of Stocks and adds them to trackedStocks. Afterwards, assign the instance variables market and cutoff to their respective fields. When this is done, register a buy, hold, and sell tracker on every stock passed in using the private helper methods in the class.
Constructor should be public.
Methods:
- getter for trackedStocks. Returns a deep copy of trackedStocks
- getters for market and cutoff
- private void registerHoldTracker(Stock stock) Should make sure that a message is printed any time a change in the stocks value causes it to be valued both >= the stocks initalValue and <= the stocks initialValue + cutoff. When this happens print out {Company Name} is valued at ${value}. Hold on to what you have..
- You should achieve this by registering an event handler on the market for the given stock.
- This method must use only an instance of a named inner class when creating the handler
- private void registerBuyTracker(Stock stock) Should make sure that a message is printed any time a change in the stocks value causes it to be valued < the stocks initialValue cutoff. When this happens print out {Company Name} just dropped to ${value}. Buy now!.
- You should achieve this by registering an event handler on the market for the given stock.
- This method must use only an anonymous inner class when creating the handler
- private void registerSellTracker(Stock stock) Should make sure that a message is printed any time a change in the stocks value causes it to be valued > the stocks initialValue + cutoff.
When this happens print out {Company Name} just rose to ${value}. Sell now!.
- You should achieve this by registering an event handler on the market for the given stock.
- This method must use only a lambda expression when creating the handler
Getters should follow best practices taught in class
Analysis Questions
Answer these at the top of StockTracker.java in a comment. Put your collab statement first and then a multiline comment with your answers.
- What are the pros and cons of using named inner classes, anonymous inner classes, and lambda expressions? (4-7 bullet points. < 200 words.)
- Why syntactically can we use a lambda expression when we are registering an event handler for the market? (1-3 sentences. < 150 words.)
- Why would we decide to use event driven programming to access the stock market? (2-5 sentences. < 220 words)
Testing your code:
If you want to test out your own code before submitting we recommend creating a separate java file and implementing a main method there.
We have also provided sample tests in Tester.java These tests are not comprehensive!
This should be the output of Tester.java:
Apple is valued at $402. Hold on to what you have.
Pfizer is valued at $609. Hold on to what you have.
Cisco is valued at $411. Hold on to what you have.
Apple is valued at $403. Hold on to what you have.
AT&T just dropped to $117. Buy now!
Pfizer just rose to $614. Sell now!
Facebook is valued at $709. Hold on to what you have.
Intel is valued at $258. Hold on to what you have.
Pfizer just rose to $622. Sell now!
Facebook just rose to $715. Sell now!
Apple is valued at $407. Hold on to what you have. General Electric just dropped to $488. Buy now!
Cisco is valued at $418. Hold on to what you have.
AT&T just dropped to $121. Buy now!
Facebook just rose to $719. Sell now!
Apple just rose to $412. Sell now!
Microsoft is valued at $354. Hold on to what you have.
Intel is valued at $254. Hold on to what you have.
Cisco is valued at $420. Hold on to what you have.
Facebook just rose to $720. Sell now!
Apple just rose to $411. Sell now!
General Electric just dropped to $482. Buy now!
Intel is valued at $258. Hold on to what you have.
Intel is valued at $254. Hold on to what you have.
Ford is valued at $303. Hold on to what you have.
Apple just rose to $420. Sell now!
Cisco is valued at $411. Hold on to what you have.
AT&T just dropped to $114. Buy now!
Ford is valued at $305. Hold on to what you have.
Facebook just rose to $728. Sell now!
Bank of America is valued at $300. Hold on to what you have.
General Electric just dropped to $477. Buy now!
Intel is valued at $255. Hold on to what you have.
Pfizer just rose to $630. Sell now!
Facebook just rose to $724. Sell now!
Note that Tester will not finishing running immediately because it is (fake) accessing data from the Stock Market.
Feel free to modify or create your own tests in the main method!
Allowed Imports
- You are allowed to import the following classes:
- util.Arrays
- util.ArrayList
- util.List
- util.function.Consumer
Rubric
- [100] StockTracker
- [3] Instance variables
- [6] Getters
- [10] Constructor
[1] constructor assigns instance variables correctly
[9] constructor registers all the trackers
- [20] registerBuyTracker
[10] Correctly registers the handler
[10] Uses an anonymous inner class
- [20] registerSellTracker
[10] Correctly registers the handler
[10] Uses a lambda expression
- [20] registerHoldTracker
[10] Correctly registers the handler
[10] Uses an instance of a named inner class [21] Analysis Questions
Javadocs
For this assignment, you will be commenting your code with Javadocs. Javadocs are a clean and useful way to document your codes functionality. For more information on what Javadocs are and why they are awesome, the online overview for them is extremely detailed and helpful.
You can generate the javadocs for your code using the command below, which will put all the files into a folder called javadoc:
$ javadoc *.java -d javadoc
The relevant tags that you need to include are @author, @version, @param, and @return. Here is an example of a properly Javadocd class:
import java.util.Scanner;/*** This class represents a Dog object.* @author George P. Burdell* @version 1.0*/ public class Dog {/*** Creates an awesome dog (NOT a dawg!) */public Dog() { }/*** This method takes in two ints and returns their sum* @param a first number* @param b second number * @return sum of a and b*/ |
public int add(int a, int b) {}} |
A more thorough tutorial for Javadocs can be found here. Take note of a few things:
- Javadocs are begun with /** and ended with */.
- Every class you write must be Javadocd and the @author and @verion tag included. The comments for a class should start with a brief description of the role of the class in your program.
- Every non-private method you write must be Javadocd and the @param tag included for every method parameter. The format for an @param tag is @param <name of parameter as written in method header> <description of parameter>. If the method has a non-void return type, include the @return tag which should have a simple description of what the method returns, semantically.
Reviews
There are no reviews yet.