Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Class not passing information

  1. #1
    Junior Member
    Join Date
    Jul 2010
    Posts
    4
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Class not passing information

    Hello, I am a student and I'm working on a problem.

    I have written other small class and method assignments without a problem, but I'm getting confused as to what's wrong with my code.

    I'm doing a simple assignment where I'm asked to create a class, and then populate and display it in another. I can compile without any errors, so it makes it a little harder to troubleshoot.

    Here is my code from the first class.

    public class PayrollML
    {
    		private String	 name;			//employee's first and last name
    		private int		 idNumber;		//Employee's ID number
    		private double	 rate;			//Employee's hourly rate
    		private double	 hours;		//employee's hours worked
     
     
    	public void setName( String name )
    	{
    		name = (name != null ? name : "No name" );
    	}
     
    	public void setIdNumber( int idNumber )
    	{
    		idNumber = ( idNumber >=0?  idNumber : 0 );
    	}
     
    	public void	setRate( double rate )
    	{
    		rate = ( rate >= 0? rate :0 );
    	}
     
    	public void setHours( double hours )
    	{
    		hours	= ( hours >= 0?  hours  : 0 );
    	}
     
    	public String getName()
    	{
    		return name;
    	}
     
    	public int getIdNumber()
    	{
    		return idNumber;
    	}
     
    	public double getRate()
    	{
    		return rate;
    	}
     
    	public double getHours()
    	{
    		return hours;
    	}
     
    }

    Here is the second class to create and instance, populate it and then display the information.

    import javax.swing.*;
     
    class PayrollMLInput
    {
    	public static void main( String [] args )  //fill in the information for the employees
    	{
     
    		String input;
    		String payName;
    		int payIdNumber;
    		double payRate;
    		double payHours;
    		double payGross;
     
    		//creates an object from the payroll class.
    		PayrollML Payee1 = new PayrollML();
     
    		[COLOR="Orange"]// I assume this area is where the problem lies.  Setting the data.  I cannot tell for sure, since it all compiles without errors.[/COLOR]
    		[COLOR="Red"]input = JOptionPane.showInputDialog( "Please enter employee name.");
    		payName = input;
    		Payee1.setName(payName);
     
    		input = JOptionPane.showInputDialog( "Please enter employee ID."); 
    		payIdNumber = Integer.parseInt(input);
    		Payee1.setIdNumber(payIdNumber);
     
    		input = JOptionPane.showInputDialog( "Please enter employee rate of pay in dollars per hour.");
    		payRate = Double.parseDouble(input);
    		Payee1.setRate(payRate);
     
    		input = JOptionPane.showInputDialog( "Please enter employee hours worked.");
    		payHours = Double.parseDouble(input);
    		Payee1.setHours(payHours);
     
    		payGross = (Payee1.getRate() * Payee1.getHours());[/COLOR]
     
    [COLOR="Orange"]//Unless this is the issue and I'm not calling it correctly.  Either way, I cannot tell for sure where my error is.  To me it looks like I've named the class and parameters correctly.[/COLOR]
    		JOptionPane.showMessageDialog( null,   "Employee is "	+ Payee1.getName()	  + "\n"
    														 + "Id Number: "	+ Payee1.getIdNumber() + "\n"
    														 + "Makes " 		+ Payee1.getRate()	  + " Per Hour\n"
    														 + "Worked "		+ Payee1.getHours()	  + " hours this week\n"
    														 + "Earned "		+ payGross 				  + " dollars");
     
    		System.exit(0);
    	}	
    }

    When I run the second program, after all the JOptionPane promts are completed, I get nulls and zero's for the displayed data.

    Any help in pointing out my error would be greatly appreciated.

    Thank you.
    Last edited by DiPep; July 25th, 2010 at 05:04 PM. Reason: For clarification


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,130
    Thanks
    52
    Thanked 2,286 Times in 2,257 Posts

    Default Re: Class not passing information

    Can you add some comments to the code showing where the problem is
    and showing what the displayed data is?

    Add some calls to println() to show all the values to the console and copy them here. Copy the second arg to the showMessageDialog() method and paste it into the println()
    Last edited by Norm; July 25th, 2010 at 04:41 PM.

  3. The Following User Says Thank You to Norm For This Useful Post:

    DiPep (July 26th, 2010)

  4. #3
    Junior Member
    Join Date
    Jul 2010
    Posts
    4
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Class not passing information

    I'm going to add the comments to the first post.

    The displayed data is this when I use the System.out.println().

    Employee is null
    Id Number: 0
    Makes 0.0 Per Hour
    Worked 0.0 hours this week
    Earned 0.0 dollars
    I think that the second class PayrollMLInput isn't passing the data to the PayrollML class. I really don't understand why.
    Last edited by DiPep; July 25th, 2010 at 05:18 PM. Reason: Update

  5. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Class not passing information

    The problem is because you are utilizing the same name for local variables (parameters) as the object variable names.

    This means that the object variable names will be "hidden". You must tell java you mean the object value by using the "this" reference.

    For example:

    private class Test
    {
        public int theValue;
     
        public void printValue(int theValue)
        {
            // object value is hidden!
            System.out.println("value passed to the function: " + theValue);
            System.out.println("value stored in this object: " + this.theValue);
        }
     
        public static void main(String[] args)
        {
            Test myTest = new Test();
            myTest.theValue = 5;
            myTest.printValue(3);
        }
    }

  6. The Following User Says Thank You to helloworld922 For This Useful Post:

    DiPep (July 26th, 2010)

  7. #5
    Junior Member
    Join Date
    Jul 2010
    Posts
    4
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Class not passing information

    helloworld922,

    I just figured out what you meant.

    I had to change code in my first class to this.

    	public void setName( String zName )
    	{
    		name = (zName != null? zName : "No name" );
    	}
     
    	public void setIdNumber( int zIdNumber )
    	{
    		idNumber = ( zIdNumber >=0?  zIdNumber : 0 );
    	}
     
    	public void	setRate( double zRate )
    	{
    		rate = ( zRate >= 0? zRate :0 );
    	}
     
    	public void setHours( double zHours )
    	{
    		hours	= ( zHours >= 0?  zHours  : 0 );

    Thank you very much.
    Last edited by DiPep; July 26th, 2010 at 09:54 AM. Reason: Eureka!

  8. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,130
    Thanks
    52
    Thanked 2,286 Times in 2,257 Posts

    Default Re: Class not passing information

    If you use unique names at all levels/scopes of your program, you won't have this problem.

    Do you understand how the "this" modifier is used? It is an object reference to the current object.
    The other part of the problem is "scope". If you have the same name at different levels of scope, the nearer one is the one used.
    The outer one is hidden.

    public void setName( String name )
    	{
    		name =
    Here name is a local variable (its defined as an arg to the method). The class member name is in a different scope and hidden.
    To get to the outer variable which is a member variable for this object use: this.name

  9. The Following User Says Thank You to Norm For This Useful Post:

    DiPep (July 26th, 2010)

  10. #7
    Junior Member
    Join Date
    Jul 2010
    Posts
    4
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Class not passing information

    Thank you Norm.

    I didn't actually see your post until I edited my post above.

    I was too busy thinking my PayrollMLInput class was the problem and I didn't think to look at the PayrollML class itself. I saw my error there and slapped my forehead.

    Thank you both for your help.
    Last edited by DiPep; July 26th, 2010 at 10:04 AM. Reason: spelling

Similar Threads

  1. Replies: 0
    Last Post: April 11th, 2010, 08:56 AM
  2. [SOLVED] Passing arrayList inside class
    By KrisTheSavage in forum Collections and Generics
    Replies: 1
    Last Post: March 27th, 2010, 12:45 PM
  3. Getting information from a folder
    By shadihrr in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: February 23rd, 2010, 03:13 PM
  4. Passing Information between classes
    By SKhoujinian91 in forum Object Oriented Programming
    Replies: 4
    Last Post: December 8th, 2009, 02:40 PM
  5. Having trouble printing object information in main class
    By KingLane in forum Object Oriented Programming
    Replies: 1
    Last Post: October 11th, 2009, 06:53 PM