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

The profit from the sale of a stock can be calculated as follows: Profit 5 ((NS

ID: 3767802 • Letter: T

Question

The profit from the sale of a stock can be calculated as follows: Profit 5 ((NS 3 SP) 2 SC) 2 ((NS 3 PP) 1 PC) where NS is the number of shares, PP is the purchase price per share, PC is the purchase commission paid, SP is the sale price per share, and SC is the sale commission paid. If the calculation yields a positive value, then the sale of the stock resulted in a profit. If the cal- culation yields a negative number, then the sale resulted in a loss. Write a method that 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 method should return the profit (or loss) from the sale of stock. Demonstrate the method in a program that asks the user to enter the necessary data and displays the amount of the profit or loss. Coding must be in java. thank you

Explanation / Answer

import java.util.Scanner; // Needed for Scanner class public class StockProfit { public static void main(String[] args) { int sale; double NS, PP, PC, SP, SC; double profit; // Create a Scanner object for kb input. Scanner kb = new Scanner(System.in); //--------------------------- // Get answers from the user //--------------------------- System.out.print("Enter the number of Shares: "); NS = kb.nextDouble(); System.out.print("Enter the Purchase Price per share: "); PP = kb.nextDouble(); System.out.print("Enter the Purchase Commission paid: "); PC = kb.nextDouble(); System.out.print("Enter the Sale Price per share: "); SP = kb.nextDouble(); System.out.print("Enter the Sale Commission paid: "); SC = kb.nextDouble(); // Calculation // Use caculate_Profit_or_Loss( NS, SP, SC, PP, PC) ; i.e define only one method // In that method you could have used you formula and returned the result to test variable profit = ((NS * SP) - SC) - ((NS * PP) + PC); //---------------------------------------------- // Determine whether the calculation yields a // positive value or negative value. //---------------------------------------------- if (profit > 0) // if(test > 0) you gained a profit { // Call Profit method double ans1 = prof(NS, SP, SC, PP, PC); // Display } else if (profit < 0) // else you incurred a loss { // Call Loss method double ans2 = loss(NS, SP, SC, PP, PC); } } // Profit method public static double prof(double ns, double sp, double sc, double pp, double pc) { double result; result = ((ns * sp) - sc) - ((ns * pp) + pc); System.out.println("The amount of the profit: " + result); return result; } public static double loss(double ns, double sp, double sc, double pp, double pc) { double result; result = ((ns * sp) - sc) - ((ns * pp) + pc); System.out.println("The amount of the loss: " + result); return result; } }