Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
153 views

Java Exercises On Methods

The document contains code examples demonstrating different Java programming concepts including: 1) Scoping - shows how variables can be declared at the class level as instance variables or within methods as local variables. The Scoping class outputs the values of variables with different scopes. 2) Factorial recursion - the FactorialTest class contains a recursive method to calculate the factorial of integers from 0 to 10. 3) Fibonacci recursion - the FibonacciTest class contains an interactive applet that takes a number from the user and uses recursion to calculate the Fibonacci value. 4) Method overloading - the MethodOverload class overloads the square method to handle both integer and double parameters, demonstrating polymorphism.

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)
153 views

Java Exercises On Methods

The document contains code examples demonstrating different Java programming concepts including: 1) Scoping - shows how variables can be declared at the class level as instance variables or within methods as local variables. The Scoping class outputs the values of variables with different scopes. 2) Factorial recursion - the FactorialTest class contains a recursive method to calculate the factorial of integers from 0 to 10. 3) Fibonacci recursion - the FibonacciTest class contains an interactive applet that takes a number from the user and uses recursion to calculate the Fibonacci value. 4) Method overloading - the MethodOverload class overloads the square method to handle both integer and double parameters, demonstrating polymorphism.

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/ 4

Exercises on methods

// Fig. 6.10: Scoping.java


// A scoping example
import java.awt.Container;
import javax.swing.*;

public class Scoping extends JApplet {


JTextArea outputArea;
int x = 1; // instance variable

public void init()


{
outputArea = new JTextArea();
Container c = getContentPane();
c.add( outputArea );
}

public void start()


{
int x = 5; // variable local to method start

outputArea.append( "local x in start is " + x );

methodA(); // methodA has automatic local x


methodB(); // methodB uses instance variable x
methodA(); // methodA reinitializes automatic local x
methodB(); // instance variable x retains its value

outputArea.append( "\n\nlocal x in start is " + x );


}

public void methodA()


{
int x = 25; // initialized each time a is called

outputArea.append( "\n\nlocal x in methodA is " + x +


" after entering methodA" );
++x;
outputArea.append( "\nlocal x in methodA is " + x +
" before exiting methodA" );
}

public void methodB()


{
outputArea.append( "\n\ninstance variable x is " + x +
" on entering methodB" );
x *= 10;
outputArea.append( "\ninstance variable x is " + x +
" on exiting methodB" );
}
}
<html>
<applet code="Scoping.class" width=280 height=260>
</applet>
</html>

1
// Fig. 6.12: FactorialTest.java
// Recursive factorial method
import java.awt.*;
import javax.swing.*;

public class FactorialTest extends JApplet {


JTextArea outputArea;

public void init()


{
outputArea = new JTextArea();

Container c = getContentPane();
c.add( outputArea );

// calculate the factorials of 0 through 10


for ( long i = 0; i <= 10; i++ )
outputArea.append(
i + "! = " + factorial( i ) + "\n" );
}

// Recursive definition of method factorial


public long factorial( long number )
{
if ( number <= 1 ) // base case
return 1;
else
return number * factorial( number - 1 );
}
}

<html>
<applet code="FactorialTest.class" width=275 height=195>
</applet>
</html>

// Fig. 6.13: FibonacciTest.java


// Recursive fibonacci method
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class FibonacciTest extends JApplet


implements ActionListener {
JLabel numLabel, resultLabel;
JTextField num, result;

public void init()


{
Container c = getContentPane();
c.setLayout( new FlowLayout() );

2
numLabel =
new JLabel( "Enter an integer and press Enter" );
c.add( numLabel );

num = new JTextField( 10 );


num.addActionListener( this );
c.add( num );

resultLabel = new JLabel( "Fibonacci value is" );


c.add( resultLabel );

result = new JTextField( 15 );


result.setEditable( false );
c.add( result );
}

public void actionPerformed( ActionEvent e )


{
long number, fibonacciValue;

number = Long.parseLong( num.getText() );


showStatus( "Calculating ..." );
fibonacciValue = fibonacci( number );
showStatus( "Done." );
result.setText( Long.toString( fibonacciValue ) );
}

// Recursive definition of method fibonacci


public long fibonacci( long n )
{
if ( n == 0 || n == 1 ) // base case
return n;
else
return fibonacci( n - 1 ) + fibonacci( n - 2 );
}
}

<html>
<applet code="FibonacciTest.class" width=325 height=75>
</applet>
</html>

// Fig. 6.16: MethodOverload.java


// Using overloaded methods
import java.awt.Container;
import javax.swing.*;

public class MethodOverload extends JApplet {


JTextArea outputArea;

public void init()


{

3
outputArea = new JTextArea( 2, 20 );
Container c = getContentPane();
c.add( outputArea );

outputArea.setText(
"The square of integer 7 is " + square( 7 ) +
"\nThe square of double 7.5 is " + square( 7.5 ) );
}

public int square( int x )


{
return x * x;
}

public double square( double y )


{
return y * y;
}
}
<html>
<APPLET CODE = "MethodOverload.class" WIDTH = 275 HEIGHT = 40 >
</APPLET>
</html>

You might also like