QuestionQuestion

Transcribed TextTranscribed Text

Problem 1: 1-D Array (35 points) In this problem you will write three methods to: a) Create a String array containing permutations of strings from two String array parameters. b) Concatenate three String arrays using the method from part (a). c) Print a String array, using an enhanced for loop (for-each). For the examples given below, first is an array of first names, middle is an array of middle names, and last is an array of last names. Assume that ALL names are different. a) (18 points) Write the method makeNames that creates and returns a String array of new names based on the method’s two parameter arrays, array1 and array2. The method creates new names in the following fashion: for each String in array1, concatenate a String from array2. Add a character space between the two strings: array1[i] + “ “ + array2[j] In the example below the array names contains 20 names including “David A”, “David B”, “David C”, …, “Lucy E”. String[] first = {"David", "Mike", "Katie", "Lucy"}; String[] middle = {"A", "B", "C", "D", "E"}; String[] names = makeNames(first, middle); If one of the parameter arrays has size 0, return the other parameter array. In the example below the array names contains 4 names: “David”, “Mike”, “Katie”, “Lucy”. String[] first = {"David", "Mike", "Katie", "Lucy"}; String[] middle = {}; String[] names = makeNames(first, middle); Use the method header: public static String[] makeNames(String[] array1, String[] array2) b) (12 points) Write an overloaded method makeNames that creates and returns a new String array of new names based on three input String arrays. makeNames will make new 2 names using the method from part (a) without using loops. Each element in the returned array will be a concatenation of three strings: a string from the first parameter, a string from the second parameter, and a string from the third parameter. In the example below the array names contains 40 names including “David A Green”, “David B Green”, “David C Green”, …, “Lucy E Wong”. String[] first = {"David", "Mike", "Katie", "Lucy"}; String[] middle = {"A", "B", "C", "D", "E"}; String[] last = {“Green”, “Wong”}; String[] names = makeNames(first, middle, last); In the example below the array names contains 20 names including “David A”, “David B”, “David C”, …, “Lucy E”. String[] first = {"David", "Mike", "Katie", "Lucy"}; String[] middle = {"A", "B", "C", "D", "E"}; String[] last = {}; String[] names = makeNames(first, middle, last); Use the method header: public static String[] makeNames(String[] array1, String[] array2, String[] array3) Do not use loops to implement the method. Instead, call the method you have written in the part (a) of this problem. Assume that the method you wrote in part (a) works as intended. Credit will not be given for this part of the problem if you reimplement any part of the code you wrote in part (a). c) (5 points) Write the method print that receives a String array as a parameter and prints one string per line. Use the method header: public static void print(String[] array) Problem 2: 2-D Array (40 points) This problem requires you to: • Read input from command line 3 • Fill a 1-D array, studentLine. • Fill a 2-D array, room, with values from studentLine. • Fill a 1-D array with values from room. Scenario: A line of students is waiting to enter the classroom at XYZ school. Front of line last student Ana Juan Jianing Jennifer Jeff Fran Ali Quinn … Sameer Yixuan Figure 1 As each student enters the room, the student is assigned a seat in row-major order. This means that the first row is filled first, then the second row, etc. All of the seats in the classroom are filled. FRONT OF CLASS Ana Juan Jianing Jennifer Jeff Fran Ali Quinn Naveed Sergio Jacob Meshita Saad Bishoy Abdor Alan Teresa Grace Nestor Christian MinSoo Alexis Kevin Senyo Xingyu Sunny Ahaoyang Ashish Sameer Yixuan Figure 2 The principal of XYZ school has decided to loan computers to all students. So that all students will have the available technological equipment set up with the appropriate software installed, each student will be issued a school laptop. The students are instructed to leave the classroom in an orderly fashion from the leftmost column to the rightmost column (column-major order) and to form a single line so that the computers can be distributed, and documentation can be kept. Ana Ali Saad Nestor Xingyu Juan Quinn Bishoy … … Yixuan Figure 3 Assume that ALL seats in the classroom will be filled. Assume the file students.txt has enough names in it to fill the classroom. The program is run using the following command: java programName #row #cols < students.txt 4 #rows and #cols are integers indicating the number of rows and columns of seats in the classroom. students.txt is a text file holding the student names. a) (5 points) Write a code-segment to: 1. Declare a 1-D String array variable, studentLine, that has space to hold all students in the class in a single line. (Figures 1 and 3) 2. Declare a 2-D String array variable, room, to represent the seats in the classroom where the number of rows and number of columns are given as command line input. (Figure2) b) (5 points) Write a method to fill the parameter array a with Strings read from the file students.txt. Use the method header: public static void fillLine(String[] a) c) (5 points) Write a method to print the student names in line (figures 1 and 3). Use the method header: public static void printLine(String[] a) d) (10 points) Write a method to fill the 2-D array, room in row-major order, with the student names in studentLine from the front of the line to the back of the line. (figure1 to figure 2). Use the method header: public static void fillRoom(String[] a, String[][] rm) e) (5 points) Write a method to print the seating assignments in the classroom (print the contents of the 2-D array from the font of the room to the back of the room (figure 2). Use the method header: public static void printRoom(String[][] rm) 5 f) (10 points) Write a method to fill studentLine with student names from the 2-D array room. The students are put into studentLine by traversing the 2-D array in column major order. Use the method header: public static void fillLineByCol(String[][] rm , String[] a) (10 points) Suppose we have a function rest() that takes an int array and returns a new int array containing everything except the original array’s first element. Use this to write a recursive function sumSquared that calculates the sum of the squares of elements in an array. For example, if arr contains the values 2, 4, and 6, then sumSquared(arr) returns 56, since 4 + 16 + 36 = 56.

Solution PreviewSolution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

* @author
*/
public class Problem1 {

    public static String[] makeNames(String[] array1, String[] array2) {
       // If one of the parameter arrays has size 0, return the other parameter array.
       if (array1.length == 0) {
            return array2;
       }

       if (array2.length == 0) {
            return array1;
       }

       String[] array = new String[array1.length * array2.length];
       int index = 0;
       /**
         * for each String in array1, concatenate a String from array2. Add a
         * character space between the two strings: array1[i] + “ “ + array2[j]
         */
       for (String string1 : array1) {
            for (String...

By purchasing this solution you'll be able to access the following files:
Solution.zip.

$64.00
for this solution

or FREE if you
register a new account!

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available Computer Science - Other Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats