Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java OBJECTIVE To demonstrate creating a class for stock data To demonstrate cre

ID: 3813052 • Letter: J

Question

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 object’s 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

STOCK 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

//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

Explanation / Answer

The question itself explains the answer.

Please find below the complete answer in bold:

public class Stock

{//start of class

// class fields

private String ticker;                //abbreviated id for stock name

private double stockPrice;       // price per share

private double numberShares; // shares owned

private double annualDividend; // annual dividend

//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);

   setNumberShares(mNumberShares);

setAnnualDividend(mDividend);

}//end method

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

public Stock ( )

{//begin method

            setTicker (null);

            setStockPrice (0.0);

setNumberShares(0.0);

setAnnualDividend(0.0);

}//end method

//class methods to store values in class fields – “set” methods

public void setTicker (String mTicker)

{//begin method

            ticker = mTicker;

}//end method

public void setStockPrice(double mStockPrice)

{//begin method

            stockPrice=mStockPrice;

}//end method

public void setNumberShares (double mNumberShares)

{//begin method

           numberShares=mNumberShares;

}//end method

public void setAnnualDividend (double mAnnualDividend)

{//begin method

annualDividend=mAnnualDividend;

}//end method

//class methods to retrieve values from class fields – “get” methods

public String getTicker ( )

{//begin method

            return ticker;

}//end method

public void getStockPrice(double mStockPrice)

{//begin method

return stockPrice;

}//end method

public void getNumberShares (double mNumberShares)

{//begin method

return numberShares;

}//end method

public void getAnnualDividend (double mAnnualDividend)

{//begin method

return annualDividend;

}//end method

//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 for calculting Annual dividend

public static double calcAnnualDividend()

{//begin method

double annualDiv;
annualDiv= annualDividend / stockPrice;
annualDiv= Math.round(annualDiv* 100)/100.0;
return annualDiv;
}
//end method

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote