Results 1 to 5 of 5
- 09-10-2008, 09:23 AM #1
Member
- Join Date
- Apr 2008
- Posts
- 10
- Rep Power
- 0
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:
This is my code so far:-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.
Employee.java
Name.javaJava 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"; } }
Address.javaJava 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"; } }
Both the Name and Address .java compiles just fine without errors.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"; } }
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
- 09-10-2008, 11:28 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Do you know about constructors in Java? And also instantiation of Java classes?
- 09-10-2008, 11:44 AM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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; } }
- 09-10-2008, 08:34 PM #4
Member
- Join Date
- Apr 2008
- Posts
- 10
- Rep Power
- 0
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:
And everything works now! Thanks again! :)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); } }
David
- 09-11-2008, 03:32 AM #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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
-
Get name of available classes
By escuja in forum CLDC and MIDPReplies: 0Last Post: 07-26-2008, 12:03 PM -
Retaining DB values as well as Dynamically generated Values.. Help Needed !
By rajivjha in forum Advanced JavaReplies: 0Last Post: 05-22-2008, 10:53 AM -
[SOLVED] Simple Q: Values between Classes
By a45b22chp in forum New To JavaReplies: 6Last Post: 04-25-2008, 05:55 PM -
Accessing boolean Values of another values in one class.
By a_iyer20 in forum Advanced JavaReplies: 4Last Post: 04-15-2008, 01:04 PM -
When do we use inner classes?
By cruxblack in forum New To JavaReplies: 5Last Post: 08-10-2007, 05:00 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks