Based on the code below, I need to modify the code in the Employee class by creating a specialised print method called employee print() and show how it will be used to print in the employee Test class! Any help please? Here is the code:
class Employee1{
String empName;
String empNum;
String empEmailAddress;
int yearOfBirth;
}
public class employeeTest {
public static void main(String[] args) {
Employee1 emp1 = new Employee1();
emp1.empName = "Sam";
emp1.empNum = "213-23-978";
emp1.empEmailAddress = "sammy#company.za";
Employee1 emp2 = new Employee1();
emp2.empName = "Tasha";
emp2.empNum = "315-90-274";
emp2.yearOfBirth = 1982;
System.out.println("Employee Name: " + emp1.empName);
System.out.println("Employee Number: " + emp1.empNum);
System.out.println("Email Adress: " + emp1.empEmailAddress);
System.out.println("Year of Birth: " + emp1.yearOfBirth);
System.out.println("Employee Name: " + emp2.empName);
System.out.println("Employee Number: " + emp2.empNum);
System.out.println("Email Address: " + emp2.empEmailAddress);
System.out.println("Year of Birth: " + emp2.yearOfBirth);
}
}
You add a method called print to the employee class called print
class Employee1{
String empName;
String empNum;
String empEmailAddress;
int yearOfBirth;
public void print() {
System.out.println("Employee name:" + empName);
System.out.println("Employee number:" + empNum);
// etc
}
}
and call it like this:
// these lines replace the System.out.println block in your code
emp1.print();
emp2.print();
class Employee1
{
String empName;
String empNum;
String empEmailAddress;
int yearOfBirth;
#Override toString(){
System.out.println(empName + empNum + empEmailAddress + yearOfBirth);
}
}
emp.toString();
You can override toString() method in class Employee like this
class Employee1{
String empName;
String empNum;
String empEmailAddress;
int yearOfBirth;
#Override
public String toString(){
return empName + emNum +.....
}
}
And use this to print in EmployeeTest sysout(instanceOfEmployee)
Related
I checked the code and saving data to the HashMap is correct, when typing ADD. Then after choosing option FIND I can get to the dedicated function but the method is unable to show me found object even if it is correct 100%.
Please check this code out and tell me why it does not find right objects in "public void showInfo(String name, String secondName)" for class Company that is sparked by TYPING "FIND" in class CompanyApp
import java.util.InputMismatchException;
import java.util.Objects;
import java.util.Scanner;
public class CompanyApp {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
options[] values = options.values();
int choose;
int EXIT_NR = 2;
Company ref = new Company();
do {
System.out.println("Available options: ");
for (options one : values) {
System.out.println(one.getDescription() + " - " + one.name());
}
System.out.println("Choose one: ");
try {
choose = options.valueOf(in.nextLine()).ordinal();
if (Objects.equals(EXIT_NR, choose)) break;
if (choose < 0 || choose >= options.values().length) {
throw new IllegalArgumentException("Choose 0, 1 or 2!");
}
options(choose);
} catch (InputMismatchException e) {
System.out.println("Choose a number ");
}
} while (1 == 1);
}
static void options(int choose){
Company ref = new Company();
Scanner info = new Scanner(System.in);
switch (choose){
case 0:
System.out.println("Type in the name of the worker: ");
String name = info.nextLine();
System.out.println("Type in the second name of the worker: ");
String secondName = info.nextLine();
System.out.println("Type in the salary: ");
double salary = info.nextDouble();
info.nextLine();
ref.add(new Employee(name, secondName, salary));
break;
case 1:
System.out.println("Type in the name of the worker you want to find: ");
String name2 = info.nextLine();
System.out.println("Type in the second name of the worker you want to
find: ");
String secondName2 = info.nextLine();
ref.showInfo(name2, secondName2);
break;
}
}
}
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
public class Company {
private Map<String, Employee> map = new HashMap<>();
public void add(Employee employee){
String key = employee.getName() + " " + employee.getSecondName();
if(!map.containsKey(key)){
map.put(key, employee);
System.out.println("Added object to map");}
}
public void showInfo(String name, String secondName){
String key = name + " " + secondName;
System.out.println("in showinfo method");
if(map.containsKey(key)){
System.out.println("found an object");
Employee employee = map.get(key);
System.out.println(employee.getName());
}}}
enum options {
ADD("Add employee "), FIND("Find employee"), EXIT("Exit program");
private String description;
options(String description) {
this.description = description;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
#Override
public String toString() {
return "options{" +
"description='" + description + '\'' +
'}';
}
}
String name;
String secondName;
double salary;
public Employee(String name, String secondName, double salary) {
this.name = name;
this.secondName = secondName;
this.salary = salary;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSecondName() {
return secondName;
}
public void setSecondName(String secondName) {
this.secondName = secondName;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
#Override
public String toString() {
return "Employee{" +
"name='" + name + '\'' +
", secondName='" + secondName + '\'' +
", salary=" + salary +
'}';
}
}
The problem is in the method static void options(int choose). You need to pass the Company-Object and use it there like this:
Call from main method (ref is the Company-Object you create in the main method)
options(choose, ref);
The options-method with the Company as second parameter:
static void options(int choose, Company ref){
Scanner info = new Scanner(System.in);
switch (choose){
case 0:
System.out.println("Type in the name of the worker: ");
String name = info.nextLine();
System.out.println("Type in the second name of the worker: ");
String secondName = info.nextLine();
System.out.println("Type in the salary: ");
double salary = info.nextDouble();
info.nextLine();
//use the passed Company here
ref.add(new Employee(name, secondName, salary));
break;
case 1:
System.out.println("Type in the name of the worker you want to find: ");
String name2 = info.nextLine();
System.out.println("Type in the second name of the worker you want to find: ");
String secondName2 = info.nextLine();
//and here
ref.showInfo(name2, secondName2);
break;
}
}
Explanation what is happening in your code
As mentioned, the problem is in the method static void options(int choose).
Here you create a new Company-Object which is not passed in any way to the main method.
This is what happens, when you use ADD and a FIND afterwards:
Call options from main method with ADD
new Company-Object is created in options
new Employee-Object is added to the Company from the previous point
the method ends -> the created Company-Object is "thrown away" (eligible for Garbage Collection)
Call options from main method with FIND
new Company-Object is created in options(therefore no Employees in it)
no Employee can be found, because there is no entry in the map of the newly created Company
The map is empty at the time when you're trying to get the data from it using FIND option. The reason for that is you recreate the Company object in the options method:
Company ref = new Company();
At the same time also the map is recreated so there are no records inside.
Also, the Company object in the main method is not used.
I seem to be getting the constructor Employee in class Employee cannot be applied to given types; error and I'm not sure how to fix it. Im fairly new to coding so the simpler explanation the better.
method where the error occurs
specifically the "new Employee();" in the first line
Collections.sort(employees, new Employee());
System.out.println("\nFirst and Last Names of Employees sorted based on first Name\n");
for (Employee employee : employees) {
System.out.println("First Name: " + employee.getfirstName() + " Last Name: " + employee.getlastName()
+ " Salary: " + employee.getSalary());
}
List<Employee> newHirees = readFile(hirefile);
// Add new Hired employees
employees.addAll(newHirees);
Collections.sort(employees, new Employee());
System.out.println("\nFirst and Last Names of Employees sorted based on first Name after adding new hires\n");
for (Employee employee : employees) {
System.out.println("First Name: " + employee.getFirstName() + " Last Name: " + employee.getLastName()
+ " Salary: " + employee.getSalaryPaid());
}
List<Employee> firedEmployees = readFile(firefile);
System.out.println("\nFirst and Last Names of Employees After removing fired employees\n");
for (Employee fired : firedEmployees) {
String firstName=fired.getFirstName();
String lastname=fired.getLastName();
for (Employee employee : employees) {
if (employee.getFirstName().trim().equals(firstName)
&& employee.getLastName().equals(lastname)) {
employees.remove(employee);
}
}
}
for (Employee employee : employees) {
System.out.println("First Name: " + employee.getFirstName() + " Last Name: " + employee.getLastName());
}
}
//Employee class
public class Employee{
String firstName;
String lastName;
String gender;
int tenure;
String rate;
double salary;
public Employee( String firstName, String lastName,String gender,int tenure, String rate,double salary )
{
this.firstName=firstName;
this.lastName=lastName;
this.gender=gender;
this.tenure=tenure;
this.rate=rate;
this.salary=salary;
}
//get and set methods
public void setfirstName(String nm) {
this.firstName=nm;}
public void setlastName(String nm) {
this.lastName=nm;}
String getfirstName() {
return this.firstName;}
String getlastName() {
return this.lastName;}
public void setGender(String nm) {
this.gender=nm;}
public void setRate(String nm) {
this.rate=nm;}
String getGender(){
return this.gender;}
String getRate(){
return this.rate;}
void setSalary(double pr){
this.salary=pr;}
double getSalary(){
return this.salary;}
void setTenure(int q){
this.tenure=q;}
int getTenure(){
return this.tenure;}
public String toString(){
String s=this.firstName+" "+this.lastName+" "+this.gender+" "+this.tenure+" "+
this.rate+" "+this.salary;
return(s);
}
}
You don't have a parameterless constructor, so new Employee() won't work.
Why are you trying to pass that as a parameter there anyway?
I'm trying to make a student class to take in and print some values about a student, but I get a "Invalid Method Declaration" error on line 29 and I have no idea why.
I am trying to contain student creation data inside one method, and a tostring() method in another method.
public class Student {
int ID;
int Age;
String Name;
double AvgLogin;
public Student(int ID,int Age,String Name,double AvgLogin)
{
this.ID = ID;
this.Age = Age;
this.Name = Name;
this.AvgLogin = AvgLogin;
}
public printer(int ID,string Name,int Age,double AvgLogin) {
System.out.println("Age : " + Age + " Average Login time : " + AvgLogin + "ID :" + ID +" Name : " + Name);
} //end method
public int getID() {
return ID;
}
public String getName() {
return Name;
}
public int getAge() {
return Age;
}
public void setAge(int Age) {
this.Age = Age;
}
public double getAvgLogin() {
return AvgLogin;
}
public void setAvgLogin(double AvgLogin) {
this.AvgLogin = AvgLogin;
}
}
//Student John = new Student(ID,Age,Name,AvgLogin)
you just missed the return type for your printer method. You should use the following code for your printer method.
public void printer(int ID,String Name,int Age,double AvgLogin) {
System.out.println("Age : " + Age + " Average Login time : " + AvgLogin + "ID :" + ID +" Name : " + Name);
}
You have two errors in your method. One of them is lack of the return type. Second one is typing string instead of String. Thirdly, it looks nice if you start variable name from lower case. You should also format code before making a question.
public void printer(int id, String name, int age, double avgLogin) {
System.out.println("Age: " + age + ", average login time: " + avgLogin + ", ID: " + id + ", name: " + name);
}
I have a practice problem that I need to complete and have done everything however I cannot get the output to match whats needed. I have tried some of the google answers but nothing seems to be working. Below is the code and the output I get vs what I want. We are not allowed to modify the main method but only the classes.
I am just confused on how to make the output from each class start on a new line.
There is this statement in the instructions but I don't understand how to go about it:
the Student class should have a public display function that calls the parent class’ display
function,
Code:
public class H255{public static void main (String[] args){while (JPL.test()){
Person pObj = new Person("Albert","Einstein");
Student sObj = new Student("John","Smith",123456,"First Year","Pullan");
Teacher tObj = new Teacher("Wayne","Pullan","Computer Science",100000,"Lecturer");
System.out.println("Person :");
pObj.Display();
System.out.println("");
System.out.println("Student :");
sObj.Display();
System.out.println("");
System.out.println("Teacher :");
tObj.Display();
}}}
class Person{
private String FirstName;
private String LastName;
public Person(String fName, String lName){
this.FirstName = fName;
this.LastName = lName;
}
public void Display(){
System.out.println("First Name: " + FirstName + " Last Name: " + LastName);
}
}
class Student extends Person{
private int id;
private String standard;
private String instructor;
public Student(String fName, String lName, int nId, String stnd, String instr){
super(fName, lName);
this.id = nId;
this.standard = stnd;
this.instructor = instr;
}
public void Display(){
System.out.println("ID: " + id + "Standard: " + standard + "Instructor: " + instructor);
}
}
class Teacher extends Person{
private String mainSubject;
private int salary;
private String type;
public Teacher(String fName, String lName, String sub, int slry, String sType){
super(fName, lName);
this.mainSubject = sub;
this.salary = slry;
this.type = sType;
}
public void Display(){
System.out.println("Main Subject: " + mainSubject + "Salary: "
+ salary + "Type: " + type );
}
}
Output:
the writing of main method like these code:
System.out.print("Person :");
pObj.Display();
System.out.print("Student :");
sObj.Display();
System.out.print("Teacher :");
tObj.Display();
because:the println method has a build in wrap feature, so just replace println with print.
Based on one of the Head First books examples I'm having a little trouble in which the toString method is causing issues with my student and they're uni and home address being outputted correctly. All i'm trying to do is output if a students uni address is empty use his home address else use the uni one.
But my test data looks like the following
John John72 Nottingham Drive
John72 Nottingham Drive
public class Test {
public static void main(String[] args){
Student student1 = new Student("John", 19, "Walkers Way");
student1.setUniAddress(72, "Nottingham Drive");
System.out.print(student1.toString());
}
}
Other Class
public class Student {
private String name;
private Address homeAddress, uniAddress;
public Student(String name, int houseNumber, String homeStreet){
this.name = name;
this.homeAddress = new Address(houseNumber, homeStreet);
}
public String getName() { return this.name; }
public Address getHomeAddress(){
if(this.uniAddress == null){
return this.homeAddress;
}else{
return getUniAddress();//this.uniAddress;
}
}
public Address getUniAddress() { return this.uniAddress; }
public void setUniAddress(int number, String add){
Address address = new Address(number, add);
uniAddress = address;
}
#Override
public String toString(){
return getName() + " " + getHomeAddress() + " " + getUniAddress() + "\n";
}
public class Address{
private int number;
private String street;
public Address(int no, String street){
this.number = no;
this.street = street;
}
#Override
public String toString(){
return name + number + " " + street + "\n";
}
}
}
Your getHomeAddress method takes care of displaying the uni address, so this line:
return getName() + " " + getHomeAddress() + " " + getUniAddress() + "\n";
can be shortened to:
return getName() + " " + getHomeAddress() + " " + "\n";
Otherwise your getHomeAddress method will pull the uni address, then your getUniAddress method will pull the uni address again.
Also in your address toString you are pulling the person's name and you probably didn't mean to (and you might not want a newline here either since you have a newline in your other toString method).
#Override
public String toString(){
return number + " " + street;
}