d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Java Help > Donation For Successful Help
Add Reply New Topic New Poll
Member
Posts: 27,185
Joined: Apr 16 2007
Gold: 161,690.00
Oct 27 2012 04:51pm
Hello guys,

I have a task where I have an abstract superclass company and the subclasses Manager, Employee and Worker
These types of classes have different parameters (see constructors) and I shall generate the following output (in an unsorted and a sorted list):

Code
Unsortierte Liste
Worker: Hansen, Hans [1]
Worker: Clausen, Claus [2]
Employee: Jensen, Jens [3] Phone: 1234
Employee: Gunnarson, Gunnar [4] Phone: 4321
Manager: Petersen, Peter [5] Phone: 2341 FL-FH 1
Manager: Paulsen, Paul [6] Phone: 3451 FL-FH 2



So the thing that I'm missing is that the generated output is this:

Code
Unsortierte Liste
Worker@1e66a917
Worker@65e73498
Employee@2e2e1b6c
Employee@550a6723
Manager@1df95673
Manager@5c9aa764



So here are my classes:

Code
public abstract class Company {

 protected String name;
 protected String pNumber;
 
public Company(String name, String pNumber) {
 this.name = name;
 this.pNumber = pNumber;
}


static Company[] aCompanyArray = new Company[6];

public abstract String showMe();

static public void showAll() {
 for(Company aCompany : aCompanyArray) {
  System.out.println(aCompany);
 }
}


 static public void sortByName() { //didn't modify this yet
  int[] aArray = new int[] { 3,2,1,4 };
  int help;
  for (int i = 0; i < aArray.length - 1; i++) {
  for (int j = i + 1; j < aArray.length; j++) {
  if (aArray[j] < aArray[i]) {
  help = aArray[i];
  aArray[i] = aArray[j];
  aArray[j] = help;
  }
  }
  }
  for (int aInt: aArray) System.out.print(aInt + " ");
  }
 

public String setName(String name) {
 if (name != null & !name.trim().isEmpty()){
 return this.name = name;
 }
 else return "undefined";
}
public String getName() {
 return name;
}

public String getpNumber() {
 return pNumber;
}

}



Code
public class Manager extends Company {

protected String telNr;
protected String carNr;

public Manager(String name, String pNumber, String telNr, String carNr) {
 super(name, pNumber);
 this.telNr = telNr;
 this.carNr = carNr;
}


@Override
public String showMe() {
 return "Manager: " + name + " [" + pNumber + "] Phone: " + telNr + " " + carNr;
}

public String setTelNr(String telNr) {
 if (name != null & !name.trim().isEmpty()){
 return this.telNr = telNr;
 }
 else return telNr;
}

public String getTelrNr() {
 return telNr;
}

public String getCarNr() {
 return carNr;
}

public String setCarNr(String carNr) {
 if (name != null & !name.trim().isEmpty()) {
  return this.carNr = carNr;
 }
 return carNr;
}
}



Code
public class Employee extends Company {

protected String telNr;


public Employee(String name, String pNumber, String telNr) {
 super(name, pNumber);
this.telNr = telNr;
}

@Override
public String showMe() {
 return "Employee: " + name + " [" + pNumber + "] Phone: " + telNr;
}

public String setTelNr(String telNr) {
 if (name != null & !name.trim().isEmpty()){
 return this.telNr = telNr;
 }
 else return telNr;
}

public String getTelrNr() {
 return telNr;
}
}


Code
public class Worker extends Company {

public Worker(String name, String pNumber) {
 super(name, pNumber);
}


@Override
public String showMe() {
 return "Worker: " + name + " [" + pNumber + "]";
}
}



The question is where is something wrong so that the output is as it should be?
If you got constructive instructions I will show my appreciation!

Oh yeah and don't wonder I didn't modify the sorting algorithm yet - it's still the one we got as a sample.

This post was edited by Gala on Oct 27 2012 04:52pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Oct 27 2012 05:13pm
This code invokes toString() on each object
Code
System.out.println(aCompany);


so you have two options:
1. override toString() on your classes
2. invoke showMe() instead of toString()

This post was edited by carteblanche on Oct 27 2012 05:15pm
Member
Posts: 27,185
Joined: Apr 16 2007
Gold: 161,690.00
Oct 27 2012 05:36pm
Quote (carteblanche @ Oct 28 2012 12:13am)
This code invokes toString() on each object
Code
System.out.println(aCompany);


so you have two options:
1. override toString() on your classes
2. invoke showMe() instead of toString()



Yeah thanks for the reply.
But actually if I use showMe it's still abstract and I have to implement it in showAll and that method is in the superpclass Company
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Oct 27 2012 05:43pm
Quote (Gala @ Oct 27 2012 07:36pm)
Yeah thanks for the reply.
But actually if I use showMe it's still abstract and I have to implement it in showAll and that method is in the superpclass Company


1. read what i said again. repeat until you understand it.
2. why do classes named Employee, Worker, and Manager extend Company? this is misuse of OOP. you should be using composition instead of subclassing it. a Company has a list of Persons (or IPersons), and Employee/Worker/Manager all extend from Person (or implement IPerson)

This post was edited by carteblanche on Oct 27 2012 05:44pm
Member
Posts: 27,185
Joined: Apr 16 2007
Gold: 161,690.00
Oct 27 2012 11:11pm
Quote (carteblanche @ Oct 28 2012 12:43am)
1. read what i said again.  repeat until you understand it.
2. why do classes named Employee, Worker, and Manager extend Company? this is misuse of OOP. you should be using composition instead of subclassing it. a Company has a list of Persons (or IPersons), and Employee/Worker/Manager all extend from Person (or implement IPerson)



Thanks for your reply.
In fact I think I do know what you mean.
However I changed the method in Company to
Code

 static public void showAll() {
  for(Company aCompany : aCompanyArray) {
   aCompany.showMe();
  }


now but nothing is actually given out... you know why?

I'm afraid that the superclass Company does not know that it "has been filled"
Rather it could also be that the implementations of the subclasses are not recognized?

Sorry to waste your time but as I said you will get paid if this is thoroughly fixed
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Oct 27 2012 11:22pm
Quote (Gala @ Oct 28 2012 01:11am)
Thanks for your reply.
In fact I think I do know what you mean.
However I changed the method in Company to
Code
static public void showAll() {
  for(Company aCompany : aCompanyArray) {
   aCompany.showMe();
  }


now but nothing is actually given out... you know why?


Because showMe() just returns a string without printing it out. if you want to print it, then you have to say println(aCompany.showMe());

Quote

I'm afraid that the superclass Company does not know that it "has been filled"
Rather it could also be that the implementations of the subclasses are not recognized?

Sorry to waste your time but as I said you will get paid if this is thoroughly fixed


what does "has been filled" mean? i dont see any method called hasBeenFilled() or anything like that.
Member
Posts: 27,185
Joined: Apr 16 2007
Gold: 161,690.00
Oct 28 2012 01:12am
Ah well now I wanna figure out what's the best way to realize the sorting algorithm.
So the sorting depends on the names (alphabetically).

So I wann go from this
Code
Worker: Hansen, Hans [1]
Worker: Clausen, Claus [2]
Employee: Jensen, Jens [3] Phone: 1234
Employee: Gunnarson, Gunnar [4] Phone: 4321
Manager: Petersen, Peter [5] Phone: 2341 FL-FH 1
Manager: Paulsen, Paul [6] Phone: 3451 FL-FH 2


To this:

Code
Worker: Clausen, Claus [2]
Employee: Gunnarson, Gunnar [4] Phone: 4321
Worker: Hansen, Hans [1]
Employee: Jensen, Jens [3] Phone: 1234
Manager: Paulsen, Paul [6] Phone: 3451 FL-FH 2
Manager: Petersen, Peter [5] Phone: 2341 FL-FH 1


I got some starting points but they seem complicated and I might get lost somewhere.
If anybody knows a smooth way I'd appreciate to hear it.

This post was edited by Gala on Oct 28 2012 01:13am
Member
Posts: 4,605
Joined: Sep 15 2011
Gold: 9,464.00
Oct 28 2012 01:59am
Best way would be to have Company implement Comparable and have your compareTo implementation use getName(), then just call Arrays.sort. But I'm guessing that isn't allowed.

This post was edited by irimi on Oct 28 2012 01:59am
Member
Posts: 27,185
Joined: Apr 16 2007
Gold: 161,690.00
Oct 28 2012 03:23am
Thx guys for the advice
I implemented it now - it's not elegant but works.

Cheers!
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll