Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

Andrew Cordell

Elizabeth Aquino

CS 116 Spring 2020 Lab #04


Due: Wednesday, February 19th, 5:00 PM
Points: 20

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.

Problem 1 [10 points]:


A. Write a class called RandomIntegerArrayCreator that:

 upon its object instantiation:


 will generate a random integer arraySize from the set {0, 1, 2, 3, 4, 5, 6,
7, 8, 9, 10, 11, 12, 13, 14, 15},

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,

 public int[] getArray() that will return the reference to the


random integer array that was generated.

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.

Given these two sample arrays (orientation changed for readability):

Array A Array B
2 4
3 3
1 4
0 0

2
1 5
5 2
1
2
2
5

Your program output should look like this:

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

Number of common elements in A and B: 5

If there are no common elements (or one or two arrays are empty), just display:

Number of common elements in A and B: 0

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 ….

Problem 2 [10 points]:


Write the following classes:

1. Class Recording that:

a) has three attributes / fields (select appropriate data types, access modifiers,
and static/non-static yourself):
i. ARTIST (cannot be changed once set),

ii. NAME (cannot be changed once set),

iii. DURATION_IN_SECONDS (cannot be changed once set),

b) has two constructors:


i. non-parametrized constructor that will set both ARTIST and NAME
attributes to “Unknown” and DURATION_IN_SECONDS to zero,

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

i. display a message “Now playing: artist - name [XXmYYs]”, where artist is


the value of the artist attribute / field, name is the value of the name
attribute / field, XX is the number of full duration minutes, and YY is the
number of remaining seconds (no need to use leading zeros; for
example duration of 201 seconds would be represented by 3m21s),
ii. display “ERROR: cannot play this recording” message if duration is zero
seconds,
f) Has a toString method that will display a message “artist - name
[XXmYYs]”, where artist is the value of the artist attribute / field, name is the
value of the name attribute / field, XX is the number of full duration
minutes, and YY is the number of remaining seconds (no need to use leading
zeros; for example duration of 201 seconds would be represented by
3m21s).

4
2. Class Playlist that:

a) has the following attributes / fields (select appropriate data types, access
modifiers, and static/non-static yourself):
i. name,

ii. numberOfRecordings, that will hold the CURRENT number of


recordings (initialize to zero),
iii. durationInSeconds, that will hold the TOTAL duration of all
recordings in the playlist (initialize to zero),
iv. MAX_PLAYLIST_SIZE, that sets the limit of recordings that this
playlist can hold (cannot be changed once set),
v. an array of recordings called recordingList,

b) has two constructors:


i. non-parametrized constructor that will:
1. set the name attribute / field value to “Unknown”,

2. set the MAX_PLAYLIST_SIZE attribute / field to 5,

3. initialize the array,


ii. parametrized constructor that constructor that will set not initialized
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 ONE mutator / getter method for the name attribute / field that makes
sure that null names are not allowed,

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.

If your Recording and Playlist classes are implemented correctly, the


following application class:

PlaylistDemo.java class

public class PlaylistDemo {

public static void main(String [] args){

// Instantiate new playlist

Playlist myPlaylist = new Playlist("CS 116 Playlist", 10);

// Add some songs to it

Recording newRecording = new Recording("Billie Eilish", "Everything I


Wanted", 201);

myPlaylist.add(newRecording);

newRecording = new Recording("Eminem", "Godzilla", 245);

myPlaylist.add(newRecording);

newRecording = new Recording("The Weeknd", "Blinding Lights", 145);

myPlaylist.add(newRecording);

newRecording = new Recording("Nicki Minaj", "Yikes", 217);

myPlaylist.add(newRecording);

newRecording = new Recording("Justin Bieber", "Intentions", 234);

myPlaylist.add(newRecording);

// "Play" the playlist

myPlaylist.play();

// Display playlist "summary"

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]

Playlist: CS 116 Playlist [17m22s]:


Billie Eilish - Everything I Wanted [3m21s]
Eminem - Godzilla [4m5s]
The Weeknd - Blinding Lights [2m25s]
Nicki Minaj - Yikes [3m37s]
Justin Bieber - Intentions [3m54s]

You might also like