Kategorier: Alle - method - implementation

av nguyễn thị thương 4 år siden

307

Organigramme en arbre

The text outlines the structure of two different Java methods designed to find the minimum value. The first method is intended to find the minimum value in an array of integers, though it lacks a full implementation.

Organigramme en arbre

public static void main(String[] args) { int[] a = {1, 1, 2, 3, 5, 8, 13}; int x = TextIO.getInt(); int isAt = search(a, x); System.out.printf("%d is in %s at %d", x, a, isAt); }

methodInvocation

argumentList

'x','a'

getInt

typeName

TextIO

localVariableDeclarationStatemetn

VariableDeclaratorList

variableDeclarator

VariableInitialiser

{1,1,2,3,5,8,13}

isAt

x

formalParameterList

variableDeclaratorld

args

String[]

main

void
public static

EX5 static int search(int[] a, int x) { for (int i = 0; i < a.length; i++) { if (x == a[i+0]) return i; } return -1; }

Method Body

returnexpression

-1

ifthenStatement

return i

x==a[i+0]

ForStatement

unaryexpression

forupdate

i++

relationexpression

i< a.length

forInit

i=0

unannprimitiveType

forrmalParameterList

FormalParameters

int x

unannPrimitiveType

numericType

identifierChars

search

réult

EX5: private static float min(float a, float b) { float min; if (a <= b) { min = a; } else { min = b; } return min; }

IfStatement

IfthenElseStatement

assignment expression

expressionStatement

min =a

min=b

relationExpression

a<=b

blockStatements

localDeclarationStatement

localVariableDeclaration

'min'

FormalParameterList

Variabledeclaratorid

idnetifierChá

MethodModifierr

EX4: private static float min(float a, float b) { float min; min = a <= b ? a : b; return min; }

MethodBody

ReturnStatement

return min

LocalVariableDeclarationStatement

LocalVariableDeclaration

VariableDeclarationList

min

MethodHeader

MethodDeclarator
FormalparameterList

formalParameter

VariableDeclaratord

b

FormalParameter

NumericType

VariableModifier

JVLetterorDigit

'i','n''

JVLetter

result

MethodModifier

private static

EX3: static int nextCount(int i) { int j; { j = i + 1; } return j; }

Method Declaration

Method Body

block
returnStatement

return j

blockStatement

LocalVariabeDeclarationStatement

LocalVảiableDeclaration

LocalVariableDeclarationList

VariableDeclarator

VariableInitializer

expression

i+1

j

UnannType

Method Header

Method Declarator
fomalParameterList

VariableDeclaratorld

iidentifier

i

unannType

UnannPrimitiveType

identifierChar

nextCount

int

Method Modifier

EX2: static float min(int[] a) { // TODO: implements this }

medthod Declaration

Sujet principal

Method body

blockstatement
comment

//TODO: implement this

Method header

MethodDeclaration
fomalParameter

variable declaratorID

a

UnnaType

int[]

identifier

JVletterorDigit

'i','n'

JVletter

'm'

Result
float

Method modifier

static