Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

21.7 HW 7 - File IO and Interfaces Homework 7: File IO and Interfaces Due on zyBooks Monday, November 12 2018 1 Objective For this

21.7 HW 7 - File IO and Interfaces

Homework 7: File IO and Interfaces Due on zyBooks Monday, November 12 2018

1 Objective

For this assignment, students will practice interfaces, File IO, polymorphism, and inheritance.

2 Problem

Write a Java program that performs operations on stock data. Instead of gathering live stock prices, we will mock our data using File IO. In order to do so, you will be given a file with stock ticker symbols and their corresponding prices. A stock ticker symbol uniquely describes a companys stock price (e.g. Apple has ticker symbol AAPL).

When a company issues shares to the public marketplace, it selects an available ticker symbol for its securities that investors use to place trade orders. A "tick" is any change in the price. This change can be either an increase or decrease in the price. A stock ticker automatically displays these ticks, along with other relevant information, like volume, that investors use to stay informed about current market conditions. We will ignore all other information for this assignment and assume that what we read in our program is the current price.

The example below displays the contents of a file, Service1.txt:

AAPL 1.25 GOOG 122.4 MSFT 22.3 ORCL 4.32 AMZN 123.4 INTC 4.53 XXYZ -2.50 

Here, we can see the price for Apple (AAPL) is 1.25, the price for Google (GOOG) is 122.4, Microsoft (MSFT) is 22.3, and so on. As can be seen, some of these prices may be negative. Handling such cases is outlined in section 3.

To retrieve these prices from the market, we rely on several stock services. These stock services store prices and ticker symbols from the input file and return price for a given ticker symbol. You will have three stock services and each will return a price for a ticker. In other words, for each service, there will be a corresponding input file. We will use an interface, StockPriceService, to create a blueprint for our concrete classes: UHStockService, ExternalService, and NLPService.

Moreover, you will compare these services. You will perform operations on these services in a class called StockMachine. The constructor for StockMachine takes a name of a file as a parameter. Then, it reads a certain subset of ticker symbols from a given file and stores them in a private field.

The input file will be in the following format: numOfTickerSymbols - number of ticker symbols in the file TickerSymbol1 - ticker symbol TickerSymbol2- ticker symbol TickerSymbol3- ticker symbol

Example:

4 AAPL AMZN INTC XXYZ 

These ticker symbols are just a subset of all ticker symbols on which you will be performing the following operations:

Find the service that has highest prices on average

Find the service that has the lowest prices on average

Find the best price for a ticker across all three services

All in all, there will be four input files:

The first three contain a list of stock ticker symbols and their corresponding prices

UHStockService.txt

NLPService.txt

ExternalService.txt

The final input file contains the ticker symbols we decide to work with, and the name of this file is passed as a parameter to the constructor. Please see section 3 for more details.

3 Assignment Outline

NOTE: Using maps is encouraged, but you may also use two arrays as an alternative in this assignment. Instead of having

Map, you may use the following:

String [] tickerSymbols double [] tickerPrices 

3.1 Interface: StockPriceService

This interface has the following properties:

Double priceForTicker(String ticker);

String nameOfService();

3.2 Class: NLPService implements StockPriceService

private Map tickerPrices; // You can assume that there will be fixed 25 ticker symbols in total if you plan to implement this with arrays.

NLPService()

The constructor reads the prices and tickers from a file, NLPService.txt, and stores them into a HashMap tickerPrices

public Double priceForTicker(String ticker)

This class overrides this method from the interface. The method takes a ticker and returns the price for a given ticker

If the price is not found, return 0

public String nameOfService() { return "NLP"; }

3.3 Class: UHStockService implements StockPriceService

private Map tickerPrices; // You can assume that there will be a fixed 25 ticker symbols in total if you plan to implement this with arrays.

UHStockService()

The constructor reads the prices and tickers from a file, UHStockService.txt, and stores them into a HashMap tickerPrices.

public double priceForTicker(String Ticker)

This class overrides this method from the interface. The method takes a ticker and returns the price for a given ticker

public String nameOfService() { return "UH"; }

3.4 Class: ExternalService implements StockPriceService

private Map tickerPrices; // You can assume that there will be a fixed 25 ticker symbols in total if you plan to implement this with arrays.

ExternalService()

The constructor reads the prices and tickers from a file, ExternalService.txt, and stores them into a HashMap tickerPrices.

public double priceForTicker(String Ticker)

This class overrides this method from the interface. The method takes a ticker and returns the price for a given ticker.

public String nameOfService() { return "External"; }

3.5 Class: StockMachine *This class will have the following implementation: *

private StockPriceService[] stockPriceServices;

private String[] tickerSymbols;

private final int NUMBEROFSERVICES = 3;0

public StockMachine(String fileName)

stockPriceServices = new StockPriceService[3];

stockPriceServices[0] = new UHStockService();

stockPriceServices[1] = new NLPService();

stockPriceServices[2] = new ExternalService();

The constructor stores each ticker symbol from the input file passed in the parameter in the tickerSymbols array.

public String getServiceWithLowestAvg()

Returns name of service with lowest non-negative average for the tickers in the tickers array

public String getServiceWithHighestAvg()

Returns name of service with the highest average for the tickers in the tickers array

public Double average(StockPriceService service)

Returns average of the tickers for a given StockPriceService

Note: this method should ignore all negative prices for ticker symbols in the calculation of the average. The length of tickerSymbols[] should be used as the divisor.

public String getBestPriceFor(String ticker)

Get the lowest GREATER THAN ZERO price for the ticker among the three services. Assume there will be no ties.

4 Grading

A submitted program that does not compile is worth 0 points. A submitted program that includes hardcoded answers is worth 0 points. Your homework is going to be graded on functionality.

Due on Zybooks on Monday, 12 Nov, 11:59 P.M.

GOOD LUCK & HAPPY CODING!

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions