Page 1 of 1 [ 5 posts ] 

ShizzleMacDaddy
Yellow-bellied Woodpecker
Yellow-bellied Woodpecker

User avatar

Joined: 4 Apr 2008
Age: 34
Gender: Male
Posts: 67
Location: California

14 Oct 2012, 3:37 am

Hello WP, it has been a very long time... I have a question regarding constructors in Java. In my Java class in college we had a homework assignment that required us to create a class called employee with the attributes of name and salary. I completed the assignment and already received a grade however the instructor said that I need to add a constructor which brings me to my question. Given the following code snippit:

Quote:
class employee
{
static Scanner console = new Scanner(System.in);
String firstName;
String lastName;
double salary;

void salary_change() {
System.out.println("How much would you like to increase " + firstName + " " + lastName + "'s salary?");
salary = salary + console.nextDouble();
System.out.println(firstName + " " + lastName + "'s salary has been changed to " + salary);
}

void quit() {
salary = 0;
System.out.println(firstName + " " + lastName + " has quit, salary has been changed to " + salary);
}

void getInfo(){
System.out.println(firstName + " " + lastName + "'s salary is currently: " + salary);
}


Would the following be the correct implementation of a constructor (or constructors)?

Quote:
class employee
{
static Scanner console = new Scanner(System.in);
String firstName;
String lastName;
double salary;

employee() {

}

employee(double sal) {
salary = sal;
}

employee(String fN, String lN, double sal) {
firstName = fN;
lastName = lN;
salary = sal;
}

void salary_change() {
System.out.println("How much would you like to increase " + firstName + " " + lastName + "'s salary?");
salary = salary + console.nextDouble();
System.out.println(firstName + " " + lastName + "'s salary has been changed to " + salary);
}

void quit() {
salary = 0;
System.out.println(firstName + " " + lastName + " has quit, salary has been changed to " + salary);
}

void getInfo(){
System.out.println(firstName + " " + lastName + "'s salary is currently: " + salary);
}


I just want to make sure that I am on the right track because the next homework assignment requires us to build off of this one.

I appreciate any input you have for me,
Thanks.
-Nick



SickInDaHead
Sea Gull
Sea Gull

User avatar

Joined: 29 Aug 2012
Age: 54
Gender: Male
Posts: 215

14 Oct 2012, 4:02 am

You can have multiple constructors, so there could not be be a "best one" but an instance of the class without a true "index" value that will be unique could be considered a bad constructor. For example if I were to build an array of that class, and use the salary constructor, and some of them have the same salary, then those are not representing unique employees.


This constructor that you already used is the best one:
employee(String fN, String lN, double sal) {
firstName = fN;
lastName = lN;
salary = sal;
}

You set the two most important fields and the salary field.

The next assignment will probably use this class, but have fields for department, position, etc. So expect that to have a constructor that sets those fields, and then a "super" call relaying name and salary (this is what I suspect).

It won't be that hard.



Trencher93
Velociraptor
Velociraptor

User avatar

Joined: 23 Jun 2008
Age: 126
Gender: Male
Posts: 464

14 Oct 2012, 5:34 am

A constructor's job is to put the newly created object into a fully-initialized state where it can be used. It will create any contained (HASA) objects, initialize fields, and so on. Arguments to the constructor tell the constructor values that it can't otherwise know.



ShizzleMacDaddy
Yellow-bellied Woodpecker
Yellow-bellied Woodpecker

User avatar

Joined: 4 Apr 2008
Age: 34
Gender: Male
Posts: 67
Location: California

14 Oct 2012, 12:02 pm

Thanks guys,


@ SickInDaHead:
Yeah I believe you are correct that the next assignment wants us to extend this class for department, position and things like that. I believe he also wants us to make an array and have it populate with several employees but I will have to re-read the assignment to make sure. I'm sure I'll be running back to WP for more re-assurance on that assignment as well.

Quote:
You can have multiple constructors, so there could not be be a "best one" but an instance of the class without a true "index" value that will be unique could be considered a bad constructor. For example if I were to build an array of that class, and use the salary constructor, and some of them have the same salary, then those are not representing unique employees.


This constructor that you already used is the best one:
employee(String fN, String lN, double sal) {
firstName = fN;
lastName = lN;
salary = sal;
}

You set the two most important fields and the salary field.

The next assignment will probably use this class, but have fields for department, position, etc. So expect that to have a constructor that sets those fields, and then a "super" call relaying name and salary (this is what I suspect).

It won't be that hard.


So what I'm gathering from this is to remove:

Quote:
employee() {

}

employee(double sal) {
salary = sal;
}


Or am I misunderstanding you?

Thanks,
-Nick



sliqua-jcooter
Veteran
Veteran

User avatar

Joined: 25 Jan 2010
Age: 38
Gender: Male
Posts: 1,488
Location: Burke, Virginia, USA

14 Oct 2012, 9:04 pm

With your code - you never actually set the variables for firstName, lastName, or salary. You don't even have any methods to use in that object to set them - so, you have uninitialized variables that you're trying to read from. If you actually tried to write code that used that object you'd pretty quickly see the problem with that. In fact, I'm somewhat surprised that you actually got that to compile.

You want to create multiple constructors for every logical possibility. So - for each employee you add to the program, you would have certain information at the very least you'd have their name, and salary. But, you might not have their home address at the time you add them to the program, you may only get that information once they start their first day and fill out forms. So, what you would do is create a constructor that required you to input all the information you *do* have (first and last name, salary) and then fill in "default" information for the information you may not have (ie. setting the homeAddress variable to = ""). You need to have the variable initialized, because if you try to use a variable that isn't initialized you get an error in your code - so you set it to a value that the code can work with, but that practically means nothing.


_________________
Nothing posted here should be construed as the opinion or position of my company, or an official position of WrongPlanet in any way, unless specifically mentioned.