#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2004
    Posts
    175
    Rep Power
    0

    Exclamation Getting values from other classes


    Hey all i am having some problems with trying to figure out what all i need in order to do the following:
    -3 private instance variables: 1 for Name (use the Name class), 1 for Address (Use the Address class), a string for SSN.
    - A constructor which initializes the SSN, Name and Address
    - getName function (returns Name of Employee)
    - getAddress function (returns Address of Employee)
    - getSSN fuction (returns SSN as string)
    - Print Name, Address and SSN.
    This is my code so far:
    Employee.java
    Code:
    class Employee
    {
        private Name Nam;
        private Address Addr;
        private String strSSN;
    
        public Employee(String theName, String theAddress, String SSN)
        {
           Nam = new Name(theName);
           Addr = new Address(theAddress);
           strSSN = SSN;
        }
    
        public String getName()
        {
           Nam = new Name(FName, MName, LName);
           return Nam;
        }
    
        public String strAddress()
        {
           Addr = new Address(Street, City, State, Zip);
           return Addr;
        }
    
        public String getSSN()
        {
           return strSSN;
        }
    
        public String toString()
        {
           return "Name: " + getName() + "\n" +
                  "Address: " + getAddress() + "\n" +
                  "SSN: " + strSSN + "\n";
        }
    }
    Name.java
    Code:
    class Name
    {
        private String strFirst;
        private String strMiddle;
        private String strLast;
    
        public Name(String FName, String MName, String LName)
        {
           strFirst = FName;
           strMiddle = MName;
           strLast = LName;
        }
    
        public String getFName()
        {
           return strFirst;
        }
    
        public String getMName()
        {
           return strMiddle;
        }
    
        public String getLName()
        {
           return strLast;
        }
    
        public String toString()
        {
           return "First Name: " + strFirst + "\n" +
                  "Middle Name: " + strMiddle + "\n" +
                  "Last Name: " + strLast + "\n";
        }
    }
    Address.java
    Code:
    class Address
    {
        private String strStreet;
        private String strCity;
        private String strState;
        private String strZip;
    
        public Address(String Street, String City, String State, String Zip)
        {
           strStreet = Street;
           strCity = City;
           strState = State;
           strZip = strZip;
        }
    
        public String getStreet()
        {
           return strStreet;
        }
    
        public String getCity()
        {
           return strCity;
        }
    
        public String getState()
        {
           return strState;
        }
    
        public String getZip()
        {
           return strZip;
        }
    
        public String toString()
        {
           return "Street: " + strStreet + "\n" +
                  "City: " + strCity + "\n" +
                  "State: " + strState + "\n" +
                  "Zip: " + strZip + "\n";
        }
    }
    Both the Name and Address .java compiles just fine without errors.

    How can i get the Employee.java to get the Name from the Name.java and the Address from the Address.java?? Am i doing it correctly with the "Nam = new Name(theName);"?

    Thanks for your time!

    David
  2. #2
  3. Daniel Schildsky
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Mar 2004
    Location
    KL, Malaysia.
    Posts
    1,557
    Rep Power
    1625

    OO programming


    Well, sorry to disappoint you. The answer is WRONG.

    You invoke the getter methods of the Name class to get the content stored in that class.

    There are 2 things you done it wrongly:
    1. The instantiation of a Name object for an employee should be done in the Employee's constructor instead of the getName() method. Since your Employee's constructor has already created an instance of Name class, doing that again in the getName() method is totally redundant and should be removed. Likewise for strAddress() method.

    2. To return the instance of the Name class and Address class, simply return the instance you instantiated in the constructor. You have made the Name instance and Address instance as class level variables, so you can simply return them in your getXXX methods.
    When the programming world turns decent, the real world will turn upside down.
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2004
    Posts
    175
    Rep Power
    0
    Trust me tvc3mye, i agree with you about the getName and getAddress in the Employee.java file. However, that is what the homework assignment asks of me so i have to do it.

    How would i go about using the getter methods if the case is that i am forced to get the information from Name.java and Address.java?

    David
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2005
    Location
    Internet
    Posts
    7,625
    Rep Power
    6089
    No, your homework does not ask you to create a new instance every time.

    Also, you want to return Name not String...

    Please read about return types etc.

    As for using accessor methods:

    java Code:
    public class Person {
         private String name;
     
         public Person(String name) { this.name = name; }
     
         public String getName() { return name; }
    }


    To use it

    java Code:
    public class Driver {
         public static void main(String[] args) {
              // make two people
              Person bob = new Person("Bob");
              Person gimp = new Person("Mr Awesome");
              // print out gimp's name
              System.out.println(gimp.getName());
              // we can access methods of the returned Object
              System.out.println(gimp.getName().length());
         }
    }


    This is using my own very simple example... I'm sure you see how to adapt it to your program
    Last edited by gimp; September 10th, 2008 at 08:52 AM.
    Chat Server Project & Tutorial | WiFi-remote-control sailboat (building) | Joke Thread
    “Rational thinkers deplore the excesses of democracy; it abuses the individual and elevates the mob. The death of Socrates was its finest fruit.”
    Use XXX in a comment to flag something that is bogus but works. Use FIXME to flag something that is bogus and broken. Use TODO to leave yourself reminders. Calling a program finished before all these points are checked off is lazy.
    -Partial Credit: Sun

    If I ask you to redescribe your problem, it's because when you describe issues in detail, you often get a *click* and you suddenly know the solutions.
    Ches Koblents
  8. #5
  9. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2004
    Posts
    175
    Rep Power
    0
    Thanks for the reply gimp: This is what i came up with:
    Employee.java
    Code:
    class Employee
    {
        private Name Nam;
        private Address Addr;
        private String strSSN;
    
        public Employee(String theSSN) {
            strSSN = theSSN;
            Addr = new Address("Street", "City", "State", "Zip");
            Nam = new Name("First", "Middle", "Last");
        }
    
        public String getName() {
           return Nam.toString();
        }
    
        public String getAddress() {
           return Addr.toString();
        }
    
        public String getSSN() {
           return strSSN;
        }
        
        public String toString() {
            return "Name: " + Nam + "\n" +
                    "Address: " + Addr + "\n" +
                    "SSN: " + strSSN;
        }
    }
    and to test it
    TestEmployee.java
    Code:
    public class TestEmployee
    {
        public static void main(String[] args)
        {
               Name nam = new Name("John", "H.", "Doe");
               Address addr = new Address("99999 Sunset Boulevard", "Beverly Hills", "CA", "99999");
               Employee ssn = new Employee("999-99-9999");
    
               System.out.println("First Name: " + nam.getFName());
               System.out.println("Middle Name: " + nam.getMName());
               System.out.println("Last Name: " + nam.getLName());
               System.out.println("Full Name: " + nam.getFName() + " " + nam.getMName() + " " + nam.getLName());
    
               System.out.println("Street: " + addr.getStreet());
               System.out.println("City: " + addr.getCity());
               System.out.println("State: " + addr.getState());
               System.out.println("Zip: " + addr.getZip());
    
               System.out.println("SSN: " + ssn.getSSN());
    
               System.exit(0);
        }
    }
    Thanks again for your help!

    David

    Comments on this post

    • tvc3mye agrees : good elaboration!
  10. #6
  11. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2005
    Location
    Internet
    Posts
    7,625
    Rep Power
    6089
    Well, does it work?

    If so, let's consider how you name variables. The standard is
    Code:
    ClassName varName;
    You do that but then you also do
    Code:
    ClassName VarName
    Which is confusing. Basically, capital first letter means a class name, or an interface, or an enum, etc; basically, some form of object or object-type. Variable names need to start lowercase, with the exception of constants/global constants which are usually defined as upper case with underscores (less strict, some don't use the underscores), like
    Code:
    public static final String GLOBAL_VAR = "127.0.0.1";
    private final String CONSTANT = "Hello there"; // not public or static

    It's an ease of readability thing really.

    Also, you already do this correctly, method names follow the same convention as variable names, as in methodName(...).

    One last note, and this is really not important: If you want to keep a descriptive name as both a parameter and an instance variable, you can do this

    Code:
    public class Person {
         private String name;
         public Person(String name) { this.name = name; }
    }
    See, you specify this.name should be assigned the value held in the passed parameter name.

    Comments on this post

    • tvc3mye agrees : good elaboration! Oops...gave the reputation points to a wrong person..can I take back from StealthRT?
    Chat Server Project & Tutorial | WiFi-remote-control sailboat (building) | Joke Thread
    “Rational thinkers deplore the excesses of democracy; it abuses the individual and elevates the mob. The death of Socrates was its finest fruit.”
    Use XXX in a comment to flag something that is bogus but works. Use FIXME to flag something that is bogus and broken. Use TODO to leave yourself reminders. Calling a program finished before all these points are checked off is lazy.
    -Partial Credit: Sun

    If I ask you to redescribe your problem, it's because when you describe issues in detail, you often get a *click* and you suddenly know the solutions.
    Ches Koblents

IMN logo majestic logo threadwatch logo seochat tools logo