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

Java Write a program that calculates stock value and dividend yield: Input the f

ID: 3783545 • Letter: J

Question

Java

Write a program that calculates stock value and dividend yield:

Input the following five variables – use an input prompt and align the input:

stock name; stock ticker; stock price; shares owned; annual dividend

Create a method that calculates stock value (price multiplied by # of shares)

Round to two decimal places

Call this method to get the stock value

Create a method that calculates the dividend yield (annual dividend divided by stock price)

Dividend yield is stated as a percentage: round to two decimal places

Call this method to get the dividend yield

Create a method that prints formatted output: a message, a value and %, if needed

Call this method to display the stock value on the monitor

Call this method to display the dividend yield on the monitor

     

      At the beginning of the program, input your name from the keyboard.

      At the end of the program, display a completed message and your name on the monitor.

TEST DATA and EXPECTED RESULTS

Run the program with data for:

Stock name            = Exelon

Stock ticker           = EXC

Stock price            = 34.90

Shares owned        = 40.15

Annual dividend    = 1.27

Expected results: (calculate what you expect the results to be)

Stock value            =

Dividend yield      =

You have $1000. Find a stock and purchase shares:

Go to http://finance.yahoo.com/ and browse for stocks (type something in the “get quotes” box)

Find a stock that has a dividend

Figure out how many shares you can purchase for $1000

Run the program with your stock data:          (fill this in)

Stock name            =

Stock ticker           =

Stock price            =

Shares owned        =

Annual dividend    =

Expected results: (calculate what you expect the results to be)

Stock value            =

Dividend yield      =

SAMPLE RUN OF PROGRAM

Welcome to the Stock Program.

Please enter the following information:

Your name           > Name

Stock name         > Exelon

Stock ticker          > EXC

Stock price           > 30.03

Shares owned       > 33.03

Annual dividend > 2.10

Stock value          = 991.89

Dividend yield     =      6.99%

Program completed. Thank you, Name

This is the template to use

import java.util.*;

public class Lab01

{//start of class

   public static void main(String [] args)  

   {// start of main method

       Scanner keyBoard = new Scanner(System.in);

       // assigns "keyBoard" to keyboard

       //System.out displays on the monitor

   //Variables and defined constants go here - add comments for each variable

   //Data Dictionary

       double stockPrice;   //price of one share of stock  

      

      

   //instructions start here

   //input: input prompts and input of data go here

   //process: calculations, process or calls to methods go here

       //example:

       //To round stockPrice to the nearest thousandth (three decimal places)

       //stockPrice = Math.round (stockPrice * 1000.0) / 1000.0;

   //output: monitor display goes here

      

   }//end of main

//methods go here

}//end of class

Explanation / Answer

// Lab01.java
import java.util.*;
public class Lab01
{//start of class

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;
}

public static void displayResults(double stockvalue, double yield)
{
System.out.println("Stock value = " + stockvalue);
System.out.println("Dividend Yield = " + yield + "%");
}

public static void main(String [] args)
{// start of main method

Scanner keyBoard = new Scanner(System.in);
String stockName;
String stockTicker;
double stockPrice;
double sharesOwned; //price of one share of stock
double annualDividend;
//instructions start here
//input: input prompts and input of data go here
System.out.println("Welcome to the Stock Program. Please enter the following information:");
System.out.print("Your name > ");
String name = keyBoard.next();
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();

//process: calculations, process or calls to methods go here
double stockvalue = getStockvalue(stockPrice,sharesOwned);
double yield = getdividendYield(stockPrice,annualDividend);
stockPrice = Math.round (stockPrice * 1000.0) / 1000.0;

//output: monitor display goes here
displayResults(stockvalue, yield);
System.out.println("Program completed. Thanks You, " + name);
}//end of main
//methods go here
}//end of class

/*
output:

Welcome to the Stock Program.
Please enter the following information:
Your name > AYush
Stock name > Exelon
Stock Ticker > EXC
Stock price > 30.03
Stocks Owned > 33.03
Annual Dividend > 2.10
Stock value = 991.89
Dividend Yield = 6.99%
Program completed. Thanks You, AYush


*/

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