Knowee
Questions
Features
Study Tools

public class Employee {    private String name;        public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public int getSal() {        return sal;    }    public void setSal(int sal) {        this.sal = sal;    }    private int sal;    public Employee(String name,int sal)    {        this.name = name;        this.sal = sal;    }    public String toString()    {        return name+" "+sal;    }    public int applyInc(int percentage)    {        return sal*percentage;    }}EmployeeStarter.java:public class EmployeeStarter {    public static void main(String[] args)     {        Employee employee = new Employee("Sangamithra",30000);  System.out.println(employee.applyInc(2));//calling the method of Employee that doubles the salary        System.out.println(employee.applyInc(e1->e1.getSal()*2));//line-1    }} How can the method, applyInc() of Employee class be redefined so that the functionality getting applied on Employee (doubling the salary) can be passed as an argument as in line-1? public int applyInc(Function<Employee, Integer> function) { return function.apply(this); }public int applyInc (Predicate<Employee> c) { return c.test(this); }public int applyInc (Consumer<Employee> c){ return c.accept(this); }Submit

Question

public class Employee {    private String name;        public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public int getSal() {        return sal;    }    public void setSal(int sal) {        this.sal = sal;    }    private int sal;    public Employee(String name,int sal)    {        this.name = name;        this.sal = sal;    }    public String toString()    {        return name+" "+sal;    }    public int applyInc(int percentage)    {        return sal*percentage;    }}EmployeeStarter.java:public class EmployeeStarter {    public static void main(String[] args)     {        Employee employee = new Employee("Sangamithra",30000);  System.out.println(employee.applyInc(2));//calling the method of Employee that doubles the salary        System.out.println(employee.applyInc(e1->e1.getSal()*2));//line-1    }} How can the method, applyInc() of Employee class be redefined so that the functionality getting applied on Employee (doubling the salary) can be passed as an argument as in line-1? public int applyInc(Function<Employee, Integer> function) { return function.apply(this); }public int applyInc (Predicate<Employee> c) { return c.test(this); }public int applyInc (Consumer<Employee> c){ return c.accept(this); }Submit

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

The method applyInc() in the Employee class can be redefined to accept a Function<Employee, Integer> as an argument. This allows the functionality of doubling the salary to be passed as an argument. Here's how you can do it:

import java.util.function.Function;

public class Employee {
    private String name;
    private int sal;

    // existing code...

    public int applyInc(Function<Employee, Integer> function) {
        return function.apply(this);
    }
}

In the EmployeeStarter class, you can now pass a lambda expression to the applyInc() method that doubles the salary:

public class EmployeeStarter {
    public static void main(String[] args) {
        Employee employee = new Employee("Sangamithra", 30000);
        System.out.println(employee.applyInc(e -> e.getSal() * 2));
    }
}

This code will double the salary of the employee. The Function<Employee, Integer> interface is a functional interface in Java that accepts one argument and produces a result. In this case, it takes an Employee object and returns an Integer. The lambda expression e -> e.getSal() * 2 is a short way of defining a function that doubles the salary of an Employee.

This problem has been solved

Similar Questions

public class Employee { public int id; public String name; private int sal; public Employee(int id, String name , int sal) { this.id = id; this.name = name; this.sal = sal; } public void setSal(int sal) { this.sal = sal; } public int getSal() { return sal; } public static List<Employee> getEmpList() { List<Employee> list = new ArrayList<>(); list.add(new Employee(111, "A", 2000)); list.add(new Employee(222, "B", 3000)); list.add(new Employee(333, "C", 4000)); list.add(new Employee(444, "D", 5000)); return list; }}public class Appp { public static void main(String[] args) { List<Employee> list = Employee.getEmpList(); for(Employee e : list) { //line 1 e.setSal(e.getSal()*2); //line 2 } //line 3 }}Choose the appropriate option to replace code line 1 to 3 to get the same output from the program. listEmployees.forEach(e -> e.setSal(e.getSal()*2));for(Employee e : listEmployees) {e -> e.setSal(e.getSal()*2);}for(Employee e : listEmployees) {forEach(e -> e.setSal(e.getSal()*2));}for(Employee e : listEmployees) {e.forEach() -> e.setSal(e.getSal()*2);}SubmitPowered by Infosys Wingspan

public class Employee {private int id;private String name;private float salary;private String pancardno;public Employee(){System.out.println("In default constructor");}public Employee(int id, String name, float salary) {this.id = id;this.name = name;this.salary = salary;}public Employee(int id, String name, float salary, String pancardno) {this(id,name,salary); this.pancardno = pancardno; }this();}

public class Account{       private int accountNumber;       private String holderName;       private double balance;                    public Account(int accountNumber, String holderName, double balance) {              this.accountNumber = accountNumber;              this.holderName = holderName;              this.balance = balance;       } } public class Main{       public static void main(String args[]) {              Account accObj1=new Account(1001,"Chandru",50000);                           System.out.println(accObj1);                    }}If the above code should give the output as “Account Number : 1001, Holder : Chandru, Balance : 50000”, which method should be overridden and that method should be written in which class.Select one:a.Override toString() method in Account classb.Override tostring() method in Account classc.Override tostring() method in Main classd.Override toString() method in Main class

[25 points] Write a Java class named Employee such that the class contains threeattributes empName, salary, and departmentName. You should be able to createobjects of this class by supplying values to any one, two, or all three of the attributes. Ifno value is supplied during the object creation, the attributes should be initialized withsome default values. You are expected to use 'this' keyword to achieve the result. Discussthe ways in which you can change or reset the attribute values of the objects created.Write appropriate mutator methods that store values in these fields and accessormethods that return the values in these fields. Once you have written the class, write aseparate program that creates three Employee objects to hold the following data:Employee Name Department Salary---------------------------------------------------------------------Susan Meyers Accounting 5000.00Mark Jones IT 8375.00Joy Rogers Manufacturing 6380.00---------------------------------------------------------------------The program should store this data in the three objects and then display the data for eachemployee on the screen

How would you instantiate the Employee class from a main method located in another class?  public class Employee{    private String name;    private double salary;     public Employee(String n, double s){      name = n;      salary = s;    } }  Employee emp1 = new Employee("Syam", 50000);  Employee emp1 = new Employee(50000);  Employee emp1 = new Employee();  Employee emp1 = new Employee(50000, "Syam");

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.