For Programmers: Free Programming Magazines  


Home > Archive > Java Help > May 2004 > Passing a value from one method to another









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Passing a value from one method to another
Jim

2004-05-13, 2:32 am

What would be the syntax for passing a variable value from one method
to another?..say I had this, and I know this isnt right .. still
working it out but..I want to get the value of n in getEasterSundayDay
and pass it to getEasterSundayMonth..how do I do that?...thanks


public class EasterSunday
{
private Integer y;

public Integer getEasterSundayYear()
{
return y;
}

public Integer getEasterSundayDay()
{
Integer a = y % 19;
Integer b = y / 100;
Integer c = y % 100;
Integer d = b/4;
Integer e = b % 4;
Integer g = (8 * b + 13 )/25;
Integer h = (19 * a + b - d - g + 15 )%30;
Integer j = c / 4;
Integer k = c % 4;
Integer m = a + 11 * h / 319;
Integer r = 2 * e + 2 * j - k - h + m + 32 % 7;
Integer n = h - m + r + 90 / 25;
Integer p = h - m + r + n + 19 % 32;

return p;
}

public Integer getEasterSundayMonth()
{

return n;
}
Roedy Green

2004-05-13, 2:32 am

On 12 May 2004 22:03:44 -0700, jim.ferris@motorola.com (Jim) wrote or
quoted :

>What would be the syntax for passing a variable value from one method
>to another?..say I had this, and I know this isnt right .. still
>working it out but..I want to get the value of n in getEasterSundayDay
>and pass it to getEasterSundayMonth..how do I do that?...thanks


I can't make any sense of your question. You need some code in your
two other methods to compute the result. You need a beginner's Java
book to get you over the initial hump. You don't have to spend any
money. See http://mindprod.com/jgloss/gettingstarted.html

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
JavaJug

2004-05-13, 5:32 am

Jim wrote:
> What would be the syntax for passing a variable value from one method
> to another?..say I had this, and I know this isnt right .. still
> working it out but..I want to get the value of n in getEasterSundayDay
> and pass it to getEasterSundayMonth..how do I do that?...thanks
>
>
> public class EasterSunday
> {
> private Integer y;
>
> public Integer getEasterSundayYear()
> {
> return y;
> }
>
> public Integer getEasterSundayDay()
> {
> Integer a = y % 19;
> Integer b = y / 100;
> Integer c = y % 100;
> Integer d = b/4;
> Integer e = b % 4;
> Integer g = (8 * b + 13 )/25;
> Integer h = (19 * a + b - d - g + 15 )%30;
> Integer j = c / 4;
> Integer k = c % 4;
> Integer m = a + 11 * h / 319;
> Integer r = 2 * e + 2 * j - k - h + m + 32 % 7;
> Integer n = h - m + r + 90 / 25;
> Integer p = h - m + r + n + 19 % 32;
>
> return p;
> }
>
> public Integer getEasterSundayMonth()
> {
>
> return n;
> }


To share your variable into several environments, you can do its
declaration in the environment above. Here, you do it with you y
variable, and you can access it in all of your methods, so why not do
the same with your n variable? You declare it with your y variable:
private Integer y;
private Integer n;
and instead of declaring it in your getEasterSundayDay() method, just
give her its value there:
n = h - ... ; (instead of Integer n = ... ;)

--
"There is a theory which states that if ever anyone discovers exactly
what the Universe is for and why it is here, it will instantly disappear
and be replaced by something even more bizarre and inexplicable.
There is another theory which states that this has already happened."
-- Douglas Adams
Jim

2004-05-13, 7:32 pm

Ok thanks that, worked..Im getting close here..now..Im trying to pass
a parameter from one class to another but it doesnt appear to be
working..check out the code for both classes:

//Main class

import java.io.*;

public class Assignment1_Master
{

// These 2 lines borrowed from
[url]http://www.csc.liv.ac.uk/~frans/COMP101/w3/input.html#example[/url]
public static InputStreamReader input = new
InputStreamReader(System.in);
public static BufferedReader keyboardInput = new
BufferedReader(input);


public static void main(String[] args)throws IOException
{

int year;


do
{

System.out.print("Determine Easter Sunday for what year:");
year = keyboardInput.read();
EasterSunday es = new EasterSunday();
es.setEasterSundayYear( year);

//System.out.println("Determine Easter Sunday for what year:" + " "
+ year);
System.out.println("In " + es.getEasterSundayYear() + " Easter
Sunday falls/fell on ");
System.out.println(es.getEasterSundayMonth() + ", " +
es.getEasterSundayDay() + ".");


}while(year != 0);
}
}

//Secondary Class

public class EasterSunday
{
private int y;
//Initialized to "Error" for debugging if n is not properly passed
from getEasterSundayDay()
private String m;

public void setEasterSundayYear( int year)
{

y = year;

}


public int getEasterSundayYear()
{
//Returns the year entered by the User
return y;
}

public int getEasterSundayDay()
{ //Implementation of the Gauss algorithm
int a = y % 19;
int b = y / 100;
int c = y % 100;
int d = b/4;
int e = b % 4;
int g = (8 * b + 13 )/25;
int h = (19 * a + b - d - g + 15 )%30;
int j = c / 4;
int k = c % 4;
int m = a + 11 * h / 319;
int r = 2 * e + 2 * j - k - h + m + 32 % 7;
int n = h - m + r + 90 / 25;
int p = h - m + r + n + 19 % 32;

//Passes the value of n to the getEasterSundayMonth method
setEasterSundayMonth(n);


//Returns the day that Easter falls on that year
return p;
}

public void setEasterSundayMonth( int month)
{

String monthName = "Error";

//Assigns a month name according to the value of m
switch (month)
{
case 1: monthName ="January"; break;
case 2: monthName ="February"; break;
case 3: monthName ="March"; break;
case 4: monthName ="April"; break;
case 5: monthName ="May"; break;
case 6: monthName ="June"; break;
case 7: monthName ="July"; break;
case 8: monthName ="August"; break;
case 9: monthName ="September"; break;
case 10: monthName ="October"; break;
case 11: monthName ="November"; break;
case 12: monthName ="December"; break;
}
//Passes month value to m
m = monthName;
}

public String getEasterSundayMonth()
{

return m;

}
}
Roedy Green

2004-05-13, 8:31 pm

On 13 May 2004 15:20:46 -0700, jim.ferris@motorola.com (Jim) wrote or
quoted :

>Ok thanks that, worked..Im getting close here..now..Im trying to pass
>a parameter from one class to another but it doesnt appear to be
>working..check out the code for both classes:


See http://mindprod.com/jgloss/collections.html

And have a look at some other people's source code. Try running them
and tinkering with them. It is amazing what you accomplish just by
slaving copying.

You also need a textbook. There is no point in trying no figure out
such completely basic things purely by experiment.

See http://mindprod.com/jgloss/gettingstarted.html

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com