Robotics C++ Physics II AP Physics B Electronics Java Astronomy Other Courses Summer Session  

Methods

 

Basic Ideas

A Very Simple Example

Conversion with Code in Main

Conversion using a Method

Passing an Array to a Method

Exercises

 

Basic Ideas

 

   The main method should be as short as possible

   The main method  should declare needed variables and call methods to do the work

   Calling a method from the main method

     Ø Use the name

     Ø If something is returned then assign it to a variable. For example, the following line of code will call a method (note the () which signifies

               a method

             as opposed to a variable) and assign the returned value to the variable amount     amount = figureIt()

     Ø The return type must be declared in the method header

                 public static void figureIt()

                {

 

                }

          Ø void means the method does not return anything

          Ø int means the method returns an integer

          Ø float means the method returns a float

 

A Very Simple Example

 

public class UsingMethods

{

            public static void main(String[] args)

            {

                        int num1 = 5;

                        int num2 = 6;

                        int sum;

                        int product;

                        announcement();

                        sum = addNumbers(num1, num2);

                        product = multiplyNumbers(num1, num2);

                        giveResults(sum, product);

            }

            static void announcement()

            {

                        System.out.println("Welcome to the sophisticated math progam");

            }

            static int addNumbers(int x, int y)

            {

                        int theSum = x + y;

                        return theSum;

            }

            static int multiplyNumbers(int a, int b)

            {

                        int theProduct = a*b;

                        return theProduct;

            }

            static void giveResults(int y, int z)

            {

                        System.out.println("The sum is: " + y);

                        System.out.println("The product is: " + z);

            }

}

 

Output

 

Welcome to the sophisticated math progam

The sum is: 11

The product is: 30

 

Conversion with Code in Main

 

import javax.swing.*;

class Methods

{

  public static void main(String[ ] args)

  {

        double convertedAmount;

        String GallonsIn = JOptionPane.showInputDialog("Enter amount in gallons");

        double gallons = Double.parseDouble(GallonsIn);

 

        convertedAmount = gallons*3.785;           

        JOptionPane.showMessageDialog(null,"The amount in liters 

             is"+convertedAmount,"Conversions",JOptionPane.PLAIN_MESSAGE);

}

 

Conversion Using a Method:

 

Example 1

 

import javax.swing.*;

class Methods

{

  public static void main(String[ ] args)

  {

        double convertedAmount;

        String GallonsIn = JOptionPane.showInputDialog("Enter amount in gallons");

        double gallons = Double.parseDouble(GallonsIn);

 

        convertedAmount = toLiters (gallons);                 

        JOptionPane.showMessageDialog(null,"The amount in liters 

             is"+convertedAmount,"Conversions",JOptionPane.PLAIN_MESSAGE);

 

  }

 

  public static double toLiters(double amount)          //note the use of static

   {

         return amount * 3.785;

   }

}

 

 

 

Example 2

 

import javax.swing.*;

public class Charing

{

    public static void main(String[ ] args)

    {

        double convertedAmount;

          String GallonsIn = JOptionPane.showInputDialog("Enter amount in gallons");

        double gallons = Double.parseDouble(GallonsIn);

        char response = toLiters (gallons);

        JOptionPane.showMessageDialog(null,"Answer is " + response, "Title", JOptionPane.PLAIN_MESSAGE);

    }

 

    public static char toLiters(double amount) //note the use of static

    {

        if (amount>0)

            return 'y';

        else

            return 'n';

    }

}