20
Methods in Code COMPLEX BOOLEAN EXPRESSIONS

Methods in Code - gorskicompsci.cagorskicompsci.ca/ICS3U/Unit2/ppt_MethodsinCode.pdfBatman lego? Then should I give it to ... tac toe game. What sorts of things do you need to do?

Embed Size (px)

Citation preview

Methods in CodeCOMPLEX BOOLEAN EXPRESSIONS

I have a lot of lego. If

you find any more

lego, you should give

it to someone else.

The code for my

decision making process

follows.

public class lego

{

public static void main (String args[])

{

new lego ();

}

public lego ()

{

char isLego = IO.inputChar ("Is it lego? ");

if (isLego == 'y' || isLego == 'Y')

System.out.println ("Give it to someone else.");

}

}

But what if it is

Batman lego?

Then should I

give it to

someone else?

Hmmm…. No. You

should give me the

batman lego but

give everything

else to another kid.

But what if it is Fire-

fighter lego?

No. I should

get that too.

How about Pirate

lego?

No. Save

that for me. How about

Except for Batman, superheroes,

fire-fighters, police, pirates, trains,

helicopters, space, star wars, cars,

animals, castles, knights, boats, I SHOULD GIVE ANY LEGO I FOUND TO OTHER KIDS.

public lego ()

{ char isLego = IO.inputChar ("Is it lego? ");

String type = IO.inputString ("What type? ");

if (isGood (isLego, type))

System.out.println ("Give it to my nephew.");

elseSystem.out.println ("Give it to someone else.");

}

public boolean isGood (char isLego, String type)

{ if (isLego != 'y' && isLego != 'Y')

return false;

else if (type.equalsIgnoreCase ("pirates") || type.equalsIgnoreCase ("boats"))

return true;

else if (type.equalsIgnoreCase ("superheroes") || type.equalsIgnoreCase ("batman"))

return true;

else if (type.equalsIgnoreCase ("star wars") || type.equalsIgnoreCase ("space"))

return true;

else if (type.equalsIgnoreCase ("cars") || type.equalsIgnoreCase ("trains"))

return true;

else if (type.equalsIgnoreCase ("planes") || type.equalsIgnoreCase ("helicopters"))

return true;

else

return false;

}

Consider a tic tac toe

game. What sorts of

things does your code

need to do?

Get the

player’s move

Check if the

move is valid

Draw the

board

Place the

player

See if there is a

winner

Print the winner

Consider a tic

tac toe game.

What sorts of

things do you

need to do?

Get the

player’s move

Check if the

move is valid

Draw the

board

Place the

player

See if there is a

winner

Print the winner

Get the

player’s move

Draw the

board

Place the

player

Print the winner

isValid

(move)

!Done()

Start

End

The diagram type that explains method relationships is a

STRUCTURE CHART.

In

Out

A key part of

methods is

things coming

in and out of

them.

What does the

code on the

front of your

sheet do?

Start off variables

Start off variables

!done

Start off variables

!done

Get guess

Start off variables

!done

Get guess

Print Response

Start off variables

!done

Get guess

Print Response

If lost, print message

Start off variables

!done

Get guess

Print Response

If lost, print message

Main

Guessing

Response IsDone

In

In

Out

Out

Start off variables

!done

Get guess

Print Response

If lost, print message

Main

Guessing

Response

s, g s, g, c boolean

IsDone

In

In

Out

Out