Wj 1103a Module 2 Exercise2

  • Uploaded by: Rajani Ramsagar
  • 0
  • 0
  • June 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Wj 1103a Module 2 Exercise2 as PDF for free.

More details

  • Words: 246
  • Pages: 2
Exercise 2: Using the for Loop The objective of this exercise is to write classes that use for loops.

Task 1 – Developing a Class Using the for Loop

In this task, you write a class that counts from 1 to any number. Follow these steps to write your class: 1. Go to the loops directory. 2. Write a class called CounterTwo containing a method called displayCount that: • Counts from 1 to the value of the MAX_COUNT constant, where the MAX_COUNT constant is a variable that you must declare and initialize to any number, using a for loop. • Displays the count. 3. Compile your program. 4. Use the CounterTwoTest.class file to test your program.

Task 2 – Developing Another Class Using the for Loop

In this task, you write a class called Sequence that displays a sequence starting with the numbers 1 and 1. Successive numbers in the sequence are the sum of the previous two numbers; for example, 1 1 2 3 5 8 13 and so on. Follow these steps to write your class: 5. Write a class called SequenceTwo containing a method called displaySequence. The displaySequence method should contain a for loop that: • •

Performs an operation to calculate a number in the sequence. Displays the first 10 numbers in the sequence. The result should look like: 1 1 2 3 5 8 13 21 34 55

6. Compile your program.

7.

Use the SequenceTwoTest.class file to test your program.

Related Documents


More Documents from "Rajani Ramsagar"