Question

In: Computer Science

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

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

}

********************************** Manager.java *************************************************

public class Manager extends Employee{
   public String department;
   public Manager(String name,String salary,String department){
       super(name,salary);
       this.department = department;
   }
  
   public void setDepartment(String department) {
       this.department = department;
   }
  
   public String getDepartment() {
       return this.department;
   }
  
   public String toString(){
       return(super.strName +" manager of department "+this.department+ " has a salary of " + super.strSalary);
   }
  
}

*********************************** TestDriver.java ***************************************

import java.util.Scanner;
public class TestDriver {

   public static void main(String[] args) {
       Scanner s = new Scanner(System.in);
       int i = 1;
       int n = s.nextInt();
       s.nextLine();
       Manager[] arr;
       if(n<=3)
       arr = new Manager[n];
       else
       arr = new Manager[3];
      
       while(i<=arr.length) {
           System.out.println("Enter "+i+"th manager's name: ");
           String name = s.nextLine();
          
           System.out.println("Enter "+i+"th manager's salary: ");
           String salary = s.nextLine();
          
           System.out.print("Enter "+i+"th manager's department: ");
           String department = s.nextLine();
          
           arr[i-1] = new Manager(name,salary,department);
           i++;
       }
      
       i=0;
       while(i<arr.length) {
           System.out.println(arr[i].toString());
           i++;
       }
   }

}

************************************ Console_Output ScreenShot *******************************************

If you will face any doubt in code, let me know through comment.

Thanks :)


Related Solutions

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 +...
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...
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...
DESCRIBE WHAT THE FOLLOWING JAVA CODE DOES: public class Main{ public static void main(String[] args) {...
DESCRIBE WHAT THE FOLLOWING JAVA CODE DOES: public class Main{ public static void main(String[] args) { new MyFrame(); } } import javax.swing.*; public class MyFrame extends JFrame{ MyPanel panel; MyFrame(){ panel = new MyPanel(); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.add(panel); this.pack(); this.setLocationRelativeTo(null); this.setVisible(true); } } import java.awt.*; import javax.swing.*; public class MyPanel extends JPanel{ //Image image; MyPanel(){ //image = new ImageIcon("sky.png").getImage(); this.setPreferredSize(new Dimension(500,500)); } public void paint(Graphics g) { Graphics2D g2D = (Graphics2D) g; //g2D.drawImage(image, 0, 0, null); g2D.setPaint(Color.blue); g2D.setStroke(new BasicStroke(5)); g2D.drawLine(0, 0, 500,...
In Java, Here is a basic Name class. class Name { private String first; private String...
In Java, Here is a basic Name class. class Name { private String first; private String last; public Name(String first, String last) { this.first = first; this.last = last; } public boolean equals(Name other) { return this.first.equals(other.first) && this.last.equals(other.last); } } Assume we have a program (in another file) that uses this class. As part of the program, we need to write a method with the following header: public static boolean exists(Name[] names, int numNames, Name name) The goal of...
INSERT STRING INTO SEPARATE CHAIN HASHTABLE & ITERATE THROUGH HASHTABLE: JAVA _________________________________________________________________________________________________________________ import java.util.*; public class...
INSERT STRING INTO SEPARATE CHAIN HASHTABLE & ITERATE THROUGH HASHTABLE: JAVA _________________________________________________________________________________________________________________ import java.util.*; public class HashTable implements IHash { // Method of hashing private HashFunction hasher; // Hash table : an ArrayList of "buckets", which store the actual strings private ArrayList<List<String>> hashTable; /** * Number of Elements */ private int numberOfElements; private int numberOfBuckets; /** * Initializes a new instance of HashTable. * <p> * Initialize the instance variables. <br> * Note: when initializing the hashTable, make sure to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT