java To demonstrate defining and initializing arrays To demo

java

To demonstrate defining and initializing arrays

To demonstrate processing arrays sequentially

To demonstrate a sequential search through an array using a method

Start with the Java program from LAB02 and save it as LAB03.java

Make the following changes:

Create parallel arrays for the stock ticker and the stock name

Parallel arrays are two separate arrays that are positionally linked,

      for example: the ticker in row 0 is the ticker for the stock name in row 0

Initialize these using file Lab03ArrayFile.txt, which contains the tickers and the stock names

Refer to Java04Strings

Create a method that will display a list of stock tickers and stock names on the monitor.

Call this method before the main process loop.

Create a method that will do a sequential search through the arrays:

Search through the ticker array to find a match for the input ticker

If no match is found, use “Invalid Stock Ticker” for the stock name

Use the corresponding stock name as output to the report

Refer to Java06SequentialSearch

NOTE: you must trim extra spaces or tabs off of two variables:

The stock name for the array AND the stock ticker from the stock input file

Input file is Lab03Input.txt and is in the CISC122 LAB03 folder

Output file to be created: Lab03Report.txt

Expected results are in a file called Lab03ExpectedResults

SAMPLE output report

Stock Value and Yield Report

Stock Ticker & Name             Price                Shares              Value               Dividend         Yield

xxxx xxxxxxxxxxxxxxxx              99.99               99.999             9999.99                 99.99         9.99%

xxxx xxxxxxxxxxxxxxxx              99.99               99.999             9999.99                 99.99         9.99%

xxxx xxxxxxxxxxxxxxxx              99.99               99.999             9999.99                 99.99         9.99%

TOTAL                                                                                  9999.99

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

//Lab02 Template

//Written by:

//Date written:

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

{//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(\"Lab02Input.txt\"));

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

   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;

   double stockValue;

   double yield;

   // writing header in output file

   final String heading1 = \"Stock Value and Dividend Yield Report\";

   final String heading2 = \" \";

   final String heading3 = \"Stock   Price   Shares   Value   Dividend Yield\";

   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

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

   stockValue = getStockvalue(stockPrice,sharesOwned);

   yield = getdividendYield(stockPrice,annualDividend);

   total = total + stockValue;

   // writing in file

reportFile.printf (\"%-5s\",stockTicker);

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\", yield);

  

  

   }//end while

   reportFile.println(\" \");

   reportFile.printf(\"Total Stock Value: %8.2f\", total );

   reportFile.println(\" \");

   reportFile.printf(\"Report produced by: 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

Solution

Lab02Input.txt is not given so I assume it contains ticker and name in each line saperated by space. Below is the updated code Please check if this suffices or you can comment for further assistance. Also, please provide the Lab02Input.txt file for proper understnding

import java.io.*;
import java.util.*;
public class Lab02
{//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
  
PrintWriter reportFile = new PrintWriter(\"reportFile.txt\");
String stockName; // stock name
String stockTicker; // stock name
Scanner fileStockIn = new Scanner(new FileReader(\"Lab02Input.txt\"));
double stockPrice; // price of one share of stock
double sharesOwned; //price of one share of stock
double annualDividend; //dividend
double total = 0;
double stockValue;
double yield;
  
  
int totalRecords=10;
int i=0;
String[] tickers=new String[totalRecords];
String[] names=new String[totalRecords];
Scanner sc = new Scanner(new FileReader(\"Lab03Input.txt\"));
while(sc.hasNext()) //while loop to stay in loop while there are more records
{
stockTicker=sc.next();
stockName=sc.next();

tickers[i]=stockTicker;
names[i]=stockName;
i++;
  
}
  
showStocks(names, tickers);
  
  
// writing header in output file
final String heading1 = \"Stock Value and Dividend Yield Report\";
final String heading2 = \" \";
final String heading3 = \"Stock Price Shares Value Dividend Yield\";
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
// 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
stockValue = getStockvalue(stockPrice,sharesOwned);
yield = getdividendYield(stockPrice,annualDividend);
total = total + stockValue;
// writing in file
reportFile.printf (\"%-5s\",stockTicker);
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\", yield);
  
  
}//end while
reportFile.println(\" \");
reportFile.printf(\"Total Stock Value: %8.2f\", total );
reportFile.println(\" \");
reportFile.printf(\"Report produced by: Name\");
System.out.println(\"Processed Successfully. Please check output file\");

// closing files
fileStockIn.close( );
reportFile.close( );
}//end of main
//methods go here
// Method to show list of tickers and names
public static void showStocks(String names[], String tickers[])
{
for(int i=0; i<names.length; i++)
{
System.out.println(i+1+\" \"+tickers[i]+\" \"+names[i]);
}
}
public static String searchTicker(String t, String names[], String tickers[])
{
for(int i=0; i<names.length; i++)
{
if(t.equals(tickers[i]))
return names[i];
  
}
  
return \"Invalid Stock Ticker\";

}
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

java To demonstrate defining and initializing arrays To demonstrate processing arrays sequentially To demonstrate a sequential search through an array using a m
java To demonstrate defining and initializing arrays To demonstrate processing arrays sequentially To demonstrate a sequential search through an array using a m
java To demonstrate defining and initializing arrays To demonstrate processing arrays sequentially To demonstrate a sequential search through an array using a m
java To demonstrate defining and initializing arrays To demonstrate processing arrays sequentially To demonstrate a sequential search through an array using a m
java To demonstrate defining and initializing arrays To demonstrate processing arrays sequentially To demonstrate a sequential search through an array using a m
java To demonstrate defining and initializing arrays To demonstrate processing arrays sequentially To demonstrate a sequential search through an array using a m

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site