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

Java - Stock Market Anxiety Lab stocks1.txt contents: Apple,AAPL,152.70 Alphabet

ID: 3800489 • Letter: J

Question

Java - Stock Market Anxiety Lab

stocks1.txt contents:

Apple,AAPL,152.70
Alphabet,GOOGL,873.96
IBM,IBM,194.37
Microsoft,MSFT,65.67
Oracle,ORCL,62.82

Intro to Computer Science Java Group Lab #4 Stock Market Anxiety (30 points) 3.38r 196 338 +76.33A 22 483.02. This this lab, you'll read and write text files containing the names of several large companies their stock symbols, and the price of their stocks: 1. Create a text file containing the company name (for example, "Apple"), its stock symbol APPL'), and the price of its stock for several companies on a stock exchange. 2. Create a class named Stock with instance variables to hold the company name, the stock symbol, and the stock's price for each company in the text file 3. Create an array which can hold (up to) 100 stock objects 4. Write a method which uses either a Scanner or a BufferedReader to read a text file containing a list of stock names, symbols, and prices into an array of Stock objects. 5. Decrease all of the stock prices by 1/3 6. Write another method which uses a PrintWriter to write the same data onto a second text file 7. Clear the array of Stock objects 8. Use the same method from step 4 above to read the data just written (with the decreased prices into the array of Stock objects again 9. Increase the prices of the stocks back to their original values (plus or minus a penny) 10. Use the method from step 6 to write them back onto a third text file. Step 1: Create the text file First, create a new workspace, project, and program named Lab4 in JCreator. This should create a src and a classes folder for your project. In the classes folder, create a text file name "stocks1. txt" with the following contents: Line 1: Apple AAPL 152.70 Line 2 Alphabet, GOOGL, 873.96 Line 3 IBM, IBM, 194.37 Line 4 Microsoft, MSFT, 65. 67 Line 5 Oracle, ORCL, 62.82 Important: Make sure that you don't have any spaces before or after the commas.

Explanation / Answer

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;


public class Stock {
  
   public String companyName;
   public String stockSymbol;
   public double stockPrice;
  
   static Stock[] stocks=new Stock[100];
   static Stock[] updatestocks=new Stock[100];
   public Stock(String companyName, String stockSymbol, double stockPrice) {
       this.companyName = companyName;
       this.stockSymbol = stockSymbol;
       this.stockPrice = stockPrice;
   }
   public String getCompanyName() {
       return companyName;
   }
   public void setCompanyName(String companyName) {
       this.companyName = companyName;
   }
   public String getStockSymbol() {
       return stockSymbol;
   }
   public void setStockSymbol(String stockSymbol) {
       this.stockSymbol = stockSymbol;
   }
   public double getStockPrice() {
       return stockPrice;
   }
   public void setStockPrice(double stockPrice) {
       this.stockPrice = stockPrice;
   }
   public String toString() {
       return "Stock [companyName=" + companyName + ", stockSymbol="
               + stockSymbol + ", stockPrice=" + stockPrice + "]";
   }
  
   public static Stock[] updatestockPrices(){
       for(int i=0;i<stocks.length;i++)
       {
           if(stocks[i]!=null)
           {
              
               updatestocks[i]=stocks[i];
double stockprice=updatestocks[i].getStockPrice();
              
              
              
               double updateprice=stockprice*0.3;
               double rounduptotwodecimal=(double)Math.round(updateprice*100)/100;
              
               System.out.println(rounduptotwodecimal);
               updatestocks[i].setStockPrice(rounduptotwodecimal);
           }
       }
       return updatestocks;
   }
  
  
   public static void main(String[] args) throws IOException {
       try {
           BufferedReader reader=new BufferedReader(new FileReader(new File("D:\stocks.txt")));
      
           String line="";
           int count=0;
          
           while((line=reader.readLine())!=null)
           {
              
               String[] words=line.split(",");
              
               String companyName=words[0];
               String stockSymbol=words[1];
               double stockPrice=Double.parseDouble(words[2]);
                 
               Stock stock=new Stock(companyName, stockSymbol, stockPrice);
              
           stocks[count]=stock;
           count++;
              
           }
          
       } catch (FileNotFoundException e) {
           System.out.println("file is not found");
       }catch(IOException e)
       {
           System.out.println("error occured while reading the file");
       }
      
       //to write the stocks into file before update
       FileWriter writer=new FileWriter(new File("D:\stocks1.txt"));
      
       for(int i=0;i<stocks.length ;i++)
       {
           if(stocks[i]!=null)
           {
               writer.write(stocks[i].toString());
               writer.flush();
               writer.write(" ");
           }
       }
      
       //after updating
      
       Stock[] stockslist=updatestockPrices();
      
       FileWriter writer1=new FileWriter(new File("D:\stocks1.txt"));
      
       for(int i=0;i<stockslist.length ;i++)
       {
           if(stockslist[i]!=null)
           {
               writer1.write(stockslist[i].toString());
               writer1.flush();
               writer1.write(" ");
           }
       }
      
   }
  
  

}

output

//before updating

Stock [companyName=Apple, stockSymbol=AAPL, stockPrice=45.81]
Stock [companyName=Alphabet, stockSymbol=GOOGL, stockPrice=262.19]
Stock [companyName=IBM, stockSymbol=IBM, stockPrice=58.31]
Stock [companyName=Microsoft, stockSymbol=MSFT, stockPrice=19.7]
Stock [companyName=Oracle, stockSymbol=ORCL, stockPrice=18.85]

//after updating

Stock [companyName=Apple, stockSymbol=AAPL, stockPrice=45.6]
Stock [companyName=Alphabet, stockSymbol=GOOGL, stockPrice=261.9]
Stock [companyName=IBM, stockSymbol=IBM, stockPrice=58.19]
Stock [companyName=Microsoft, stockSymbol=MSFT, stockPrice=19.5]
Stock [companyName=Oracle, stockSymbol=ORCL, stockPrice=18.59]

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