Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with Get and Set Methods

Status
Not open for further replies.

tberger1

MIS
Sep 12, 2006
16
US
I have a program that I need to convert the existing methods to get and set methods but I am struggling with how to do this. I have one set in place. Can anyone help in changing some of the others to get or set as appropriate? Any help is appreciated.

TJ

Here is the code:

import java.util.Scanner;

public class Odometer
{

int milesPerGallon;
int milesDriven;
int startTrip;
int endTrip;
int newOdometer;
int gals;
int galsUsed;

public int calculateMPG()
{
milesPerGallon = milesDriven/gals;
System.out.println("Gallons Used " + milesPerGallon);

return (milesPerGallon);
}

public void setOdometer()
{
newOdometer = milesDriven;
System.out.println("Total Miles on Odometer " + newOdometer);
milesDriven = 0;
System.out.println("New Trip miles " + milesDriven);
}

public int calculateMiles()
{
milesDriven = startTrip + endTrip;
System.out.println("Starting Trip " + startTrip);
System.out.println("Starting Trip " + endTrip);
System.out.println("Total Trip " + milesDriven);
return (milesDriven);
}

public void readMileage()
{
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter the starting number of miles on odometer");
System.out.println("Enter the ending number of miles on odometer");
startTrip = keyboard.nextInt();
endTrip = keyboard.nextInt();
}

public void readGallons()
{
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter # per gallons");
gals = keyboard.nextInt();
}

}


 
Usually Get & Set methods follow the following basic format:
Code:
public [b]some_type[/b] GetSomething()
{
   return m_Something;
}

public void SetSomething( [b]some_type[/b]  something )
{
   m_Something = something;
}
Although, they might need to do a little more work if they are also validating the value being set or reading the value from somewhere else...
 
and the accepted practice in Java is to use lower-case for the start of method names, so...
Code:
public some_type [b]g[/b]etSomething()
{
   return m_Something;
}

public void [b]s[/b]etSomething( some_type  something )
{
   m_Something = something;
}

Tim
 
And IHMO, a good practice is to not use underscore for type names or variables:

Code:
public someType getSomething()
{
   return mSomething;
}

public void setSomething( someType  something )
{
   mSomething = something;
}

Cheers,
Dian
 
and the "accepted practice" in C is to use macros and put everything in the global namespace... but that doesn't mean it's good or that everyone uses it.

I prefer to begin all public functions with a capital letter, and all non-public functions with a small letter; and no matter how much programs like Eclipse tell me it's not standard practice, I'm still gonna fight with it until it does it my way. ;-)
 
And IHMO, a good practice is to not use prefixes at all, because they are less readable.
Modern IDEs can show you whether something is a member or not.

Code:
public someType getSomething()
{
   return something;
}

public void setSomething (someType  something)
{
   this.something = something;
}

seeking a job as java-programmer in Berlin:
 
stefanwagner said:
a good practice is to not use prefixes at all, because they are less readable.
I'd have to disagree with that. I find it much more readable to put m_ infront of member variables.
Even if modern IDEs can distinguish member variables from normal variables, that doesn't always help. If you do a diff of two versions of some source code, you'll probably be looking at the diff in a non-IDE window like WinDiff, CVS or Perforce. Second, some companies like to do big code reviews in a large group, so people just print out the code and bring it to the code review meeting. On paper, you don't have an IDE to help you figure out what is what.
 
Use of underscores, prefixes etc is going to have to lay in the realm of personal / corporate preference, I think.

However, where using lowercase letters for the first character of method names is concerned, especially in the case of get/set/is, then doesn't the JavaBean specification add a more formal requirement for this.

Tim
 
The more a programming language looks like natural language, the more readable it is. Underscores, asterisks, ampersands and so on make a lot of languages almost cryptical.

One of the better things of Java is that OO desing, encapsulation and naming conventions make it easier to read, understand and maintain.

Cheers,
Dian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top