I created my code. But I keep getting an error because of this message on the sc
ID: 3679895 • Letter: I
Question
I created my code. But I keep getting an error because of this message on the screen. How do I define EmployeeTest as its own file so the program can run ? ( I ALSO INCLUDED THE CODE )
public class Employee {
private String first;
private String last;
private double salary;
public Employee(String first, String last, double salary) {
this.first = first;
this.last = last;
if(salary > 0) {
this.salary = salary;
}
else {
salary = 0;
}
}
public String getFirst() {
return first;
}
public void setFirst(String first) {
this.first = first;
}
public String getLast() {
return last;
}
public void setLast(String last) {
this.last = last;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
if(salary > 0) {
this.salary = salary;
}
}
}
public class EmployeeTest {
public static void main(String[] args) {
Employee Employee("John","Smith",5000);
Employee two = new Employee("Joe","Schmoe",1250);
System.out.println(one.getFirst() + " " + one.getLast() + " makes $" + one.getSalary() + "/month");
System.out.println(two.getFirst() + " " + two.getLast() + " makes $" + two.getSalary() + "/month");
one.setSalary(one.getSalary() * 1.1);
two.setSalary(two.getSalary() * 1.1);
System.out.println(one.getFirst() + " " + one.getLast() + " makes $" + one.getSalary() + "/month");
System.out.println(two.getFirst() + " " + two.getLast() + " makes $" + two.getSalary() + "/month");
}
}
Explanation / Answer
Make Employee.java(does not contain main method) as seperate class file.and EmployeeTest.java(which contain main method) as a seperate class as below.Run EmployeeTest.java class then you can able to get the output.
Employee.java
package org.students;
public class Employee {
private String first;
private String last;
private double salary;
public Employee(String first, String last, double salary) {
this.first = first;
this.last = last;
if(salary > 0) {
this.salary = salary;
}
else {
salary = 0;
}
}
public String getFirst() {
return first;
}
public void setFirst(String first) {
this.first = first;
}
public String getLast() {
return last;
}
public void setLast(String last) {
this.last = last;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
if(salary > 0) {
this.salary = salary;
}
}
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
EmployeeTest.java
package org.students;
public class EmployeeTest {
public static void main(String[] args) {
Employee Employee("John","Smith",5000);
Employee two = new Employee("Joe","Schmoe",1250);
System.out.println(one.getFirst() + " " + one.getLast() + " makes $" + one.getSalary() + "/month");
System.out.println(two.getFirst() + " " + two.getLast() + " makes $" + two.getSalary() + "/month");
one.setSalary(one.getSalary() * 1.1);
two.setSalary(two.getSalary() * 1.1);
System.out.println(one.getFirst() + " " + one.getLast() + " makes $" + one.getSalary() + "/month");
System.out.println(two.getFirst() + " " + two.getLast() + " makes $" + two.getSalary() + "/month");
}
}
----------------------------------------------------------------------------------------------------------------------------------------------------------------
In other way you write like this to make the program execute as below.
package org.students;
public class Employee{
private String first;
private String last;
private double salary;
public Employee(String first, String last, double salary) {
this.first = first;
this.last = last;
if(salary > 0) {
this.salary = salary;
}
else {
salary = 0;
}
}
public String getFirst() {
return first;
}
public void setFirst(String first) {
this.first = first;
}
public String getLast() {
return last;
}
public void setLast(String last) {
this.last = last;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
if(salary > 0) {
this.salary = salary;
}
}
public static void main(String[] args) {
Employee Employee("John","Smith",5000);
Employee two = new Employee("Joe","Schmoe",1250);
System.out.println(one.getFirst() + " " + one.getLast() + " makes $" + one.getSalary() + "/month");
System.out.println(two.getFirst() + " " + two.getLast() + " makes $" + two.getSalary() + "/month");
one.setSalary(one.getSalary() * 1.1);
two.setSalary(two.getSalary() * 1.1);
System.out.println(one.getFirst() + " " + one.getLast() + " makes $" + one.getSalary() + "/month");
System.out.println(two.getFirst() + " " + two.getLast() + " makes $" + two.getSalary() + "/month");
}
}
--------------------------------------------------------------------------------------------------------------------------------------------------------------
Output:
John Smith makes $5000.0/month
Joe Schmoe makes $1250.0/month
John Smith makes $5500.0/month
Joe Schmoe makes $1375.0/month
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.