Objectives

  • Write Java statements to create and use simple arrays
  • Write Java statements to iterate through a simple array using a foreach loop

Overview

This week the text introduces the concept of a simple array. This structure will allow us to create a variable that can hold a collection of items from the same data type. It also teaches us about another looping structure, the foreach loop, which is intended to be used to go through and read each element in an array. Once again we'll be looking at your code to be sure you are using the correct looping type, while, for, foreach, do/while.

For this exercise, we will simulate an interesting scenario where there is a system board with a row of unlit lights, along with their corresponding switches. The problem is such that we will have one person for each light on the board. One at a time, the people will come up to the board and switch the lights in a specific manner, as described. People and lights will be numbered using a 1based system. The first person (#1) will simply start with the light #1 and toggle every light (so that after their 'turn', all the lights will be on). The second person (#2) will then start at light #2 and toggle every second light. The third person (#3) will then start at light #3 and toggle every third light. And so on until each person has taken their turn. At the end, we'll want to see a count of the lights that are turned on, as well as a listing that describes the status of each light in the board.

The Problem Statement Light

You will need to develop a class named Light, which will keep track of the information about a light. Please know that the Light can either be on or it can be off.

This class should define methods:

  • A constructor that creates a light that is currently off
  • A method named toggle that can be used to change the status of the Light (a Light that's off will be turned on and vice versa).
  • A method getStatus that will return a String of either "On" or "Off", depending on the current state of the Light.

The Problem Statement - LightBoard

This class will keep track of a collection of Light objects. In order to create a LightBoard, the number of Light objects must be specified.

This class will need to define the following methods:

  • A constructor that accepts the number of Lights on the board. Note that after you've created the array to hold the Lights, you'll need to create and add a new Light to each element.
  • A method named getNumberOfLightsOn
  • A method named describeBoard that returns a String that describes the status of each Light present and the total number of Lights that are on.
  • A method named simulateStage, which will accept a number representing the person's number (numbers will be 1based) who is about to switch the lights according to the method described above.
  • A second runSimulation method, but this one will not accept any parameters. This method will be used to run the full simulation for each person in the group, again as described above.

Be sure to read through all items before you begin.

1. Create a new Java project named YourLastNameLab07

2. Add a new package named edu.westga.cs6311.lights.controller with two classes to be used for your informal tests:

  • LightDemo
  • LightDriver

3. Create a second package named edu.westga.cs6311.lights.model that contains the Light and LightBoard classes described in the Problem Statement.

As you are working, be sure to practice incremental development by writing informal test code in your LightDemo class to confirm correct functionality of each method. If you've forgotten how to write informal test code.

4. After you are certain that your code passes the informal tests, it's time to turn our attention to the actual application that we'll be creating. Add a package named edu.westga.cs6311.lights.view. Inside, create a LightBoardTUI class. This class should have exactly two instance variables: a Scanner and a LightBoard.

This class must define at least:

  • A constructor
  • A method named run, which accepts no parameters and has no return value. Have this method get a number of lights for the LightBoard, then run the full simulation, and display the resulting board at the end.

5. Finally, return to the controller package and add a new class named InteractiveDriver that will have a main method that creates a LightBoardTUI object and use it to call run.

Sample Output:

Run 1:

Welcome to the Light Board Simulator

Enter the number of lights present: 3

After running the simulation, the board status is:
Light 0 is On
Light 1 is Off
Light 2 is Off
There are a total of 1 light(s) on.

Run 2:

Welcome to the Light Board Simulator

Enter the number of lights present: 6

After running the simulation, the board status is:
Light 0 is On
Light 1 is Off
Light 2 is Off
Light 3 is On
Light 4 is Off
Light 5 is Off
There are a total of 2 light(s) on.
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.