Step 3. Your starting code has a function readNames returns an array of student names. The temporary code you were given always returns the same five names shown above (Ava, Ben, .). Now you will change this function so that it reads from stdIn an integer N 2 0, followed by N names. You can assume that after N, the input will always be exactly N names, each on one line that could be read using the function stdIn.readString. There are several example data files in the directory that conform to this specification. For example names3. txt contains 3 names (Ava, Ben and Carol). Your function should return an array containing those N student names. For example, if you provide the file names3.txt on StdIn your version of readNames would return the three names in that text file, and the output would be: $ java-introcs ZoomRooms greedy 3 < names3.txt Room: e Ava Ben Carol Room: 1 Note that with the code we have so far we will always print out exactly two rooms, regardless of the number of students or the room capacity. We will fix that next. Step 4. Now you have a simple math problem. The second command line argument specifies the room size (the maximum capacity for each room). For example, in the command shown above it's 3 (right after the word "greedy"). Since the room size is 3, it means that Ava, Ben and Carol can all fit in Room 0. The function roomsNeeded should compute the number of rooms needed; but as provided it always just returns 2, no matter what. It takes two arguments: numStudents (the total number of students) and roomSize (the size of each room, as specified on the command line). Change this function to return numStudents divided by roomSize , rounding up to an integer in cases where it does not divide evenly. (Hint: there's a useful function the Math library.) Now you should get the following output, because only one room is needed: $ java-introcs ZoomRooms greedy 3 < names3.txt

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question
Step 1. The starting code you ran in Step 0 only prints the first room. Modify the printRooms function so that it prints all
numRooms rooms, by using a loop. If your changes are successful, when you compile and run it, ZoomRooms should now
print out the headers for Rooms 0 and 1, as shown below. Note that there are still no student names printed yet, which will
be addressed in the next step.
$ java-introcs ZoomRooms greedy 3
Room: 0
Room: 1
Step 2. Next you will modify the printRoom function so that it prints names of students in a room, not just the room number.
This function has an argument room (the room number). It has two other arguments that are parallel arrays:
assignedRooms and studentNames . The first array specifies which room each student is assigned to, while the second
contains their names. Here's an example of what those arrays might look like in this program:
index
assignedRooms
studentNames
Ava
Ben
2
Carol
3
1
Dan
4
1
Emma
Write a loop that considers each student in turn, and prints out the names of those students whose entry in assignedRooms
match the room argument. For the example arrays shown above, if room == 1, then printRoom would output the names
Dan and Emma under the dashed line. So running the program again should produce this:
$ java-introcs ZoomRooms greedy 3
Transcribed Image Text:Step 1. The starting code you ran in Step 0 only prints the first room. Modify the printRooms function so that it prints all numRooms rooms, by using a loop. If your changes are successful, when you compile and run it, ZoomRooms should now print out the headers for Rooms 0 and 1, as shown below. Note that there are still no student names printed yet, which will be addressed in the next step. $ java-introcs ZoomRooms greedy 3 Room: 0 Room: 1 Step 2. Next you will modify the printRoom function so that it prints names of students in a room, not just the room number. This function has an argument room (the room number). It has two other arguments that are parallel arrays: assignedRooms and studentNames . The first array specifies which room each student is assigned to, while the second contains their names. Here's an example of what those arrays might look like in this program: index assignedRooms studentNames Ava Ben 2 Carol 3 1 Dan 4 1 Emma Write a loop that considers each student in turn, and prints out the names of those students whose entry in assignedRooms match the room argument. For the example arrays shown above, if room == 1, then printRoom would output the names Dan and Emma under the dashed line. So running the program again should produce this: $ java-introcs ZoomRooms greedy 3
Step 3. Your starting code has a function readNames returns an array of student names. The temporary code you were
given always returns the same five names shown above (Ava, Ben, ...). Now you will change this function so that it reads
from StdIn an integer N 2 0, followed by N names. You can assume that after N, the input will always be exactly N names,
each on one line that could be read using the function StdIn.readString . There are several example data files in the
directory that conform to this specification. For example names3.txt contains 3 names (Ava, Ben and Carol).
Your function should return an array containing those N student names. For example, if you provide the file names3.txt on
StdIn your version of readNames would return the three names in that text file, and the output would be:
$ java-introcs ZoomRooms greedy 3 < names3.txt
Room: 0
Ava
Ben
Carol
Room: 1
Note that with the code we have so far we will always print out exactly two rooms, regardless of the number of students or
the room capacity. We will fix that next...
Step 4. Now you have a simple math problem. The second command line argument specifies the room size (the maximum
capacity for each room). For example, in the command shown above it's 3 (right after the word "greedy"). Since the room
size is 3, it means that Ava, Ben and Carol can all fit in Room 0.
The function roomsNeeded should compute the number of rooms needed; but as provided it always just returns 2, no matter
what. It takes two arguments: numStudents (the total number of students) and roomSize (the size of each room, as
specified on the command line).
Change this function to return numStudents divided by roomSize , rounding up to an integer in cases where it does not
divide evenly. (Hint: there's a useful function the Math library.) Now you should get the following output, because only one
room is needed:
$ java-introcs ZoomRooms greedy 3 < names3.txt
Transcribed Image Text:Step 3. Your starting code has a function readNames returns an array of student names. The temporary code you were given always returns the same five names shown above (Ava, Ben, ...). Now you will change this function so that it reads from StdIn an integer N 2 0, followed by N names. You can assume that after N, the input will always be exactly N names, each on one line that could be read using the function StdIn.readString . There are several example data files in the directory that conform to this specification. For example names3.txt contains 3 names (Ava, Ben and Carol). Your function should return an array containing those N student names. For example, if you provide the file names3.txt on StdIn your version of readNames would return the three names in that text file, and the output would be: $ java-introcs ZoomRooms greedy 3 < names3.txt Room: 0 Ava Ben Carol Room: 1 Note that with the code we have so far we will always print out exactly two rooms, regardless of the number of students or the room capacity. We will fix that next... Step 4. Now you have a simple math problem. The second command line argument specifies the room size (the maximum capacity for each room). For example, in the command shown above it's 3 (right after the word "greedy"). Since the room size is 3, it means that Ava, Ben and Carol can all fit in Room 0. The function roomsNeeded should compute the number of rooms needed; but as provided it always just returns 2, no matter what. It takes two arguments: numStudents (the total number of students) and roomSize (the size of each room, as specified on the command line). Change this function to return numStudents divided by roomSize , rounding up to an integer in cases where it does not divide evenly. (Hint: there's a useful function the Math library.) Now you should get the following output, because only one room is needed: $ java-introcs ZoomRooms greedy 3 < names3.txt
Expert Solution
steps

Step by step

Solved in 2 steps with 2 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY