In this article, you will learn how make re-usable methods to perform operations for you, instead of typing the same code over and over again
The object of the program I am using as an example is for the program to display a point value(250, 500, or 1000) when a string(param0) is passed to it. The point value is determined by the average word length. If the average word length is less than or equal to 3, the pointval is 250; if it is 4 or 5, the pointval is 500; if it is more or equal to 6, the pointval is 1000. The main program is called Test.java, and the class is called HowEasy.java. For example- if the string passed to the class is "john smith", the returned value is 500, because (4 + 5) / 2 = 4.5.
Test.java
CODE :
import java.util.*;
public class Test
{
public static void main(String[] args)
{
HowEasy statement = new HowEasy();
System.out.println(statement.pointVal("john smith"));
}
}
See how easy it makes writing main programs to use the HowEasy class? 9 lines!!
CODE :
HowEasy statement = new HowEasy();
This makes an object statement use the HowEasy class. statement could be anything you wanted it to be, like foobar or blahhh, but in the second line you would have to change statement to foobar or blahhh. This is the same concept as when you want to get user input for a program, and you write the Scanner reader = new Scanner(System.in); line.
You could change reader to blahhh or foobar too, but when you get the input, it would have to be variable = blahhh.nextLine(); instead of variable = reader.nextLine();.
CODE :
This sends the pointVal parameter of statement (which is in the HowEasy class) to John Smith. Then, it prints the result. You could also write it another way, but is a bit longer.
CODE :
int whatsReturnedFromTheClass = statement.pointVal("John Smith");
System.out.println(whatsReturnedFromTheClass);
This would do the same thing as before, but it would get the pointVal the statement returned, and save it as whatsReturnedFromTheClass. Then it prints out whatsReturnedFromTheClass
HowEasy.java
CODE :
import java.util.*;
public class HowEasy{
public int pointVal(String param0) {
String problemStatement;
int tokens;
int chars;
int averagewordlength;
int points = 0;
problemStatement = param0;
StringTokenizer st = new StringTokenizer (problemStatement);
tokens = st.countTokens();
chars = problemStatement.length();
averagewordlength = chars / tokens;
if (averagewordlength <= 3) {
points = 250;
}
if (averagewordlength == 4) {
points = 500;
}
if (averagewordlength == 5) {
points = 500;
}
if (averagewordlength >= 6) {
points = 1000;
}
return points;
}
}
This class is what saves you time, if you were writing multiple programs to do the same thing and print out a point value based on the string entered in.
CODE :
public class HowEasy{
If this was a normal java program that could run by itself, you would see something like public static void bla bla bla. The void means that it runs by itself, or is not given any data to work with(besides if it prompts user for data). public class declares a new class.
CODE :
public int pointVal(String param0) {
This declares a public integer pointVal, which is passed a parameter; the parameter is a String, assigned the variable name param0. this is where the statement.pointVal(); comes from. pointVal becomes a property of whatever is declared in the Test.java(statement in this case). In the Test.java, the reason you write HowEasy is because it tells the program in what class the pointVal(or other property you set) is in
CODE :
return points;
this sends back the value of points(250, 500, or 1000) to whatever called it. In Test.java, the value would be printed. again, if it said
CODE :
int whatsReturnedFromTheClass = statement.pointVal("John Smith");
in Test.java, whatsReturnedFromTheClass would be set to what was returned, which was points. so whatsReturnedFromTheClass = points. Using the HowEasy class, you could easily whip up another program to, for example, print "you use small words", "you use medium words", or "you use big words" based on the average word length of what is sent, without writing any code about how to come up with the values of 250, 500, or 1000. eg.,
CODE :
import java.util.*;
public class WordSize
{
public static void main(String[] args)
{
HowEasy statement = new HowEasy();
Scanner reader = new Scanner(System.in);
String sentance = "";
int wordvalue;
System.out.println("Type a sentance to see if you use small, medium, or big words.");
sentance = reader.nextLine();
wordvalue = statement.pointVal(sentance);
if (wordvalue == 250) System.out.println("You use small words");
if (wordvalue == 500) System.out.println("You use medium words");
if (wordvalue == 1000) System.out.println("You use big words");
}
}
I wrote that in 2 minutes without even thinking about how I was to get the average word length. The HowEasy class saved me 35 lines of redundant code.
Cast your vote on this article *Note: the order of the votes has been reversed.
You fail at knowing why \"void\" is used. You also only talk about object methods, ignoring class methods. The pointVal() method also should have been static, since their is no logic behind associating it with an object in the given example.
I think I\'m quite good at Java, and I can say it\'s really easy and useful, especially when I have to make a web application:D
This site is the collective work of the
HackThisSite staff. Please don't reproduce in part or whole without permission.
Page Generated: Mon, 01 Dec 2008 21:48:55 -0500 Exec:
10 Page loaded in 0.12883 seconds!
-accepted by me, taken out random <br>s