Method not calculating properly

Why won't this calculate tax payed please anyone? it just prints out 0, I was testing it out on David O Sullivan first just too see if it would work, hence why it not in with the others.


Code:
public class Accountancy {

  
    public static void main(String[] args) {
        
        Customer a1 = new Customer();
        Customer a2 = new Customer();
        Customer a3 = new Customer();
        
        a1.CustomerName = "Kevin O'Sullivan";
        a1.OpeningBalance = 20000;
        a1.DebitPurchases = 5000;
        a1.CreditPayment = 2000;
        a1.ClosingBalance = 0;
        a1.HoursWorked = 40.5;
        a1.HourlyRate = 20.0;
        a1.HighTaxRate = 42.0;
        a1.TotalWage = 0;
        
        System.out.println("Your name is: " +a1.CustomerName);
        System.out.println("Your opening Balance is: " +a1.OpeningBalance);
        System.out.println("Your closing Balance now is: " +a1.CalculateCustomerBalance(a1.OpeningBalance,a1.CreditPayment,a1.DebitPurchases,a1.ClosingBalance));
        System.out.println("This Week you Worked: " +a1.HoursWorked);
        System.out.println("Your Hourly Rate is: " +a1.HourlyRate);
        System.out.println("You pay the tax rate of 42%!");
        System.out.println("Your total wage this week is: " +a1.WeeklyWage(a1.HourlyRate,a1.HoursWorked,a1.TotalWage));
        System.out.println();
        
    
        a2.CustomerName = "Jerry O'Sullivan";
        a2.OpeningBalance = 50000;
        a2.DebitPurchases = 10000;
        a2.CreditPayment = 5000;
        a2.ClosingBalance = 0;
        a2.HoursWorked = 56.5;
        a2.HourlyRate = 30.0;
        a2.HighTaxRate = 42.0;
        a2.TotalWage = 0;
        
        System.out.println("Your name is: " +a2.CustomerName);
        System.out.println("Your opening Balance is: " +a2.OpeningBalance);
        System.out.println("Your Closing Balance is: " +a2.CalculateCustomerBalance(a2.OpeningBalance,a2.CreditPayment,a2.DebitPurchases,a2.ClosingBalance));
        System.out.println("This Week you Worked: " +a2.HoursWorked);
        System.out.println("Your Hourly Rate is: " +a2.HourlyRate);
        System.out.println("You pay the tax rate of 42%!");
        System.out.println("Your total wage this week is: " +a2.WeeklyWage(a2.HoursWorked,a2.HourlyRate,a2.TotalWage));
        System.out.println();
        
        
        a3.CustomerName = "David O'Sullivan";
        a3.OpeningBalance = 700;
        a3.DebitPurchases = 500;
        a3.CreditPayment = 100;
        a3.ClosingBalance = 0;
        a3.HoursWorked = 65.4;
        a3.HourlyRate = 15.0;
        a3.HighTaxRate = 42.0;
        a3.TotalTaxPayed = 0;
        a3.TotalWage = 0;
        
        System.out.println("Your name is: " +a3.CustomerName);
        System.out.println("Your opening Balance is: " +a3.OpeningBalance);
        System.out.println("Your Closing Balance is: " +a3.CalculateCustomerBalance(a3.OpeningBalance,a3.CreditPayment,a3.DebitPurchases,a3.ClosingBalance));
        System.out.println("This week you worked: " +a3.HoursWorked);
        System.out.println("Your Hourly Rate is: " +a3.HourlyRate);
        System.out.println("You pay the tax rate of 42%!");
        System.out.println("Your total wage this week is: " +a3.WeeklyWage(a3.HoursWorked,a3.HourlyRate,a3.TotalWage));
        System.out.println("The amount of tax you pay weekly is: " +a3.TaxCalculator(a3.TotalTaxPayed,a3.TotalWage,a3.HighTaxRate));
        
        System.out.println();


Code:
public class Customer {
 
    protected String CustomerName;
    protected double OpeningBalance;
    protected double DebitPurchases;
    protected double CreditPayment;
    protected double ClosingBalance;
    protected double HoursWorked;
    protected double HourlyRate;
    protected double TotalWage;
    protected double HighTaxRate;
    protected double TotalTaxPayed;
    
    
    
    public double CalculateCustomerBalance(double OpeningBalance,double CreditPayment,double DebitPurchases,double ClosingBalance)
    {
        return ClosingBalance = OpeningBalance + CreditPayment - DebitPurchases;
    
    }
    
    public double WeeklyWage(double HourlyRate,double HoursWorked,double TotalWage)
  {
   return TotalWage =  HoursWorked * HourlyRate;

  }

 public double TaxCalculator(double TotalWage,double HighTaxRate,double TotalTaxPayed)
 {
     return TotalTaxPayed = TotalWage * HighTaxRate;
 }
 
Your functions in the customer class shouldn't be returning
TotalTaxPayed = ... It should just return the result of the calculation directly, e.g. for your tax calculator:

return TotalWage * HighTaxRate;

In addition, it doesn't make sense for you to pass in the parameters to those functions because you have already assigned the values as properties on your class. In the functions you can just reference them with this.HighTaxRate. In your tax calculator function the TotalWage property can then be replaced with a call to the WeeklyWage function.

The functions should be public void then instead of public double ?
 
No, a void would mean it doesn't return anything, it will still be returning the result of your calculation. You shouldn't be assigning to a variable in your return statement. Either assign to a variable and then return it, or just return the result of the calculation directly.

Ok cheers for replying, I did what what you said

Code:
public double TaxCalculator(double TotalWage,double HighTaxRate)
 {
     return  TotalWage * HighTaxRate;
 }

but its still printing out the answer as 0 :confused:

Code:
 System.out.println("The amount of tax you pay weekly is: " +a3.TaxCalculator(a3.TotalWage,a3.HighTaxRate));
 
Having a problem here in that it gets as far as asking how old your child is and how many kids you have but it won't do the calculation,it just bypasses it and loops around asking you pick another selection, any idea? thanks.

Code:
         Menu();
      
        System.out.println("Your child benefit is: " +ChildBenefit());
    } 
      
        public static void Menu()
        {
      
        int Choice;
        do
        {
          
            Scanner sc = new Scanner(System.in);
          
            System.out.println("Please Pick an option by pressing 1,2,3 or 0 to Exit the program");
            Choice = sc.nextInt();
          
            switch(Choice)
            {
            case 1:
            ChildBenefit();
            break;
          
            case 2:
            WaterCharges();
            break;
          
            case 3:
            ElectricalCharges();
            break;
          
            case 0:
            System.out.println("Thank you and goodbye!");
            break;
          
            default:
                System.out.println("Not a vaild selection!");
                break;
            }
      
            }while(Choice != 0);
        
        }
  

  
    public static double ChildBenefit()
    {
       int Number_Of_Children;
       int ChildAge;
    
       Scanner sc = new Scanner(System.in);
       System.out.println("Please enter the age of your child: ");
       ChildAge = sc.nextInt();
    
       if(ChildAge <= 18)
       {
           System.out.println("Please enter the number of Children you have: ");
           Number_Of_Children = sc.nextInt();
           double result = Number_Of_Children * 27.60;
           return result;
       }
        else
           {
            System.out.println("The age of the person entered is over the limit and thus receive no benefits!");
            return 0;
           }
       }
    
  
  
    public static void WaterCharges()
    {
        System.out.println();
    }
  
    public static void ElectricalCharges()
    {
        System.out.println();
    }
 
You probably want something like this (still **** code):

Code:
        Menu();
    }
 
    public static void Menu()
    {
 
        int Choice;

        do
        {
            Scanner sc = new Scanner(System.in);
     
            System.out.println("Please Pick an option by pressing 1,2,3 or 0 to Exit the program");
            Choice = sc.nextInt();
     
            switch(Choice)
            {
            case 1:
                System.out.println("Your child benefit is: " + ChildBenefit());
                break;
 
SNIP


If you have a method that returns a value and you don't use the return value immediately, or deliberately store it somewhere for later, then it has gone.

I tried it your way before posting and it wouldn't work, a red line appeared underneath the system.out.println line.
 
It was just a quick cut-n-paste of a part of your code to suggest a slightly more sensible structure.

If the IDE is showing an error isn't it telling you what it thinks is wrong?

I've become a lot better at understanding errors when they happen thankfully.


The below code is suppose to return true or false if the number entered is within a certain range, its not printing out the true or false though and I can't understand why.

Code:
public static void main(String[] args) {
        
        new MinValue().rangeTest(0,1,100);

    }

    private boolean rangeTest(int val,int low,int high)
    {
        Scanner sc = new Scanner(System.in);
        
        System.out.println("PLEASE ENTER A NUMBER:");
        val = sc.nextInt();
        
        if(val > low  && val < high)
        {
            return true;
        }
        else
            return false;


Any ideas?
 
You have a function that nominally returns a boolean value.

You aren't capturing the return value (true/false), and you certainly aren't doing anything to output it.

I got it working but only because I changed it to static method.....


Code:
Scanner sc = new Scanner(System.in);
            
         int val = 0;
         int Low = 1;
         int High = 100;
        
        
        System.out.println("PLEASE ENTER A NUMBER:");
        val = sc.nextInt();
        
        if(rangeTest(val,Low,High))
        {
            System.out.println("This is true");
        }
        else
        {
            System.out.println("THIS IS FALSE");
        }
    
    
    }

    public static boolean rangeTest(int value,int low,int high)
    {
        
        
        if(value > low  && value < high)
        {
            return true;
        }
        else
        {
            return false;
        }
 
Does anyone know how to set up the command prompt in eclipse? so when you run a program instead of printing out to the console the black command prompt box will appear?
 
Are you asking how to setup Eclipse so that instead of writing to the built in console it opens a black Windows command prompt instead,

Thanks for responding Hades.

That's exactly what I'm looking for, there's no particular reason why I need It, it would nice to know how to do it is all, I've tried google but I can't find what I looking for.
 
Sorry for not replying sooner; I've been very busy.

As far as I know Eclipse won't open a command prompt to run Java in. It just uses it's built in console. It would be possible to get your Java program to launch the command prompt and then run another Java program inside that. But I'd need to take a while to work that one out as it's been some time ince I properly used Java.

However, if you wanted to use Eclipse to write Java code and then run it yourself in your own command prompt then that's easy...

Firstly it's useful to understand what Eclipse (or any IDE) is doing when it 'compiles' Java code. It's actually just calling the javac compiler which is part of the Java Development Kit. Javac will take source code and create bytecode in the form of class files (it can also create jar and war files which are a collection of class files, but that's a little more advanced). Let's take this example helloworld code:

Code:
public class helloworld {

    public static void main(String[] args) {
        System.out.println("Hello world");
    }

}


This would be saved as a file called hellowold.java. When you save it in Eclipse it will automatically be compiled to a new file called helloworld.class (you could also do this manually if you install the JDK and simply type javac helloworld.java in the command prompt). Eclipse will save this class file in your workspace directory under a new sub-directoy called bin.

So in order to run that helloworld java program from the command prompt you need to do the following:

1) Save the file in Eclipse so that it creates a class file (or compile it manually using the JDK javac command).
2) Find where Eclipse has created the class file. For example if your Eclispe workspace dircetory is c:\workspaces and your project name is Project1 then Eclipse will probably save it as c:\workspaces\Projet1\bin\hellowworld.class
3) Open a command prompt. Make sure you have Java installed (see below).
4) type cd java c:\workspaces\Project1\bin (or wherever your class file is)
5) type java helloworld (don't add .class on the end, leave that out because java knows it should be a .class)


How to Check If You Have Java Installed

1) Open a command prompt and type java --version
2) If the command can't be found then you need to download java from www.java.com


Thanks so much Hades for the effort you put into the above post it very much appreciated :).
 
Hope someone here can help me out, this program prints out employee details and asks the user to enter how many hours they've worked,Run into a bit trouble here, in the method gettotalpay its not returning the any answer but 0, any ideas? thanks.

Code:
public class Employee {

    private String name;
    private int age;
    private String employeeNumber;
    private double amountPerHour;
    private double hoursWorked;
    private double totalWage;
    private static final String MOTTO = "Work Hard Play Hard!";

    public Employee() {

        this("x",0,"xx",0.0,0.0);
    }

    public Employee(String Name){

        this(Name,0,"xx",0.0,0.0);
    }

    public Employee(String Name,int Age){

        this(Name,Age,"xx",0.0,0.0);

    }

    public Employee(String Name,int Age,String EmployeeNumber){

        this(Name,Age,EmployeeNumber,0.0,0.0);
    }

    //public Employee(String Name,int Age,String EmployeeNumber,double AmountPerHour){

   //    this(Name,Age,EmployeeNumber,Age,AmountPerHour,0.0);
   // }

    public Employee(String Name,int Age,String EmployeeNumber,double AmountPerHour,double HoursWorked){

        this.name = Name;
        this.age = Age;
        this.employeeNumber = EmployeeNumber;
        this.amountPerHour = AmountPerHour;
        this.hoursWorked =HoursWorked;

    }

    public static String getMOTTO()
    {
      return Employee.MOTTO;
    }

    public void setName(String value)
    {
       this.name = value;
    }

    public String getName()
    {
        return this.name;
    }

    public void setAge(int value)
    {
        this.age = value;
    }

    public int getAge()
    {
        return this.age;
    }

    public void setEmployeeNumber(String value)
    {
        this.employeeNumber = value;
    }

    public String getEmployeeNumber()
    {
        return this.employeeNumber;
    }

    public void setAmountPerHour(double APH)
    {
        this.amountPerHour = APH;
    }

    public double getAmountPerHour()
    {
        return this.amountPerHour;
    }

    public void setHoursWorked(double HS)
    {
        this.hoursWorked = HS;
    }

    public double getHoursWorked()
    {
      return this.hoursWorked;
    }

    public double gettotalPay()
    {
        Scanner sc = new Scanner(System.in);

        System.out.println("Please enter the total hours your worked this week:");
        double hoursWorked = sc.nextDouble();

        double totalWage = this.amountPerHour * this.hoursWorked;
        return totalWage;
    }

     public String toString()
     {
        StringBuilder strB = new StringBuilder();

        strB.append(" Name: ");
        strB.append(this.getName() + "\n");
        strB.append(" Age: ");
        strB.append(this.getAge() + "\n");
        strB.append(" EmployeeNumber: ");
        strB.append(this.getEmployeeNumber() + "\n");
        strB.append("Rate Per Hour: ");
        strB.append(this.getAmountPerHour() + "\n");
        strB.append(" Total Wage this week: ");
        strB.append(this.gettotalPay() + "\n");
        return strB.toString();
     }





}


Code:
    new Employee_Tester().tester_Application();

    }

    public void tester_Application()
    {
        Employee s1 = new Employee();
        s1.setName("John");
        s1.setAge(32);
        s1.setEmployeeNumber("112345");
        s1.setAmountPerHour(8.75);
        s1.setHoursWorked(0);

        System.out.println(s1.toString());

        //Employee s2 = new Employee();

        //Employee s3 = new Employee();

        //Employee s4 = new Employee();

        //Employee s5 = new Employee();

        //Employee s6 = new Employee();

        //Employee s7 = new Employee();[/CODE
 
Back
Top Bottom