Knowee
Questions
Features
Study Tools

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

Question

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

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

Solution

The correct option to replace code line 1 to 3 to get the same output from the program is:

list.forEach(e -> e.setSal(e.getSal()*2));

This is a Java 8 feature called Lambda Expressions, which allows you to pass functions as method parameters. In this case, for each Employee object in the list, it sets the salary to be twice its current value.

Similar Questions

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

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 Person{             int age;             String name;             public Person(String n, int a){                 age = a;                 name = n;             }             public String toString(){                 return (name+ ":" + age );             }         }         public class Employee extends Person implements Cloneable {             public Employee(String n, int a){                 super(n, a);             }             public Employee clone()throws CloneNotSupportedException{                 return (Employee)super.clone();              }         }         public class Test {             public static void main(String[] args) {                 Employee[] e1 = {new Employee("Hari",30), new Employee("geeta",23)};                 Employee[] e2 = e1.clone();                 e2[1].name = "rani";                 System.out.println(e1[1] + ", " + e2[1]);              }          }

You have a class Student:public class Student{ public int studentId; public String studentName; public String studentGrade; public Student() { } public Student(int studentId, String studentName, String studentGrade){ this.studentId = studentId; this.studentName = studentName; this.studentGrade = studentGrade; }}You have a child class CollegeStudent:public class CollegeStudent extends Student {}Then you execute the following snippet of code:CollegeStudent myCollegeStudent = new CollegeStudent(1, "Alice", "3rd");What is the error in this snippet of code?Instruction: Choose the option that best answers the question. Answer ChoicesYou cannot specify multiple constructors for the parent class, though this is allowed in child classesYou cannot use the parameterized constructor in the base class to instantiate the CollegeStudent derived classYou need to have additional constructors in the base class to support the instantiation of the CollegeStudent derived classYou have to use the “object” keyword to create an object of a derived class

Given the following set of classes:public class Customer { private String userName; private String password; public Customer(String userName, String password) { this.userName = userName; this.password = password; } public void setPassword(String password) { this.password = password; } public String getPassword() { return password; } public void setUserName(String userName) { this.userName = userName; } public String getUserName() { return userName; }} class CustomerTest { public static void main(String[] args) { Customer testAcct = new Customer("user","pass"); testAcct.setPassword("Java"); System.out.println(testAcct.toString()); }}What does the toString() method need to look like to result in the following output:UserName: userPassword: Javaa.)public String toString() { String state = "UserName: " + userName + "\n"; state += "Password: " + password + "\n"; return state;}b.)public String toString() { String state = "UserName: " + userName + "\n"; state += "Password: " + password + "\n"; return password;}c.)public String toString() { String state = "UserName: " + userName + "\n"; state = "Password: " + password + "\n"; return state;}d.)public String toString() { String state = "username: " + userName + "\n"; state += "password: " + password + "\n"; return state;}

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.