Control - Loops


Control - For Loops

WhileExamples.java

Deitel 4


Control - nested loops

ForExamples.java

Deitel 5


Classwork


Create a project, and in it a class with a main. 

Part A

In your main:

Using a for loop, print the numbers from 15 to 25 (inclusive)

Using a for loop, print the even numbers from 20 to 80 (inclusive)

Using a for loop, print the numbers from 100 to 0 (inclusive), largest to smallest.


Part B

  Create a class Frog with instance vars energy, an int, and scared, a boolean.  (hint: this class has instance vars so it also has...)

  Give Frog a method startle, which prints that the Frog was startled, and sets its scared boolean to true.

  Give Frog a method hop, which prints "Hopping" a number of times based on the Frog's energy-- so, if its energy was 43, it would print 43 times.  Then adjust the code so that if a Frog is scared, it will hop (print) twice as many times as its energy would usually cause (do NOT change the energy; never change an instance variable unless that is the method's job!). (hint: there are several ways to make this adjustment, you will need an if)

  Back in your main, create a frog, give it some energy, make it hop, startle it, print it, and then make it hop again.

[EC] In main, use a loop to make Frogs with from 1 to 10 energy, and do the above to each one.  (hint, you don't have to store each Frog in a different variable)