7 Replies - 1466 Views - Last Post: 14 February 2010 - 10:05 AM Rate Topic: -----

#1 jrooneo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 04-February 10

Creating and calling methods

Posted 13 February 2010 - 08:23 PM

I am having problems creating and calling methods. I have a long equation that when given a year will output the month and day of Easter. I am very confused on how to take an integer year from a command line argument as input into a method and output a string variable. I have posted my code here. I need a better explanation on dealing with methods.

public class easter {
	int year;
	
	public void main(String args[]){
		
		if (args.length==1){
			year= Integer.parseInt(args[0]);
		}
		else{
			System.out.println("Please add only a single year after the command as a command line argument.");
			System.exit(0);
		}
	
		if (year>=325 & year<=9999){
			calcEaster(year);
			System.out.println(easter);
		}
		else if (year<325){
			System.out.println("There was no set date for Easter before the year of 325 A.D.");
		}
		else if (year>9999){
			System.out.println("Your number is above range. Please choose a lower number.");
		}
}
	public int calcEaster(){
		int A;
		int B;
		int C;
		int D;
		int E;
		int F;
		int G;
		int H;
		int I;
		int K;
		int L;
		int M;
		int N;
		int P;
		
		A=year%19;
		B=year/100;
		C=year%100;
		D=B/4;
		E=B%4;
		F=(B+8)/25;
		G=(B-F+1)/3;
		H=(19*A+B-D-G+15)%30;
		I=C/4;
		K=C%4;
		L=(32+2*E+2*I-H-K)%7;
		M=(A+11*H+22*L)/451;
		N=(H+L-7*M+114)/31;
		P=(H+L-7*M+114)%31;
		easter = "Easter will be on "+N+"/"+(P+1)+" in "+year;
	}
}


Is This A Good Question/Topic? 0
  • +

Replies To: Creating and calling methods

#2 xor-logic  Icon User is offline

  • HAL9000 was an Apple product
  • member icon

Reputation: 128
  • View blog
  • Posts: 764
  • Joined: 04-February 10

Re: Creating and calling methods

Posted 13 February 2010 - 09:13 PM

View Postjrooneo, on 13 February 2010 - 07:23 PM, said:

I am having problems creating and calling methods. I have a long equation that when given a year will output the month and day of Easter. I am very confused on how to take an integer year from a command line argument as input into a method and output a string variable. I have posted my code here. I need a better explanation on dealing with methods.

public class easter {
	int year;
	
	public void main(String args[]){
		
		if (args.length==1){
			year= Integer.parseInt(args[0]);
		}
		else{
			System.out.println("Please add only a single year after the command as a command line argument.");
			System.exit(0);
		}
	
		if (year>=325 & year<=9999){
			calcEaster(year);
			System.out.println(easter);
		}
		else if (year<325){
			System.out.println("There was no set date for Easter before the year of 325 A.D.");
		}
		else if (year>9999){
			System.out.println("Your number is above range. Please choose a lower number.");
		}
}
	public int calcEaster(){
		int A;
		int B;
		int C;
		int D;
		int E;
		int F;
		int G;
		int H;
		int I;
		int K;
		int L;
		int M;
		int N;
		int P;
		
		A=year%19;
		B=year/100;
		C=year%100;
		D=B/4;
		E=B%4;
		F=(B+8)/25;
		G=(B-F+1)/3;
		H=(19*A+B-D-G+15)%30;
		I=C/4;
		K=C%4;
		L=(32+2*E+2*I-H-K)%7;
		M=(A+11*H+22*L)/451;
		N=(H+L-7*M+114)/31;
		P=(H+L-7*M+114)%31;
		easter = "Easter will be on "+N+"/"+(P+1)+" in "+year;
	}
}

Ok, a few things here.

A ) It's a convention (but not a requirement) class names are capitalized. It helps when you're sharing code. Also, variables sharing a name with your class is confusing.

B ) You use a variable called easter, which I assume is a string from the content you assign to it. But I don't see where you declared that variable, so I'm betting that generates errors.

C ) calcEaster() has a return type int, but you never return anything in that method.

D ) Your problem is not with accepting command line args. You seem to have done that just fine.

E ) All you need to do it seems (assuming your calcs are correct) is print the string easter. There are a couple ways to do this. First, you could declare easter as a static string at the beginning of the program.
public class Easter {
  static String easter = "";
    main() {}
}


That should make your program function just fine. Another thing you can do, and what I'd recommend doing, is changing the return type of calcEaster() to String, and add the following line at the end of calcEaster:
  easter = "Easter will be on "+N+"/"+(P+1)+" in "+year;
  return easter;
}



Then change
if (year>=325 & year<=9999)
    calcEaster(year);
    System.out.println(easter);
}


to
if (year>=325 & year<=9999)
    System.out.println(calcEaster(year));
}



Finally, a word on dealing with passing arguments between methods. First, when you create a method:
public void whatever(int x) {
}


The above method declaration states that when this method is called, it will be expecting an integer value, and it will assign it to variable x for this method. So when you call it, you would pass it either an integer literal (a number) or an integer variable.
public static void main(String[] args) {
   whatever(5);
}
//or
public static void main(String[] args) {
   int y = 5;
   whatever(y);
}



Now, to return a value, it is declared as a return type in the method declaration:
public int whatever(int x) {
}


You can see above that I've replaced void with int. This says that this method will pass back a value of type int. However, it doesn't do this automatically. You have to define what gets passed back using the return keyword:
public int whatever(int x) {
  return x;  //return variable
}
//or
public int whatever(int x) {
  return 5;  //return literal
}


Note that with return you can either pass back a variable containing a value of the declared return type, or pass back a literal of the declared type.

Now once you have a method that returns a value, you have to do something with the returned value when you call it. For example, if it returns an int, you can assign it to an int in the calling method, or you can just print it (there are some other things you can do too, but you'll figure those out easy if you understand this).
public static void main(String[] args) {
   int x = 5;
   int y = whatever(5);
   System.out.println(y);
   int y = whatever(x);
   System.out.println(y);
   System.out.println(whatever(5));
   System.out.println(whatever(x));
}

public int whatever(int x) {
   int z = x;
   return z;
}


Above, all the println methods print the same thing.

Get it? Any questions?

This post has been edited by xor-logic: 13 February 2010 - 09:14 PM

Was This Post Helpful? 2
  • +
  • -

#3 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10461
  • View blog
  • Posts: 38,753
  • Joined: 27-December 08

Re: Creating and calling methods

Posted 13 February 2010 - 09:18 PM

A basic format for methods is as follows, noting that the access modifier and static portions are optional in general, though if you are invoking the method from a static method, then the invoked method must be static:
<access-modifier> <static> <return-type> name(<param-list>){
  ..code..
}



To declare a parameter (or a "placeholder" to pass values or references to objects), you declare them inside the parentheses of the method definition including type and name. So for example, if you want to write an add() method that adds two numbers and returns the result, you might use the following header:
//public- method has no restrictions in terms of outside components accessing it
//double- this method returns a value of the primitive type double
//add- the name of this method
//a and b- the two parameters to add. note how I use type and name when defining them.
//when you invoke this method, you would just provide two double values separated by commas
public double add(double a, double B)/>{
   return (a+B)/>;
}

//Invocation in another method:
double x = add(3.5, 4.2); //x is assigned the value of 3.5+4.2



Now the only reason your method couldn't return something if it was a constructor (which if you haven't gotten to them, none of your methods here are constructors) or if it was declared void as the return-type. So since the calcEaster() method is an int method, it must return an int value or variable in its definition, like add() returns a double in my example. Then to save the result, you must assign it to a variable or keep invoking it each time you need the value. Note how I assigned the result of the add() method to a double variable.

For more information on defining methods, check out the Sun Tutorial:
http://java.sun.com/...OO/methods.html
Was This Post Helpful? 0
  • +
  • -

#4 jrooneo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 04-February 10

Re: Creating and calling methods

Posted 13 February 2010 - 09:57 PM

Thank you very much. I'm not used to Java yet. Upon making your corrections I have single error.

calcEaster() in easter cannot be applied to (int)
the error is associated to the line
System.out.println(calcEaster(year));


Here are the changes I made to the calcEaster() method.
public String calcEaster()
{...
easter = "Easter will be on "+N+"/"+(P+1)+" in "+year;
		return easter;}



I also changed the easter string variable to
static String easter = " ";



Is there any further help you can give me?
Was This Post Helpful? 0
  • +
  • -

#5 xor-logic  Icon User is offline

  • HAL9000 was an Apple product
  • member icon

Reputation: 128
  • View blog
  • Posts: 764
  • Joined: 04-February 10

Re: Creating and calling methods

Posted 13 February 2010 - 09:59 PM

calcEaster() has to accept an int, since that's what you're sending to it. That goes in the parentheses in the method declaration.
Was This Post Helpful? 1
  • +
  • -

#6 jrooneo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 04-February 10

Re: Creating and calling methods

Posted 13 February 2010 - 10:16 PM

Wow, thank you. I had read 3 different descriptions about methods and none of them really described them well. The program now works great. Again, thank you.
Was This Post Helpful? 0
  • +
  • -

#7 xor-logic  Icon User is offline

  • HAL9000 was an Apple product
  • member icon

Reputation: 128
  • View blog
  • Posts: 764
  • Joined: 04-February 10

Re: Creating and calling methods

Posted 13 February 2010 - 11:20 PM

View Postjrooneo, on 13 February 2010 - 09:16 PM, said:

Wow, thank you. I had read 3 different descriptions about methods and none of them really described them well. The program now works great. Again, thank you.


Just as a side note, if a variable is a class variable (static), it really doesn't need to be passed between methods, since it's accessible to the whole class.
Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10461
  • View blog
  • Posts: 38,753
  • Joined: 27-December 08

Re: Creating and calling methods

Posted 14 February 2010 - 10:05 AM

This applies to instance variables (declared in the class and not in any methods, but without the static modifier) as well. They are visible to the entire class.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1