[go: up one dir, main page]

0% found this document useful (0 votes)
47 views2 pages

JF 2 14 Practice

Uploaded by

akhilaakula53
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views2 pages

JF 2 14 Practice

Uploaded by

akhilaakula53
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

academy.oracle.

com

Java Fundamentals
2-14: Java Methods and Classes
Practice Activities

Lesson Objectives:
• Describe a method, class, and instance
• Describe a scenario where an IF control structure would be used
• Describe a scenario where a WHILE control structure would be used
• Recognize the syntax for a method, class, function, and procedure
• Describe input and output

Vocabulary:
Identify the vocabulary word for each definition below.

Statements that allow you to select and execute specific blocks of code while skipping
other sections.

An object of a class.

A specification, such as a blueprint or pattern and a set of instructions, of how to construct


something.

A piece of code that sends a message to an object asking it to perform an action.

Control structure that allows you to execute specific sections of the code a number of
times.

Try It/Solve It:


1. In Alice 3, you will create an animation of a child exercising. You will set variables and define a while control
statement. You will ask the end user for input and have the child say something (similar to creating output). Note: If
you already created the first part of the “Child Exercising” animation in a previous practice activity, open that
animation and skip to step 8 of these instructions.
a. Create a world with a child. Save the project as “Child Exercising”.
b. Have the child wave. Assign the value of the wave to “I’m happy”.
c. Have the child say “I would like to exercise today.”
d. Have the child do several exercises (side stretches, touch toes, jumping jacks). After exercising, have the
child stand and say, “I'm all done exercising.”
Copyright © 2019, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their
respective owners.
2

e. Change the code so that before the child exercises, you declare a variable of type Integer called numSets.
Set the default value to 3. Save your animation.
f. Use this value to control how many sets of exercises the child does.
g. Have the child wave and say goodbye at the end of the animation.
h. Modify the numSets variable so that it asks the user how many sets the child should do. (Hint: use the
getIntegerfromUser function.)
i. Using a while loop have the child say the number of sets remaining. When there is one set remaining, the
child should say “Last set.” (Hint: use an IF control statement.)
j. Save your animation.

2. Build on the previous animation from practice activity #1 above and add two other children to the animation with dif-
ferent variables (numset, numset2, numset3) to control their number of sets. Save your animation.

3. If you look at the code for the exercise program you can see that there is a lot of duplication in the myFirstMethod.
a. Create a procedure called exercise and move the code block that has the while loop and the “I’m done
exercising” statement from myFirstMethod to the new exercise procedure. Do this through the clipboard.
b. Create a parameter called numSets of type wholenumber
c. Add a local variable to the exercise procedure named sets of type wholenumber set its initial value equal to
numsets.
d. Change the instances of numsets in the code to sets
e. Replace the line that says numsets = numsets -1 to use the sets variable instead.
f. Save your animation.

4. Implement your new exercise method


a. Add a doTogether statement under the variable declaration section in myFirstMethod
b. Add an exercise call for each of the children using their associated variable (numset, numset2, numset3)
c. Disable the existing code that makes the children exercise.
d. Run your animation to test it.
e. Delete the code that is no longer required.
f. Save your animation.

5. In Alice 3, create an animation that uses both the IF and WHILE control structures to control an object's movement.

6. Complete the following Java Syntax Review Sheet:

Construct Syntax
IF ELSE syntax

WHILE syntax

Copyright © 2019, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their
respective owners.

You might also like