Question

In: Computer Science

ALL IN JAVA public class Employee {    public String strName, strSalary;       public Employee()...

ALL IN JAVA

public class Employee
{
   public String strName, strSalary;
  
   public Employee()
   {
       strName = " ";
       strSalary = "$0";
   }
  
   public Employee(String Name, String Salary)
   {
       strName = Name;
       strSalary = Salary;
   }
  
   public void setName(String Name)
   {
       strName = Name;
   }
  
   public void setSalary(String Salary)
   {
       strSalary = Salary;      
   }
  
   public String getName()
   {
       return strName;
   }
  
   public String getSalary()
   {
       return strSalary;
   }
  
   public String toString()
   {
       return(strName + " has a salary of " + strSalary);
   }

}

you will find a class called Employee. This class should include a constructor which sets name to blanks and salary to $0.00 and a constructor which sets name to a starting name and salary to a set amount. Additionally, the class should include methods to set the name and salary and return the name and salary. Create another method to return the name and salary nicely formatted as a string (hint – research the toString method).

You will create a new class called Manager that inherits from the class Employee. Add a field named department of type String. Supply a method toString that prints the manager's name, department, and salary. Remember, you may not change anything in the Employee class.

You will then create a test class that uses the Manager class.   The test class should prompt the user to enter name, department and salary. This information should be stored in an array. Upon entry of 3 employee records, the program should output the information you entered.

Your program should be able to handle a maximum of 3 entries.

You may write the program as a console application or using dialog boxes.

Solutions

Expert Solution

// Employee.java

public class Employee
{
   public String strName, strSalary;
  
   public Employee()
   {
       strName = " ";
       strSalary = "$0";
   }
  
   public Employee(String Name, String Salary)
   {
       strName = Name;
       strSalary = Salary;
   }
  
   public void setName(String Name)
   {
       strName = Name;
   }
  
   public void setSalary(String Salary)
   {
       strSalary = Salary;      
   }
  
   public String getName()
   {
       return strName;
   }
  
   public String getSalary()
   {
       return strSalary;
   }
  
   public String toString()
   {
       return(strName + " has a salary of " + strSalary);
   }

}

//end of Employee.java

// Manager.java

public class Manager extends Employee
{
   // department
   private String department ;
  
   // default constructor to set department to empty string
   public Manager()
   {
       super(); // calls Employee constructor
       department = "";
   }
  
   // parameterized constructor
   public Manager(String Name, String Salary, String Department)
   {
       super(Name, Salary); // calls Employee constructor
       department = Department; // sets department
   }
  
   // return String representation of Manager
   public String toString()
   {
       // return the string representation of employee and append the department
       return super.toString()+" works in department "+department;
   }
}

//end of Manager.java

// EmployeeTester.java

import java.util.Scanner;

public class EmployeeTester {

   public static void main(String[] args) {

       // create an array of 3 Employee
       Employee[] employees = new Employee[3];
       String name, salary, department;
       Scanner sc = new Scanner(System.in);
       // loop to input details of 3 Managers
       for(int i=0;i<employees.length;i++)
       {
           System.out.print("Enter Manager name: ");
           name = sc.nextLine();
           System.out.print("Enter Manager salary: ");
           salary = sc.nextLine();
           System.out.print("Enter Manager department: ");
           department = sc.nextLine();
           System.out.println();
           employees[i] = new Manager(name, salary, department);
       }
      
       // display the details of the managers entered
       System.out.println("Manager details:");
       for(int i=0;i<employees.length;i++)
           System.out.println(employees[i]);
   }

}

//end of EmployeeTester.java

Output:


Related Solutions

Class Employee (All IN JAVA) public class Employee {public String strName, strSalary; public Employee(){strName = "...
Class Employee (All IN JAVA) public class Employee {public String strName, strSalary; public Employee(){strName = " ";strSalary = "$0";} public Employee(String Name, String Salary){strName = Name;strSalary = Salary;} public void setName(String Name){strName = Name;} public void setSalary(String Salary){strSalary = Salary;}public String getName(){return strName;} public String getSalary(){return strSalary;} public String toString(){return(strName + " has a salary of " + strSalary); Create another method to return the name and salary nicely formatted as a string (hint – research the toString method). You...
21. Consider the code below: [13] Employee class: class Employee { public: Employee(string theName, float thePayRate);...
21. Consider the code below: [13] Employee class: class Employee { public: Employee(string theName, float thePayRate); protected: string getName() const; float getPayRate() const; float pay(float hoursWorked) const; private: string name; float payRate; }; Definitions for some of the methods follow: Employee::Employee(string theName, float thePayRate) { name = theName; payRate = thePayRate; } float Employee::pay(float hoursWorked) const { return hoursWorked * payRate; } Manager Class: #include "employee.h" class Manager : public Employee { public: Manager(string theName, float thePayRate, bool isSalaried); protected:...
Java public class UpperCaseString { //1      protected String content; // 2      public UpperCaseString(String content)...
Java public class UpperCaseString { //1      protected String content; // 2      public UpperCaseString(String content) { // 3           this.content = content.toUpperCase(); // 4      } // 5      public String toString() { // 6           return content.toUpperCase(); // 7      } // 8      public static void main(String[] args) { // 9           UpperCaseString upperString =              new UpperCaseString("Hello, Cleo!"); // 10           System.out.println(upperString); // 11      } // 12 } // 13 THE FOLLOWING 3 QUESTIONS REFER...
java programing Q: Given the following class: public class Student { private String firstName; private String...
java programing Q: Given the following class: public class Student { private String firstName; private String lastName; private int age; private University university; public Student(String firstName, String lastName, int age, University university) { this.firstName = fisrtName; this.lastName = lastName; this.age = age; this.university = university; } public String getFirstName(){ return firstName; } public String getLastName(){ return lastName; } public int getAge(){ return age; } public University getUniversity(){ return university; } public String toString() { return "\nFirst name:" + firstName +...
public class Book{     public String title;     public String author;     public int year;    ...
public class Book{     public String title;     public String author;     public int year;     public String publisher;     public double cost;            public Book(String title,String author,int year,String publisher,double cost){        this.title=title;         this.author=author;         this.year=year;         this.publisher=publisher;         this.cost=cost;     }     public String getTitle(){         return title;     }         public String getAuthor(){         return author;     }     public int getYear(){         return year;     }     public String getPublisher(){         return publisher;...
Write program in Java import java.util.Scanner; public class Lab7Program { public static void main(String[] args) {...
Write program in Java import java.util.Scanner; public class Lab7Program { public static void main(String[] args) { //1. Create a double array that can hold 10 values    //2. Invoke the outputArray method, the double array is the actual argument. //4. Initialize all array elements using random floating point numbers between 1.0 and 5.0, inclusive    //5. Invoke the outputArray method to display the contents of the array    //6. Set last element of the array with the value 5.5, use...
// problem2.java import java.util.*; public class problem_a { public static void main(String[] args) { // test...
// problem2.java import java.util.*; public class problem_a { public static void main(String[] args) { // test the smallest method System.out.print("smallest(1, 0, 2) -> "); System.out.println( smallest(1, 0, 2) ); // test the average method System.out.print("average(95, 85, 90) -> "); System.out.println( average(95, 84, 90) ); } // end main /* * smallest(double, double, double) -> double * * method is given 3 numbers, produces the smallest of the three * * examples: * smallest(1, 0, 2) -> 0.0 */ public static...
WRITE THIS JAVA CODE IN PSEUDOCODE!! import java.util.Scanner; public class License { public static void main(String[]...
WRITE THIS JAVA CODE IN PSEUDOCODE!! import java.util.Scanner; public class License { public static void main(String[] args) { char correctAnswers[] = {'B', 'D', 'A', 'A', 'C', 'A', 'B', 'A', 'C', 'D', 'B', 'C', 'D', 'A', 'D', 'C', 'C', 'B', 'D', 'A'}; char userAnswers[] = new char[correctAnswers.length]; Scanner scanner = new Scanner(System.in); for (int i = 0; i < userAnswers.length; i++) { String answer = ""; System.out.printf("Question #%d. Enter your answer( A, B, C or D): ", i + 1); do...
Suppose we have a Java class called Person.java public class Person { private String name; private...
Suppose we have a Java class called Person.java public class Person { private String name; private int age; public Person(String name, int age) { this.name = name; this.age = age; } public String getName(){return name;} public int getAge(){return age;} } (a) In the following main method which lines contain reflection calls? import java.lang.reflect.Field; public class TestPerson { public static void main(String args[]) throws Exception { Person person = new Person("Peter", 20); Field field = person.getClass().getDeclaredField("name"); field.setAccessible(true); field.set(person, "Paul"); } }...
java code ============ public class BankAccount { private String accountID; private double balance; /** Constructs a...
java code ============ public class BankAccount { private String accountID; private double balance; /** Constructs a bank account with a zero balance @param accountID - ID of the Account */ public BankAccount(String accountID) { balance = 0; this.accountID = accountID; } /** Constructs a bank account with a given balance @param initialBalance the initial balance @param accountID - ID of the Account */ public BankAccount(double initialBalance, String accountID) { this.accountID = accountID; balance = initialBalance; } /** * Returns the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT