7 Replies - 4269 Views - Last Post: 20 April 2010 - 01:35 PM Rate Topic: -----

#1 Guest_pistolizer*


Reputation:

Set and get method with constructor

Posted 20 April 2010 - 09:42 AM

Hi all, I am currently going over my midterm which I did very poorly on. the final question of the exam is as follows:

Create a class called Date that includes three instance variables - a month (type int), a day (type int) and a year (type int). Provide a constructor that initializes the three instance variables and assumes that the values provided are correct. Provide a set and get method for each instance variable. Provide a method displayDate that displays the month, day and year separated by forward slashes. this class will be used by the test application below:


public class DateTest {
        
    /**
     * Creates a new instance of <code>DateTest</code>.
     */

    public static void main(String[] args) {
Date date1 = new Date(3, 4, 2010);
System.out.print("the initial date is:");
date1.displayDate();

date1.setMonth(11);
date1.setDay(1);
date1.setYear(2011);

System.out.print("\nDate with new values is: ");
date1.displayDate();

System.out.println();


    }
}





That is all given. I then went to code the class Date on the next page as follows:

public class Date {
private int month;
private int day;
private int year;

    public Date(int monthValue, int dayValue, int yearValue) {
    	
    	month=monthValue;
    	day=dayValue;
    	year=yearValue;    	
    		
    }
    
  private void setMonth(int month) {
  	
  	int month;
  
  } 
    
 public void getmonth(){
 	return month;
 }   
    
  private void setDay(int day) {
  	
  	int day;
  
  } 
    
 public void getDay(){
 	return Day;
 }   
 	
 public void getYear(){
 	return year;
 }      
 
 public void displayDate(){
 	
 	System.out.printf("%d/%d/%d", month(), day(), year());
 }
    
    
}


I know I am missing a basic understanding of how to use set and get methods. Can anyone shed some light on how to better answer this question? Thanks so much

Is This A Good Question/Topic? 0

Replies To: Set and get method with constructor

#2 pdkharkar  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 63
  • View blog
  • Posts: 345
  • Joined: 19-January 09

Re: Set and get method with constructor

Posted 20 April 2010 - 09:58 AM

setter and getter method are used to set and get i.e. to assign and to retrieve the values for the variables
let us take a look
look at the following code
public class TestGetSet{

  int value;
  TestGetSet(){
  //this is the constructor and you can initialize values here
  }
  
  public void setValue(int someValue){ // here return type is void as we are not returning any value
   this.value = someValue ; // look here we are passing the value someValue as parameter and setting the value of instance variable
                    //  value to someValue
  }
  
  public int getValue(){ // note here that return type is int as we are returning an integer 
 
  return value;  // here we are returning the value of the instance variable 
  }
}




Now suppose you have some other class and you want to set the values of the instance variable value of the class
you create the object of the class and then just call the getter and setter methods as follows

class anotherclass
{
public static void main(String [] args){
 TestGetSet obj = new TestGetSet();
 obj.setValue(10); // this will set the value of the variable to 10
 obj.getValue(); // this will return 10

}

}

Was This Post Helpful? 1
  • +
  • -

#3 Guest_pistolizer*


Reputation:

Re: Set and get method with constructor

Posted 20 April 2010 - 10:36 AM

Thanks for your help! I am still not able to combile the main method though as it is saying the set methods have private access.. I believe I am following your guidelines here but I am obviously missing something. here is my new code:

Set/get methods:

public class Date {
	
private int month;
private int day;
private int year;


    public Date(int monthValue, int dayValue, int yearValue) {
    	

    }
    
  private void SetMonth(int setmonth) {
  	
  	this.month=setmonth;
  
  } 
    
 public int getmonth(){
 	return month;
 }   
    
  private void SetDay(int setday) {
  	
  	this.day=setday;
  
  } 
    
 public int getDay(){
 	return day;
 }   
 	
  private void SetYear(int setyear) {
  	
  	this.year=setyear;
  
  }  	
 	
 	
 public int getYear(){
 	return year;
 }      
 
 public void displayDate(){
 	
 	System.out.printf("%d/%d/%d", getmonth(), getDay(), getYear());
 }
    
    
}



main method:

public class DateTest {
        
    /**
     * Creates a new instance of <code>DateTest</code>.
     */

    public static void main(String[] args) {
Date date1 = new Date(3, 4, 2010);
System.out.print("the initial date is:");
date1.displayDate();

date1.SetMonth(11);
date1.SetDay(1);
date1.SetYear(2011);

System.out.print("\nDate with new values is: ");
date1.displayDate();

System.out.println();


    }
}

Was This Post Helpful? 0

#4 ianian112  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 119
  • View blog
  • Posts: 378
  • Joined: 28-November 09

Re: Set and get method with constructor

Posted 20 April 2010 - 10:47 AM

try declaring your setters as public voids, instead of private
Was This Post Helpful? 0
  • +
  • -

#5 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 520
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: Set and get method with constructor

Posted 20 April 2010 - 10:47 AM

set style methods should always be public, not private. They should be accessible outside of the class itself.
Was This Post Helpful? 0
  • +
  • -

#6 Guest_pistolizer*


Reputation:

Re: Set and get method with constructor

Posted 20 April 2010 - 11:07 AM

View PostLocke, on 20 April 2010 - 09:47 AM, said:

set style methods should always be public, not private. They should be accessible outside of the class itself.


Thanks guys! It is working now. Just one more question (i think). Is there a need for the "this." It seems to work without have to call that.
Was This Post Helpful? 0

#7 NoobKnight  Icon User is offline

  • D.I.C Head

Reputation: 51
  • View blog
  • Posts: 238
  • Joined: 14-July 09

Re: Set and get method with constructor

Posted 20 April 2010 - 11:33 AM

It is not required, "this" means the instance fields, not the fields passed in to the methods or constructor. Even though they are commonly the same. You used different field names so you would be okay to not use "this".
Was This Post Helpful? 0
  • +
  • -

#8 Dogstopper  Icon User is online

  • The Ninjaducky
  • member icon



Reputation: 2857
  • View blog
  • Posts: 10,962
  • Joined: 15-July 08

Re: Set and get method with constructor

Posted 20 April 2010 - 01:35 PM

"this" is used to denote an attribute or method of that class, as opposed to a local method. This is useful as explained above.

What if an instance variable are named the same?
public class SameVarName {
    private int variable;

    public void setVariable(int variable) {
        // Now what?! variable = variable doesn't work...
        // Use 'this'!
        this.variable = variable;
    }
}



And often, its just nice to specify this object.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1