3) Methods - A Deeper Look

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Experience has shown that the best way to develop and maintain a large

program is to construct it from small, simple pieces, or modules. This


technique called divide and conquer.

Static methods can be called without the need for an object of the class to exist.
Java is able to keep track of which method is currently executing, how local
variables of methods are maintained in memory and how a method knows
where to return after it completes execution.

Declaring Methods with Multiple Parameters


// Maximo.java
// Programmer-declared method maximum with three
double parameters.

import java.util.Scanner;

public class Maximo


{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);

System.out.print("Digite tres numeros do


tipo double separados por espaço: ");

double number1 = input.nextDouble();


double number2 = input.nextDouble();
double number3 = input.nextDouble();

// determine the maximum value


double max = maximum(number1, number2,
number3);

System.out.println("O numero maximo e: " +


max);
}

Ibraimo Aly Learning Java


// returns the maximum of its three double
parameters
public static double maximum(double x, double
y, double z)
{
double maximumValue = x; // assume x is the
largest to start

if (y > maximumValue)
maximumValue = y;

if (z > maximumValue)
maximumValue = z;

return maximumValue;
}
}

The set of packages available in Java is quite large. Java includes packages for
complex graphics, advanced graphical user interfaces, printing, advanced
networking, security, database processing, multimedia, accessibility (for people
with disabilities), concurrent programming, cryptography, XML processing
and many other capabilities.

Case Study: Secure Random-Number Generation

// RandomNumbers.java
// Shifted and scaled random integers.
import java.security.SecureRandom;

public class RandomNumbers


{
public static void main(String[] args)
{
// randomNumbers object will produce secure
random numbers
Ibraimo Aly Learning Java
SecureRandom randomNumbers = new
SecureRandom();

// loop 20 times
for (int i = 1; i <= 20; i++)
{
// pick random integer from 1 to 6
int face = 1 + randomNumbers.nextInt(6);

System.out.printf("%d ", face); //


display generated value

// if i is divisible by 5, start a new


line of output
if (i % 5 == 0)
System.out.println();
}
}
}

Method Overloading

Overloading is used to implement methods that perform similar tasks for


arguments of different types or for different numbers of arguments.
// MethodOverload.java
// Overloaded method declarations.

public class MethodOverload


{
public static void main(String[] args)
{
System.out.printf("Media do tipo int e
%d%n", media(5));
System.out.printf("Media do tipo double e
%f%n", media(3.5));
}

// square method with int argument


public static int media(int intValue)
{
System.out.printf("%nMedia mediante um
Ibraimo Aly Learning Java
argumento do tipo int: %d%n",
intValue);
return intValue * intValue;
}

// square method with double argument


public static double media(double doubleValue)
{
System.out.printf("%nMedia mediante um
argumento do tipo double: %f%n",
doubleValue);
return doubleValue * doubleValue;
}
}

(Optional) GUI and Graphics Case Study: Colors and Filled Shapes

// DrawSmiley.java

import java.awt.Color;
import java.awt.Graphics;
import javax.swing.*;
import javax.swing.UIManager.*;

public class DrawSmiley extends JPanel


{
public void paintComponent(Graphics g)
{
super.paintComponent(g);

// draw the face


g.setColor(Color.YELLOW);
g.fillOval(10, 10, 200, 200);

// draw the eyes


g.setColor(Color.BLACK);
g.fillOval(55, 65, 30, 30);
g.fillOval(135, 65, 30, 30);

// draw the mouth


g.fillOval(50, 110, 120, 60);
Ibraimo Aly Learning Java
// "touch up" the mouth into a smile
g.setColor(Color.YELLOW);
g.fillRect(50, 110, 120, 30);
g.fillOval(50, 120, 120, 40);
}
}

// DrawSmileyTest.java
// Test application that displays a smiley face.
import javax.swing.JFrame;

public class DrawSmileyTest


{
public static void main(String[] args)
{
DrawSmiley panel = new DrawSmiley();
JFrame obj = new JFrame();

obj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
obj.add(panel);
obj.setSize(500, 500);
obj.setVisible(true);
obj.setLocationRelativeTo(null);
}
}

Ibraimo Aly Learning Java

You might also like