writing and calling methods - PowerPoint PPT Presentation

1 / 12
About This Presentation
Title:

writing and calling methods

Description:

Starter 'uses' MathUser. MathUser 'has a' MathClass. a deficiency ... public Starter( ) { new MathUser( ); MathClass calc = new Mathclass ... – PowerPoint PPT presentation

Number of Views:17
Avg rating:3.0/5.0
Slides: 13
Provided by: msresearch
Category:

less

Transcript and Presenter's Notes

Title: writing and calling methods


1
writing and calling methods
2
  • package CSE115MethodExamples
  • public class MathClass
  • public MathClass( ) // empty
    constructor
  • public void add( int A, int B)
  • int C
  • C A B
  • System.out.println( C )
  • // end Add
  • public void multiply( int A, int B)
  • int C
  • C A B
  • System.out.println( C )
  • // end Multiply
  • // end class MathClass

MathClass is a very useful class, but what does
"void" mean?
3
  • package CSE115MethodExamples
  • public class MathUser
  • public MathUser( )
  • MathClass calc new MathClass( )
  • calc.add (10, 4 )
  • calc.multiply( 1200, 167 )
  • package CSE115MethodExamples
  • public class Starter
  • public static void main(String args )
  • MathUser app new MathUser( )

Starter "uses" MathUser
MathUser "has a" MathClass
4
a deficiency
  • MathClass.add and MathClass.multiply ONLY PRINT
    TO THE SCREEN
  • What if you need the results in MathUser?

5
  • package CSE115MethodExamples
  • public class MathClass
  • public MathClass( ) // no constructor
  • public int add( int A, int B)
  • int C
  • C A B
  • return( C )
  • // end Add
  • public int multiply( int A, int B)
  • int C
  • C A B
  • return( C )
  • // end Multiply
  • // end class MathClass

replace the "prints" with "returns"
6
  • package CSE115MethodExamples
  • public class MathUser
  • public MathUser( )
  • int x, y
  • MathClass calc new Mathclass( )
  • x calc.add (10, 4 )
  • y calc.multiply( 1200, 167 )
  • System.out.println( x " " y )

7
MathClass "specification"
  • package CSE115MethodExamples
  • public interface IMathClass
  • public int add( int A, int B)
  • public int multiply( int A, int B)
  • // end interface IMathClass

Why?
8
why specify an interface? especially if the class
already works?
  • So that in the future, other programmers can
    produce a better MathClass that works the same
    way
  • It is a way of advertising what the MathClass does

9
final MathClass
  • package CSE115MethodExamples
  • public class MathClass implements IMathClass
  • public MathClass( ) // no constructor
  • public int add( int A, int B)
  • int C
  • C A B
  • return( C )
  • // end Add
  • public int multiply( int A, int B)
  • int C
  • C A B
  • return( C )
  • // end Multiply
  • // end class MathClass

10
MathUser
Starter
IMathClass
uses
has a
implements, or realizes
MathClass calc new Mathclass( )
public Starter( ) new MathUser( )
MathClass
public class MathClass implements IMathClass
11
MathClass
MathClass
C
  • C MathClass( )
  • int add( int A, int B)
  • int multiply( int A, int B )

12
IMathClass
MathClass
I
  • int add( int A, int B)
  • int multiply( int A, int B )
Write a Comment
User Comments (0)
About PowerShow.com