Search code examples
javamethodsgetter-setter

New to coding and getters and setters are not working to a new class?


So I have been writing a code for a lab in which we need to create a class titled payroll where we have the getters and setters for name, address, pay and hours worked and then create a method to print the address and the name and another method to calculate hours worked x pay and print that with address and name on another class titled demoPayroll. My getters and setters arent transferring over to the demoPayroll though. This is the code I have. Any help is greatly appreciated!

import java.util.Scanner;

public class DemoPayroll {

    public static void main(String[] args) {
        Payroll newEmpInfoObject = new Payroll();

        System.out.println("Enter name");
        Scanner keyboard = new Scanner(System.in);
        String name = keyboard.nextLine();
        System.out.println("Enter Address");
        String address = keyboard.nextLine();
        System.out.println("Enter Hourly Pay");
        double payrate = keyboard.nextDouble();
        System.out.println("Enter Hours Worked");
        double hours = keyboard.nextDouble();

        newEmpInfoObject.printEmpInfo();
        newEmpInfoObject.getGrossPayEarned();
    }
}

public class Payroll {
    private String name;
    private String address;
    private double payrate;
    private double hours;

    public String getName() {
        return name;
    }

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

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public double getPayrate() {
        return payrate;
    }

    public void setPayrate(double payrate) {
        this.payrate = payrate;
    }

    public double getHours() {
        return hours;
    }

    public void setHours(double hours) {
        this.hours = hours;
    }

    public Object printEmpInfo() {
        System.out.println(name);
        System.out.println(address);
        return address;
    }
}

Solution

  • First of all, I might think you have copied the program from an external source, since there is lots of compilation errors. Anyways... try this this might work...

    import java.util.Scanner;

    public class DemoPayroll {

    public static void main(String[] args) {
        Payroll newEmpInfoObject = new Payroll();
    
        System.out.println("Enter name");
        Scanner keyboard = new Scanner(System.in);
        String name = keyboard.nextLine();
        System.out.println("Enter Address");
        String address = keyboard.nextLine();
        System.out.println("Enter Hourly Pay");
        double payrate = keyboard.nextDouble();
        System.out.println("Enter Hours Worked");
        double hours = keyboard.nextDouble();
        System.out.println("Enter Weeks");
        int week = keyboard.nextInt();
    
        Payroll pay=new Payroll();
    
        pay.printEmpInfo(name,address);
        System.out.println(pay.getGrossPayEarned(payrate,hours,week));
    }
    

    }

    class Payroll {

    private double payrate;
    private double hours;
    
    public Object printEmpInfo(String name,String address) {
        System.out.println(name);
        System.out.println(address);
        return address;
    }
    public double getGrossPayEarned(double payrate,double hours,int week) {
        return (hours)*((hours/week)*52)/12;
    }
    

    }