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

Write a program to calculate the profit (or loss) of the stock transaction. In a

ID: 3786722 • Letter: W

Question

Write a program to calculate the profit (or loss) of the stock transaction. In a purchasing Input section, ask the user for:

The number of shares of stock purchased (as an integer)

The purchase price for 1 share of stock (as a double)

The commission rate (as a double)

In a purchasing Procession section, calculate:

The cost to purchase x amount of shares at $x.xx

The amount of the commission (in dollar and cents)

The total cost of the transaction

In a purchasing Output section, display:

The number of shares purchased, the price per share, and the cost for the stock

The commission rate and the commission price

The total cost of the transaction

In a selling Input section, ask the user for:

The number of shares of stock sold (as an integer)

The selling price for 1 share of stock (as a double)

The commission rate (as a double)

In a purchasing Procession section, calculate:

The amount of money received from selling x amount of shares at $x.xx

The amount of the commission (in dollar and cents)

The total amount of money received from the transaction

In a purchasing Output section, display:

The number of shares sold, the price per share, and the amount of money received for the stock

The commission rate and the commission price

The total amount of money received from the transaction

Finally, display the profit (or loss) from the 2 transactions.

Example Run:

Enter the number of shares of stock purchased: 100
Enter the purchase price for 1 share of the stock: $20.25 Enter the commission rate to purchase the stock: 5

You purchased 100 shares of stock at $20.25 per share for a cost of $2025.00.
The commission for the purchase at 5.00% was $101.25, for a total price of $2126.25

Enter the number of shares of stock sold: 100
Enter the selling price for 1 share of the stock: $29.89 Enter the commission rate to sell the stock: 5

You sold 100 shares of stock at $29.89 per share for $2989.00. The commission for the sale at 5.00% was $149.45,
for a total price of $2839.55

The amount of profit (or loss) earned by this transaction was $713.30.

i need help

Explanation / Answer


import java.util.*;
public class share {
   public static void main(String[] args) {
       // TODO Auto-generated method stub
       Scanner sc=new Scanner(System.in);       System.out.println("enter number of shares of stock purchased:");
       int purchased=sc.nextInt();
       System.out.println("Enter the purchase price for 1 share of the stock");
       float prate=sc.nextFloat();
       System.out.println("Enter the commission rate to purchase the stock");
       float pcomm=sc.nextFloat();
       float totalpcost=100*prate;
       System.out.println("You purchased " +purchased + " shares of stock at" + prate+ " per share for a cost of $" +totalpcost);
       float allcomm=totalpcost/100;
       float alltotal=allcomm*pcomm;
       float finalpurchasedcost=alltotal+totalpcost;
       System.out.println("The commission for the purchase at"+pcomm+" % was $"+alltotal+", for a total price of $"+finalpurchasedcost);
       System.out.println();
       System.out.println("enter number of shares of stock sold:");
       int sold=sc.nextInt();
       System.out.println("Enter the selling price for 1 share of the stock");
       float srate=sc.nextFloat();
       System.out.println("Enter the commission rate to selling the stock");
       float scomm=sc.nextFloat();
       float totalscost=100*srate;
       System.out.println("You selling " +sold + " shares of stock at" + srate+ " per share for a cost of $" +totalscost);
       float allscomm=totalscost/100;
       float allstotal=allscomm*scomm;
       float finalsellingcost=totalscost-allstotal;
       System.out.println("The commission for the selling at"+scomm+" % was $"+allstotal+", for a total price of $"+finalsellingcost);
       if(finalsellingcost>finalpurchasedcost)
           System.out.println("profit earnde is"+(finalsellingcost-finalpurchasedcost));
       else
           System.out.println("loss earnde is"+(finalpurchasedcost-finalsellingcost));
          
   }
}
output:


enter number of shares of stock purchased:
100
Enter the purchase price for 1 share of the stock
20.25
Enter the commission rate to purchase the stock
5
You purchased 100 shares of stock at20.25 per share for a cost of $2025.0
The commission for the purchase at5.0 % was $101.25, for a total price of $2126.25
enter number of shares of stock sold:
100
Enter the selling price for 1 share of the stock
29.89
Enter the commission rate to selling the stock
5
You selling 100 shares of stock at29.89 per share for a cost of $2989.0
The commission for the selling at5.0 % was $149.45, for a total price of $2839.55
profit earnde is713.30005

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