CSci 157 Introduction to Programming - Lab 7


The purpose of this lab is to gain experience with looping constructs, the Scanner class, and ActiveObjects.

Part I.  Create a class LoopTester as described below. This class will not use Objectdraw.
Number 1:

System.out.println("This method sums three values");
sum = 0;

for ( i=0; i<3; i=i+1 ) {
System.out.println(" Next value to sum, please? ");
x = in.nextInt();
sum = sum + x;
}
System.out.println("Sum = " + sum);
Number 2:
    for (i=0; i<10; i=i+1)
for (j=0; j<10; j=j+2)
System.out.println(i*j);

Number 3:


double checkAmt, bankBalance = 500.00 ;
System.out.println("You're balance is $" + bankBalance );

do {
System.out.println("Debit Amt? ");

// user enters positive values representing checkcard purchases

checkAmt = in.nextDouble();
System.out.println(" - $" + checkAmt);
bankBalance = bankBalance - checkAmt;
} while ( bankBalance > 0 );

System.out.println("You are overdrawn by $" + ( - bankBalance ) );
Number 4:
x = 10.0;
while (x < 100.0) {
System.out.println("" + x + "is less than 100");
x = x * .5;
}

NOW: in BlueJ, create a new object of type LoopTester and use the (right-click) menu to test each method individually.  Run them twice each and record the results of each run. For the two that request user input, try them with input of your own devising but record what values you input along with the results. If the result is an infinite loop - record that. Write up the results (cleanly, legibly, and well labeled) and turn in to the instructor.

Part II. Playing with Active Objects

snow

  1. Switch driver and observer.
     
  2. Create a new project called Snow.  Download the Snow project files into the project folder:  Snow, Cloud, FallingSnow, and snow.gif.  Compile the project and test it.

  3. Spend some time reading the code in each file and getting a basic understanding of it.  Discuss it with your partner so you both have enough of a grasp of it to do the next few problems.

  4. The x-coordinate and speed of each snowflake is controlled by a single random number generator, specified in Cloud.  Add a second number generator so it can be used to control the speed.  Experiment with a range of possible values until you like the results.

  5. The snow image is transformed to a VisibleImage in the FallingSnow constructor.  Using the VisibleImage constructor that takes a width and height parameter, make the snowflakes bigger or smaller.  Experiment until you get something you like.

  6. Search the Web for pictures of individual snowflakes (in GIF, JPG, or PNG format).  Download at least two, and use the getImage method to load them into your program. Randomly select between these images when creating a new FallingSnow object.  Note: you will probably need to modify both Snow.java and Cloud.java.

  7. Demonstrate your changes to the Instructor.