Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2007

Tip: Looking for answers? Try searching our database.

Using set and get methods in an inherited class

Thread view: 
rleroux@telus.net - 31 Jan 2007 23:38 GMT
Hello,

I have an inherited class where I'm acceptting input and setting the
input to my private variables in my superclass (in my acceptPay
method).  If I display the variable by using the get method (see rem'd
line in the acceptPay method) there's no issue.

However, when I try to display the variable in my displayinfo method,
I get 0.0 instead of the value that should be in the variable.

What am I overlooking in the example below?
Tnx.

class CalPayroll extends Pay
{
    //Display output
    public void displayinfo()
    {

        System.out.println(getHours());
        System.out.println();
    }

    //Accept input
    public void acceptPay()throws IOException
    {
        //Set variables to use
        float HoursWorked;
        int StraightHours;
        float HourlyRate;
        char ex;

        System.out.print("Enter number of hours worked (00.0):");
        //Create a new object to access the acceptinputFloat in the Accept
Class

                               Accept passHoursWorked = new Accept();
        HoursWorked = passHoursWorked.acceptInputFloat();

        setHours(HoursWorked);
                                                //System.out.println(getHours()); //THIS WORKS TO GET THE
HOURS THAT WAS SET ABOVE

        CalPayroll display = new CalPayroll();
        display.displayinfo();
    }
}
Hal Rosser - 01 Feb 2007 00:35 GMT
> Hello,
>
[quoted text clipped - 42 lines]
> display.displayinfo();
> }

Each instance of the class has its own copy of the instance variables.
You created a new CalPayroll and called its displayinfo method before you
set the instance variables, so having only been declared, but not 'set' the
value of your variable is 0.0.
Mark Space - 01 Feb 2007 00:36 GMT
This is pretty simple, actually.

>     //Accept input
>     public void acceptPay()throws IOException
[quoted text clipped - 4 lines]
>         float HourlyRate;
>         char ex;

These are local variables, not class variables.

They only work inside this method.

>         System.out.print("Enter number of hours worked (00.0):");
>         //Create a new object to access the acceptinputFloat in the Accept
[quoted text clipped - 4 lines]
>
>         setHours(HoursWorked);

I'm not sure exactly what's going on here.  What is the Accept() object
for?  You also don't show us what the routine "setHours() does.
CalPayroll appears to have no class variables at all.  But it doesn't
matter....

>     //System.out.println(getHours()); //THIS WORKS TO GET THE HOURS THAT WAS SET ABOVE
>
>         CalPayroll display = new CalPayroll();
>         display.displayinfo();

You make a brand new object here, display, and you don't set any
variable for it.  You don't call display.setHours() or anything, so of
course its internal variables are all still initialized to their
defaults (0.0 in the case of a float).

>     }
> }
rleroux@telus.net - 01 Feb 2007 01:12 GMT
> This is pretty simple, actually.
>
[quoted text clipped - 40 lines]
>
> - Show quoted text -

Hi,

To answer you question:
Accept passHoursWorked = new Accept();
> >            HoursWorked = passHoursWorked.acceptInputFloat();

Accept is yet another class (saved as Accept.java).  This class
contains methods to take input from the users and parses the data to
be either a  float, int or char and return the values back.  In this
case when a person enters in hours worked, it will pass the input to
my Accept class and into the method of acceptInputFloat() to convert
the data to a float.  This is being dong as I'm needing to use the
bufferedReader method and I have several more classes that is
accepting input and doing the conversion appropriately,  Just creating
one class saves on having multiple BufferedReader statements.
rleroux@telus.net - 01 Feb 2007 01:41 GMT
On Jan 31, 5:12 pm, "rler...@telus.net" <rler...@telus.net> wrote:

> > This is pretty simple, actually.
>
[quoted text clipped - 59 lines]
>
> - Show quoted text -

Got it working.  As Hal mentioned earlier I called displayinfo()
before setting the rate for that new object, so I moved my set routine
to after the new object and before calling display.displayinfo()
so I have it as display.setHours(HoursWorked);


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.