Need help Using "get and set methods"

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Nov 2004
Posts: 7
Reputation: Maria5683 is an unknown quantity at this point 
Solved Threads: 0
Maria5683 Maria5683 is offline Offline
Newbie Poster

Need help Using "get and set methods"

 
0
  #1
Nov 10th, 2004
I'm using the Scanner class and need help in setting up get and set methods for where the program asks the user to enter a first name and a last name as String type.( 2 methods, 1 for first, 1 for last) I need to know how to store the names because...

I am creating a client of the first class and need to call the methods in order to display what the user inputted at the end of the program.

How do you type code for this?

Any help is appreciated.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,681
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 727
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Need help Using "get and set methods"

 
0
  #2
Nov 10th, 2004
First, do you really need set methods? Most of the time you can use constructor arguments to set the fields and set methods aren't required:
  1. Scanner in = new Scanner ( System.in ).useDelimiter ( ' ' );
  2.  
  3. System.out.print ( "Full name (ex. John Smith): " );
  4. String first = in.next();
  5. String last = in.next();
  6.  
  7. Client obj = new Client ( first, last );
Then writing the get methods only consists of returning the necessary field. The chances of an object representing a person needing to change the name are very slim, so set methods don't make much sense except in very specific situations.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC