0

Hey guys, I got such great help last time I decided to come back for more :). Hope I'm not bothering with these questions, my teacher isn't much for being nice or explaining..

Right now we are getting into methods, I know that they aren't that difficult but I for the life of me can't seem to wrap my head around them.

I know that we have the main method, and then methods after that are user created. What I don't understand is how I send variables to the user created methods, and when to initialize or if I need to multiple times since the methods are separate. I know how to return the variables, that's easy enough.

Thanks.

3
Contributors
6
Replies
7
Views
6 Years
Discussion Span
Last Post by peter_budo
0

Simple examples

public class SimpleMethods{
	
	public int additionMethod(int x, int y){
		int c = x + y;
		return c;
	}
	
	public int subtractionMethod(int a, int b){
		return a - b;
	}
	
	public int multiplicationMethod(int a, int b){
		return a * b;
	}
	
	public int divisionMethod(int a, int b){
		return a / b;
	}
	
	public int modulusMethod(int a, int b){
		return a % b;
	}
}
public class MethodsExample{
	private static SimpleMethods simpleMethods = new SimpleMethods();
	
	public static void main(String[] args){
		int a = 8;
		int b = 4;
		
		System.out.println("multiplication of a by number 10 is " + multiplicationByTen(a));
		
		int c = simpleMethods.additionMethod(a, b);
		System.out.println("c = "+c);
		
		System.out.println("subtraction of a and b is "+ simpleMethods.subtractionMethod(a, b));
		System.out.println("multiplication of a and b is "+ simpleMethods.multiplicationMethod(a, b));
		System.out.println("division of a and b is "+ simpleMethods.divisionMethod(a, b));
		System.out.println("modulus of a and b is "+ simpleMethods.modulusMethod(a, b));
		
		callAllMethods(a, b);
	}
	
	public static int multiplicationByTen(int a){
		return a * 10;
	}
	
	public static void callAllMethods(int a, int b){
		System.out.println("call from callAllMethods - addition of a and b is "+ simpleMethods.additionMethod(a, b));
		System.out.println("call from callAllMethods - subtraction of a and b is "+ simpleMethods.subtractionMethod(a, b));
		System.out.println("call from callAllMethods - multiplication of a and b is "+ simpleMethods.multiplicationMethod(a, b));
		System.out.println("call from callAllMethods - division of a and b is "+ simpleMethods.divisionMethod(a, b));
		System.out.println("call from callAllMethods - modulus of a and b is "+ simpleMethods.modulusMethod(a, b));
	}
}
  • First method call ( multiplicationByTen(a) )is on a method that is accessible inside MethodsExample class.
  • Second method call ( simpleMethods.additionMethod(a, b) ) is on a method that is available through simpleMethods instance of class SimpleMethods. Here you can see that result of the method can be assigned to a variable and can be used later on by caller method or class depending on variable declaration (local/global)
  • At the end you will find method call ( callAllMethods(a, b) ) that will call method callAllMethods that inside it body will call methods of simpleMethods instance to demonstrate that you can call methods from a methods and you can call it as many times you need it

Hope this helps

0

Ok I think I understand. I tried to use a method to find out if a number is prime or not, but I don't think I did it correctly. The program just keeps running if the number is not prime.

import java.util.Scanner;
public class prime   
{
public static void main (String[] args)
{
    Scanner keyboard = new Scanner(System.in);
    System.out.println("please enter a num");
    int num= keyboard.nextInt();       

    boolean flag= isPrime(num);
    System.out.println(flag);
}

public static boolean isPrime (int num)
{
    int i=2;
    boolean flag=true;
    
    while ((i<num/2)&&(flag=true))
        {
            if (num%i==0)
                flag=false;
             
            else i++;
            }
            
            return flag;
}
}
0

Ah I see. Stupid little mistakes.

I think overall I understand methods, but I was confused today when my professor told us that it doesn't matter what parameters you give a method when calling it, how does that work?

0

I think that is probably only a part of his statement, because without any context of the discussion that doesn't make any sense.

0

If you mean method overloading than it is this

public void addNumbers(double a, double b){
//code here
}

public void addNumbers(int a, int b){
//code here

/**
* Somewhere in the class
*/
double x = 2.1;
double y = 2.3;
double z = addNumbers(x,y);

int k = addNumbers(5, 7);
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.