Question

Having some trouble with this Java homework assignment In this assignment you will be processing employee...

Having some trouble with this Java homework assignment

In this assignment you will be processing employee data. You have a number of employees (that number determined by user input). You will be recording each employee’s name, salary, and telephone number, as well as any other data you need or think is appropriate. It is important that you record the employee telephone number in valid form, therefore you will develop two exception classes. You will be developing the following classes: The class Employee which will be derived from the class Person (linked here: Person.java).Preview the document The class Employee should have constructors, accessor methods, mutator methods a toSting method, an equals method and any additional methods you feel necessary. Every Employee object should record the employee’s name, salary, and telephone number, as well as any other data you need or think is appropriate. The class TelephoneNumberLengthException for when the telephone number entered—without dashes or spaces—is not exactly ten digits. When an exception is thrown, the user should be reminded of what she or he entered, told why it is inappropriate, and asked to reenter the data. The class TelephoneNumberCharacterException for when any character in the telephone number is not a digit. When an exception is thrown, the user should be reminded of what she or he entered, told why it is inappropriate, and asked to reenter the data. The class DemoTelephoneNumber_yourInitials.java (where yourInitials represent your initials), a driver a program to enter employee data, including telephone number and salary, into an array. The maximum number of employees is 100, but your program should also work for any number of employees less than 100. After all data has been entered, your program should display the records for all employees, with an annotation stating whether the employee’s salary is above or below average. Submit all 4 files.

Person.java file is

public class Person

{

private String name;

public Person()

{

name = "No name yet.";

}

public Person(String initialName)

{

name = initialName;

}

public void setName(String newName)

{

name = newName;

}

public String getName()

{

return name;

}

public void writeOutput()

{

System.out.println("Name: " + name);

}

public boolean hasSameName(Person otherPerson)

{

return (this.name.equalsIgnoreCase(otherPerson.getName()));

}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

====================

JAVA PROGRAM

====================

/////////////Person.java////////////////

package test.emp;

//Given Class Person
public class Person

{

   private String name;
  
   public Person(){
       name = "No name yet.";  
   }
  
   public Person(String initialName){
       name = initialName;
   }
  
   public void setName(String newName){  
       name = newName;
   }
  
   public String getName(){
       return name;
   }
  
   public void writeOutput(){
       System.out.println("Name: " + name);
   }
  
   public boolean hasSameName(Person otherPerson){
       return (this.name.equalsIgnoreCase(otherPerson.getName()));
   }

}

//////////////////////////Employee.java///////////////////////////

package test.emp;

//Class: Employee
public class Employee extends Person{
   //private fields
   private double salary;
   private String telephoneNumber;
  
   //constructors
   public Employee() {
       super();
   }

   public Employee(String initialName) {
       super(initialName);
   }

   public Employee(double salary, String telephoneNumber)
           throws TelephoneNumberCharacterException, TelephoneNumberLengthException {
       super();
       this.salary = salary;
       setTelephoneNumber(telephoneNumber);
   }
  
   public Employee(String name, double salary, String telephoneNumber)
           throws TelephoneNumberCharacterException, TelephoneNumberLengthException {
       super(name);
       this.salary = salary;
       setTelephoneNumber(telephoneNumber);
   }

   //getters and setters
  
   public double getSalary() {
       return salary;
   }

   public void setSalary(double salary) {
       this.salary = salary;
   }

   public String getTelephoneNumber() {
       return telephoneNumber;
   }
   //setTelephone number
   public void setTelephoneNumber(String telephoneNumber)
           throws TelephoneNumberCharacterException,TelephoneNumberLengthException {
       String tempTelNum = telephoneNumber;
       //replace - and space with ""
       if(tempTelNum!=null){
           tempTelNum = tempTelNum.replace("-", "");
           tempTelNum = tempTelNum.replace(" ", "");
       }
       //check if all characters of telephone are digit or not
       for(char ch: tempTelNum.toCharArray()){
           if(!Character.isDigit(ch)){
               throw new TelephoneNumberCharacterException("Character in telephone number is not a digit.");
           }
       }
       //check if telephone number is of 10 digit
       if(tempTelNum.length()!=10){
           throw new TelephoneNumberLengthException("Telephone number is not of 10 digit.");
       }
      
       //set telephone number
       this.telephoneNumber = tempTelNum;
   }

   @Override
   public String toString() {
       return "Name = " + getName() +", salary=" + salary + ", telephoneNumber=" + telephoneNumber ;
   }
  
   /**
   * Returns if employee slary is below avg salary
   * @param avgSalary
   * @return
   */
   public boolean isBelowAverage(double avgSalary){
       if(this.salary < avgSalary)
           return true;
       return false;
   }
  
  
}

///////////////////////TelephoneNumberLengthException.java///////////////////////////

package test.emp;

//TelephoneNumberLengthException class
public class TelephoneNumberLengthException extends Exception{
   private static final long serialVersionUID = 526844318754237282L;
  
   public TelephoneNumberLengthException(String msg){
       super(msg);
   }
}

///////////////////////////TelephoneNumberCharacterException .java///////////////////////

package test.emp;

//TelephoneNumberCharacterException class
public class TelephoneNumberCharacterException extends Exception{
   private static final long serialVersionUID = -8070146771275401191L;
  
   public TelephoneNumberCharacterException(String msg){
       super(msg);
   }
}

////////////////////////////DemoTelephoneNumber_AM.java////////////////////

package test.emp;

import java.text.DecimalFormat;
import java.util.Scanner;

//Driver class: DemoTelephoneNumber_AM
public class DemoTelephoneNumber_AM {
  
   //mainMethod
   public static void main(String[] args) {
       //employee array with capacity of holding 100 employee data
       Employee[] emps = new Employee[100];
       Scanner sc = new Scanner(System.in);//create scanner
       System.out.println("Add employee Info:");
       int sumOfSalary = 0;//to hold running sum of salary
       int counter = 0;//count exact number of employee records added in array
       //start infinite while loop
       while(true){
           System.out.println("Name: ");
           String name = sc.nextLine();//read name
           Employee e = new Employee(name);
           System.out.println("Salary: ");
           double salary = Double.parseDouble(sc.nextLine());//read and parse salary into double
           e.setSalary(salary);
          
           sumOfSalary += salary;//calculate running sum of salary
           //read relephone number as long as it is not valid
           do{
               System.out.println("Telephone: ");
               String tel = sc.nextLine();
               try{
                   e.setTelephoneNumber(tel);
                   break;
               }catch(TelephoneNumberCharacterException te1){
                   System.out.println(te1.getMessage()+" Please try again!");
                  
               }catch(TelephoneNumberLengthException te2){
                   System.out.println(te2.getMessage()+" Please try again!");
               }
           }while(true);
          
           emps[counter++] = e;//add employee in array
          
           //check if another employee needs to be added in array or not
           System.out.println("Press 0 to Exit or any other key to add another:");
           String input = sc.nextLine();
           if(input.equals("0")){
               sc.close();
               break;
           }
       }
      
       //calculate and print average salary
       double avgSalary = sumOfSalary/counter;
       System.out.println("average salary is = "+ new DecimalFormat("#.00").format(avgSalary));
      
      
       System.out.println();
       System.out.println("Printing Employee info: ");
       System.out.println();
       //print employee info
       for(int i = 0 ;i < counter; i++){
           Employee e = emps[i];
           System.out.print(e);
           if(e.isBelowAverage(avgSalary)){
               System.out.print(" : Below Average Salary.");
           }else{
               System.out.print(" : Above Average Salary.");
           }
           System.out.println();
       }

   }

}

==============================

OUTPUT

==============================

Add employee Info:
Name:
Suresh
Salary:
12456
Telephone:
123-456-7890-111-222
Telephone number is not of 10 digit. Please try again!
Telephone:
111-222-3344
Press 0 to Exit or any other key to add another:
1
Name:
Vijay
Salary:
21030
Telephone:
222-aaa-2222
Character in telephone number is not a digit. Please try again!
Telephone:
222-333-2222
Press 0 to Exit or any other key to add another:
1
Name:
Harry
Salary:
19546
Telephone:
2135468797
Press 0 to Exit or any other key to add another:
1
Name:
Peter
Salary:
21021
Telephone:
1122 33 44 55
Press 0 to Exit or any other key to add another:
0
average salary is = 18513.00

Printing Employee info:

Name = Suresh, salary=12456.0, telephoneNumber=1112223344 : Below Average Salary.
Name = Vijay, salary=21030.0, telephoneNumber=2223332222 : Above Average Salary.
Name = Harry, salary=19546.0, telephoneNumber=2135468797 : Above Average Salary.
Name = Peter, salary=21021.0, telephoneNumber=1122334455 : Above Average Salary.

Add a comment
Know the answer?
Add Answer to:
Having some trouble with this Java homework assignment In this assignment you will be processing employee...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT