//MethodsTest.java //methods demo. /* module of code that can be given ("passed") some data via its "parameters"/"arguments" when "called", does some computation, brings back ("returns") a single value of specified type. Can be defined with 0 or more parameters but call must supply all values. Can be used multiple places and times. Plugged in to programs where needed. 1000's of built-in methods exist in Java. Some of the built-in methods with return type and argument types: int Integer.parseInt(String) //1 parameter, of type String. returns int double Math.sqrt(double) //1 double parameter. returns a double double Math.random() //0 parameters. returns double String JOptionPane.showInputDialog(String) void drawLine(int,int,int,int) //no return value (thus no return statement) void paint(Graphics) void actionPerformed( ActionEvent) //declaration/definition: creating your own methods: returnDataType methodName (type param1, type param2... ) { type localVariable1; //only exists inside the method type localVariable2; //code... //changing the parameters of primitive type (int,char,double) does not //change the calling values. but changing objects (String, arrays) does //change the calling objects. return valueOfReturnDataType; //ends method call } //call: methodName(value1,value2); //or, more likely: returnDataType var; var = methodName(value1,value2); */ import java.awt.*; import javax.swing.*; public class MethodsTest extends JApplet { public static void main(String[] argv) { //public void init() { new MethodsTest(); //call the constructor. IGNORE } public MethodsTest() { //IGNORE //test the isLeapYear method: String input; int year; do { input = JOptionPane.showInputDialog("Enter a year (0 to quit)" ); year = Integer.parseInt(input); if (isLeapYear(year)) //if method returns true JOptionPane.showMessageDialog(null, "Is a leap year"); else JOptionPane.showMessageDialog(null, "Is not a leap year"); } while (year != 0); //************************************ //test the getGrade method: char grade; grade = getGrade(); JOptionPane.showMessageDialog(null, "thanks for "+grade); //************************************ //test the daysInMonth method: do { input = JOptionPane.showInputDialog( null, "Enter month number (1-12)"); int monthNum; monthNum = Integer.parseInt(input); input = JOptionPane.showInputDialog( null, "Enter year"); year = Integer.parseInt(input); int days = daysInMonth(monthNum, year); JOptionPane.showMessageDialog(null, "#days is "+days); } while (year != 0); //********************************** //test the m1 method: int n=5; //method can NOT change value of calling primitive argument! m1( n ); JOptionPane.showMessageDialog(null, "after m1 call n="+n); //test the doStringStuff method: //method CAN change value of calling object argument! String copy, s; input = JOptionPane.showInputDialog("Enter a string" ); copy = input; //make copy of original //method that changes the calling String argument: doStringStuff( input ); JOptionPane.showMessageDialog(null, "input string is now: "+input); input = copy; //restore original //method that does not change argument, but returns changed string s = doStringStuff2( input ); JOptionPane.showMessageDialog(null, "s string is now: "+s); } //method to test if a year is a leap year or not //parameter/argument is year "passed" to method at call. boolean isLeapYear( int year ) { if (year%4==0 && year%100!=0 || year%400==0) return true; else return false; } //method to input a valid grade from user. //all this messy stuff is bundled into a module that can be used multiple //times and places. //No parameter is needed for this method. char getGrade() { String input; char grade; boolean badGrade; do { input = JOptionPane.showInputDialog("Enter a grade"); grade = input.charAt(0); grade = Character.toUpperCase(grade); badGrade = false; //assume is valid grade if (grade!='A' && grade!='B' && grade!='C' && grade!='D' && grade!='F' && grade!='S' && grade!='N') { JOptionPane.showMessageDialog(null, "ERROR invalid grade. "+ "Must be A, B, C, D, F, S, or N\n"+ "Reenter this classes' grade.", "Bad input", JOptionPane.ERROR_MESSAGE); badGrade = true; //no, it's bad } } while (badGrade); //loop if true return grade; } //method to return number of days in a month. //year parameter needed for leap year int daysInMonth( int month, int year) { int monthDays; switch (month) { case 1: monthDays = 31; break; case 2: if (isLeapYear(year)) monthDays = 29; else monthDays = 28; break; case 3: monthDays = 31; break; case 4: monthDays = 30; break; case 5: monthDays = 31; break; case 6: monthDays = 30; break; case 7: monthDays = 31; break; case 8: monthDays = 31; break; case 9: monthDays = 30; break; case 10: monthDays = 31; break; case 11: monthDays = 30; break; case 12: monthDays = 31; break; default: //in case bad month value passed to method monthDays = 0; } return monthDays; //this method would be better using an array of the days per month } void m1 (int n) { n++; //will NOT change value of calling argument JOptionPane.showMessageDialog(null, "in m1 call n="+n); } //changes String (object) argument void doStringStuff (String s ) { s = s.toUpperCase(); } //does not change argument but returns object String doStringStuff2 (String s ) { String s2; s2 = s.toUpperCase(); return s2; } }