Java:: Output errors?
Author |
Message |
antitru5t
|
Posted: Tue Feb 17, 2009 10:11 pm Post subject: Java:: Output errors? |
|
|
I am currently working on a JAVA Assignment and I am HAVING A TERRIBLY HARD TIME WORKING THROUGH IT. Please help me. ...
Here are the business rules to my problem::
If the pay is from 0.00 to 399.99, the tax rate should be 7%.
If the pay is from 400.00 to 899.99, the tax rate should be 11%.
If the pay is from 900.00 and up, the tax rate shouldbe 15%.
The proper output should be something like this when numbers are entered from the SCANNER::
Enter number of hours worked (0.00) <0 for Quick exit>: 39
Enter first number of hours straight (integer or 0 to disable): 40
Enter hourly rate of worker (00.00): 10
===================================================
Here is how my EXACT output should be like::
=================================================
Gross pay is: $390.00
Tax is : 7%
Net pay is : $362.70
--------------------------------------------------------------
am not getting the correct output like this... Instead, I HAVE A HUGE, HECTIC PROBLEM that has startled me for hours. I am getting an output like this:
Gross pay is: ($356.00) <----- These calculations are all WRONG.
Tax Rate: 0.11
I can give you the whole code. Can you help me fix it?
The main class ASSIGN2A.JAVA
code: |
import java.lang.*;
import java.io.*; // FileReader Class
import java.util.*;
import java.text.DecimalFormat;
public class Assign2a
{
public static void main(String[] args)
{
System.out.println("Pay");
System.out.println("From To Tax Rate(%)");
System.out.println("0.00 399.99 7 ");
System.out.println("400.00 899.99 11 ");
System.out.println("900.00 and up 15 ");
System.out.println("");
CalPayroll cp = new CalPayroll();
cp.acceptPay();
Payroll pr = new Payroll();
pr.calc_payroll();
cp.displayinfo();
}
}
|
CALPAYROLL.JAVA
code: |
import java.util.*;
import java.text.NumberFormat;
public class CalPayroll extends Payroll
{
public void acceptPay()
{
float hours;
int hoursStraight;
float hRate;
System.out.println("Payroll Computations");
System.out.println("");
Accept ac = new Accept();
System.out.println("Enter number of hours worked (00.0) <0 for Quick exit>:");
hours = ac.acceptInputFloat();
super.setHours(hours);
System.out.println("Enter first number of hours straight (integer or 0 to disable):");
hoursStraight = ac.acceptInputInt();
super.setHrsStr(hoursStraight);
System.out.println("Enter hourly rate of worker (00.00):");
hRate = ac.acceptInputFloat();
super.setRate(hRate);
System.out.println("===============...
System.out.println("===============...
}
public void displayinfo()
{
NumberFormat dollars = NumberFormat.getCurrencyInstance();
System.out.println("Gross pay is : " + dollars.format(calc_payroll()));
System.out.println("Tax is : " + tax(calc_payroll()));
// System.out.println("Net pay is : " Payroll.calc_payroll());
}
}
|
PAY.JAVA
code: |
public class Pay
{
private float hoursWorked;
private float rate;
private int straightTime;
public double taxRate;
public float grosspay;
public float overTime;
public float adjustedTime;
public float getHours()
{
return hoursWorked;
}
public void setHours(float hw)
{
hoursWorked = hw;
}
public float getRate()
{
return rate;
}
public void setRate(float rateA)
{
rate = rateA;
}
public int getHrsStr()
{
return straightTime;
}
public void setHrsStr(int sw)
{
straightTime = sw;
}
public double calc_payroll()
{
grosspay = getRate() * getHrsStr();
if(straightTime == 0)
{
grosspay = getRate() * getHrsStr();
}
return grosspay;
}
public double tax(double gpay) // use else if
{
if (gpay <= 899.99)
{
taxRate = 0.11;
}
else
if (gpay <= 399.00)
{
taxRate = 0.07;
}
else
{
taxRate = 0.15;
}
return taxRate;
}
}
|
PAYROLL.JAVA
code: |
public class Payroll extends Pay
{
public double calc_payroll()
{
double theGross = super.calc_payroll();
double theTax = tax(theGross); // pass gross pay to tax
double netPay = (theTax * theGross) - theGross;
return netPay;
}
}
|
I need desperate help QUICKLY. . . =( .. I am so lost.
- Nathe |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
DemonWasp
|
Posted: Tue Feb 17, 2009 10:49 pm Post subject: RE:Java:: Output errors? |
|
|
First off, this appears to be wrong:
Java: |
CalPayroll cp = new CalPayroll();
cp.acceptPay();
Payroll pr = new Payroll();
pr.calc_payroll();
cp.displayinfo();
|
What you probably meant here is:
Java: |
CalPayroll payroll = new CalPayroll();
payroll.acceptPay();
payroll.calc_payroll();
payroll.displayinfo();
|
That way, it's all done with the same object.
Perhaps worse, your definition of "calc_payroll()" appears to vary wildly. You need to decide EXACTLY what each method is going to take, and what it's going to return. In Payroll, calc_payroll returns the NET pay, but in Pay, it returns the GROSS pay. You may want to consider methods being named more descriptively: "getGrossPay" and "getNetPay" come to mind.
Side notes (not to be insulting, just to make it easier for you to read your code and for us to help you):
1. Compsci.ca supports a nonstandard feature: syntax tags! Use [ syntax = " language " ] [ / syntax ] to highlight your code (minus all those spaces, and substituting your language of choice, of course).
2. You do not need to say super.methodName() to call methodName unless you're specifically calling a method in a superclass (that is, you've overridden methodName in this, but you want to call the one in super.
3. Java has a naming convention intended to make reading code easier. This convention is fairly simple. For classnames, each word starts with a capital: MyClassName. For variable names and method names, each word except the first starts with a capital: myClassInstance, myMethodName. Files are named exactly as the class in the file, with .java on the end. There's more to it, but that's the basics. |
|
|
|
|
![](images/spacer.gif) |
OneOffDriveByPoster
|
Posted: Tue Feb 17, 2009 11:16 pm Post subject: Re: Java:: Output errors? |
|
|
I didn't look close enough to see if your code is otherwise correct, but I notice that you are doing calculations at (single) float precision. I would suggest replacing your floats with doubles. I personally would use BigInteger with scaled values, but I don't suppose that is necessary here. |
|
|
|
|
![](images/spacer.gif) |
wtd
|
Posted: Wed Feb 18, 2009 1:34 am Post subject: RE:Java:: Output errors? |
|
|
You should be using floating point to deal with money at all.
Has no one here seen Office Space?
$1.25 is not a dollar and a quarter. It's exactly 125 pennies. |
|
|
|
|
![](images/spacer.gif) |
DemonWasp
|
Posted: Wed Feb 18, 2009 3:13 am Post subject: RE:Java:: Output errors? |
|
|
Quote: Assignment
I figured that using floats was probably acceptable in this case. But yes, were this a financial application, BigInteger would be the correct choice. |
|
|
|
|
![](images/spacer.gif) |
|
|