Question
Please answer this Question 3 The LightPanel class contains a 2-dimensional array of values using numbers to represent lights in a matrix. An example might
Please answer this Question 3
The LightPanel class contains a 2-dimensional array of values using numbers to represent lights in a matrix. An example might be a digital message board comprised of individual lights that you might see at a school entrance or sports scoreboard. You will write two methods, one to determine if a column is in error and one to fix the error by traversing the array column by column. The LightPanel class contains the instance variable panel, which is a two-dimensional array containing integer values that represent the state of lights on a grid. The two-dimensional array may be of any size. Lights may be on, off, or in an error state. The instance variable onValue represents an integer value for the on state of a light. The instance variable offValue represents an integer value for the off state of a light. The onValue and offValue instance variables may be of any valid integer value. Any other integer value in the panel array represents an error state for a light. Here is the partially completed LightPanel class: public class LightPanel{ private int[][] panel; private int onValue; private int offValue; public LightPanel(int[][] p, int on, int off){ panel = p; onValue = on; offValue = off; } public boolean isColumnError(int column){ //returns true if the column contains 1 or more lights in error //returns false if the column has no error lights //to be implemented in part a } public void updateColumn(){ //shifts a column to replace a column in error //to be implemented in part b } //there may be other instance variables, constructors, and methods not shown } Given the example for the panel array below: The onValue = 8, offValue = 3 and all other values are errors. In the panel below, there are five array elements with the onValue of 8, thus there are five lights on. There are four array elements with the offValue of 3, thus there are four lights off. The values of 0 and 4 represent an error state. 3 3 8 8 8 3 0 3 4 8 8 0 Part A: The Boolean method isColumnError takes an integer parameter indicating a column of panel and determines if there exists a light in an error state in that column. The method returns true if one or more lights of the column are in an error state and returns false if there are no lights in an error state. Write the isColumnError method below: //precondition: panel, onValue and offValue have been initialized //postcondition: method returns true if col of the panel array contains one or more lights in an error state and false if col of the panel array has no lights in an error state. public boolean isColumnError(int col){ } Part B: The updateColumn method will update any column of panel containing an error state. You must call the isColumnError() method created in Part A to determine if a column is in error. You can assume that the method works as expected. Any column of panel containing a light in an error state will copy the contents of the column immediately to the right of it regardless of errors contained in the copied column. If the last column on the right contains an error state, it will copy the contents of the first column of the array. For example, given the panel array with contents: 5 5 7 7 7 5 0 5 4 7 7 0 For this example, onValue = 7 and offValue = 5; A call to updateColumn() would result in the following modification to the panel array: 5 5 7 7 5 5 0 5 7 7 7 0 The first column contains 5, 7, 4 where 4 is an error state so the contents of the second column are copied over. The second column contains 5, 5, 7 which has no errors, so no changes are made. The third column contains 7, 0, 7 where 0 is an error state so the contents of the third column are copied over. 5 5 7 7 5 5 5 5 7 7 0 0 Notice the third column still contains an error that will not be fixed. The last column contains 7, 5, 0 where 0 is an error state. So, the contents of the first column (which was modified in the first step) are copied to the last column. 5 5 7 5 5 5 5 5 7 7 0 7 The above array is the final value of the panel array after the call to updateColumn ( ) completes. Write the updateColumn( ) method below. public void updateColumn(){ }
Question 4
Complete the following method which counts the number of times a given phrase, str, is a match to a string in the ArrayList arr. Return the number of words in arr that are identical to str. If arr contains the strings {"be happy", "happy", "I am happy", "happy happy joy joy", "happy"} The method call countOccurrences(arr, "happy") would return 2. The method call countOccurrences(arr, "joy") would return 1. public int countOccurrences (ArrayList String arr, String str) { //to be written }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started