Mar 20, 2014 10:09 PM
(This post was last modified: Mar 20, 2014 10:10 PM by shellbullet101.)
Ok I'm a beginner in java ive just started two weeks ago I am stuck on a particular problem and I would appreciate it if anyone could help me. I'm not asking for anyone to do it for me...just a guide in the right direction. Here is the program instructions
. Create an Employee class with following attributes / variables:
name
age
department
3. Create a class called Department which will contain a list of employees.
a) Department class will have a method which will return its employees ordered by age.
b) The value of department should be called Accounting
4. Create a static method in a separate class called PrimeAgeChecker to check if an employee’s age is prime number or not. This method should be able to accept an employee object as an argument to do the check.
5. Create a class called Company which will contain a list of departments. Create a main method in this class to load the following departments and corresponding employee information into the company:
Department Name Age
Accounting Bill 55
Accounting Todd 45
Print out departments, employee names, ages and if age is prime number for each employee in the company using the method in PrimeAgeChecker class. When you print out Accounting and Information Systems departments, make sure they are printed in ascending order by employee age using the method you created in step 2a.
And this is what I have so far
http://postimg.org/image/xoth5bgsx/
Thanks to anyone willing to help a beginner.
Are these the exact instructions provided to you?
So exactly what are you having trouble with? Sorting employee's by age, ??
Mar 21, 2014 03:38 AM
(This post was last modified: Mar 21, 2014 03:45 AM by shellbullet101.)
(Mar 21, 2014 01:07 AM)I Eat Pixels Wrote: Are these the exact instructions provided to you?
Yes, these are the exact instructions. Minus the end part where it asks for information department too, that part isn't needed.
(Mar 21, 2014 02:04 AM)Feminist Wrote: So exactly what are you having trouble with? Sorting employee's by age, ??
Yes, how the end result outcome should look like. I appreciate you guys for responding.
Mar 21, 2014 11:11 AM
(This post was last modified: Mar 21, 2014 11:56 AM by I Eat Pixels.)
(Mar 20, 2014 10:09 PM)shellbullet101 Wrote: b) The value of department should be called Accounting
This does not make any sense to me. Why have a department variable if it is always called Accounting?
There is a lot of cleaning up to do, but this rough code should help you out.
Company
Code import java.util.*;
public class Company{
ArrayList<Department> departmentList = new ArrayList<Department>();
public Department checkDepartment(String name){
for(Department dep: departmentList){
if(dep.getName().equals(name)){//Department already exists
return dep;
}
}
Department d = new Department(name);
departmentList.add(d);//Add to list
return d;
}
public static void main(String[] args){
Scanner in = new Scanner(System.in);
Company c = new Company();
String input = in.nextLine();
while(in.hasNextLine() && input.length() != 0){
String[] inputArray = input.split(" ");//Split input into array
Department d = c.checkDepartment(inputArray[0]);
d.newEmployee(Integer.parseInt(inputArray[2]), inputArray[1], d);//May want to handle errors
input = in.nextLine();
}
for(Department dep:c.departmentList){//Prints all the employees in the departmentList
ArrayList<Employee> empList = dep.getEmployees();
for(Employee emp: empList){
emp.printInfo();
}
}
}
}
Department
Code import java.util.*;
public class Department{
ArrayList<Employee> employeeList = new ArrayList<Employee>();
String department;
public Department(String a){
this.department = a;
}
public void newEmployee(int age, String name, Department department){
Employee e = new Employee(age, name, department);
this.employeeList.add(e);
}
public String getName(){
return department;
}
public ArrayList<Employee> getEmployees(){
Collections.sort(employeeList, new Comparator<Employee>(){
@Override
public int compare(Employee e1, Employee e2){
final int age1 = e1.getAge();
final int age2 = e2.getAge();
return age1 < age2 ? -1 : age1 > age2 ? 1 : 0;//Sort youngest -> oldest
}
});
return employeeList;
}
}
Employee
Code public class Employee {
String name;
int age;
Department department;
public Employee(int age, String name, Department department){
this.age = age;
this.name = name;
this.department = department;
}
int getAge(){
return age;
}
public void printInfo(){
primeHelper p = new primeHelper();
System.out.println("Name: " + name + " Age: " + age + " Dep: " + department.getName() +
" Prime: " + p.PrimeAgeChecker(age));
}
}
primeHelper
Code import java.math.*;
public class primeHelper{
public boolean PrimeAgeChecker(int age){
BigInteger bi = new BigInteger(String.valueOf(age));
boolean prime = bi.isProbablePrime(10);
return prime;
}
}
Input:
A bob 88
A Jim 87
A Tom 7
IT Bitch 19
IT Thomas 27
IT Timbo 77
Output:
Name: Tom Age: 7 Dep: A Prime: true
Name: Jim Age: 87 Dep: A Prime: false
Name: bob Age: 88 Dep: A Prime: false
Name: Bitch Age: 19 Dep: IT Prime: true
Name: Thomas Age: 27 Dep: IT Prime: false
Name: Timbo Age: 77 Dep: IT Prime: false
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
Thanks a lot to everyone who helped I now understand the code a lot better than before by seeing the end result of it thanks a bunch.
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.
All you did so far was create a couple classes and add a couple variables. So what you are currently doing is asking for people to show the path to do the work, and this isn't programming at all. A big part of of programming (if not all) is problem solving, and by reviewing what you have I can tell you are a beginner. My Adivce LEARN FROM BOOKS/VIDEOS and then come back to this.
(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.
|