Click here to Skip to main content
15,898,222 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Java
public class Demo {

	public static String EvenOrOdd(int n) {
		String s = "";

		if (n % 2 == 0) {
			s = n + " is an Even number";
		} else {
			s = n + " is an Odd number";
		}
		return s;
	}

	public static String isPerfect(int n) {
		int sum = 0;
		String s = "";

		for (int i = 1; i < n; i++) {
			sum += i;

		}
		if (sum == n) {
			s = n + " is a perfect number";
		} else {
			s = n + " is not a perfect number";
		}

		return s;

	}

	public static int gcd(int a, int b) {
		int gcd = 1;
		int k = 2;

		while (k <= a && k <= b) {
			if (a % k == 0 && b % k == 0)
				gcd = k;
			k++;
		}

		return gcd;
	}

	public static int lcm(int a, int b) {
		int x;
		x = (a > b) ? a : b;
		while (true) {
			if (x % a == 0 && x % b == 0)
				return x;
			++x;
		}

	}

	public static String Palindrome(int n) {
		int palindrome = n;
		int reverse = 0;
		String s = "";

		while (palindrome != 0) {
			int remainder = palindrome % 10;
			reverse = reverse * 10 + remainder;
			palindrome = palindrome / 10;
		}

		if (n == reverse) {
			s = n + " is a Palindrome";
		} else {
			s = n + " is not a Palindrome";
		}
		return s;
	}

	public static int Reverse(int n) {
		int reverse = 0;

		while (n != 0) {
			reverse = reverse * 10;
			reverse = reverse + n % 10;
			n = n / 10;
		}

		return reverse;

	}

	public static int SumDigits(int n) {
		int sum = 0;

		while (n != 0) {
			sum += n % 10;
			n /= 10;
		}
		return sum;

	}

	public static String Prime(int n) {
		int j = 2;
		int result = 0;
		String s = "";

		while (j <= n / 2) {
			if (n % j == 0) {
				result = 1;
			}
			j++;
		}
		if (result == 1) {
			return s = n + " is not Prime";
		} else {
			return s = n + " is Prime";
		}

	}

	public static void CallRectangleMethod() {
		int width = (int) (Math.random() * 11);
		int height = (int) (Math.random() * 11);
	}

	// public static int RectangleArea(int width, int height) {
	// return width * height;
	// }

	// public static int RectanglePerimeter(int width, int height) {
	// return 2 * (width * height);
	// }

	public static void main(String[] args) {

	}
}

This is my code.
As you can see there is a method called "CallRectangleMethod" which do not take any parameters. And as you can see the method body in contains 2 randomly generated integers.
So what i am trying to do is i want to assign those two integers that were randomnly generated as parameters into the methods below called "RectangleArea" and "RectanglePerimeter".

What I have tried:

I tried calling the method name.get but unfortunately there exists no built-in methods like that in Java. So i need a little help
Posted
Updated 5-May-16 21:31pm

You can't.
The CallRectangleMethod effectively does nothing: it generates two random values, assigns them to local variables and then exits, throwing away the local variables it just used. It returns nothing, takes no parameters and has no way of supplying info to the outside world (other than via class level variables which you don't have, and which would be a poor idea in this case).
Probably the best way would be to make it return a Dimension (Java Platform SE 7 )[^] instance, and use that.
 
Share this answer
 
Try to use global variables.

Declare global variable and assign value to them with randomly generated integers. Then use this global variable values in required method.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900