Download Java Stock Portfolio Assignment: Stocks and StockTest Classes - Prof. Gregory Shaw and more Study notes Computer Science in PDF only on Docsity! Stock Portfolio Assignment (4) (Copy and paste when necessary) Stocks.java /* File: Stocks.java * * @ Name * * I affirm that this program is entirely my own work and * none of it is the work of any other person. */ /** * A class to compute stock portfolio values in eighths and decimal values. * * Stocks objects begin with these values: company name, number of shares, and * stock price, which is divided into two portions: Dollars and eighths * portions. */ public class Stocks { // instance variables private String company ; // company name private int shares ; // number of shares private int stockDollars ; // stock price in the dollars portion private int stockEighths ; // stock price in the eighths portion /** * Create a stocks object with these values: company name, number of * shares, and price, which is divided into two portions: Dollars and * eighths portions. * * @param company the name of the company's stocks * @param shares the number of shares * @param stockDollars the dollars portion of the stock price * @param stockEighths the eighths portion of the stock price */ public Stocks(String company, int shares, int stockDollars, int stockEighths) { this.company = company ; // company name this.shares = shares ; // number of shares this.stockDollars = stockDollars ; // dollars portion of stock price this.stockEighths = stockEighths ; // eighths portion of stock price // "This" will prevent Java from shadowing the instance variables. } /** * Get company name. * @return company name */ public String getCompany() { return company ; } /** * Get company's number of shares. * @return number of shares */ public int getShares() { return shares ; } /** * Get company's dollars portion of stock price. * @return dollars portion of stock price */ public int getStockDollars() { return stockDollars ; } /** * Get company's eighths portion of the stock price. * @return eighths portion of the stock price */ public int getStockEighths() { return stockEighths ; } /** * The change in the company's stock price. * @param newDollars the change in the dollars portion of the stock price * @param newEighths the change in the eighths portion of the stock price */ public void modify(int newDollars, int newEighths) { // totalEighths may be over 8 eighths here, but the upcoming lines will // adjust improper fractions into proper fractions and display them. Port.modify(dollars,eighths) ; stockDollars = Port.getStockDollars() ; stockEighths = Port.getStockEighths() ; // Get and print the updated stock price System.out.println("Closing Price per Share: " + stockDollars + " " + stockEighths + "/8\n") ; // Get and print the updated portfolio value decimal = Port.decimal() ; System.out.println("Closing Portfolio Value: $" + Port.decimal() + "\n") ; } } // THIS OUTPUT WITH THE MODIFIED STOCK PRICE BY $7 AND 2 EIGHTHS /* OUTPUT run: @ Name (§U0#) Company: American Veeblefetzer, Inc. Shares Held: 100 Opening Price per Share: 37 5/8 Opening Portfolio Value: $3762.5 Closing Price per Share: 44 7/8 Closing Portfolio Value: $4487.5 BUILD SUCCESSFUL (total time: 0 seconds) * * THE OUTPUT WITH THE MODIFIED STOCK PRICE BY $7 AND 7 EIGHTHS * OUTPUT run: @ Name (§U0#) Company: American Veeblefetzer, Inc. Shares Held: 100 Opening Price per Share: 37 5/8 Opening Portfolio Value: $3762.5 Closing Price per Share: 45 4/8 Closing Portfolio Value: $4550.0 BUILD SUCCESSFUL (total time: 23 seconds) */