Jump to content

How to multiply INT and DOUBLE....

- - - - -

  • Please log in to reply
7 replies to this topic

#1
yourmom615

yourmom615

    Learning Programmer

  • Members
  • PipPipPip
  • 33 posts
So the last part of this wannabee java, I am trying to multiple INT and DOUBLE, due to the fact that commission is base off of percentages......how do I do this when all variable type match and the IDE is telling me a possible loss of precision...any suggestion....?????

import java.util.Scanner;

public class Project2 {


    public static void main(String[] args) {

        

        Scanner input = new Scanner(System.in);

        

        int HE = 1, PE = 2, SA = 3;

        int employeeType = 0;

        int weeklySal= 0,hoursWorked = 0;//Hourly-based Employees (HE)

        int salaryDue =0;

        int numberPieces = 0,wage_Per_peice = 0; //For Piece-based Employees(PE)           

        double items_sold = 0, baseSalary = 0;

        double employeeCommission = 0,commissionDue = 0;

         

         

        System.out.println("Employee Type: HE (1), PE (2), SA (3)");

        employeeType = input.nextInt();

               

        while ( employeeType == 1){

          System.out.println("Please enter # hours worked: ");

        hoursWorked = input.nextInt();

        if ( hoursWorked > 70){

          System.out.println("Hours need be less than 71 weekly: ");

         }

        System.out.println("Please enter hourly pay: ");

        weeklySal = input.nextInt();

        salaryDue = weeklySal * hoursWorked;

        System.out.println("Weeks Pay: " + salaryDue);

        System.exit( 0 );

        }

        

        while ( employeeType == 2){

        System.out.println("Please enter # of pieces produced: ");

        numberPieces = input.nextInt();

                

        System.out.println("Please wage per piece: ");

        wage_Per_peice = input.nextInt();

                                    

        salaryDue = numberPieces * wage_Per_peice;

        System.out.println("Weeks Pay: " + salaryDue);

        System.exit( 0 );

        }

          

        while ( employeeType == 3){

        System.out.println("Please enter # items sold: ");

        items_sold = input.nextDouble();

        

        System.out.println("Please weekly base salary: ");

        baseSalary = input.nextDouble();

        

        System.out.println("Please enter your commision: ");

        employeeCommission= input.nextDouble();

        [COLOR="red"]salaryDue = items_sold * baseSalary;[/COLOR]        

        commissionDue = salaryDue * employeeCommission;

        

        System.out.println("Weeks Pay: " + commissionDue);

        System.exit( 0 );

        }   

    }

}


Red is the line in question.....

Edited by yourmom615, 21 March 2011 - 06:32 PM.
New Issue


#2
yourmom615

yourmom615

    Learning Programmer

  • Members
  • PipPipPip
  • 33 posts
See Above!!!!!!!!!!!

#3
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
I'd change salaryDue to double instead of int.

#4
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
The loss of precision is because salaryDue is an int, and when you multiply two doubles (which items_sold and baseSalary both are) this results in a double. Since slarayDue is an int, assigning it a double will result in a loss of precision. Because Java likes to harass you over BS, you have to be explicit:
salaryDue = (int) (items_sold * baseSalary);

As a question to your interface logic, how is it that an employees base salary is dependent on the number of items that employee sold? This insinuates a purely commission-based employment system (for example, car salesman), but you then multiply that value by the employeeCommission value (Which is cryptically asked just as "Please enter your commission", which could mean a great deal of different things), so I'm not sure how this works exactly. Are you certain you have the program logic right for what you intend?
Wow I changed my sig!

#5
Mozana

Mozana

    Learning Programmer

  • Members
  • PipPipPip
  • 70 posts
I think the best practise is to make salaryDue double beecause it is currency, one other thing int don't take type double but double does take int and thus wil not result in the problem;

#6
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
I would use integers and work with the money as if it were cents, meaning 1.00$ would be an integer of value 100.
When you need to display it, divide by 100 or just process as String and throw in a comma before the last 2 characters.
integers are easier as they have no floating point issues.
if( doubleVariable== 0.0 )
Will rarely be true, even if you think it contains 0. In reality it propably contains like 0.000000000000019 or so.

#7
eafkuor

eafkuor

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 218 posts
Or, if you find it easier, you could use two different variables: one for the integer part, one for the decimal part.

#8
yourmom615

yourmom615

    Learning Programmer

  • Members
  • PipPipPip
  • 33 posts
Thanks for the info....I was able to manipulate it later and found that using double is better due to the currency issue.
As far as, the commission part on the last employee type, I got clarification as to what actually needed done and the commission will be based off a percentage of the items sold * the item price and then adding the base weekly salary into it. I have added the code below to show what I mean. Any critiques are welcome. Thanks for the kind words and advice. Hats off to you!!!!

while ( employeeType == 3){ 

        if ( week_Number > 0 && week_Number <= 53){     

        System.out.println("Please enter number between 1-52: " );

        week_Number =input.nextInt();        

       }

        System.out.println("Please enter number of items sold: ");

        items_sold = input.nextDouble();

        

        System.out.println("Please enter price of each item: ");

        item_Price = input.nextDouble();

        

        System.out.println("Please weekly base salary: ");

        baseweekly_Salary = input.nextDouble();

        

        System.out.println("Please enter your commision: ");

        employeeCommission= input.nextDouble();

        salaryDue = (items_sold * item_Price);

        final_SA_wages = (salaryDue *employeeCommission)+ baseweekly_Salary;

              

        

        System.out.println("Weeks Pay: " + commissionDue);

        System.out.println("ABC Company");

        System.out.println("=============================");

        System.out.println("Week #:" + week_Number);

        System.out.println("@@@@@@@");

        System.out.println("Employee type:" + employeeType );

        System.out.println("@@@@@@");

        System.out.println("Weekly Base Salary:" +baseweekly_Salary) ;

        System.out.println("Number of items sold:" + items_sold  );

        System.out.println("Salary Due:" + final_SA_wages);

        System.out.println("=============================");

        

        System.exit( 0 );

        }  

This is still a work in progress so be easy....please!




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users