Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

public class Employee { private String fullName; private double monthlySalary; private double annualSalary; //initialize constructor name public Employee (String fullName, double monthlySalary, double annualSalary){ this.fullName=fullName;

public class Employee { private String fullName; private double monthlySalary; private double annualSalary;

//initialize constructor name

public Employee (String fullName, double monthlySalary, double annualSalary){ this.fullName=fullName; this.monthlySalary=monthlySalary; this.annualSalary=annualSalary; } public double annualSalary() { return monthlySalary*12; } //Override the toString method @Override public String toString() { return "Full Name:"+ fullName + " Monthly Salary:" + monthlySalary; } }

//The second subclass is Executive

public class Executive extends Employee{

private double stockprice;

private final int Bonus=30000;

//initialize constructor name public Executive(String fullName, double monthlySalary, double stockprice) { super(fullName,monthlySalary); this.stockprice=stockprice; } public double annualSalary() { if(stockprice>50) return monthlySalary*12+ Bonus; else return monthlySalary*12; } @Override public String toString() { return "fullName : "+ fullName+" Monthly Salary : "+ monthlySalary + "Stock Price : " + stockprice; }

}

public class Salesman extends Employee{ private double annualSales; private double commissionRate; public Salesman(String fullName, double monthlySalary, double commissionRate, double annualSales){

super(fullName, monthlySalary); this.annualSales=annualSales; } public double annualSalary() { double annualSalary=monthlySalary*12; return annualSalary+(annualSales*commissionRate); } /**Override the toString method*/ @Override public String toString() { return "fullName :"+ fullName+ " Monthly Salary : "+monthlySalary+ " Commission Rate: "+commissionRate+ " Annual Sales :"+ annualSales; } }

import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner;

//Main Method public class PrintEmployee{ public static void main(String[] args) { final int count=10; int countEmp=0; Employee[] employ1=new Employee[count]; int countComm=0; Salesman[] sale=new Salesman[count]; int execCount=0; Executive[] exec=new Executive[count]; int countEmp1=0; Employee[] employ2=new Employee[count]; int countComm1=0; Salesman[] sale1=new Salesman[count]; int execCount1=0; Executive[] exec1=new Executive[count]; String fileName="Employee data.txt"; Scanner fileScanner=null; double salary; double commission; double sales; double stock;

try { fileScanner=new Scanner(new File(fileName)); while(fileScanner.hasNextLine()) {

int year=fileScanner.nextInt(); String workers=fileScanner.next(); if(workers.equals("Employee") &year==2014) { String fullName=fileScanner.next(); salary=fileScanner.nextDouble(); employ1[countEmp]=new Employee(fullName, salary); countEmp++; } else if(workers.equals("Employee") &year==2015) { String fullName=fileScanner.next(); salary=fileScanner.nextDouble(); employ2[countEmp]=new Employee(fullName, salary); countEmp1++; } else if(workers.equals("Based on commission")&year==2014) { String fullName=fileScanner.next(); salary=fileScanner.nextDouble(); commission=fileScanner.nextDouble(); sales=fileScanner.nextDouble(); sale[countComm]= new Salesman(fullName, salary, commission, sales); countComm++; } else if(workers.equals("Based on Commission")&year==2015) { String fullName=fileScanner.next(); salary=fileScanner.nextDouble(); commission=fileScanner.nextDouble(); sales=fileScanner.nextDouble(); sale1[countComm1]= new Salesman(fullName, salary, commission, sales); countComm1++; } else if(workers.equals("Executive")&year==2014) { String fullName=fileScanner.next(); salary=fileScanner.nextDouble(); stock=fileScanner.nextDouble(); exec[execCount]=new Executive(fullName, salary, stock); execCount++; } else if(workers.equals("Executive")&year==2015) { String fullName=fileScanner.next(); salary=fileScanner.nextDouble(); stock=fileScanner.nextDouble(); exec1[execCount1]=new Executive(fullName, salary, stock); execCount1++; } } } catch (Exception e) { System.out.println(e); } double avgOf2014=0; System.out.println("Year - 2014 : "); for (int j = 0; j< countEmp; j++) { System.out.println(employ1[j].toString()+ " The annual salary : "+employ1[j].annualSalary()); avgOf2014+=employ1[j].annualSalary(); } for (int j = 0; j < countComm; j++) { System.out.println(sale[j].toString()+ " The annual salary : "+sale[j].annualSalary()); avgOf2014+=sale[j].annualSalary(); } for (int j = 0; j< execCount; j++) { System.out.println(exec[j].toString()+ " The annual salary : "+exec[j].annualSalary()); avgOf2014+=exec[j].annualSalary(); } // average of all salaries System.out.println("Average of 2014 salaries : " +avgOf2014/(countEmp+countComm+execCount)); //salary average of 2015 double avgOf2015=0; System.out.println("Year - 2015 : "); for (int j = 0; j < countEmp1; j++) { System.out.println(employ2[j].toString()+ " Annual Salary : "+employ2[j].annualSalary()); avgOf2015+=employ2[j].annualSalary(); } for (int j = 0; j < countComm1; j++) { System.out.println(sale1[j].toString()+ " The annual salary : "+sale1[j].annualSalary()); avgOf2015+=sale1[j].annualSalary(); } for (int j = 0; j < execCount1; j++) { System.out.println(exec1[j].toString()+ " The annual salary : "+exec1[j].annualSalary()); avgOf2015+=exec1[j].annualSalary(); } System.out.println("The average of 2015 salaries : " +avgOf2015/(countEmp1+countComm1+execCount1)); } }

************Please help me compile it

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Formal SQL Tuning For Oracle Databases Practical Efficiency Efficient Practice

Authors: Leonid Nossov ,Hanno Ernst ,Victor Chupis

1st Edition

3662570564, 978-3662570562

More Books

Students also viewed these Databases questions