Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Done in Java with Objects Exercise - Stock Market Tracking Object O O 1. Create the stock object class to store the data for a

Done in Java with Objects

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

Exercise - Stock Market Tracking Object O O 1. Create the stock object class to store the data for a single stock. The data shown gives 4 instance variables to begin with - decide which data types to use for each. Use the following names for the instance variables: o symbol o stockName startingPrice previous Change 2. Write a default constructor and a 4-arg constructor for creating Stock objects. Write get/set methods for each instance variable. Write a new toString method to help with testing. Test your object class by making a Tester class and using data from the table or pdf (which has more stocks to choose from). 3. Add two new instance variables to the class: current Price, current Change where both are set to match the values of the startingPrice and previous change when an object is constructed (therefore a new 6-arg constructor is not needed). Add these to the toString method so they can be tested. Exercise - Stock Market Tracking Object 4. Write the updateStock method which takes no arguments and does not return a value. When called, the method simulates the passage of time during the trading day, where the currentPrice will be updated up/down by the current Change amount using the following formula/algorithm: x= currentChange * 15000 (truncated to make it an int; this is done so that there is a random chance that the stock will increase/decrease) r=random int 0..x OR x..0 (when x is negative if current Change is negative) o current Change = (r - x/3) / 10000 (to make it back into a double with 4-decimal-places) currentPrice = currentPrice + current Change 5. Test your new method by creating a number of Stock objects and printing out O O Exercise - Stock Market Tracking Object O O O O For example, if the stock data for Apple is used from the table (shown in italics below), and Math.random() returns 0.67890123 when the random value is calculated, then the calculations show that Apple stocks continued to increase in value by over 40 cents: x = +0.88 * 15000 = 13200 r = random value [O.. 13200) = 8961 current Change = (8961 - 13200/3)/ 10000 = +0.4561 current Price = 173.07 +0.4561 = 173.52 For example, if the stock data for Zynga is used from the table (shown in italics below), and Math.random() returns 0.1234789 when the random value is calculated, then the calculations show that Zynga stocks have turned their losses around and is now increasing slightly by 3 cents: 0 x = -0.10 * 15000 = -1500 r = random value [-1500.. 0] = -185 current Change = (-185 - (-1500/3)) / 10000 = +0.03147816 current Price = 8.96 + 0.03147816 = 8.99 O o O Symbol Name Price (Intraday) Change % Change Volume Avg Vol (3 month) Market Cap PE Ratio (TTM) 52 Week Range F 25.19 +0.17 +0.68% 144.998M 105.603M 100.665B 35.33 9.97 25.87 . 50.08 BAC 47.91 -0.85 -1.74% 72.953M 46.094M 392.099B 14.34 29.57 AAPL 173.07 +0.88 +0.51% 80.325M 91.635M 2.8277 30.85 116.21 182.94 AMD 136.88 +4.14 +3.12% 70.088M 59.655M 165.298B 42.22 72.50 164.46 AMC 20.57 -0.09 -0.44% 56.213M 42.724M 10.559B N/A 2.57 72.62 . T 27.18 +0.38 +1.42% 54.847M 54.008M 194.092B 210.70 22.02 33.88 WFC 58.06 +2.06 +3.68% 53.944M 25.723M 231.499B 11.73 29.68 58.87 LVS 42.99 +5.33 +14.15% 49.781M 8.781M 32.844B N/A 33.75 66.77 . ZNGA 8.96 -0.10 -1.10% 48.577M 30.661M 10.129B N/A 5.57 12.32 Ford Motor Company Bank of America Corporation Apple Inc. Advanced Micro Devices, Inc. AMC Entertainment Holdings, Inc. AT&T Inc. Wells Fargo & Company Las Vegas Sands Corp. Zynga Inc. Banco Bradesco S.A. American Airlines Group Inc. Ita Unibanco Holding S.A. JPMorgan Chase & Co. Virgin Galactic Holdings, Inc. Petrleo Brasileiro S.A. Petrobras NVIDIA Corporation Microsoft Corporation Citigroup Inc. . BBD 3.7700 +0.0700 +1.89% 47.3M 38.481M 36.53B 8.61 3.25 5.70 . AAL 18.49 -0.85 -4.40% 44.983M 35.657M 11.973B N/A 15.02 26.09 . ITUB 4.2500 +0.0200 +0.47% 42.794M 42.374M 41.565B 7.91 3.55 5.57 JPM 157.89 -10.34 -6.15% 39.475M 11.347M 466.608B 9.99 127.35 172.96 SPCE 10.14 +0.11 +1.10% 39.519M 12.857M 2.616B N/A 9.75 62.80 PBR 12.61 +0.31 +2.52% 39.166M 29.771M 82.244B 3.37 7.06 12.62 NVDA 269.42 +3.67 +1.38% 39.476M 45.853M 671.395B 83.08 115.67 346.47 MSFT 310.20 +5.40 +1.77% 39.005M 27.829M 2.329T 34.70 212.63 349,67 n 66.93 -0.85 -1.25% 35.786M 22.62M 132.807B 6.25 57.40 80.29 NIO NIO Inc. 30.95 +0.47 +1.54% 34.863M 47.167M 49.228B N/A 27.52 64.60 Overview In this assignment you will design, write and test a complete Java object class (which we began in class) along with a driver program to simulate how stocks change in value over time. How the stocks perform over time is randomized rather than using real daily data or live data. The beginning stock data is taken from actual, recent data from January 16th. Assignment Tasks 1. Add to the stock object class that was started in class (make sure that it is completely working before starting the assignment of course) by adding the printTableEntry method. This method takes no arguments and does not return anything. The printTableEntry method simply uses the data held within the instance to simulate the information shown in the table below, to print out the following information for the stock: AAPL Apple Inc. 173.07 +0.88 +0.51% Symbol - code/letters representing the stock (use a field so that everything will line up in columns like a table) Stock Name - full name of the company/stock (use a field so that everything will line up in columns like a table) Current Price - the current price of the stock (not the starting price at the beginning of the trading period) formatted to two decimal places (use a field so that everything will line up in columns like a table) Current Change - the current change in the price of the stock (not the previous change in price from the previous day of trading) formatted to two decimal places including the +/- to show if the stock is currently increasing or decreasing (use a field so that everything will line up in columns like a table) Percent Change - calculate this value from the difference between the Starting Price and the Current Price for the stock, and print the value formatted to two decimal places including the +/- to show if the stock is currently increasing or decreasing, and the %-sign afterwards (use a field so that everything will line up in columns like a table). Note - when your program begins, there is no difference between the starting and current prices so this will initially display as +0.00%. handle the formatting of text (in fields padded with spaces) and real numbers (to two decimal places) you will need to use System.out.printf() along with formatting commands to control the output. Reading - have a look in the Big Java textbook at section 4.3.2 Formatting Output which outlines how to format different kinds of output and use text fields so that your output will line up into columns. Note - the printf command does not start a new line of output (println has to be called separately, which you should do at the end of this method). Test this new method by creating a bunch of stocks, updating all of their prices and then calling printTableEntry for each stock to see how they appear together (the different text and values should all line up into columns). 2. Add another method to the stock object class called closeOfTrading. This method takes no arguments and does not return anything. The closeOfTrading method simply updates the instance variables of the object, which is done at the end of the day when the stock markets close. The method should make the following changes: Starting Price - is reset to the Current Price (the current price of the stock at the end of the day becomes the new starting price for the next day of trading) Previous Change - is reset to the Current Change (ready for the next day of trading) 3. Change the updatestock method in the stock class to make the volatility of the stocks a little more varied by doing the following: In the formula for calculating currentChange, it divides x by 3 currentChange = (r-x/3)/ 10000 Instead of always dividing by 3, change this calculation so that it divides by a random number in the range 1..5 (where 3 is the middle ground) 4. Create a new driver program which simulates tracking the stocks you have chosen to model over a period of time (measured in days). The driver program will display the chosen stocks as they start the simulation, and their values after each day of trading, as follows: Use the data shown in the pdf list of stocks (on Brightspace) to instantiate a list of stocks to use in your simulation. Include Apple (AAPL) and Zynga (ZNGA) plus any other stocks shown in the pdf, to give at least 20 stocks total. Prints out a complete table showing the starting values of all stocks in your list for Day 0 (before any changes have occurred). The starting prices and previous change values shown should match what is shown in the pdf document. Asks the user for the number of days to run the simulation for (where 5 is a full week of trading, and 20-23 would be a full month) Run the simulation for the number of days given, where each day is labelled/printed out, and the following methods are performed on each stock: o updateStock printTableEntry closeOfTrading o

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

Recommended Textbook for

Rules In Database Systems Third International Workshop Rids 97 Sk Vde Sweden June 26 28 1997 Proceedings Lncs 1312

Authors: Andreas Geppert ,Mikael Berndtsson

1997th Edition

3540635165, 978-3540635161

More Books

Students also viewed these Databases questions