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

OBJECTIVE To demonstrate using input and output files To demonstrate proper repo

ID: 3785798 • Letter: O

Question

OBJECTIVE

To demonstrate using input and output files

To demonstrate proper report formatting and spacing

To demonstrate summary totals

SPECIFICATIONS

Start with the LAB02 and copy in java statements from Lab01

Keep the input of the user name from the keyboard and the output message to the monitor

Make the following changes:

INPUT

Instead of accepting stock input from the keyboard, read it from an input file

Input file is

Lab02Input.txt (the following number is lab02input.txt)

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 NIK

Input record consists of the following fields:

Stock price (double)

Number of shares (double)

Annual dividend (double)

Stock ticker (String)

This file must be copied to the folder where you saved your Java program

PROCESS

You must add a loop because your program will read through and process the entire data file

Every time you go through the loop, you will read in a new record (set of data)

(Keep the processing for stock value and dividend yield)

Accumulate total stock value (goes in the loop)

OUTPUT

Output file to be created: Lab02Report.txt

Write a report heading (happens once, goes before the loop)

Write a column heading (happens once, goes before the loop)

Write a detail line for each stock (use printf) (happens once for each record, goes in the loop)

Write a summary line with the total stock value (use printf) (happens once, after the loop)

End report with a message and your name

output report (include blank lines)

in case if you need to look what lab01 look like the following below is Lab01

Start with the Java program Lab01

Add comments for: your name, date, purpose of the program

Write a program that calculates stock value and dividend yield:

Input the following five variables – use an input prompt and align the input:

stock name; stock ticker; stock price; shares owned; annual dividend

Create a method that calculates stock value (price multiplied by # of shares)

Round to two decimal places

Call this method to get the stock value

Create a method that calculates the dividend yield (annual dividend divided by stock price)

Dividend yield is stated as a percentage: round to two decimal places

Call this method to get the dividend yield

Create a method that prints formatted output: a message, a value and %, if needed

Call this method to display the stock value on the monitor

Call this method to display the dividend yield on the monitor

     

      At the beginning of the program, input your name from the keyboard.

      At the end of the program, display a completed message and your name on the monitor.

-------------------------------------

SO I NEED HELP DOING LAB02 BUT TO DO LAB02 YOU WILL NEED TO REFER BACK TO LAB01 BECAUSE IN LAB02 YOU NEED TO GET FILE AND INPUT OF LAB01

IMPORTANT IT SHOULD BE JAVA PROGRAM AND ALSO USE PRINTF FOR FORMATTING METHOD.

Explanation / Answer

public class Lab02

{

   public static void main(String [] args) throws FileNotFoundException

   {

      DecimalFormat two = new DecimalFormat("00.00");

      Scanner keyboard = new Scanner(System.in);

      Scanner inputFile = new Scanner(new FileReader("Lab02Input.txt"));

      double sPrice = inputFile.nextDouble();

      double sOwned = inputFile.nextDouble();

      double aDiv   = inputFile.nextDouble();

      double mValue;

      double mYield;

      mValue = getValue(sPrice, sOwned);

      mYield = getYield(sPrice, aDiv);

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

      reportFile.println("Stock Value and Yield Report");

      reportFile.println("");

      reportFile.print("Stock   ");

      reportFile.print("Price   ");

      reportFile.print("Shares   ");

      reportFile.print("Value   ");

      reportFile.print("Dividend   ");

      reportFile.println("Yield");

      while(inputFile.hasNext())

      {

         if (inputFile.hasNext())

         {

            reportFile.print("        ");

            reportFile.print(sPrice+"    ");

            reportFile.print(sOwned+"   ");

            getValue(sPrice, sOwned);

            reportFile.print(two.format(mValue)+"   ");

            reportFile.print(aDiv+"      ");

           getYield(sPrice, aDiv);

            reportFile.println(two.format(mYield)+"   ");

            inputFile.close();

            reportFile.close();

         }

/*else

        {

            inputFile.close();

            reportFile.close();

         }*/

   }

     //inputFile.close();

      //reportFile.close();

}

public static double getValue(double sPrice, double sOwned)

      {

         //DecimalFormat two = new DecimalFormat("00.00"); //Decimal format

         double mValue;

        mValue = (sPrice * sOwned);

            //System.out.printf("Stock Value is " + mValue);

        return mValue;

      }

      public static double getYield(double sPrice, double aDiv)

      {

         //DecimalFormat two = new DecimalFormat("00.00"); //Decimal format

         double mYield;

         mYield = (aDiv * sPrice);

            //System.out.printf(" Dividend Yield is " + two.format(mYield));

         return mYield;

      }

try {

    BufferedReader br = new BufferedReader(new FileReader(filePath));

    String line;

    while ((line = br.readLine()) != null) {

        // do somthing

    }

    br.close();

}

catch (Exception e)

{

    System.out.print("Problem!");

}

}