So exactly what are you having trouble with? Sorting employee's by age, ??
Was bored and ended up programming it for you. I didn't realize that I Eat Pixels already did it for you until after I programmed it. Anyways, heres the code:
PHP Code
<?php package net.hackforums;
/** * @author GunzPk * @version 1.0 */ public class Company {
/** * The starting point of the application. * * @param args The command-line arguments. */ public static void main(String[] args) { Department department = new Department(); department.sortEmployees(); department.getEmployees(); }
}
PHP Code
<?php package net.hackforums;
/** * @author GunzPk * @version 1.0 */ public class Department {
/** * Contains & initializes all of the employee objects. */ private Employee[] employee = { new Employee("Accounting", "Joyce", 65), new Employee("Accounting", "Jake", 32), new Employee("Accounting", "Henry", 54), new Employee("Accounting", "Chris", 20), new Employee("Accounting", "Amy", 73) };
/** * Sorts out all of the employee's information in the employee object. */ public void sortEmployees() { String tempDepartment; String tempName; int tempAge;
boolean isSorting = true;
while (isSorting) { isSorting = false;
for (int i = 0; i < employee.length - 1; i++) { if (employee[i].getAge() < employee[i + 1].getAge()) { tempDepartment = employee[i].getDepartment(); tempName = employee[i].getName(); tempAge = employee[i].getAge();
employee[i].setDepartment(employee[i + 1].getDepartment()); employee[i].setName(employee[i + 1].getName()); employee[i].setAge(employee[i + 1].getAge());
employee[i + 1].setDepartment(tempDepartment); employee[i + 1].setName(tempName); employee[i + 1].setAge(tempAge);
isSorting = true; } } } }
/** * Prints out all of the employees information. */ public void getEmployees() { for (int i = 0; i < employee.length; i++) { System.out.println(employee[i].toString()); } } }
PHP Code
<?php package net.hackforums;
/** * @author GunzPk * @version 1.0 */ public class Employee {
/** * The employee's department. */ private String department;
/** * The employee's name. */ private String name;
/** * The employee's age. */ private int age;
/** * The Employee constructor. Assigns all the necessary variables with the * given parameters. * * @param department The employee's department. * @param name The employee's name. * @param age The employee's age. */ public Employee(String department, String name, int age) { this.setDepartment(department); this.setName(name); this.setAge(age); }
/** * Sets the department variable with the given parameter. * * @param department The department of the employee. */ public void setDepartment(String department) { this.department = department; }
/** * Sets the name variable with the given parameter. * * @param name The name of the employee. */ public void setName(String name) { this.name = name; }
/** * Sets the age variable with the given parameter. * * @param age The age of the employee. */ public void setAge(int age) { this.age = age; }
/** * Gets the employee's department. * * @return The employee's department. */ public String getDepartment() { return this.department; }
/** * Gets the employee's name. * * @return The employee's name. */ public String getName() { return this.name; }
/** * Gets the employee's age. * * @return The employee's age. */ public int getAge() { return this.age; }
/** * Returns the employee's information. */ @Override public String toString() { return this.getDepartment() + " - " + this.getName() + " " + this.getAge() + " " + PrimeAgeChecker.isPrimeNumber(this);
} }
PHP Code
<?php package net.hackforums;
/** * @author GunzPk * @version 1.0 */ public class PrimeAgeChecker {
/** * Returns whether or not the employee's age is a prime number. * * @param employee The employee object which holds their department, name, and age. * @return Whether or not the employee's age is a prime number. */ public static boolean isPrimeNumber(Employee employee) {
if (employee.getAge() % 2 == 0) { return false; }
for (int i = 3; i * i <= employee.getAge(); i += 2) { if (employee.getAge() % i == 0) { return false; } } return true; } }
output:
PHP Code
<?php Accounting - Amy 73 true Accounting - Joyce 65 false Accounting - Henry 54 false Accounting - Jake 32 false Accounting - Chris 20 false
Heres a download link if you'd rather download the project folder: http://uppit.com/ivys6f37wt84/Program.rar
I have a followup question to this if the thread contributors are still active - what if the value of Department should be only one of a list of values, rather than always being "Accounting"?
(Apr 27, 2014 07:41 PM)reasondefies Wrote: I have a followup question to this if the thread contributors are still active - what if the value of Department should be only one of a list of values, rather than always being "Accounting"?
Quote: private Employee[] employee = {
new Employee("Accounting", "Joyce", 65),
new Employee("Accounting", "Jake", 32),
new Employee("Accounting", "Henry", 54),
new Employee("Accounting", "Chris", 20),
new Employee("Accounting", "Amy", 73)
};
By changing the text above that is in bold, you'll be changing the value of the Department variable 😝
Yeah, copy and pasting source code for a homework assignment really helps you understand... totally. For sure yeah best way to learn.
You will get really far when you don't understand anything. Yeah totally.
(Apr 29, 2014 09:17 PM)Shellow5 Wrote: Yeah, copy and pasting source code for a homework assignment really helps you understand... totally. For sure yeah best way to learn.
You will get really far when you don't understand anything. Yeah totally.
It's ok if you want to assume that is what's happening here, and I can't speak for the original poster - but as someone who is working toward being able to complete an assignment very similar to this one as a pre-requisite for a Java intensive I would like to attend (and as the very first piece of programming I have ever been expected to write), seeing at least partially completed code and being able to think through what is included in it is helpful while I am also working through several tutorials. If I just wanted someone to do it for me I imagine I could do it in about ten minutes on elance.
|