



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Quiz; Class: OBJECT-ORIENTED PROG I; Subject: Computer Science; University: University of Maryland; Term: Unknown 1989;
Typology: Quizzes
1 / 5
This page cannot be seen from the preview
Don't miss anything!




CMSC 131 Quiz 6 Worksheet
The next quiz will be on Monday , Nov 1 during your lab session (either at 10 am or 11 am). The following list provides more information about the quiz: .
The following exercises cover the material to be covered in Quiz #6. Solutions to these exercises will not be provided, but you are welcome to discuss your solutions with TAs and instructors during office hours.
When asked for a “piece of code” you do not need to provide an entire class definition or even an entire method. Just present the Java statements and any variable declarations as needed to make your solution clear.
Problem 1
Write a piece of code that determines whether two arrays of String references have the same elements, but perhaps in a different order. For example, given the following arrays:
String[] a = {“Jose”, “Rose”, “Mary”, “Jack”}; String[] b = {“Rose”, “Jose”, “Jack”, “Mary”}; String[] c = {“Luis”, “Laura”};
we can see that a and b are the only arrays that have the same elements.
Problem 2
Write a piece of code that removes the first element of an array of String references. A new array with a size that is one less the size of the original array should be created, and elements from the original array should be shifted down by one position. You may assume that the array contains at least one element to start.
Problem 3
Write a static method which takes as parameter an array of String references and returns a new array where null entries from the original array have been removed. For example, given the array:
String[] a = {“Jose”, “Rose”, null, “Jack”};
the method will return an array with the following elements:
String[] b = {“Jose”, “Rose”, “Jack”};
Problem 4
Write a static method that takes as parameter an array of String references and returns a complete duplicate of the array. That is, the String objects referred to by the new array are copies of the original String objects.
Problem 5
Write a static method that takes as parameters an array of String references and a String object. The method will insert the String into the array in such a way the array is kept sorted. You can assume that the String objects in the provided array are already sorted and they occupy the leftmost entries of the array. You can also assume that available (“empty”) entries of the array are set to null. If the array is already “full” then no insertion should occur.
Problem 6
Write a static method that takes as parameter an array of String references. The method will return the index of the array entry corresponding to the String with the maximum length in the array. If there are two or more String objects with the same maximum value then return the lowest index found.
The following supplemental problems have been provided for your own practice. The quiz will not be based on these problems, but they will be good practice for upcoming exams.
Problem 1
Write a piece of code that appends the contents of one array of String references to the end of another. A new array (with a length corresponding to sum of the lengths of the originals arrays) should be created.
Problem 2
Write a static method that takes as parameters two arrays of String references and returns a new array representing the merging of the two arrays. The merging operation will create a new array by selecting alternate elements from the source arrays. For example, given the following two arrays:
String[] a = {“Jose”, “Rose”, “Mary”, “Jack”}; String[] b = {“Pete”, “Larry”};
The new array will have the elements:
String[] c = {“Jose”, “Pete”, “Rose”, “Larry”, “Mary”, “Jack”};
Problem 6
The following problem relies on the Computer class you defined above. Define a class called Lab with the following specifications:
Instance variables
String labName; // name of the computer lab Computer[] computers; // array of references to computer objects int currentNumComputers; // current number of computer objects in the array
Methods
Lab(String labNameIn, int maxNumComputersIn);
The contructor will create an array of computers with a length equal to maxNumComputersIn and it will initialize the name of the computer with the corresponding parameter. Notice that the currentNumComputers must be set to 0.
boolean add(Computer computerIn)
You must not add a computer who is already present in the collection. Each computer is uniquely identified by its name.