Convert this C++ code to java // Stock Profit #include <iostream> #include <ioma
ID: 3781777 • Letter: C
Question
Convert this C++ code to java
// Stock Profit
#include <iostream>
#include <iomanip>
using namespace std;
// Function prototype
double profit(double, double, double, double, double);
int main()
{
int ns; // Number of shares
double sp; // Sale price per share
double sc; // Sale commission
double pp; // Purchase price per share
double pc; // Purchase commission
double prof; // Profit from a sale
// Get the number of shares.
cout << "How many shares did you buy and then sell? ";
cin >> ns;
// Get the purchase price per share.
cout << "What price did you pay for the stock "
<< "per share? ";
cin >> pp;
// Get the purchase commission.
cout << "What was the purchase commission? ";
cin >> pc;
// Get the sale price per share.
cout << "What was the sale price per share? ";
cin >> sp;
// Get the sales commission.
cout << "What was the sales commission? ";
cin >> sc;
// Get the profit or loss.
prof = profit(ns, pp, pc, sp, sc);
// Display the result.
cout << "The profit from this sale of stock is $"
<< setprecision(2) << fixed << showpoint
<< prof << endl;
return 0;
}
// ********************************************************
// The profit function accepts as arguments the number of *
// shares, the purchase price per share, the purchase *
// commission paid, the sale price per share, and the *
// sale commission paid. The function returns the profit *
// (or loss) from the sale of stock as a double. *
// ********************************************************
double profit(double ns, double pp, double pc,
double sp, double sc)
{
return ((ns * sp) - sc) - ((ns * pp) + pc);
}
Explanation / Answer
StockProfit.java
import java.text.DecimalFormat;
import java.util.Scanner;
public class StockProfit {
public static void main(String[] args) {
DecimalFormat df=new DecimalFormat("#.##");
//Scanner class object is used to read the inouts entered by the user
Scanner kb=new Scanner(System.in);
int ns; // Number of shares
double sp; // Sale price per share
double sc; // Sale commission
double pp; // Purchase price per share
double pc; // Purchase commission
double prof; // Profit from a sale
// Get the number of shares.
System.out.print("How many shares did you buy and then sell? ");
ns=kb.nextInt();
// Get the purchase price per share.
System.out.print("What price did you pay for the stock per share? ");
pp=kb.nextDouble();
// Get the purchase commission.
System.out.print("What was the purchase commission? ");
pc=kb.nextDouble();
// Get the sale price per share.
System.out.print("What was the sale price per share? ");
sp=kb.nextDouble();
// Get the sales commission.
System.out.print("What was the sales commission? ");
sc=kb.nextDouble();
// Get the profit or loss.
prof = profit(ns, pp, pc, sp, sc);
// Display the result.
System.out.print("The profit from this sale of stock is $"+df.format(prof));
}
// ********************************************************
// The profit function accepts as arguments the number of *
// shares, the purchase price per share, the purchase *
// commission paid, the sale price per share, and the *
// sale commission paid. The function returns the profit *
// (or loss) from the sale of stock as a double. *
// ********************************************************
private static double profit(int ns, double pp, double pc, double sp,
double sc) {
return ((ns * sp) - sc) - ((ns * pp) + pc);
}
}
_____________________
Output:
How many shares did you buy and then sell? 1000
What price did you pay for the stock per share? 10
What was the purchase commission? 1.5
What was the sale price per share? 20
What was the sales commission? 1.5
The profit from this sale of stock is $9997
__________Thank You