Java Start with the LAB02 template Keep the input of the use
Java
Start with the LAB02 template.
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 and is in the CISC122 LAB02 folder
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
SAMPLE output report (include blank lines)
“x”s and “9”s represent spacing for the report
Stock Value and Yield Report
Stock Price Shares Value Dividend Yield
xxxx 99.99 99.999 9999.99 99.99 9.99%
xxxx 99.99 99.999 9999.99 99.99 9.99%
xxxx 99.99 99.999 9999.99 99.99 9.99%
TOTAL 9999.99
Report produced by: your name
//Lab02 Template
//Written by:
//Date written: 2/1/17
//Purpose of program: Calculate stocks, read in from a file, print to a file
//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 Lab02template
{//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(\"Lab02Stocks.txt\"));
PrintWriter reportFile = new PrintWriter(\"Lab02Report.txt\");
//System.out displays on the monitor
//Variables and defined constants go here
Scanner keyBoard = new Scanner(System.in);// not used
String stockName; // stock name
String stockTicker; // stock name
double stockPrice; // price of one share of stock
double sharesOwned; //price of one share of stock
double annualDividend; //dividend
while(fileStockIn.hasNext()) //while loop to stay in loop while there are more records
{//begin while
System.out.println(\"Welcome to the Stock Program.\ Please enter the following information:\");
System.out.print(\"Your name > \");
String name = keyBoard.nextLine(); // take out keyboard put in what called input file
System.out.print(\"Stock name > \");
stockName = keyBoard.next();
System.out.print(\"Stock Ticker > \");
stockTicker = keyBoard.next();
System.out.print(\"Stock price > \");
stockPrice = keyBoard.nextDouble();
System.out.print(\"Stocks Owned > \");
sharesOwned = keyBoard.nextDouble();
System.out.print(\"Annual Dividend> \");
annualDividend = keyBoard.nextDouble();
double stockvalue = getStockvalue(stockPrice,sharesOwned);
double yield = getdividendYield(stockPrice,annualDividend);
}//end while
fileStockIn.close( );
reportFile.close( );
}//end of main
//methods go here
public static double getStockvalue(double stockPrice, double sharesOwned)
{
double stockvalue = stockPrice*sharesOwned;
stockvalue = Math.round (stockvalue * 100.0) / 100.0;
return stockvalue;
}
public static double getdividendYield(double stockPrice, double annualDividend)
{
double yield = (annualDividend/stockPrice)*100;
yield = Math.round (yield * 100.0) / 100.0;
return yield;
}
}//end of class
Solution
Hi, Please find my implementaton.
Please let me know in case of any issue.
//Lab02 Template
//Written by:
//Date written: 2/1/17
//Purpose of program: Calculate stocks, read in from a file, print to a file
//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.*;
import java.text.DecimalFormat;
public class Lab02template
{//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(\"Lab02Stocks.txt\"));
PrintWriter reportFile = new PrintWriter(\"Lab02Report.txt\");
DecimalFormat twoPrecisionDf=new DecimalFormat(\"#.###\");
DecimalFormat threePrecisionDf=new DecimalFormat(\"#.##\");
//String stockName; // stock name
String stockTicker; // stock name
double stockPrice; // price of one share of stock
double sharesOwned; //price of one share of stock
double annualDividend; //dividend
double total = 0;
// writing header in output file
reportFile.write(\"Stock\\t\\tPrice\\t\\tShares\\t\\tValue\\t\\tDividend\\t\\tYield\");
reportFile.write(\"\ \");
while(fileStockIn.hasNext()) //while loop to stay in loop while there are more records
{//begin while
//System.out.println(\"Welcome to the Stock Program.\ Please enter the following information:\");
//System.out.print(\"Your name > \");
//String name = keyBoard1.nextLine(); // take out keyboard put in what called input file
//System.out.print(\"Stock name > \");
//stockName = keyBoard1.next();
//System.out.print(\"Stock price > \");
// reading stock price
stockPrice = fileStockIn.nextDouble();
//System.out.print(\"Stocks Owned > \");
// reading number of shares
sharesOwned = fileStockIn.nextDouble();
//System.out.print(\"Annual Dividend> \");
// reading Annual dividend
annualDividend = fileStockIn.nextDouble();
//System.out.print(\"Stock Ticker > \");
// reading Stock ticker
stockTicker = fileStockIn.next();
// calculating stock value and yield
double stockvalue = getStockvalue(stockPrice,sharesOwned);
double yield = getdividendYield(stockPrice,annualDividend);
total = total + stockvalue;
// writing in file
reportFile.write(stockTicker+\"\\t\\t\"+twoPrecisionDf.format(stockPrice)+\"\\t\\t\\t\"+
threePrecisionDf.format(sharesOwned)+\"\\t\\t\\t\"+twoPrecisionDf.format(stockvalue)+\"\\t\\t\\t\"+
twoPrecisionDf.format(annualDividend)+\"\\t\\t\\t\"+twoPrecisionDf.format(yield));
reportFile.write(\"\ \");
}//end while
reportFile.write(\"\ \");
reportFile.write(\"TOTAL: \"+twoPrecisionDf.format(total));
reportFile.write(\"\ \");
reportFile.write(\"Report produced by: your name\");
System.out.println(\"Processed Successfully. Please check output file\");
// closing files
fileStockIn.close( );
reportFile.close( );
}//end of main
//methods go here
public static double getStockvalue(double stockPrice, double sharesOwned)
{
double stockvalue = stockPrice*sharesOwned;
stockvalue = Math.round (stockvalue * 100.0) / 100.0;
return stockvalue;
}
public static double getdividendYield(double stockPrice, double annualDividend)
{
double yield = (annualDividend/stockPrice)*100;
yield = Math.round (yield * 100.0) / 100.0;
return yield;
}
}//end of class
########## Lab02Stocks.txt ##########
34 54 76 pravesh
99 45 99 kumar
######### Lab02Report.txt ###########
Stock Price Shares Value Dividend Yield
pravesh 34 54 1836 76 223.53
kumar 99 45 4455 99 100
TOTAL: 6291
Report produced by: your name





