Knowee
Questions
Features
Study Tools

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");

Question

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");

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

Solution

To instantiate the Employee class from a main method located in another class, you would do the following:

  1. First, ensure that the class where the main method is located has access to the Employee class. This can be done by having both classes in the same package or by importing the Employee class into the class with the main method.

  2. In the main method, you can create a new instance of the Employee class by calling its constructor and passing the required parameters. The constructor of the Employee class requires two parameters: a String for the name and a double for the salary.

Here is how you would do it:

public class Main {
    public static void main(String[] args) {
        Employee emp1 = new Employee("Syam", 50000);
    }
}

In this code, "Syam" is the name of the employee and 50000 is the salary. This will create a new Employee object with the name "Syam" and a salary of 50000.

The other options you provided are incorrect because they either do not provide the correct number of arguments or they provide the arguments in the wrong order. The Employee constructor expects first a String and then a double, not the other way around or any other combination.

This problem has been solved

Similar Questions

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 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 { 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

[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

Create a Java  base class called Employee. Use this class to store two double-type values that could be used to compute the salary of employees. Also,the base class should have a static variable ‘totalemp’  to keep track of the total number of employees created. Derive two specific classes called part_time and full_time from the base employee. Add a member function called as get_basic() to get the basic pay and another member called display_salary() to compute and display the salary of the employees. Using these three classes, design a program that will accept the basic pay of part_time and full_time employees interactively. Remember the two values given as input will be treated as the basic pay of the employees and the following information is used for calculating the salaries of both the part-time as well as full-time employees. Print invalid if the basic_salary of part-time employee and basic_salary of full-time employee are not met the ”Boundary Condition”Part time                                         Full timeHRA = 20% of basic pay                    HRA = 30% of basic payDA = 72% of basic pay                      DA = 80% of basic paySalary = Basic + HRA + DA              Salary = Basic + HRA + DABoundary Condition:0 < basic_salary of part-time employee <= 10000000 < basic_salary of full-time employee <= 1000000Input Format:First line to enter the basic_salary of part-time employee and basic_salary of full-time employeeOutput Format:Total salary of part-time employee and total salary of full-time employee.Total number of employees processed Sample Testcase Input-1:50007000000Sample Testcase Output-1:8900.0invalid1 Sample Testcase Input-2:30000600000Sample Testcase Output-2:42720.0110400.02 Sample Testcase Input-3:0900000Sample Testcase Output-3:invalid216000.01

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.