Wednesday, February 10, 2010

Alice - - Day 7

Functions that Return Values

Read through the linked tutorial

Lab Project
Complete the following lab, save to the X:\ in the proper class folder

Begin with the program named Alice0135b created through the tutorial.

Create and add a new function belonging to the Coach object named getChicken.

Update the main method to that shown in Listing 3.

Listing 3. Updated main method for Alice135LabProjA.a2w.

public void main ( ) {

// Copyright 2007 R.G.Baldwin
// Program demonstrates defining two new functions.
doInOrder {

print( ( Distance to chicken is: + ( ( coach.getDistanceToChicken ( ) ) .toString() ) ) );
( coach.getChicken ( ) ) .turn( RIGHT , 2 revolutions );
}
}

When you play your world, it will display the distance from the coach to the chicken as shown in Figure 4 (it is not necessary to match the value of the distance shown in Figure 4). Then the chicken will turn to the right, making two complete revolutions around its own green axis.

Save your world in a file named Alice135LabProjA.a2w and be prepared to deliver it to your instructor in whatever manner the instructor specifies.

Make certain that your preferences are set to Java Style in Color.

Select Export Code For Printing... on the File menu and save your source code in a file named Alice135LabProjA.html. Also be prepared to deliver this file to your instructor in whatever manner the instructor specifies.

_______________________________________

Data Types and Variables

Read through the linked tutorial

Lab Project

Create a new world using the grass template. Place a Coach object in the world.

Rename the default method main. The method named main must be empty except for comments and a call to a world-level method named processData.

Write the code in the method named processData to do the following:

  • Get the coach's name from the user as shown in Figure 8 and store the name in a variable of type String named coachesName.
  • Display the coach's name for five seconds as shown in Figure 9.
  • Get the coach's age from the user as shown in Figure 10 and store the age in a variable of type Number named coachesAge.
  • Display the coach's age for five seconds as shown in Figure 11.

Figure 8. Input dialog for coach's name.

Figure 9. Program output for coach's name.

Figure 10. Input dialog for coach's age.

Figure 11. Program output for coach's age.

Save your world in a file named Alice140LabProjA.a2w and save to the X:\ in the proper class folder.

Make certain that your preferences are set to Java Style in Color.

Select Export Code For Printing... on the File menu and save your source code in a file named Alice140LabProjA.html. Also be prepared to deliver this file to your instructor in whatever manner the instructor specifies.

No comments:

Post a Comment