0% found this document useful (0 votes)
58 views

Java Methods

Modules in Java are called methods and classes. Methods are invoked by method calls and can be programmer-defined to perform specific tasks. The Java API provides a rich collection of pre-defined classes and methods for common operations like math, strings, I/O and more. Programmers can write their own methods to reuse functionality in a program.

Uploaded by

Man Runner
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views

Java Methods

Modules in Java are called methods and classes. Methods are invoked by method calls and can be programmer-defined to perform specific tasks. The Java API provides a rich collection of pre-defined classes and methods for common operations like math, strings, I/O and more. Programmers can write their own methods to reuse functionality in a program.

Uploaded by

Man Runner
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 5

Methods

Modules in java are called methods and classes. Java programs are written by
combining new methods and classes the programmer writes with “prepackaged” methods
and classes available in the Java API (also referred to as the Java class library) and in
various other method and class libraries.

The Java API provides a rich collection of classes and methods for performing common
mathematical calculations, string manipulations, character manipulations, input/output,
error checking and many other useful operations.

As well, the programmer can write methods to define specific tasks that need to be used
at many points in a program. They are called programmer-defined methods.
A method is invoked by a method call.

// Fig. 6.3: SquareInt.java


// A programmer-defined square method
import java.awt .Container; class
import javax.swing.*;

public class SquareInt extends JApplet {


public void init()
{ Package
String output = "";

JTextArea outputArea = new JTextArea( 10, 20 );


// get the applet's GUI component display area
Container c = getContentPane(); GUI component object
Attached to the container
// attach outputArea to Container c
c.add( outputArea ); a method --- it assigns
c the result of the call to
method getContentPane

int result;
for ( int x = 1; x <= 10; x++ ) { Calling the square method
result = square( x );
output += "The square of " + x +
" is " + result + "\n";
}
outputArea.se tText( output ); Declares the container reference C
}
method of the container
// square method definition arguments
public int square( int y )
{ programmer-defined method
return y * y;
}
}

// Fig. 6.3: SquareInt.java


// A programmer-defined square method
import java.awt.Container;
import javax.swing.*;

public class SquareInt extends JApplet {


public void init()
{
String output = "";

JTextArea outputArea = new JTextArea( 10, 20 );

// get the applet's GUI component display area


Container c = getContentPane();

// attach outputArea to Container c


c.add( outputArea );

int result;

for ( int x = 1; x <= 10; x++ ) {


result = square( x );
output += "The square of " + x +
" is " + result + "\n";
}

outputArea.setText( output );
}

// square method definition


public int square( int y )
{
return y * y;
}
}
// Fig. 6.4: Maximum.java
// Finding the maximum of three doubles
import java.awt.Container;
import javax.swing.*;

public class Maximum extends JApplet {


public void init()
{
JTextArea outputArea = new JTextArea();

String s1 = JOptionPane.showInputDialog(
"Enter first floating-point value" );
String s2 = JOptionPane.showInputDialog(
"Enter second floating-point value" );
String s3 = JOptionPane.showInputDialog(
"Enter third floating-point value" );

double number1 = Double.parseDouble( s1 );


double number2 = Double.parseDouble( s2 );
double number3 = Double.parseDouble( s3 );

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

outputArea.setText( "number1: " + number1 +


"\nnumber2: " + number2 +
"\nnumber3: " + number3 +
"\nmaximum is: " + max );

// get the applet's GUI component display area


Container c = getContentPane();

// attach outputArea to Container c


c.add( outputArea );
}

// maximum method definition


public double maximum( double x, double y, double z )
{
return Math.max( x, Math.max( y, z ) );
}
}

// Fig. 6.7: RandomInt.java


// Shifted, scaled random integers
import javax.swing.JOptionPane;

public class RandomInt {


public static void main( String args[] )
{
int value;
String output = "";
for ( int i = 1; i <= 20; i++ ) {
value = 1 + (int) ( Math.random() * 6 );
output += value + " ";

if ( i % 5 == 0 )
output += "\n";
}

JOptionPane.showMessageDialog( null, output,


"20 Random Numbers from 1 to 6",
JOptionPane.INFORMATION_MESSAGE );

System.exit( 0 );
}
}

// Fig. 6.8: RollDie.java


// Roll a six-sided die 6000 times
import javax.swing.*;

public class RollDie {


public static void main( String args[] )
{
int frequency1 = 0, frequency2 = 0,
frequency3 = 0, frequency4 = 0,
frequency5 = 0, frequency6 = 0, face;

// summarize results
for ( int roll = 1; roll <= 6000; roll++ ) {
face = 1 + (int) ( Math.random() * 6 );

switch ( face ) {
case 1:
++frequency1;
break;
case 2:
++frequency2;
break;
case 3:
++frequency3;
break;
case 4:
++frequency4;
break;
case 5:
++frequency5;
break;
case 6:
++frequency6;
break;
}
}
JTextArea outputArea = new JTextArea( 7, 10 );

outputArea.setText(
"Face\tFrequency" +
"\n1\t" + frequency1 +
"\n2\t" + frequency2 +
"\n3\t" + frequency3 +
"\n4\t" + frequency4 +
"\n5\t" + frequency5 +
"\n6\t" + frequency6 );

JOptionPane.showMessageDialog( null, outputArea,


"Rolling a Die 6000 Times",
JOptionPane.INFORMATION_MESSAGE );
System.exit( 0 );
}
}

You might also like