Docsity

Java Stock Portfolio Assignment: Stocks and StockTest Classes - Prof. Gregory Shaw, Study notes of Computer Science

Toggle sidebar
bg1
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
pf3
pf4
pf5

Partial preview of the text

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.

public class StocksTest { public static void main(String[] args) // Have the user enter the data (company name, number of // shares, dollars, eighths) { System.out.println("@ Name (\u00A7U0#)") ; String company = JOptionPane.showInputDialog ("Write the Company Name Here.") ; int shares = Integer.parseInt(JOptionPane.showInputDialog ("Place the Number of Shares Below")) ; int stockDollars = Integer.parseInt(JOptionPane.showInputDialog ("Place the Dollar Portion of the Stock Price Below.")) ; int stockEighths = Integer.parseInt(JOptionPane.showInputDialog ("Place the Portion of the Stock Price Below.")) ; // Create a StockPortfolio object Stocks Port = new Stocks(company,shares,stockDollars,stockEighths); // Print Port's data System.out.println("Company: " + company + "\n") ; System.out.println("Shares Held: " + shares + "\n" ) ; System.out.println("Opening Price per Share: " + stockDollars + " "

  • stockEighths + "/8\n") ; // Get and print the portfolio value as a decimal double decimal = Port.decimal() ; System.out.println("Opening Portfolio Value: $"
  • decimal + "\n") ; // Have the user enter the change in the price, in dollars and eighths int dollars = Integer.parseInt(JOptionPane.showInputDialog("Enter the"
  • " Modified Dollar Portion of the Stock Price below.")) ; int eighths = Integer.parseInt(JOptionPane.showInputDialog("Enter "
  • "the Modified Eighths Portion of the Stock Price below.")) ; // Call mutator method to update the dollars and eighths

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/ Opening Portfolio Value: $3762. Closing Price per Share: 44 7/ Closing Portfolio Value: $4487. 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.