Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

this is the third part of the question, the other parts are posted in different posts: I have posted the other parts of the question

this is the third part of the question, the other parts are posted in different posts:

I have posted the other parts of the question in different posts. please look at them

I cant post the whole code in one question.

here is the link for the other parts of the question:

part one:https://www.chegg.com/homework-help/questions-and-answers/part-one-question-parts-posted-different-parts-lab-3-wrote-classes-represent-playlists-son-q69214126?trackid=vrNXvErL

part two: https://www.chegg.com/homework-help/questions-and-answers/second-part-question-parts-posted-different-posts-import-static-orgjunitjupiterapiassertio-q69214234?trackid=vrNXvErL

third part is in this post.

part four: https://www.chegg.com/homework-help/questions-and-answers/last-part-question-first-three-parts-posted-different-posts-answer-two-classes-import-java-q69214467?trackid=vrNXvErL

import static org.junit.jupiter.api.Assertions.assertArrayEquals;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;

class Lab3SongTest {

@Test

void testGetters() {

String title = "The Sound of Muzak";

String artist = "Porcupine Tree";

int[] time = {59, 4};

Song song = new Song(title, artist, time);

assertEquals(title, song.getTitle());

assertEquals(artist, song.getArtist());

assertArrayEquals(time, song.getTime());

title = "Her Majesty";

artist = "The Beatles";

time = new int[] {23};

song = new Song(title, artist, time);

assertEquals(title, song.getTitle());

assertEquals(artist, song.getArtist());

assertArrayEquals(time, song.getTime());

}

@Test

void testImmutability() {

String title = "Ten Years Gone";

String artist = "Led Zeppelin";

int seconds = 55;

int minutes = 6;

int[] time = {seconds, minutes};

Song song = new Song(title, artist, time);

time[0] = -12345;

time[1] = -54321;

assertEquals(seconds, song.getTime()[0]);

assertEquals(minutes, song.getTime()[1]);

int[] returnedTime = song.getTime();

returnedTime[0] = -12345;

returnedTime[1] = -54321;

assertEquals(seconds, song.getTime()[0]);

assertEquals(minutes, song.getTime()[1]);

}

}

___________________________________________________________________________________________________________________________________________

import static org.junit.jupiter.api.Assertions.assertArrayEquals;

import static org.junit.jupiter.api.Assertions.assertEquals;

import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.BufferedReader;

import java.io.FileReader;

import java.io.IOException;

import org.junit.jupiter.api.Test;

class PlaylistTest {

@Test

void testConstructorFile() throws IOException {

String info0 = "Fake Plastic Trees; Radiohead; 4:52";

String info1 = "Subterranean Homesick Alien; Radiohead; 4:27";

String info2 = "2 + 2 = 5; Radiohead; 3:21";

Playlist playlist = new Playlist("playlists/radiohead.txt");

assertEquals(info0, playlist.getSong(0).toString());

assertEquals(info1, playlist.getSong(1).toString());

assertEquals(info2, playlist.getSong(2).toString());

info0 = "One Big Holiday; My Morning Jacket; 5:21";

info1 = "Steam Engine; My Morning Jacket; 7:26";

info2 = "Tropics (Erase Traces); My Morning Jacket; 5:10";

playlist = new Playlist("playlists/my-morning-jacket.txt");

assertEquals(info0, playlist.getSong(0).toString());

assertEquals(info1, playlist.getSong(1).toString());

assertEquals(info2, playlist.getSong(2).toString());

}

@Test

void testAddSongsFile() throws IOException {

String info0 = "A Spoonful Weighs a Ton; The Flaming Lips; 3:32";

String info1 = "What Is the Light?; The Flaming Lips; 4:05";

String info2 = "Fight Test; The Flaming Lips; 4:14";

String info3 = "Yoshimi Battles the Pink Robots, Pt. 1; "

+ "The Flaming Lips; 4:45";

String info4 = "Do You RealizejQuery224012068664758139236_1614461491947; The Flaming Lips; 3:32";

String info5 = "The Yeah Yeah Yeah Song (With All Your Power); "

+ "The Flaming Lips; 4:51";

Playlist playlist = new Playlist();

playlist.addSongs("playlists/the-flaming-lips-soft-bulletin.txt");

assertEquals(info0, playlist.getSong(0).toString());

assertEquals(info1, playlist.getSong(1).toString());

playlist.addSong(new Song(info2));

assertEquals(info0, playlist.getSong(0).toString());

assertEquals(info1, playlist.getSong(1).toString());

assertEquals(info2, playlist.getSong(2).toString());

playlist.addSongs("playlists/the-flaming-lips-yoshimi.txt");

assertEquals(info0, playlist.getSong(0).toString());

assertEquals(info1, playlist.getSong(1).toString());

assertEquals(info2, playlist.getSong(2).toString());

assertEquals(info3, playlist.getSong(3).toString());

assertEquals(info4, playlist.getSong(4).toString());

playlist.addSong(new Song(info5));

assertEquals(info0, playlist.getSong(0).toString());

assertEquals(info1, playlist.getSong(1).toString());

assertEquals(info2, playlist.getSong(2).toString());

assertEquals(info3, playlist.getSong(3).toString());

assertEquals(info4, playlist.getSong(4).toString());

assertEquals(info5, playlist.getSong(5).toString());

}

@Test

void testToString() {

String info0 = "Dreams; Fleetwood Mac; 4:14";

String info1 = "The Chain; Fleetwood Mac; 4:28";

String info2 = "Silver Springs; Fleetwood Mac; 4:29";

Playlist playlist = new Playlist();

assertEquals("", playlist.toString());

playlist.addSong(new Song(info0));

String songStrings = info0;

assertEquals(songStrings, playlist.toString());

playlist.addSong(new Song(info1));

songStrings = info0 + System.lineSeparator() + info1;

assertEquals(songStrings, playlist.toString());

playlist.addSong(new Song(info2));

songStrings = info0 + System.lineSeparator() + info1

+ System.lineSeparator() + info2;

assertEquals(songStrings, playlist.toString());

}

@Test

void testSaveSongs() throws IOException {

String info0 = "You're All I Need to Get By; "

+ "Marvin Gaye & Tammi Terrell; 2:38";

String info1 = "What's Going On; Marvin Gaye; 3:53";

String info2 = "Trouble Man; Marvin Gaye; 3:49";

String filename = "playlists/marvin-gaye.txt";

Playlist playlist = new Playlist();

playlist.addSong(new Song(info0));

playlist.saveSongs(filename);

String[] expectedLines = {info0};

assertTrue(checkFile(expectedLines, filename));

playlist.addSong(new Song(info1));

playlist.saveSongs(filename);

expectedLines = new String[] {info0, info1};

assertTrue(checkFile(expectedLines, filename));

playlist.addSong(new Song(info2));

playlist.saveSongs(filename);

expectedLines = new String[] {info0, info1, info2};

assertTrue(checkFile(expectedLines, filename));

}

@Test

void testGetTotalTime() {

String info0 = "Her Majesty; The Beatles; 23";

String info1 = "Johnny B. Goode; Chuck Berry; 2:41";

String info2 = "A Farewell to Kings; Rush; 5:51";

String info3 = "Can't You Hear Me Knocking; The Rolling Stones; 7:15";

String info4 = "Stairway to Heaven; Led Zeppelin; 8:02";

String info5 = "Close to the Edge; Yes; 18:42";

String info6 = "Supper's Ready; Genesis; 23:06";

String info7 = "Symphony No. 9; Ludwig van Beethoven; 1:09:00";

Playlist playlist = new Playlist();

assertArrayEquals(new int[] {0}, playlist.getTotalTime());

playlist.addSong(new Song(info0));

assertArrayEquals(new int[] {23}, playlist.getTotalTime());

playlist.addSong(new Song(info1));

assertArrayEquals(new int[] {4, 3}, playlist.getTotalTime());

playlist.addSong(new Song(info2));

assertArrayEquals(new int[] {55, 8}, playlist.getTotalTime());

playlist.addSong(new Song(info3));

assertArrayEquals(new int[] {10, 16}, playlist.getTotalTime());

playlist.addSong(new Song(info4));

assertArrayEquals(new int[] {12, 24}, playlist.getTotalTime());

playlist.addSong(new Song(info5));

assertArrayEquals(new int[] {54, 42}, playlist.getTotalTime());

playlist.addSong(new Song(info6));

assertArrayEquals(new int[] {0, 6, 1}, playlist.getTotalTime());

playlist.addSong(new Song(info7));

assertArrayEquals(new int[] {0, 15, 2}, playlist.getTotalTime());

}

// Define a helper method that checks the lines of a text file.

private static boolean checkFile(String[] expectedLines, String filename)

throws IOException {

BufferedReader reader = new BufferedReader(new FileReader(filename));

// Return false if any of the lines don't match.

for (String expected : expectedLines) {

String actual = reader.readLine();

if (actual == null || !expected.equals(actual)) {

reader.close();

return false;

}

}

reader.close();

return true;

}

}

_______________________________________________________________________________________________________________________________

import static org.junit.jupiter.api.Assertions.assertArrayEquals;

import static org.junit.jupiter.api.Assertions.assertEquals;

import static org.junit.jupiter.api.Assertions.assertTrue;

import static org.junit.jupiter.api.Assertions.fail;

import java.lang.reflect.Field;

import java.lang.reflect.Modifier;

import org.junit.jupiter.api.Test;

class SongTest {

@Test

void testConstantDeclarations() {

String[] constantNames = {"INFO_DELIMITER", "TIME_DELIMITER",

"IDX_TITLE", "IDX_ARTIST", "IDX_TIME"};

try {

for (String name : constantNames) {

Field field = Song.class.getDeclaredField(name);

int modifiers = field.getModifiers();

assertTrue(Modifier.isPrivate(modifiers));

assertTrue(Modifier.isStatic(modifiers));

assertTrue(Modifier.isFinal(modifiers));

}

} catch (NoSuchFieldException e) {

fail();

}

}

@Test

void testConstructorString() {

String info = "It's a Boy; The Who; 38";

Song song = new Song(info);

assertEquals("It's a Boy", song.getTitle());

assertEquals("The Who", song.getArtist());

assertArrayEquals(new int[] {38}, song.getTime());

info = "Secret World; Peter Gabriel; 7:03";

song = new Song(info);

assertEquals("Secret World", song.getTitle());

assertEquals("Peter Gabriel", song.getArtist());

assertArrayEquals(new int[] {3, 7}, song.getTime());

info = "Symphony No. 9; Ludwig van Beethoven; 1:09:00";

song = new Song(info);

assertEquals("Symphony No. 9", song.getTitle());

assertEquals("Ludwig van Beethoven", song.getArtist());

assertArrayEquals(new int[] {0, 9, 1}, song.getTime());

}

@Test

void testToString() {

Song song = new Song("It's a Boy", "The Who", new int[] {38});

String info = "It's a Boy; The Who; 38";

assertEquals(info, song.toString());

song = new Song("Secret World", "Peter Gabriel", new int[] {3, 7});

info = "Secret World; Peter Gabriel; 7:03";

assertEquals(info, song.toString());

song = new Song("Symphony No. 9", "Ludwig van Beethoven",

new int[] {0, 9, 1});

info = "Symphony No. 9; Ludwig van Beethoven; 1:09:00";

assertEquals(info, song.toString());

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Programming With Visual Basic .NET

Authors: Carsten Thomsen

2nd Edition

1590590325, 978-1590590324

More Books

Students also viewed these Databases questions

Question

What were the reasons for their resistance?

Answered: 1 week ago