Skip to content

Instantly share code, notes, and snippets.

@SamuelSchwent
Created March 3, 2016 15:19
Show Gist options
  • Select an option

  • Save SamuelSchwent/20c550ee904caec29202 to your computer and use it in GitHub Desktop.

Select an option

Save SamuelSchwent/20c550ee904caec29202 to your computer and use it in GitHub Desktop.
Java - Overriding toString and this Example
package Challenge6;
public class DisplayEmployee {
public static void main(String[] args) {
//Ch3 Challenge 1 Get/Set & constructors
/*
Employee employee1 = new Employee();
Employee employee2 = new Employee();
Employee employee3 = new Employee();
employee1.setName("Susan Meyers");
employee1.setIdNumber(47899);
employee1.setDepartment("Accounting");
employee1.setPosition("Vice President");
employee2.setName("Mark Jones");
employee2.setIdNumber(39119);
employee2.setDepartment("IT");
employee2.setPosition("Programmer");
employee3.setName("Joy Rogers");
employee3.setIdNumber(81774);
employee3.setDepartment("Manufacturing");
employee3.setPosition("Engineer");
*/
//Ch6 Challenge 6 Constructors
Employee employee1 = new Employee("Susan Meyers", 47899, "Accounting", "VicePresident");
Employee employee2 = new Employee("Mark Jones", 39119);
Employee employee3 = new Employee();
System.out.println("Name: " + employee1.getName());
System.out.println("ID Number: " + employee1.getIdNumber());
System.out.println("Department: " + employee1.getDepartment());
System.out.println("Position: " + employee1.getPosition());
System.out.println(employee2.toString());
System.out.println("Name: " + employee3.getName());
System.out.println("ID Number: " + employee3.getIdNumber());
System.out.println("Department: " + employee3.getDepartment());
System.out.println("Position: " + employee3.getPosition());
}
}
package Challenge6;
public class Employee {
String name;
int idNumber;
String department;
String position;
public Employee(String name, int idNumber, String department, String position)
{
this.name = name;
this.idNumber = idNumber;
this.department = department;
this.position = position;
}
public Employee(String name, int idNumber)
{
this.name = name;
this.idNumber = idNumber;
department = "";
position = "";
}
public Employee()
{
name = "";
idNumber = 0;
department = "";
position = "";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getIdNumber() {
return idNumber;
}
public void setIdNumber(int idNumber) {
this.idNumber = idNumber;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
public String getPosition() {
return position;
}
public void setPosition(String position) {
this.position = position;
}
public String toString()
{
return ("Name: " + name + "\nID: " + idNumber + "\nDepartment: " + department + "\nPosition: " + position)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment