|
|||||||
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
#1 |
|
Member (8 bit)
|
Java question: How do you multiply in decimal
I'm trying to set a variable and mulitplying it by 1.5. I can multiply it with 2 easily but when I do 1.5 it runs into problems
int orate = (otime * trate)* 2 ; |
|
|
|
|
|
#2 |
|
Member (9 bit)
Join Date: Jan 2004
Location: Kansas City(westwood), KS
Posts: 458
|
are you geting a decimal answer? If so that is your problem. The integer data type is only integers. Declare orate as a double and you'll be set.
Here is how it should look double orate = (otime * trate) * 2; |
|
|
|
|
|
#3 | |
|
Member (7 bit)
|
what colecifer said is definitely your best bet. however, if "orate" has to be of type int, and what you want is to multiply "(otime * trate)" by 1.5 and then round the result, then you'll need to use:
Quote:
Edit: I made a stupid suggestion about type casting originally, but Java automatically will convert int values to a double for a mathematical operation. The way your code was as you originally posted it, Java would multiply (otime * trate) * 1.5 and then truncate the result. So if you got 2.9, then the value assigned to orate would be 2. Last edited by raja; 10-16-2004 at 05:10 PM. |
|
|
|
|
|
|
#4 |
|
Member (8 bit)
|
The program I am writing is suppose to record the net salary for an employee. The user inputs their id, hours worked, rates per hour etc, and it's suppose to come up with their net salary minus tax deductions etc.
import java.io.* ; class Empsal { public static void main(String args[])throws IOException { InputStreamReader istream = new InputStreamReader(System.in) ; BufferedReader bufRead = new BufferedReader(istream) ; System.out.println("=============================================="); System.out.println("Welcome To The Employee Salary Record Program"); System.out.println("=============================================="); System.out.println("Please Enter In Your Employeee Id number: "); String empid = bufRead.readLine(); System.out.println("Enter your first name: "); String firstname = bufRead.readLine(); System.out.println("Enter your last name: "); String lastname = bufRead.readLine(); System.out.println("Please Enter How Many Hours You've Worked: "); String workhour = bufRead.readLine(); int hwork = Integer.parseInt(workhour); System.out.println("Please Enter In Your Rate per Hour: "); String thisrate = bufRead.readLine(); int trate = Integer.parseInt(thisrate); int gross = hwork * trate; int otime = hwork - 40; double orate = (otime * trate)* 1.5; double taxr = gross * .10; double nets = gross + orate - taxr; /**1. Employee ID * 2. Employee Name * 3. Number of hours worked * 4. Rate per hour * 5. Tax rate */ System.out.println("============================================"); System.out.println("Employee ID: " + empid); System.out.println("First: " + firstname); System.out.println("Last: " + lastname); System.out.println("Hours Worked " + workhour); System.out.println("Rate Per Hour " + thisrate); if (hwork>40) { System.out.println("Your Overtime Hours: " + otime); } System.out.println("Your Overtime Pay: " + orate); System.out.println("Your gross Salary is " + gross); System.out.println("Current Tax Deduction 10%: Your Deduction is " +taxr); System.out.println("Your Total Net Salary is: " + nets); System.out.println("==========================================="); } } |
|
|
|
|
|
#5 |
|
Member (8 bit)
|
The next steps would be error checking and switch statements
|
|
|
|
|
|
#6 | |
|
Member (7 bit)
|
that looks good, but here are a few suggestions - use them or don't, it's up to you.
1) For hourly rate, I'd make it of type double. Not everyone works for exactly a whole dollar value per hour. You could do the same thing with hours worked, if you wanted. However, using type double for either of these means you'll have to change your "gross" variable to type double, also. If you use a double for hours worked, you'll also have to change the overtime (otime) variable to a double. 2) If you want to make sure that your double variable results print out in "X.XX" format (i.e. so you always have exactly two digits after the decimal), then an easy method to use is this: Quote:
|
|
|
|
|
|
|
#7 |
|
Member (8 bit)
|
Through all of this I didn't realized there was another problem in my program. Now I've been able to seperate the over time hours from regular hours with this statement.
int = hwork - 40; now I need to seperate regular hours from my overtime....
|
|
|
|
|
|
#8 |
|
Member (7 bit)
|
I'm not sure I understand what you mean. What "regular hours" are you extracting from your overtime hours variable? Your "regular hours" are already stored in the hwork variable.
|
|
|
|
|
|
#9 |
|
Member (8 bit)
|
ok, i just got the calculations for the regular hours after some major brain tease. thanks for that tip, i will try it out
|
|
|
|
|
|
#10 |
|
Member (8 bit)
|
well basiclly the problem was that when i was calculating the gross, it was including the overtime hours in that equation. so even if the user inputs 45 hours times 8 dollars an hour, it will seperate the 5 from the 45 to calculate the over times hours by 1.5 but still calculate the 45 hours as regular time.
|
|
|
|
|
|
#11 |
|
Member (8 bit)
|
switch statements and menu !!!!
Here's a menu I wrote for the program and I can't seem to get it to work. *gulp, help~!
import java.io.*; public class ca { public static void main(String args[]) throws IOException { char cchoice; /* Main Menu 1. Input Employee Info. 2. Display Employee Info. 3. Display Regular Time Info. 4. Display Overtime Info. 5. Display Tax Info. 6. Display Gross and Net Salary. 7. Exit Program */ switch (cchoice) { case '1': System.out.println("You selected 'Input Employee Info.'"); break; case '2': System.out.println("You selected 'Display Employee Ifno.'"); break; case '3': System.out.println("You selected 'Display Regular Time Info'."); break; case '4': System.out.println("You selected 'Display Overtime Info'."); break; case '5': System.out.println("You selected 'Display Tax Info'."); break; case '6': System.out.println("You selected 'Display Gross and Net Salary'."); break; case '7': System.out.println("You selected 'Exit Program'."); break; default: System.out.println("Invalid selection."); //break; } } } |
|
|
|
|
|
#12 |
|
Member (7 bit)
|
You currently don't have anything in there to make the program actually display the menu options and then prompt for a selection from the user!
|
|
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|