943,482 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 4550
  • Java RSS
Nov 10th, 2004
0

Need help Using "get and set methods"

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Maria5683 is offline Offline
7 posts
since Nov 2004
Nov 10th, 2004
0

Re: Need help Using "get and set methods"

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:
Java Syntax (Toggle Plain Text)
  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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jul 17th, 2010
0
Re: Need help Using "get and set methods"
class MyDate
{
int dd,mm,yy;
public void initDate ()
{
dd=mm=yy=0;
}
public void setDate(int d,int m,int y)
{
dd=d;
mm=m;
yy=y;
}
public void dispDate()
{
System.out.println(
Reputation Points: 10
Solved Threads: 0
Newbie Poster
saurabh.agrawal is offline Offline
2 posts
since Jul 2010
Jul 17th, 2010
0
Re: Need help Using "get and set methods"
class MyDate
{
int dd,mm,yy;
public void initDate ()
{
dd=mm=yy=0;
}
public void setDate(int d,it m,int y)
{
dd=d;
mm=m;
yy=y;
}
public void dispDate()
{
System.out.println("Date is:"+dd+"-"+mm+"-"+yy);
}
public static vo main (String args[])
{
MyDate D1;
d1=new MyDate();
d1.initDate();
d1.dispDate();
d1.setDate(3,7,90);
d1.dispDate();
}
}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
saurabh.agrawal is offline Offline
2 posts
since Jul 2010
Jul 17th, 2010
0
Re: Need help Using "get and set methods"
Please use code tags when posting your code. use the rightmost icon above the text input field. The one that says: [CODE]
Reputation Points: 925
Solved Threads: 502
Posting Expert
NormR1 is online now Online
5,078 posts
since Jun 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Android with Java
Next Thread in Java Forum Timeline: Need help with an assignment





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC