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

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...
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...
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...
public class Graph { private ST<String, SET<String>> st; public Graph() { st = new ST<String, SET<String>>();...
public class Graph { private ST<String, SET<String>> st; public Graph() { st = new ST<String, SET<String>>(); } public void addEdge(String v, String w) { // Put v in w's SET and w in v's SET. if (!st.contains(v)) st.put(v, new SET<String>()); if (!st.contains(w)) st.put(w, new SET<String>()); st.get(v).add(w); st.get(w).add(v); } public Iterable<String> adjacentTo(String v) { return st.get(v); } public Iterable<String> vertices() { return st.keys(); } // See Exercises 4.5.1-4 for V(), E(), degree(), // hasVertex(), and hasEdge(). public static void main(String[] args)...
In Java: Design a class that checks if a String is made of tokens of the...
In Java: Design a class that checks if a String is made of tokens of the same data type (for this, you may only consider four data types: boolean, int, double, or char). This class has two instance variables: the String of data and its delimiter. Other than the constructor, you should include a method, checkTokens, that takes one parameter, representing the data type expected (for example, 0 could represent boolean, 1 could represent int, 2 could represent double, and...
Write in Java: Write a method called: public static String[] noIdenticalCombine(String[] array1, String[] array2) { //...
Write in Java: Write a method called: public static String[] noIdenticalCombine(String[] array1, String[] array2) { // instructions: returns an array that contains all the Strings in array1 and array2 but without repetition. order does not matter, but it will return array1's elements and then array2's element that are not in array1. Assume there are no duplicates are in array1 and array2. Could use count which is how many str there are in array2, where !contains(array1, str). May an array of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT