Using java Create a class called Employee that includes 3 instance variables—a f
ID: 3697086 • Letter: U
Question
Using java Create a class called Employee that includes 3 instance variables—a first name (type String), a last name (type String), and a monthly salary (double). Provide a constructor that initializes the 3 instance variables. Provide a set and a get method for each instance variable. If the monthly salary is not positive, do not set its value. Write a test application named EmployeeTest that demonstrates class Employee’s capabilities. Create 2 Employee objects and display each object’s yearly salary. Then give each Employee a 10% raise and display each Employee’s yearly salary again.
This is the code that i recieved from the last person who answered this question:
When I try to run it in netbeans I get on getting the error message :Error: Could not find or load main class javaapplication51.JavaApplication51
import java.util.Scanner;
class Employee {
private String firstName;
private String lastName;
private double monthlySalary;
public Employee(String f, String l, double m){
firstName = f;
lastName = l;
if(m < 0){ // you can also use setMonthlySalary(m)
monthlySalary =0;
}
else {monthlySalary = m;}
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String fname) {
firstName = fname;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lname) {
lastName = lname;
}
public double getMonthlySalary() {
return monthlySalary;
}
public void setMonthlySalary(double m) {
if(m < 0){
monthlySalary =0;
}
else monthlySalary = m;
}
};
class EmployeeTest {
public static void main(String[] args){
Scanner S = new Scanner(System.in);
String fname;
String lname;
double sal,newsalary;
System.out.println("First Employee : ");
System.out.println("Enter the first name: ");
fname = S.next();
System.out.println("Enter the last name: ");
lname = S.next();
System.out.println("Enter the Salary: ");
sal = S.nextDouble();
Employee e1 =new Employee(fname,lname ,sal );
System.out.println("Second Employee : ");
System.out.println("Enter the first name: ");
fname = S.next();
System.out.println("Enter the last name: ");
lname = S.next();
System.out.println("Enter the Salary: ");
sal = S.nextDouble();
Employee e2 =new Employee(fname,lname ,sal );
System.out.println("the yearly salary of "+e1.getFirstName()+" "
+e1.getLastName()+" :");
System.out.println(e1.getMonthlySalary()*12);
newsalary= e1.getMonthlySalary()*0.1+e1.getMonthlySalary();
e1.setMonthlySalary(newsalary);
System.out.println("the new yearly salary of "+
e1.getFirstName()+" "+e1.getLastName()+" :");
System.out.println(e1.getMonthlySalary()*12);
e1.getMonthlySalary();
System.out.println("the yearly salary of "+e2.getFirstName()+" "
+e2.getLastName()+" :");
System.out.println(e2.getMonthlySalary()*12);
newsalary= e2.getMonthlySalary()*0.1+e2.getMonthlySalary();
e2.setMonthlySalary(newsalary);
System.out.println("the new yearly salary of "+
e2.getFirstName()+" "+e2.getLastName()+" :");
System.out.println(e2.getMonthlySalary()*12);
e2.getMonthlySalary();
}
}
Explanation / Answer
Hello there,
Please find below code, although the above one also works fine ,
The only problem would be you should have 2 java classes named Employee and EmployeeTest.
Include them in default package or if keeping in any package you would need to add this statement at the head of the file.
package yourpackagename;
in both the files.
I have added one method raiseSalary in Employee class to raise it's salary .
======
package assignment;
class Employee {
private String firstName;
private String lastName;
private double monthlySalary;
public Employee(String f, String l, double m) {
firstName = f;
lastName = l;
if (m < 0) { // you can also use setMonthlySalary(m)
monthlySalary = 0;
} else {
monthlySalary = m;
}
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String fname) {
firstName = fname;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lname) {
lastName = lname;
}
public double getMonthlySalary() {
return monthlySalary;
}
public void setMonthlySalary(double m) {
if (m < 0) {
monthlySalary = 0;
} else
monthlySalary = m;
}
public void raiseSalary(double perc) {
double newsalary = this.getMonthlySalary() * (perc / 100) + this.getMonthlySalary();
this.setMonthlySalary(newsalary);
}
}
package assignment;
import java.util.Scanner;
class EmployeeTest {
public static void main(String[] args) {
Scanner S = new Scanner(System.in);
String fname;
String lname;
double sal, newsalary;
System.out.println("First Employee : ");
System.out.println("Enter the first name: ");
fname = S.next();
System.out.println("Enter the last name: ");
lname = S.next();
System.out.println("Enter the Salary: ");
sal = S.nextDouble();
Employee e1 = new Employee(fname, lname, sal);
System.out.println("Second Employee : ");
System.out.println("Enter the first name: ");
fname = S.next();
System.out.println("Enter the last name: ");
lname = S.next();
System.out.println("Enter the Salary: ");
sal = S.nextDouble();
Employee e2 = new Employee(fname, lname, sal);
System.out.println("the yearly salary of " + e1.getFirstName() + " " + e1.getLastName() + " :");
System.out.println(e1.getMonthlySalary() * 12);
e1.raiseSalary(10);
System.out.println("After 10% hike the new yearly salary of " + e1.getFirstName() + " " + e1.getLastName() + " :");
System.out.println(e1.getMonthlySalary() * 12);
e1.getMonthlySalary();
System.out.println("the yearly salary of " + e2.getFirstName() + " " + e2.getLastName() + " :");
System.out.println(e2.getMonthlySalary() * 12);
e2.raiseSalary(10);
System.out.println("After 10% hike the new yearly salary of " + e2.getFirstName() + " " + e2.getLastName() + " :");
System.out.println(e2.getMonthlySalary() * 12);
e2.getMonthlySalary();
}
}
====O/P====
First Employee :
Enter the first name:
Dip
Enter the last name:
Pit
Enter the Salary:
45000
Second Employee :
Enter the first name:
Hem
Enter the last name:
Det
Enter the Salary:
27000
the yearly salary of Dip Pit :
540000.0
After 10% hike the new yearly salary of Dip Pit :
594000.0
the yearly salary of Hem Det :
324000.0
After 10% hike the new yearly salary of Hem Det :
356400.0
Let me know if you have any queires.
Thanks.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.