Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java OBJECTIVE To demonstrate creating a class for stock data To demonstrate creating fields and methods within a class To demonstrate creating constructors for a

Java

OBJECTIVE

To demonstrate creating a class for stock data

To demonstrate creating fields and methods within a class

To demonstrate creating constructors for a class

To demonstrate creating an object from the class using set methods

To demonstrate creating an object from the class using a constructor

REQUIRED

A report of your work, in a flat pocket folder, in the following order:

1. Grade form Lab04GradeForm.doc

2. This program description Lab04Description.doc

3. Problem Analysis Lab04ProblemAnalysis.doc

4. The listing of the class description Stock.java

5. Listing of the source program using the class Lab04A.java

6. Run of the program/ output

7. Listing of the source program using the class Lab04B.java

8. Run of the program/ output

SPECIFICATIONS

Write a class for stock data.

Then create an object from this class and use it in two versions of Lab03.

Write a class for stock data.

1. The class will have the following four fields:

Ticker Price of one share Shares owned Annual dividend

2. Create void set methods to store values in the fields, one for each field

3. Create a value-returning get method to retrieve values from the fields, one for each field

4. Create a value-returning method to calculate and return stock value

5. Create a value-returning method to calculate and return dividend yield

6. Create a constructor method to accept the four fields (use the set methods for these fields)

Ticker Price of one share Shares owned Annual dividend

7. Create a noArg constructor (use the set methods for these fields)

8. Save the class as a separate file

Write a program that creates an object from the stock class.

10. Start with Lab03 and rename it as Lab04A

Change input files to Lab04ArrayFile, Lab04InputFile

11 Create an object from the Stock class and use the noArg constructor

Then place values into the fields by using the set methods

12. Use the class method to get stock value and dividend yield and all other data

13. Continue to do the sequential search for stock name

14. Remove the unused methods from Lab03 since you will use the objects methods

Write another program that creates an object from the stock class.

15. Start with Lab04A and rename it as Lab04B

16. Create an object from the Stock class and use the constructor

NOTE: you may need to create the object in a different logical place than in Lab04A

17. The rest of the program should remain the same

Lab03

//Lab03

//Written by: Name //Date written: 1/29/17

//Purpose of program:

//this makes available all extra utilities from Java library including scanner import java.util.*;

//this makes available all extras from Java library needed for files import java.io.*;

public class Lab03 {//start of class public static void main(String [] args) throws FileNotFoundException //needed for files {// start of main method Scanner keyBoard = new Scanner(System.in); // assigns "keyBoard" to keyboard Scanner fileStockIn = new Scanner(new FileReader("fileStockIn.txt")); Scanner Lab03ArrayFile = new Scanner(new FileReader("Lab03ArrayFile.txt"));

PrintWriter reportFile = new PrintWriter("Lab03Report.txt");

//System.out displays on the monitor //parallel arrays for stock ticker and stock name int mSub; int maxSub; //array for stock ticker String [ ] bStockTicker = new String [21]; String [ ] bStockName = new String [21]; mSub = 0; while(Lab03ArrayFile.hasNext()) { bStockTicker[mSub] = Lab03ArrayFile.next().trim(); bStockName[mSub] = Lab03ArrayFile.nextLine().trim(); mSub ++; maxSub = mSub;

} printout (bStockTicker, bStockName);

//Variables and defined constants go here double stockPrice; //price of one share of stock String stockTicker; //nickname of stock String stockName; //name of stock double sharesOwned; //number of shares owned double annualDividend; //the yearly dividend of the stock double stockValue; //stock value double dividendYield; //stocks dividend yield double totalStockValue = 0; //total stock value String searchTicker; final String HEADING1 = "Stock Value and Dividend Yield Report"; //heading 1 final String HEADING2 = " "; //heading 2 final String HEADING3 = "Stock Price Shares Value Dividend yield"; //heading 3

//report and column heading reportFile.println (HEADING1); reportFile.println (HEADING2); reportFile.println (HEADING3);

while(fileStockIn.hasNext()) //while loop to stay in loop while there are more records {//begin while //Input from file stockPrice = fileStockIn.nextDouble (); sharesOwned = fileStockIn.nextDouble (); annualDividend = fileStockIn.nextDouble(); stockTicker = fileStockIn.nextLine ().trim();

//calculate stockValue = getStockValue(stockPrice,sharesOwned); dividendYield = getDividendYield(annualDividend,stockPrice); totalStockValue = totalStockValue + stockValue; stockName = getStockName(bStockTicker,stockTicker,bStockName); //Output from file reportFile.printf ("%-5s",stockTicker); reportFile.printf ("%-20s",stockName); reportFile.printf ("%8.2f",stockPrice); reportFile.printf ("%8.2f",sharesOwned); reportFile.printf ("%8.2f",stockValue); reportFile.printf ("%8.2f",annualDividend); reportFile.printf ("%8.2f%n",dividendYield); }//end while reportFile.println (" "); reportFile.printf ("Total Stock Value: %8.2f",totalStockValue); reportFile.println (" "); reportFile.printf ("Report produced by: Name");

fileStockIn.close( ); reportFile.close( ); }//end of main

//methods go here

//start of method for calculating stock value public static double getStockValue (double mStockPrice,double mSharesOwned) { double mStockValue; mStockValue = mStockPrice * mSharesOwned; mStockValue = Math.round(mStockValue * 100)/100.0;

return mStockValue; //value in mStockValue is returned }

//end of method for calculating stock value

//start of method for calculating dividend yield public static double getDividendYield (double mAnnualDividend,double mStockPrice) { double mDividendYield; mDividendYield = mAnnualDividend / mStockPrice; mDividendYield = Math.round(mDividendYield * 100)/100.0;

return mDividendYield; //value in mDividendYield is returned }

//end of method for calculating dividend yield

//start of method for output of stock value public static void printOut (String mStockValue, double mValue, char mSymbol) { System.out.printf ("%-20s", mStockValue); System.out.print ("="); System.out.printf ("%8.2f", mValue); System.out.printf ("%c%n", mSymbol); System.out.println ( );

}

//end method for output of stock value

//start of method to display list of stock tickers and stock names on monitor

public static void printout (String[] mStockTicker, String[] mStockName) { int bSub; for (bSub = 0; bSub < 21; bSub++) { System.out.printf ("%-20s", mStockTicker [bSub]); System.out.printf ("%-20s", mStockName [bSub]); System.out.println ( ); } }

//end of method to display list of stock tickers and stock names on monitor

//start of sequential search method

public static String getStockName (String[] stockTicker, String searchTicker, String[] stockName) { int bSub = 0; String mStockName = "invalid stock name"; boolean validStockTicker = false; while (bSub < 21 && validStockTicker == false) { if (searchTicker.equals (stockTicker [bSub])) { mStockName = stockName[bSub]; validStockTicker = true; } bSub = bSub + 1; }//end while loop return mStockName; }//end method

//end of method for search

}//end of class

public class Stock

{//start of class

// class fields

private String ticker; //abbreviated id for stock name

private double stockPrice; // price per share

//define other two fields

//constructors

//constructor to create object and set all class fields

public Stock (String mTicker, double mStockPrice, double mNumberShares, double mDividend)

{//begin method

setTicker (mTicker);

setStockPrice (mStockPrice);

//set other two fields

}//end method

//noArg constructor to create object and set all class fields to default values

public Stock ( )

{//begin method

setTicker (null);

setStockPrice (0.0);

//set other two fields

}//end method

Template

//class methods to store values in class fields set methods

public void setTicker (String mTicker)

{//begin method

ticker = mTicker;

}//end method

//three other set methods

//class methods to retrieve values from class fields get methods

public String getTicker ( )

{//begin method

return ticker;

}//end method

//three other get methods

//class calc methods to produce calculated data from class fields

public double calcStockValue ( )

{//begin method

double stockValue; //stock price times number of shares

stockValue = Math.round ( stockPrice * numberShares * 100) / 100.0;

return stockValue;

}//end method

//one other calc method

Input File

42.87 23.33 2.10 EXC 12.00 83.33 0.17 ATVI 28.15 35.00 0.80 MSFT 42.98 23.26 0.65 CVS 33.64 29.72 2.20 TXN 55.51 18.01 2.00 NVS 16.00 62.50 0.40 SPLS 19.81 50.47 0.24 CSCO 30.09 33.23 1.76 T 39.29 25.45 0.60 DIS 18.65 53.00 0.29 SNE 50.21 19.21 0.72 AXP 102.69 9.74 1.44 NIKE

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

More Books

Students also viewed these Databases questions

Question

suggest a range of work sample exercises and design them

Answered: 1 week ago