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.

Recommended Answers

All 4 Replies

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:

Scanner in = new Scanner ( System.in ).useDelimiter ( ' ' );

System.out.print ( "Full name (ex. John Smith): " );
String first = in.next();
String last = in.next();

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.

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(

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();
}
}

Please use code tags when posting your code. use the rightmost icon above the text input field. The one that says: (CODE)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.