Results 1 to 5 of 5
  1. #1
    StealthRT is offline Member
    Join Date
    Apr 2008
    Posts
    10
    Rep Power
    0

    Question 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
    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
    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
    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
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    See the changes I have done in your code. May be this is what you are looking.

    Java Code:
    class Employee
    {
        private Name Nam;
        private Address Addr;
    
        public Employee() {
            Addr = new Address("Street", "City", "State", "Zip");
            Nam = new Name("First", "Middle", "Last");
        }
    
        private String getName() {
           return Nam.toString();
        }
    
        private String getAddress() {
           return Addr.toString();
        }
    
        private String getSSN() {
           return "SSN";
        }
        
        private String printValues() {
            return "Name: " + getName() + "\n" +
                    "Address: " + getAddress() + "\n" +
                    "SSN: " + getSSN();
        }
    
        public static void main(String[] args) {
            System.out.println(new Employee().printValues());
        }
    
    }
    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){
           this.strStreet = Street;
           this.strCity = City;
           this.strState = State;
           this.strZip = Zip;
        }
    
        @Override
        public String toString()
        {
           return strStreet + " " + strCity + " " + strState + " " + strZip;
        }
    }
    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;
        }
    
        @Override
        public String toString() {
           return strFirst + " " + strMiddle + " " + strLast;
        }
    }

  4. #4
    StealthRT is offline Member
    Join Date
    Apr 2008
    Posts
    10
    Rep Power
    0

    Default

    Thank you Eranga! I make the suggestions on the Employee.java file and kept the same format for the other 2. Then i make a testEmployee.java with the data below:
    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("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);
        }
    }
    And everything works now! Thanks again! :)

    David

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Yes you can do it different ways. I just give an option you have, because I'm not clear what exactly you want to do. Basically in your previous classes, make an error on constructors.

    And also you no need to call System.exit(0) in main method. Main method simply exit the system after execution is completed. It's done by the JVM. So that line in your last code is useless.

Similar Threads

  1. Get name of available classes
    By escuja in forum CLDC and MIDP
    Replies: 0
    Last Post: 07-26-2008, 12:03 PM
  2. Replies: 0
    Last Post: 05-22-2008, 10:53 AM
  3. [SOLVED] Simple Q: Values between Classes
    By a45b22chp in forum New To Java
    Replies: 6
    Last Post: 04-25-2008, 05:55 PM
  4. Replies: 4
    Last Post: 04-15-2008, 01:04 PM
  5. When do we use inner classes?
    By cruxblack in forum New To Java
    Replies: 5
    Last Post: 08-10-2007, 05:00 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •