Java Programming for Eclipse
Java basics (variable declaration, numeric expression,
assignment statement, input using Scanner, and output).
Problem Description
A person named name purchased numberShares shares of Microsoft stock at the price of buyPrice
per share and paid the stockbroker $15 transaction fee. Two weeks later, the person sold the
numberShares shares at sellPrice per share and paid another $15 for the transaction. Write a Java
program to calculate and display the following:
1. the dollar amount paid for the shares
2. the dollar amount of the shares sold
3. the total transaction fee paid to the broker (including both buy and sale)
4. the amount of profit (or loss) made after selling the shares.
The values of name, numberShares, buyPrice, sellPrice are input from the user.
For example, if the interactive execution looks like this (user’s inputs are shown in bold):
What’s your name? Joseph
How many shares bought? 250
Buy price? 28.31
Sale price? 30.79
Code for program
import java.util.Scanner;
public class Enter Your Class Here {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner myInput = new Scanner(System.in);
//declare variables
String name;
double numberShares, buyPrice, sellPrice, amountPurchase, amountSellPurchase, netProfit, netProfitFinal;
int transactionFee = 15;
//Prompt
System.out.print ("What is your name? ");
name = myInput.nextLine();
System.out.print("How many shares bought? ");
numberShares = myInput.nextDouble();
System.out.print("Buy Price? ");
buyPrice = myInput.nextDouble();
System.out.print("Sale Price? ");
sellPrice = myInput.nextDouble();
//calculations
amountPurchase = numberShares * buyPrice;
amountSellPurchase = numberShares * sellPrice;
netProfit = amountSellPurchase - amountPurchase;
netProfitFinal = netProfit - transactionFee*2;
//Display the resulting information
System.out.println("Hello " + name + "Here is your information for your stock transaction");
System.out.println("Number of Shares is " + numberShares);
System.out.println("Amount of Purchase $" + amountPurchase);
System.out.println("Amount of sell $" + amountSellPurchase);
System.out.println("Transaction fee paid $" + transactionFee*2);
System.out.println("Net Profit: "+ netProfitFinal);
}
}
No comments:
Post a Comment