public class Person { // person name protected String name; // defualt construct
ID: 3536136 • Letter: P
Question
public class Person
{
// person name
protected String name;
// defualt constructor
public Person()
{
name ="No Name Yet";
}
// 1st argument constructors
public Person(String initialName)
{
name = initialName;
}
//Sets new name
public void setName (String newName)
{
name = newName;
}
//Returns person name
public String getName()
{
return name;
}
// inputs name to console
public void writeOutput()
{
System.out.println("Name : " + name);
}
//returns true in names are the same if not false
public boolean hasSameName(Person otherPerson)
{
return
this.name.equalsIgnoreCase(otherPerson.name);
}
}
// employee
public class Employee extends Person
{
//data members
private double annualSalary;
private int hiredYear;
private String ID;
// constructor with parameters
public Employee(String initialName, double initialSalary, int joinedYear, String id)
{
super(initialName);
annualSalary = initialSalary;
hiredYear = joinedYear;
ID = id;
}
//sets annual salary
public void setAnnualSalary(double newSalary)
{
annualSalary = newSalary;
}
//sets hired year
public void sethiredYear(int year)
{
hiredYear = year;
}
//sets employee id
public void setID(String newID)
{
ID =newID;
}
//returns annual salary
public double getAnnualSalary()
{
return annualSalary;
}
//returns hired year
public int getHiredYear()
{
return hiredYear;
}
// returns employee id
public String getID()
{
return ID;
}
public boolean equals(Employee otherEmployee)
{
if (name.equals(otherEmployee.name))
if (annualSalary == otherEmployee.annualSalary)
if(hiredYear == otherEmployee.hiredYear)
if(ID == otherEmployee.ID)
return true;
return false;
}
//prints the employee name salary hired yr and id
public void display()
{
System.out.println("Employee Name: " + name);
System.out.println("Employee Annual Salary: " + annualSalary);
System.out.println("Employee Hired Year: " + hiredYear);
System.out.println("Employee ID: " + ID);
System.out.println();
}
}
--------------------------------------------------------------------------------------------------------------------------------
public class TestEmployee
{
public static void main (String[] args)
{
// employee objects
Employee e1 = new Employee("John", 50000, 2004, "A101");
e1.display();
}
}
______________________________________________________________________
instead of inputing the data i want to beable to have the user input it all would like someone to help me with the coding to allow the
user to do so
Explanation / Answer
import java.util.*;
public class Person
{
// person name
protected String name;
// defualt constructor
public Person()
{
name ="No Name Yet";
}
// 1st argument constructors
public Person(String initialName)
{
name = initialName;
}
//Sets new name
public void setName (String newName)
{
name = newName;
}
//Returns person name
public String getName()
{
return name;
}
// inputs name to console
public void writeOutput()
{
System.out.println("Name : " + name);
}
//returns true in names are the same if not false
public boolean hasSameName(Person otherPerson)
{
return this.name.equalsIgnoreCase(otherPerson.name);
}
}
// employee
public class Employee extends Person
{
//data members
private double annualSalary;
private int hiredYear;
private String ID;
// constructor with parameters
public Employee(String initialName, double initialSalary, int joinedYear, String id)
{
super(initialName);
annualSalary = initialSalary;
hiredYear = joinedYear;
ID = id;
}
//sets annual salary
public void setAnnualSalary(double newSalary)
{
annualSalary = newSalary;
}
//sets hired year
public void sethiredYear(int year)
{
hiredYear = year;
}
//sets employee id
public void setID(String newID)
{
ID =newID;
}
//returns annual salary
public double getAnnualSalary()
{
return annualSalary;
}
//returns hired year
public int getHiredYear()
{
return hiredYear;
}
// returns employee id
public String getID()
{
return ID;
}
public boolean equals(Employee otherEmployee)
{
if (name.equals(otherEmployee.name))
if (annualSalary == otherEmployee.annualSalary)
if(hiredYear == otherEmployee.hiredYear)
if(ID == otherEmployee.ID)
return true;
return false;
}
//prints the employee name salary hired yr and id
public void display()
{
System.out.println();
System.out.println("Employee details are as follows");
System.out.println();
System.out.println("Employee Name: " + name);
System.out.println("Employee Annual Salary: " + annualSalary);
System.out.println("Employee Hired Year: " + hiredYear);
System.out.println("Employee ID: " + ID);
System.out.println();
}
}
public class TestEmployee
{
public static void main (String[] args)
{
// employee objects
Scanner in = new Scanner(System.in);
System.out.println("Enter name of employee");
String name = in.next();
System.out.println("Enter annual salary of employee");
int salary = in.nextInt();
System.out.println("Enter annual salary of employee");
int hiredyear = in.nextInt();
System.out.println("Enter name of employee");
String ID = in.next();
Employee e1 = new Employee(name,salary,hiredyear,ID);
e1.display();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.