Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

THIS IS A CONTINUATION OF MULTIPLE ACTIVITIES WITHIN A LAB, please reffer to my

ID: 3681685 • Letter: T

Question

THIS IS A CONTINUATION OF MULTIPLE ACTIVITIES WITHIN A LAB, please reffer to my previous post if you have trouble understanding the question. This question is for activity 2 within my lab. Here's the link to the original post https://www.chegg.com/homework-help/questions-and-answers/welcome-lab-7-week-going-get-practice-arrays-methods-loops-strings-time-see-reading-file-u-q11527019

Activity 2. Say It Out Loud sequence. In this activity, given the first number of a sequence (called “seed” and given as a string), you will have to generate the rest of a sequence of integers (that you will store as strings, for convenience purposes) as follows:

Each integer is obtained by reading aloud the previous integer.

For instance, if the sequence starts with “1”, then the sequence goes as follows:

1

which is read aloud as “one 1” therefore the next number is “11”

11

which is read aloud as “two 1’s” therefore the next number is “21”

21

which is read aloud as “one 2 one 1” à therefore yielding “1211”

1211

which is read aloud as “one 1 one 2 two 1’s” yielding “111221”

111221

which is read aloud as “three 1’s two 2’s one 1” yielding “312211”

312211

etc.

For the sake of another example, when starting with a “8”, then the sequence goes as follows:1

8

which is read aloud as “one 8” yielding “18”

18

which is read aloud as “one 1 one 8” yielding “1118”

1118

which is read aloud as “three 1’s one 8” yielding “3118”

3118

which is read aloud as “one 3 two 1’s one 8” yielding “132318”

132118

etc.

The method you will have to implement is called: SayItOutLoud

Parameters:

A string – the seed; and

A 1D array of strings – the sequence that you have to build (so you will store the sequence of integers in it, as strings)

Return type: void

Here is the pseudocode of this method (you have to implement (translate) it Java*).

Remember, in this method, we are filling an array of Strings, which represents the sequence of integers. We need to generate a certain number of integers in the sequence. This number is the length of the array that is passed to the method as a parameter. We call this array “sequence”.

* You can also implement your own approach, but then you have to provide your pseudocode first.

The first element of the sequence is seed

For each element i in the sequence (from second to last) to be computed, do {

            We call seed the previous element (element (i-1) of the sequence)

              Set the ith element of sequence to be the empty string

              While (we have not read all characters of seed) do {

                        Call current the current character of seed (at first, it will be the first

                          character of seed)

                          Set the amount of such character to 1 (there is at least one of it)

                          While (contiguous next characters are still the same as current) do {

                                     Increase amount by 1 each time we find a similar character

                                       contiguously

                          }              

                          Add to ith element of the sequence the character(s) corresponding to

                          number “amount”

                          Add to ith element of the sequence the character named current

                          

                          Move to first different character (or the end of the string if we have reached

                          it)

              }

}

Instructions:

If you do not wish to use the above pseudocode, write your own pseudocode for method SayItOutLoud in a docx file named “yourLastName-yourFirstName-lab7.docx”.

Modify the code provided in Lab7Spring16.java where prompted (and only where prompted) to make sure that the code follows the above instructions.

Note: inability to follow instructions will be penalized by 10 points.

Now, here is what you have to turn in:

The modified java file called Lab7Spring16.java.

(Only if you have not used the above pseudocode:) The file “yourLastName-yourFirstName-lab7.docx”in which you have described your pseudocode (= your algorithm).

------------------------------------------------------------------------------------------------------------------This is where the instructions end, below is the java file that was provided keep in mind the instructions are only for sections within the code that are labeled for activity 2. Activity 1 has already been answered. I will post Activity 3 in a separate post.

JAVA FILE:

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;

public class Lab7Spring16 {
  
/************************ ACTIVITY 1: PRINTING METHODS ********************************/
// this method takes a 1D array of integers and prints it
public static void printArray(int[] myArray) {
// your code goes here
int k1=myArray.length; //ARRAY LENGTH

for(int k=0;k //PRINT ELEMENT WITH ONE SPACE
System.out.print(myArray[k]+" ");
}
  
// this method takes a 2D array of integers
// and prints it row per row
public static void printArray(int[][] myArray) {
// your code goes here
int k1=myArray.length; //ARRAY LENGTH

for(int k=0;k //CALL METHOD TO DISPLAY ROWS OF GIVEN 2D ARRAY

printArray(myArray[k]); //CALL

System.out.println();

}
}
  
// this method takes a 1D array of strings and prints it
// one string per line
public static void printArray(String[] myArray) {
// your code goes here
int k1=myArray.length; //ARRAY LENGTH

for(int k=0;k //PRINT EACH STRING IN NEW LINE

System.out.println(myArray[k]);

}   
}
  
/************************ ACTIVITY 2: SEQUENCE METHOD ***********************************/
// this method takes an empty array of strings and a seed string (which contains only digits)
// and fills it with the SayItOutLoud sequence starting with the string seed
// Hint: this means that the first element of the array of strings will be seed
public static void SayItOutLoud(String[] sequence, String seed) {
// your code goes here
}
  
/************************ ACTIVITY 3: MAGIC SQUARE METHOD ******************************/
// this method takes a 2D array of integers and
// checks if it is a magic square 2D array
// if it is, the method returns true
// otherwise, it returns false
public static boolean MagicSquare(int[][] table) {
  
// your code goes here
  
return true;
}
  
// this method computes the sum of row number "row" of a given 2D array of integers
public static int sumRow(int[][] table, int row) {
int sum = 0;
// your code goes here
return sum;
}
  
// this method computes the sum of column number "column" of a given 2D array of integers
// we will assume the table is not ragged
public static int sumColumn(int[][] table, int column) {
int sum = 0;
// your code goes here
return sum;
}

// this method computes the sum of numbers in the main diagonal of a given 2D array of integers
public static int sumDiagonal1(int[][] table) {
int sum = 0;
// your code goes here
return sum;
}
  
// this method computes the sum of numbers in the secondary diagonal of a given 2D array of integers
public static int sumDiagonal2(int[][] table) {
int sum = 0;
// your code goes here
return sum;
}
  
/************************ AUXILIARY METHOD, GIVEN TO YOU *******************************/
// this method takes a 1D array of strings (each string represents an integer)
// and returns an array of integers of the same size
public static int[] toInteger(String[] array) {
int[] result = new int[array.length];
for (int i=0; i result[i] = Integer.parseInt(array[i]);   
}
return result;
}
  
/************************ MAIN METHOD ***************************************************/
public static void main (String[] args) throws IOException {
  
// get the user name from System.in
Scanner in = new Scanner(System.in);
System.out.println("What is your name?");
String name = in.next();
  
// create a frame and use it to greet the user
JFrame frame = new JFrame("CS1401 Lab 7");
JOptionPane.showMessageDialog(frame,
"Welcome to Lab7, " + name + "!",
"CS1401 Lab 7",
JOptionPane.INFORMATION_MESSAGE);
  
/************ INTERACTION RELATED TO ACTIVITY 1 *************************************/
String seed = JOptionPane.showInputDialog(frame, "What do you want to want to start the sequence from?",
"CS1401 Lab 7",
JOptionPane.INFORMATION_MESSAGE);
System.out.printf("The user's wants to start the SayItOutLoud sequence from %s. ", seed);
int length = Integer.parseInt(JOptionPane.showInputDialog(frame, "How many numbers for you want to generate in the sequence?",
"CS1401 Lab 7",
JOptionPane.INFORMATION_MESSAGE));
System.out.printf("The user's wants to generate " + length + " elements in the SayItOutLoud sequence ");
  
String[] sequenceArray = new String[length];
// here: call the method SayItOutLoud on the array sequenceArray and the first element seed
// your code goes here
  
// now print the result of applying SayItOutLoud
// your code goes here
  
/************ INTERACTION RELATED TO ACTIVITY 2 *************************************/
  
String filename = JOptionPane.showInputDialog(frame, "In which file is your data stored?",
"CS1401 Lab 7",
JOptionPane.INFORMATION_MESSAGE);
FileReader fr = new FileReader(filename);
BufferedReader textReader = new BufferedReader(fr);
// below is the code (given to you) that allows to read a text file like the one given to you as an example (square1.txt)
int size = Integer.parseInt(textReader.readLine());
int[][] square = new int[size][size];
  
for (int i=0; i< size; i++) {
square[i] = toInteger(textReader.readLine().split(" "));
}
  
// print what you just stored in a 2D array using the method printArray
// your code goes here
  
// here enter your code so that you print out where the data you read is a magic square or not
  
/************ END OF PROGRAM *********************************************************/
// goes back to System.in and frees back the prompt command
System.exit(0);

}
  
}

-----------------------------------------------------------------------END of Java file

Again this is only for activity 2, thank you ask any questions if you have any.

1

which is read aloud as “one 1” therefore the next number is “11”

11

which is read aloud as “two 1’s” therefore the next number is “21”

21

which is read aloud as “one 2 one 1” à therefore yielding “1211”

1211

which is read aloud as “one 1 one 2 two 1’s” yielding “111221”

111221

which is read aloud as “three 1’s two 2’s one 1” yielding “312211”

312211

etc.

Explanation / Answer

Hi Please find implementation of SayItOutLoud functiion. I have defined a helper function for SayItOutLoud.

public static void SayItOutLoud(String[] sequence, String seed) {
       for (int i=0; i<sequence.length; i++) {
           sequence[i] = seed;
           seed = lookandsay(seed);   
       }
}
   // helper function of SayItOutLoud
   public static String lookandsay(String number){
       StringBuilder result= new StringBuilder();
     
       char repeat= number.charAt(0);
       number= number.substring(1) + " ";
       int times= 1;
     
       for(char actual: number.toCharArray()){
           if(actual != repeat){
               result.append(times + "" + repeat);
               times= 1;
               repeat= actual;
           }else{
               times+= 1;
           }
       }
       return result.toString();
   }