Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Coding a beginner program and get run time error possible loss of precision

  1. #1
    Junior Member
    Join Date
    Aug 2014
    Posts
    7
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Coding a beginner program and get run time error possible loss of precision

    I'm working on an assignment that states this:

    Modify the Week Two Java™ application using Java™ NetBeans™ IDE to meet these additional and changed business requirements:

    The company has recently changed its total annual compensation policy to improve sales.
    A salesperson will continue to earn a fixed salary of <Add salary figure from Week Two here>. The current sales target for every salesperson is < Add a target sales figure here (e.g. $120,000)>.
    The sales incentive will only start when 80% of the sales target is met. The current commission is <Add percentage here> of total sales.
    If a salesperson exceeds the sales target, the commission will increase based on an acceleration factor. The acceleration factor is <Add a number greater than 1 (e.g. 1.25)>.
    The application should ask the user to enter annual sales, and it should display the total annual compensation.
    The application should also display a table of potential total annual compensation that the salesperson could have earned, in $5000 increments above the salesperson’s annual sales, until it reaches 50% above the salesperson’s annual sales.


    The Java™ application should also meet these technical requirements:

    The application should have at least one class, in addition to the application’s controlling class.
    The source code must demonstrate the use of conditional and looping structures.
    There should be proper documentation in the source code.


    So here is the code I have come up with, but I keep getting these errors:

    Main.java:56: error: possible loss of precision
    for (projSales=sales+5000;projSales <= maxSales; projSales +=5000)
    ^
    required: int
    found: double
    Main.java:58: error: package system does not exist
    system.out.println(projSales + "\t\t" + accel); "




     
    class NewProgram
    {
    	public static void main (String[] args)
    	{
    		double salary; //salesperson's salary
    		double sales; //sales amount for year
    		double commission; // commission without incentives
    		double incentive; //when incentive kicks in
    		double accel; //commission accelerated factor
    		double salesTarget; // is half the salary plus 10,000$
    		double totalIncentiveEarned; //total made from incentive program
    		double totalIncome; //salary + totalIncentiveEarned
    		double maxSales;  //for the table to show 50 percent above the annual sales
     
    		Scanner keyboard= new Scanner(System.in);
     
    		//Enter Salary
    		System.out.print("What is salesperon's fixed salary?");
    		salary=keyboard.nextDouble();
     
    		//Enter Salespersons  sales for year
    		System.out.print("What was their sales for the year?");
    		sales=keyboard.nextDouble();
     
     
    			commission= sales * .05;
    			salesTarget=(salary * .5);
    			incentive=.8*salesTarget;
    			accel=commission*1.5;
    			int projSales;
     
     
    		if (incentive >= 80% salesTarget)
    		{
    			accel=sales*1.5;
    				System.out.println("Congrats you qualify for the new incentive program!");
    				totalIncome=accel+salary;
    				System.out.println("Your commission for the new program is " + accel);
    				System.out.println("Your total income for the year is " + totalIncome);
    		}
    			else 
    			{
    				System.out.println("You did not qualify for the sales incentive program");
    				System.out.println("Your total commission was" + commission);
    				System.out.println("Your total income for the year is" + totalIncome);
    			}
     
    	System.out.print("Your potential earnings were");
    		for (projSales=sales+5000;projSales <= maxSales; projSales +=5000)
    		{
    			system.out.println(projSales + "\t\t" + accel);
    		}
     
    		}
     
    	}


    I also cant figure out why the:
    System.out.print("Your potential earnings were");
    		for (projSales=sales+5000;projSales <= maxSales; projSales +=5000)
    		{
    			system.out.println(projSales + "\t\t" + accel);

    wont export into table format, it increasing the originally entered sales amount by 5000 and showing the potential compensation


  2. #2
    Junior Member
    Join Date
    Apr 2014
    Posts
    13
    Thanks
    3
    Thanked 2 Times in 1 Post

    Default Re: Coding a beginner program and get run time error possible loss of precision

    The problem with
    system.out.println(projSales + "\t\t" + accel);
    is that Java is case sensitive, and you must capitalize "System."

    The Int/Double problem that you have is not something that should cause a runtime error, but still something to be aware of. The variable projSales is an int, which cannot record decimal values. The other numeric variables you use are doubles, which do. The compiler is warning you that when you do equations mixing the two, you can lose precision by having the int variable truncate decimal values. I would recommend changing the type of projSales to double.

    Hope that helps!

  3. The Following 2 Users Say Thank You to Sedu For This Useful Post:

    Ada Lovelace (August 18th, 2014), GregBrannon (August 18th, 2014)

  4. #3
    Junior Member
    Join Date
    Aug 2014
    Posts
    7
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Coding a beginner program and get run time error possible loss of precision

    [/COLOR]So I got all of it to work. Here is my next problem.

    The code is set up that if the person has sales that total less than 80% of the target sale amount, then it will pop up saying "you did not qualify under the program."

    The incentives kick in when the person reaches 80% of the sales target, outlined in this part of the code:
    commission= sales * .05;
    			salesTarget=(salary * .5);
    			incentive=.8*salesTarget;
    			accel=commission*1.5;
    			int projSales;
     
     
    		if (incentive >= 80% salesTarget)
    		{
    			accel=sales*1.5;
    				System.out.println("Congrats you qualify for the new incentive program!");
    				totalIncome=accel+salary;
    				System.out.println("Your commission for the new program is " + accel);
    				System.out.println("Your total income for the year is " + totalIncome);
    		}
    			else 
    			{
    				System.out.println("You did not qualify for the sales incentive program");
    				System.out.println("Your total commission was" + commission);
    				System.out.println("Your total income for the year is" + totalIncome);
    			}

    however, no matter if the number is less than 80% or greater than 80%, the code is still outputting CONGRATS you qualify.

    Shouldnt the else statement be kicking any number totalling less than 80% of the sales target to say not qualified? What am I missing?[COLOR="Silver"]

  5. #4
    Member Ada Lovelace's Avatar
    Join Date
    May 2014
    Location
    South England UK
    Posts
    414
    My Mood
    Angelic
    Thanks
    27
    Thanked 61 Times in 55 Posts

    Default Re: Coding a beginner program and get run time error possible loss of precision

    if (incentive >= 80% salesTarget)
    That is not how you calculate percentages in programming. (Believe me, I wish it was

    You need to use the * and / operators in the expression. I would also suggest creating a
    separate variable to just hold the calculated percent and then using that in the condition
    statement you wrote.

    Hint: % = somevalue * 100 / somevalue

    Wishes Ada xx
    If to Err is human - then programmers are most human of us all.
    "The Analytical Engine offers a new, a vast, and a powerful language . . .
    for the purposes of mankind
    ."
    Augusta Ada Byron, Lady Lovelace (1851)

  6. The Following 2 Users Say Thank You to Ada Lovelace For This Useful Post:

    GregBrannon (August 18th, 2014), Summer (August 20th, 2014)

  7. #5
    Junior Member
    Join Date
    Aug 2014
    Posts
    7
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Coding a beginner program and get run time error possible loss of precision

    So I have double incentive=.8*salesTarget; set earlier in the code, but then for the if statement am trying to say if incentive is greater than 80% of salesTarget, do the rest of code. How would I put that in the code correctly lol

  8. #6
    Member Ada Lovelace's Avatar
    Join Date
    May 2014
    Location
    South England UK
    Posts
    414
    My Mood
    Angelic
    Thanks
    27
    Thanked 61 Times in 55 Posts

    Default Re: Coding a beginner program and get run time error possible loss of precision

    Quote Originally Posted by Summer View Post
    So I have double incentive=.8*salesTarget; set earlier in the code, but then for the if statement am trying to say if incentive is greater than 80% of salesTarget, do the rest of code. How would I put that in the code correctly lol
    Well first you need to calculate what 80% of the sales target is going to be first.
    When you have that number (see formula in post #4). Your sales target has a base-value
    so just calculate 80% of that. When you preform your if test,
    it would be something like:

    if (testingValue >= calculatedPercent)

    When that works, you can always use your variable which holds
    80% of whatever the sales target is elsewhere in your code. It might also
    be an idea to make it final to ensure it is never accidently changed.

    Wishes Ada xx
    If to Err is human - then programmers are most human of us all.
    "The Analytical Engine offers a new, a vast, and a powerful language . . .
    for the purposes of mankind
    ."
    Augusta Ada Byron, Lady Lovelace (1851)

  9. The Following User Says Thank You to Ada Lovelace For This Useful Post:

    Summer (August 20th, 2014)

Similar Threads

  1. "possible loss of precision, found double, required int" HELP
    By kkatchh in forum Loops & Control Statements
    Replies: 3
    Last Post: November 6th, 2011, 10:50 AM
  2. Possible Loss of Precision
    By Canadian_Pirate in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 11th, 2011, 01:41 AM
  3. Possible loss of precision (double/int)
    By haloboy in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 8th, 2011, 02:23 AM
  4. [SOLVED] Loss of Precision (Double/Int)
    By Scotty in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 9th, 2010, 01:45 PM
  5. [SOLVED] "possible loss of precision", except not, code doesn't work, simple question
    By Perd1t1on in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 24th, 2010, 07:11 PM