Results 1 to 9 of 9
Like Tree1Likes
  • 1 Post By SurfMan

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

  1. #1
    sithclone3 is offline Member
    Join Date
    Jun 2014
    Posts
    89
    Rep Power
    0

    Default 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
    Java Code:
    paidBroker = commission + commissionAfterSale;
    is being added to
    Java Code:
    noProfit = profit - paidBroker;
    .

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

    Java 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);
    
    	}
    
    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: The difference from one equation is being added to the total of another

    Look at lines 62, 65, and 70.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    sithclone3 is offline Member
    Join Date
    Jun 2014
    Posts
    89
    Rep Power
    0

    Default Re: The difference from one equation is being added to the total of another

    Quote Originally Posted by jim829 View Post
    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

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default 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
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    sithclone3 is offline Member
    Join Date
    Jun 2014
    Posts
    89
    Rep Power
    0

    Default 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.
    Last edited by sithclone3; 01-21-2015 at 09:50 PM.

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default 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
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    sithclone3 is offline Member
    Join Date
    Jun 2014
    Posts
    89
    Rep Power
    0

    Default Re: The difference from one equation is being added to the total of another

    Quote Originally Posted by jim829 View Post
    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.

  8. #8
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,995
    Rep Power
    9

    Default Re: The difference from one equation is being added to the total of another

    Quote Originally Posted by sithclone3 View Post
    I fixed it. I got rid of noProfit, that seemed to the issue.
    No profit sucks anyway :)
    DarrylBurke likes this.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  9. #9
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: The difference from one equation is being added to the total of another

    Quote Originally Posted by sithclone3 View Post
    I fixed it. I got rid of noProfit, that seemed to the issue.
    Read that quote in SurfMan's signature.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Help with Equation
    By TJRedeemer in forum New To Java
    Replies: 11
    Last Post: 09-28-2013, 12:10 AM
  2. Need Help With Equation in Java
    By Alsark in forum New To Java
    Replies: 4
    Last Post: 02-12-2013, 04:04 PM
  3. Help with equation
    By _Jk_ in forum New To Java
    Replies: 10
    Last Post: 03-17-2011, 08:15 PM
  4. equation
    By bobo67 in forum New To Java
    Replies: 5
    Last Post: 09-06-2010, 06:40 PM
  5. differential equation RK4
    By arvindmer in forum New To Java
    Replies: 3
    Last Post: 01-08-2009, 01:27 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •