7 Replies - 447 Views - Last Post: 03 October 2011 - 07:48 AM Rate Topic: -----

#1 kaikarden  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 40
  • Joined: 11-September 08

question about passing values to object properties

Posted 02 October 2011 - 08:40 PM

Hey there everyone, i've run across a little snag in the process of attempting to make one of my programs function properly. i was instructed to make a class titled Employee that contained 2 string variables (first and last name) and 1 double (monthly salary) and have a second class (i named it program) that creates 2 demo objects out of the Employee class, assign the 2 demo objects values for all the variables and then test they're working with some simple math. I've got the Employee class all programmed including gets and sets for all the appropriate encapsulated variables, however when i try to pass values along to the set methods in the program class, for some reason they get lost in transition and never actually get assigned.

If someone could take a quick glance at my code and see if they can spot something that i did not, i would be very appreciative.

Thanks in advance!

Program Class:
public class Program
{
  public static void main(String[] args)
  {
    double joYSalary;
    double jaYSalary;
    
    Employee johnDoe = new Employee();
    Employee janeDoe = new Employee();
    
    johnDoe.setFirstName("John");
    johnDoe.setLastName("Doe");
    johnDoe.setMSalary(1850.00);
    
    janeDoe.setFirstName("Jane");
    janeDoe.setLastName("Doe");
    janeDoe.setMSalary(2000.00);
    
    joYSalary = johnDoe.getMSalary() * 12;
    jaYSalary = janeDoe.getMSalary() * 12;
    
    System.out.println("John's yearly salary: " + joYSalary);
    System.out.println("Jane's yearly salary: " + jaYSalary);
    
    joYSalary *= 1.10;
    jaYSalary *= 1.10;
    
    System.out.println("John's yearly salary: " + joYSalary);
    System.out.println("Jane's yearly salary: " + jaYSalary);
  }
}


Employee Class:
public class Employee
{
  private String firstName;
  private String lastName;
  private double mSalary;
  
  public double getMSalary()
  {
    return mSalary;
  }
  public void setMSalary(double a)
  {
    a = mSalary;
  }
  public void setFirstName(String a)
  {
    a = firstName;
  }
  public void setLastName(String a)
  {
    a = lastName;
  }
}


I would debug the program but the compiler the class requires i use is still in the beta stages and the debug feature is currently inop. thanks again in advance.

SIDE NOTE: I did write this program in C#, the language i'm most comfortable with and i got it to work just fine, and it looks like the code should be working to me, just can't figure out why the values are being passed.

Is This A Good Question/Topic? 0
  • +

Replies To: question about passing values to object properties

#2 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7571
  • View blog
  • Posts: 12,713
  • Joined: 19-March 11

Re: question about passing values to object properties

Posted 02 October 2011 - 08:43 PM

You have the assignments the wrong way around in your setters. You want lastName = a and so forth, not a = lastName.

a is the value you're passing in: it's value you want the variable to have. lastName where you want that value to end up.

This post has been edited by jon.kiparsky: 02 October 2011 - 08:45 PM

Was This Post Helpful? 2
  • +
  • -

#3 kaikarden  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 40
  • Joined: 11-September 08

Re: question about passing values to object properties

Posted 02 October 2011 - 08:44 PM

View Postjon.kiparsky, on 02 October 2011 - 08:43 PM, said:

You have the assignments the wrong way around in your setters. You want "lastName = a" and so forth, not "a = lastName".


ah! silly me. Thanks for spotting my yoda conditions haha
Was This Post Helpful? 0
  • +
  • -

#4 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1817
  • View blog
  • Posts: 4,625
  • Joined: 14-March 10

Re: question about passing values to object properties

Posted 02 October 2011 - 08:45 PM

Look at this line and related: a = mSalary; the right hand value is the one assigned to the left operand like saying x =2; means assign 2 to x. So your lines should look mSalary = a; and all others like so
Was This Post Helpful? 2
  • +
  • -

#5 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7571
  • View blog
  • Posts: 12,713
  • Joined: 19-March 11

Re: question about passing values to object properties

Posted 02 October 2011 - 08:46 PM

Just out of curiousity, why are you calling the variable that holds your salary "mSalary"? That looks like something a Hungarian would do.
Was This Post Helpful? 0
  • +
  • -

#6 kaikarden  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 40
  • Joined: 11-September 08

Re: question about passing values to object properties

Posted 02 October 2011 - 08:46 PM

the program ran perfectly after i aleviated the yoda conditions, thanks jon and smohd!

@Jon, the reason is because it's monthly salary, i originally had an intent to name a second variable called aSalary for Annual Salary but that didn't even make it to the pseudocode because there are two trial objects instead of one. I like to keep my variables short so it saves on typing.

This post has been edited by kaikarden: 02 October 2011 - 08:49 PM

Was This Post Helpful? 0
  • +
  • -

#7 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7571
  • View blog
  • Posts: 12,713
  • Joined: 19-March 11

Re: question about passing values to object properties

Posted 03 October 2011 - 07:43 AM

Quote

I like to keep my variables short so it saves on typing.


If I were to give you the best advice I have, it would be to make your variable names descriptive first, and short second. This is even more important for fields than it is for locals, since fields are found throughout the class, and even (as in your getMSalary method) outside the class. Imagine that a user is using your class without access to your code: how do they know what "mSalary" is?
Now, imagine that you come across this code in your company's codebase, and the guy who wrote it has long since got hit by the Google bus and moved on to greener pastures. Would you rather figure out what "mSalary" is, or would you rather see "monthlySalary"?

Now, imagine that you wrote this code a year and a half ago, and someone's found a bug in it and they need it fixed right now. You've got a hundred things on your plate, and your boss is yelling at you and you don't have time to deal with this, and you haven't looked at this or thought about it in the last year.
Are you still happy that, a year and a half ago, you saved half a dozen keystrokes?
Was This Post Helpful? 2
  • +
  • -

#8 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10390
  • View blog
  • Posts: 38,447
  • Joined: 27-December 08

Re: question about passing values to object properties

Posted 03 October 2011 - 07:48 AM

jon.kiparsky is absolutely right. On top of that, in the scheme of programming (no pun intended ;)), a few keystrokes is insignificant. I've worked on school projects that are a couple hundred lines of code, and I've worked on professional projects that are tens of thousands of lines of code. Trust me- make your code easy on others as well as on yourself. You never know who will be maintaining it, and you don't want to irritate others with poor variable names. We have a saying in this industry- "Always write your code as if the person maintaining it will be a serial killer that has your address." :)
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1