Create a class named Employee. The class must contain private member variables: name and salary. It should contain getter methods for returning the name and salary. It should also contain setter methods for setting the name and salary. The class must include a constructor, the constructor must be designed to set both the name and the salary. In addition, the Employee class must contain a toString() method that prints both name and salary.

EBK JAVA PROGRAMMING
9th Edition
ISBN:9781337671385
Author:FARRELL
Publisher:FARRELL
Chapter11: Advanced Inheritance Concepts
Section: Chapter Questions
Problem 5RQ
icon
Related questions
Question

Create a class named Employee. The class must contain private
member variables: name and salary. It should contain getter
methods for returning the name and salary. It should also contain
setter methods for setting the name and salary. The class must
include a constructor, the constructor must be designed to set both
the name and the salary. In addition, the Employee class must
contain a toString() method that prints both name and salary.

 

Create a class named Test. The class must contain the main
method. In the main method, you should create three objects from
class Employee and store them in an array of type Employee (you
must prompt the user to enter name and salary for each object).
Then, you should print the name and salary of each object (using
either the getters or the toStrong() method).

 

 

DO NOT COPY CHEGG

Expert Solution
Employee.java

public class Employee
{
    //declaring instance variables as private
    private String name;
    private double salary;
    
    //getters
    public String getName()
    {
        return this.name;
    }
    
    public double getSalary()
    {
        return this.salary;
    }
    
    //setters
    public void setName(String name)
    {
        this.name=name;
    }
    
    public void setSalary(double salary)
    {
        this.salary=salary;
    }
    
    //parameterized constructor
    public Employee(String name, double salary)
    {
        setName(name);
        setSalary(salary);
    }
    
    public String toString()
    {
        return "Name: "+name+"\tSalary: "+salary;
    }
}

trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Knowledge Booster
Unreferenced Objects
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT
Microsoft Visual C#
Microsoft Visual C#
Computer Science
ISBN:
9781337102100
Author:
Joyce, Farrell.
Publisher:
Cengage Learning,
Programming Logic & Design Comprehensive
Programming Logic & Design Comprehensive
Computer Science
ISBN:
9781337669405
Author:
FARRELL
Publisher:
Cengage