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 2 of 2

Thread: PayCheck class... percision error.. help!

  1. #1
    Junior Member
    Join Date
    Oct 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation PayCheck class... percision error.. help!

    this is a program where you enter the wage and hours you work and it spits out the amount of money you should be receiving.. i am getting a possible loss of percision error.

    here is my code
    import java.util.Scanner;
     
    public class PayCheck 
    {
     
        public PayCheck() 
        {
        	Scanner in = new Scanner(System.in);
            System.out.println("Wage: ");
            double wage = in.nextDouble();
            System.out.println("Hours: ");
            double hours = in.nextDouble();    
        }
        public int getPayCheck()
        {
        	double pay;
        	if (hours > 40)
        	{
        		pay = (wage*hours)+((hours%40)*(overtime*wage));
        	}
        	else
        	{
        		pay = wage*hours;
        	}
        	return pay;	
        }
        private double hours;
        private double overtime = 1.5;
        private double wage;
     
     
    }
    thanks
    Last edited by helloworld922; October 19th, 2009 at 04:29 PM.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: PayCheck class... percision error.. help!

    The modulus operator only operates on integers (I think). Also, you're "double adding" the over-forty hours (one copy at standard wage, and another at the overtime wage).

    pay = (wage*40) + ((hours-(hours%40)*40)*(overtime*wage))

Similar Threads

  1. Truncated class file error
    By Kok in forum Exceptions
    Replies: 4
    Last Post: June 23rd, 2009, 12:23 PM