# The difference from one equation is being added to the total of another

• 01-21-2015, 04:17 AM
sithclone3
The difference from one equation is being added to the total of another
I am writing a program for finding the profit made after paying the broker. here is my code, I am wondering why
Code:

`paidBroker = commission + commissionAfterSale;`
Code:

`noProfit = profit - paidBroker;`
.

Here is my full program, you can find these lines of code on lines 64 and 70.

Code:

```import java.util.Scanner; public class StockTransaction {         public static void main(String[] args) {                 // TODO Auto-generated method stub                                 //Variable Declaration Section                 double amceStockPurchasePrice;    //Price per Acme Stock                 int acmeStockPurchased;          //Number of Acme Stock purchased                 double commissionRate;            //Commission of total price paid to broker                 double acmeStockSoldPrice;        //Price per Acme Stock sold                 double grossTotalPaid;            //Gross Total paid for Acme Stock                 double grossTotalReceived;        //Gross Total Received after selling Acme Stock                 double commission;                //Price paid to broker                 double profit;                    //Profit made from selling stock                 double commissionAfterSale;      //Price paid to broker after the Joe sold his Acme Stock             double paidBroker;                //Amount Joe paid his broker in total             double noProfit;                  //Total Amount of money Joe has after he paid his broker                             //Variable Initialization Section                                 //Keyboard                 Scanner keyboard = new Scanner(System.in);                                 //Code Section + Output Section                 System.out.print("How much was each Acme Share? \$");                 amceStockPurchasePrice = keyboard.nextDouble();                                 System.out.print("How many shares did Joe purchase? ");                 acmeStockPurchased = keyboard.nextInt();                                 //Calculate Gross Total Paid                 grossTotalPaid = acmeStockPurchased * amceStockPurchasePrice;                                                 System.out.print("Joe paid \$" + grossTotalPaid + " for " + acmeStockPurchased + " shares. ");                                 System.out.print("\nWhat percentage was Joe's brokers commission? ");                 commissionRate = keyboard.nextDouble();                                 //Calculate Commission after Joe bought the stock                 commission = grossTotalPaid * commissionRate;                                 System.out.print("Joe's broker was paid \$" + commission);                                 System.out.print("\nJoe decided to sell his stock, how much did he sell each share for? \$");                 acmeStockSoldPrice = keyboard.nextDouble();                                 System.out.print("How many shares did Joe sell? ");                 acmeStockPurchased = keyboard.nextInt();                                 //Calculate Gross Total Received after sales                 grossTotalReceived = acmeStockSoldPrice * acmeStockPurchased;                                 System.out.print("Joe recieved \$" + grossTotalReceived + " for selling all " + acmeStockPurchased + " shares.");                                 //Calculate Commission after Joe sold the stock                 commissionAfterSale = grossTotalReceived * commissionRate;                                 System.out.println("\nJoes broker was paid \$" + commissionAfterSale + " after the sale.");                                 //Calculate Profit Joe made from selling his stock                 profit = grossTotalReceived - grossTotalPaid - commission - commissionAfterSale;                                 //Calculate Amount Spent by Joe from paying his broker                 paidBroker = commission + commissionAfterSale;                                 System.out.print("Joe's broker was in total paid " + paidBroker);                                 //Calculate total after Joe paid his broker                 noProfit = profit - paidBroker;                                 if(profit > paidBroker)                         System.out.print("\nJoe recieved \$" + profit + " in profit.");                 else if(profit == paidBroker)                         System.out.print("\nJoe made no profit.");                                else                         System.out.println("\nJoe lost \$" + noProfit);         } }```
• 01-21-2015, 03:11 PM
jim829
Re: The difference from one equation is being added to the total of another
Look at lines 62, 65, and 70.

Regards,
Jim
• 01-21-2015, 07:49 PM
sithclone3
Re: The difference from one equation is being added to the total of another
Quote:

Originally Posted by jim829
Look at lines 62, 65, and 70.

Regards,
Jim

I do not see how this adds noProfit to paidBroker though. Can you explain this to me, please.

Regards,
Sithclone3
• 01-21-2015, 08:18 PM
jim829
Re: The difference from one equation is being added to the total of another
I thought you said that paidBroker is added to noProfit (first post). In any event, please provide the input values and the output values and point out what the output should actually be.

Thanks,
Jim
• 01-21-2015, 09:47 PM
sithclone3
Re: The difference from one equation is being added to the total of another
This is my output:

How much was each Acme Share? \$32.87

How many shares did Joe purchase? 1000

Joe paid \$32870.0 for 1000 shares.

What percentage was Joe's brokers commission? .02

Joe's broker was paid \$657.4

Joe decided to sell his stock, how much did he sell each share for? \$33.92

How many shares did Joe sell? 1000

Joe recieved \$33920.0 for selling all 1000 shares.

Joes broker was paid \$678.4 after the sale.

Joe's broker was in total paid 1335.8

Joe lost \$-1621.6

Joe's profit is \$1050, he paid his broker \$1335.8, this means he lost \$285.8. Instead its added the 285.5 to the 1335.8 which gives me 1621.6.
• 01-21-2015, 09:58 PM
jim829
Re: The difference from one equation is being added to the total of another
In line 62 you subtract the broker's fees to get the net profit. But in line 70 you subtract them out yet again. I haven't tested this, but that looks like a problem.

Regards,
Jim
• 01-21-2015, 11:23 PM
sithclone3
Re: The difference from one equation is being added to the total of another
Quote:

Originally Posted by jim829
In line 62 you subtract the broker's fees to get the net profit. But in line 70 you subtract them out yet again. I haven't tested this, but that looks like a problem.

Regards,
Jim

I fixed it. I got rid of noProfit, that seemed to the issue.
• 01-21-2015, 11:59 PM
SurfMan
Re: The difference from one equation is being added to the total of another
Quote:

Originally Posted by sithclone3
I fixed it. I got rid of noProfit, that seemed to the issue.

No profit sucks anyway :)
• 01-22-2015, 09:17 AM
gimbal2
Re: The difference from one equation is being added to the total of another
Quote:

Originally Posted by sithclone3
I fixed it. I got rid of noProfit, that seemed to the issue.

Read that quote in SurfMan's signature.