Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Rather confused -------External classes!!  RSS feed

 
Dave Morley
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to wade through a Java correspondence course and I have an assignment to complete, which I have already completed and it compiles without a problem. I am however one of those irritating people who cannot just accept that it runs I need to understand what is written and why it works like it does. I will paste the code here and then ask my questions.

First let me write down what they asked for: and I quote

" You have been asked to write a java class named PayRec that when called creates a programmer defined payroll record. the fields should include last name, first name, position , rate and hours.. You also should include corresponding get methods to allow for future searching and sorting"

here is the code that I wrote:

/* Project 4: Writing external methods
*Programmer: Dave Morley
*Date: 28-08-2004
*Program Name: PayRec
*/

import java.io.*;

public class PayRec
{
// declaring variables
String firstName;
String lastName;
String position;
float rate;
float hours;

public PayRec(String f, String l, String p, float r, float h)
{
firstName = f;
lastName = l;
position = p;
rate = r;
hours = h;
}

public PayRec(String f, String l, String p)
{
firstName = f;
lastName = l;
position = p;
}



public String getFirstName()
{
return firstName;
}
public String getLastName()
{
return lastName;
}
public String getPosition()
{
return position;
}
public float getRate()
{
return rate;
}
public float getHours()
{
return hours;
}

}


My questions are as follows:the method constructor "PayRec accepts 5 arguments f,l,p,r,and h. I dont understand why you use the letters f,l, etc as arguments and thenunder that method you then declare that firstName = f and so on surely that is duplicating the work ???

secondly they ask me to create a second method constructor also called PayRec which overloads the class but only accepts the l,f, and p arguments.

This is fine but if an external driver progem wanted to get just these arguments surely the method constructor should have a different name i.e. PayRec1 which would return only the 3 args as opposed to PayRec which would return all the args. And then how would you differentiate in your external program that you wanted the PayRec with the 3 args as opposed to the PayRec with all the args.

I am sure that I am missing the point so all patience is greatly appreciated.

Many Thanks in advance
Dave M
Cape Town
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good questions.

Answering your second question first, constructors with different numbers of parameters but the same name, or even the same number of parameters but with at least one parameters with a different type, are called overloaded constructors. The compiler automatically matches the constructor arguments in the new operator to the correct constructor.

As for your first question, f, l, p, r, and h are parameters, a special kind of local variable. They only last until the constructor is done. firstName, lastName, etc., are instance variables. They last as long as the object exists and will retain the information supplied to the constructor,

There is a cool way to code the constructor to make this clear:


Look up this and see if you can figure out how it works.
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
PayRec1 which would return only the 3 args as opposed to PayRec which would return all the args.


Both constructors don't return arguments.
They consume arguments.

Let's look at a method first:

This method returns a single int, and consumes two ints.
You may call it like this:

or ignore the return value - which doesn't look very clever in this example:

But what does a Constructor return?

I would say: An object of type PayRec, (while language-lawyers might shake their heads)
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave can speak for himself, but I interpreted "return" in the sense of a side effect. The constructor assigns the values of the arguments to instance variables accessed through the this reference to the object under construction. This is one way a method returns a result.

[OK, call me a language lawyer]
 
Dave Morley
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am starting to get it and I don't know if anyone is still watching this topic. I am 8 or 9 hrs ahead of most of you so the time difference can play a bit of havoc. I think what Is still confusing me is this. In the driver program that they use as an example, you create an array which holda PayRec objects, I have no problem with this. Based on the fact that you have two constructor methods one with 5 args one with 3, you are creating to types of objects with the same name. How would you then process or sort the array if you need to sort it with 5 args and only 3 have come back to the main driver program???

If you are creating two objects that may share the same information then surely in the driver program you must differentiate between lets say PayRec object and PayRec object which in my mind should be PayRec1 object so when you sort the array say based on name will the driver program be able to do this taking into account that the PayRec1 object might not include any names, only position and .......... so on.

I have a feeling that my thinking on this may be well out??

Thanks AnyWay

Dave M
Cape Town

 
Ray Stojonic
Ranch Hand
Posts: 326
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How would you then process or sort the array if you need to sort it with 5 args and only 3 have come back to the main driver program?


There are a number of ways to handle this situation

- driver sorts only on the fields that are guaranteed to be there

- driver validates data returned from PayRec methods and handles null or empty strings.

- PayRec incorporates a valid flag and matching isValid method to allow the driver to know when all fields have been set

- throw an Exception and let the user know that their data is bad
 
Mike Gershman
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The two different constructors help create the same type of object.

At the point where the constructor code gets control, the object exists and your 5 instance variables have already been initialized to null or +0.0f as appropriate. Your constructor modifies 3 or 5 of these variables to the values in the arguments.

Sorting on numeric fields is no problem. When sorting on String fields you should test the variable for null and treat that case as the lowest possible value, or whatever you want.

There is no difference at all between these two objects:
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When posting code, please be sure to surround the code with the [code] and [/code] UBB Tags. This will help to preserve the formatting of the code, thus making it easier to read and understand.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!