Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 09-10-2008, 11:23 AM
Member
 
Join Date: Apr 2008
Posts: 10
StealthRT is on a distinguished road
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:
Quote:
-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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 09-10-2008, 01:28 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,566
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Do you know about constructors in Java? And also instantiation of Java classes?
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 09-10-2008, 01:44 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,566
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
See the changes I have done in your code. May be this is what you are looking.

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()); } }
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; } }
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; } }
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 09-10-2008, 10:34 PM
Member
 
Join Date: Apr 2008
Posts: 10
StealthRT is on a distinguished road
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:
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
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 09-11-2008, 05:32 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,566
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Get name of available classes escuja CLDC and MIDP 0 07-26-2008 02:03 PM
Retaining DB values as well as Dynamically generated Values.. Help Needed ! rajivjha Advanced Java 0 05-22-2008 12:53 PM
[SOLVED] Simple Q: Values between Classes a45b22chp New To Java 6 04-25-2008 07:55 PM
Accessing boolean Values of another values in one class. a_iyer20 Advanced Java 4 04-15-2008 03:04 PM
When do we use inner classes? cruxblack New To Java 5 08-10-2007 07:00 PM


All times are GMT +3. The time now is 10:44 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org