Professional Documents
Culture Documents
CS 116 Spring 2020 Lab #04: Due: Wednesday, February 19th, 5:00 PM Points: 20
CS 116 Spring 2020 Lab #04: Due: Wednesday, February 19th, 5:00 PM Points: 20
Elizabeth Aquino
Instructions:
1. Use this document template to report your answers and create separate java files
for your classes. Enter all lab partner names at the top of first page.
2. You don’t need to finish your lab work during the corresponding lab session.
3. ZIP your Java files and lab report into a single file. Name the file as follows:
LastName_FirstName_CS116_Lab4_Report.zip
4. Submit the final document to Blackboard Assignments section before the due
date. No late submissions will be accepted.
5. ALL lab partners need to submit a report, even if it is the same document.
Objectives:
1. (10 points) Demonstrate the ability to use loops, arrays and multiple classes to
solve a problem.
2. (10 points) Demonstrate the ability to use loops, arrays and multiple classes to
solve a problem.
1
create a random integer array of size arraySize (15 OR LESS) with
elements from the the set {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10} (integers can appear
multiple times in this array,
has two accessor methods:
public int getArraySize() that will return the array size,
B. Write a class called CommonElements with a single method main that will:
Create and obtain two integer arrays (arrayA and arrayB) using
RandomIntegerArrayCreator type objects and its methods,
find the number of common elements between arrayA and arrayB (say: if
integer 2 appears in arrayA once and twice in arrayB, that counts as ONE
common element between the two),
display the result using the format shown below (see Sample Output box).
Constraints / notes:
All array elements are integers from the the set {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10} and
can appear multiple times in each array,
Arrays A and B do NOT have to be of the same size,
Arrays A and B CAN be empty (no elements),
Arrays A and B will NOT be sorted.
Array A Array B
2 4
3 3
1 4
0 0
2
1 5
5 2
1
2
2
5
Sample output
Array A: 2 3 1 0 1 5
Array B: 4 3 4 0 5 2 1 2 2 5
Element: # in A: # in B:
0 1 1
1 2 1
2 1 3
3 1 1
5 1 2
If there are no common elements (or one or two arrays are empty), just display:
Test your class with different arrays. Your solution does not need to be efficient, just
effective.
3
HINT: You can use additional array(s) to keep track of / count ….
a) has three attributes / fields (select appropriate data types, access modifiers,
and static/non-static yourself):
i. ARTIST (cannot be changed once set),
ii. parametrized constructor that will set all class attributes to desired
values IF these area not null and valid/legal. Otherwise set them the
same way as in the non-parametrized constructor,
c) has accessor / getter methods for all class attributes / fields,
d) has NO mutator / getter methods,
e) has a play method that will
4
2. Class Playlist that:
a) has the following attributes / fields (select appropriate data types, access
modifiers, and static/non-static yourself):
i. name,
e) has an add method that allows you to add a new recording to your playlist
that:
i. prevents from adding “null recordings”,
ii. prevents from exceeding playlist size,
iii. updates all corresponding attributes / fields accordingly,
iv. returns true if adding was successful (recording added), and false
otherwise (recording could not be added),
f) has a method play that will use Recording object method to “play” all
recordings in the playlist in sequence (see example below). If the playlist is
empty, it should display “ERROR: empty playlist” message,
5
g) has a method toString that will display a playlist summary as shown in
example below.
PlaylistDemo.java class
myPlaylist.add(newRecording);
myPlaylist.add(newRecording);
myPlaylist.add(newRecording);
myPlaylist.add(newRecording);
myPlaylist.add(newRecording);
myPlaylist.play();
System.out.println(myPlaylist.toString());
Should generate this output (note how play and toString methods work):
6
Now playing: Billie Eilish - Everything I Wanted [3m21s]
Now playing: Eminem - Godzilla [4m5s]
Now playing: The Weeknd - Blinding Lights [2m25s]
Now playing: Nicki Minaj - Yikes [3m37s]
Now playing: Justin Bieber - Intentions [3m54s]