All Weeks Java Programming: Principles of Software Design Coursera Quiz Answers
Java Programming: Principles of Software Design Week 01 Quiz Answer
Searching Earthquake Data
Q 1. For the assignment you wrote the method quakesOfDepth to print all the earthquakes from a data source whose depth is between a given minimum and maximum value, and also to print out the number of earthquakes found. Modify this method to run your program on the file nov20quakedata.atom (a file with information on 1518 quakes) for quakes with depth between -8000.0 and -5000.0, exclusive.
How many such earthquakes are there?
127
Q 2. For the assignment you wrote the method quakesByPhrase to print all the earthquakes from a data source whose title has a phrase in it at a specified location, and also to print out the number of earthquakes found. Modify this method to run your program on the file nov20quakedata.atom (a file with information on 1518 quakes) for quakes with the phrase “Explosion” at the start of the earthquake’s title.
How many such earthquakes are there?
157
Q 3. For the assignment you wrote the method quakesByPhrase to print all the earthquakes from a data source whose title has a phrase in it at a specified location, and also to print out the number of earthquakes found. Modify this method to run your program on the file nov20quakedata.atom (a file with information on 1518 quakes) for quakes with the phrase “California” at the end of the earthquake’s title.
How many such earthquakes are there?
19
Q 4. For the assignment you wrote the method quakesByPhrase to print all the earthquakes from a data source whose title has a phrase in it at a specified location, and also to print out the number of earthquakes found. Modify this method to run your program on the file nov20quakedata.atom (a file with information on 1518 quakes) for quakes with the phrase “Creek” as a substring in the earthquake’s title.
How many such earthquakes are there?
364
Q 5. For the assignment you wrote the method findLargestQuakes to print a given number of earthquakes from a data source in order from largest magnitude. Modify this method to run your program on the file nov20quakedata.atom (a file with information on 1518 quakes) for the three earthquakes with the largest magnitude.
What is the magnitude of the third largest earthquake? (Give your answer to two decimal places. Note that the US convention uses periods instead of commas to indicate a decimal.)
5.10
Q 6. For the assignment you wrote the method findLargestQuakes to print a given number of earthquakes from a data source in order from largest magnitude. Modify this method to run your program on the file nov20quakedata.atom (a file with information on 1518 quakes) for the five earthquakes with the largest magnitude.
What country is the fifth largest earthquake in?
Answer: Japan
Filtering Data
Q1. For the assignment you wrote the method quakesWithFilter in the class EarthQuakeClient2 to filter earthquakes using two criteria, those that are less than 10,000,000 meters (10,000 km) from Tokyo, Japan whose location is (35.42, 139.43), and that are in Japan (this means “Japan” is the last word in the title). Modify this method to run your program on the file nov20quakedata.atom (a file with information on 1518 quakes) with the same criteria. You should also print out the number of such earthquakes found.
How many such earthquakes are there?
- 74
Q2. For the assignment you wrote the method quakesWithFilter in the class EarthQuakeClient2 to filter earthquakes using two criteria, those with magnitude between 4.0 and 5.0 inclusive and depth between -35,000.0 and -12,000.0 inclusive. Modify this method to run your program on the file nov20quakedata.atom (a file with information on 1518 quakes) with the same criteria. You should also print out the number of such earthquakes found.
How many such earthquakes are there?
- 15
Q3. For the assignment you wrote the method testMatchAllFilter in the class EarthQuakeClient2 to filter earthquakes using three criteria, those with magnitude between 0.0 and 2.0, to test the depth between -100,000.0 and -10,000.0, and if the letter “a” is in the title. Modify this method to run your program on the file nov20quakedata.atom (a file with information on 1518 quakes) with the same criteria. You should also print out the number of such earthquakes found. Make sure you are using the updated (1/12/16) EarthQuakeParser class.
How many such earthquakes are there?
- 187
Q4. For the assignment you wrote the method testMatchAllFilter2 in the class EarthQuakeClient2 to filter earthquakes using three criteria, those with magnitude between 0.0 and 3.0, to test for the distance from Tulsa, Oklahoma at location (36.1314, -95.9372) is less than 10,000,000 meters (or 10,000 km), and if the substring “Ca” is in the title. Modify this method to run your program on the file nov20quakedata.atom (a file with information on 1518 quakes) with the same criteria. You should also print out the number of such earthquakes found. Make sure you are using the updated (1/12/16) EarthQuakeParser class.
How many such earthquakes are there?
- 17
Q5. When the getName method signature is added to the Filter interface, which of the following is most likely NOT a needed change to the DepthFilter class?
- A new method getName to return the name of the filter.
- A parameter in the constructor to initialize the name of the filter.
- A new private String instance variable to store the name of the filter.
- A new method setName to change the name of the filter.
Earthquakes: Programming and Interfaces
Q 1. For the assignment you wrote the method quakesOfDepth to print all the earthquakes from a data source whose depth is between a given minimum and maximum value, and also to print out the number of earthquakes found. Modify this method to run your program on the file nov20quakedata.atom (a file with information on 1518 quakes) for quakes with depth between -12,000.0 and -10,000.0, exclusive.
How many such earthquakes are there?
Answer:
127
Q 2. For the assignment you wrote the method quakesOfDepth to print all the earthquakes from a data source whose depth is between a given minimum and maximum value, and also to print out the number of earthquakes found. Modify this method to run your program on the file nov20quakedata.atom (a file with information on 1518 quakes) for quakes with depth between -4000.0 and -2000.0, exclusive.
How many such earthquakes are there
Answer:
157
Q 3. For the assignment you wrote the method quakesByPhrase to print all the earthquakes from a data source whose title has a phrase in it at a specified location, and also to print out the number of earthquakes found. Modify this method to run your program on the file nov20quakedata.atom (a file with information on 1518 quakes) for quakes with the phrase “Quarry Blast” at the start of the earthquake’s title.
How many such earthquakes are there?
Answer :
19
Q 4. For the assignment you wrote the method quakesByPhrase to print all the earthquakes from a data source whose title has a phrase in it at a specified location, and also to print out the number of earthquakes found. Modify this method to run your program on the file nov20quakedata.atom (a file with information on 1518 quakes) for quakes with the phrase “Alaska” at the end of the earthquake’s title.
How many such earthquakes are there?
Answer:
364
Q 5. For the assignment you wrote the method quakesByPhrase to print all the earthquakes from a data source whose title has a phrase in it at a specified location, and also to print out the number of earthquakes found. Modify this method to run your program on the file nov20quakedata.atom (a file with information on 1518 quakes) for quakes with the phrase “Can” as a substring anywhere in the earthquake’s title.
How many such earthquakes are there?
Answer:
58
Q 6. For the assignment you wrote the method findLargestQuakes to print a given number of earthquakes from a data source in order from largest magnitude. Modify this method to run your program on the file nov20quakedata.atom (a file with information on 1518 quakes) for the 20 earthquakes with the largest magnitude.
What was the magnitude of the 20th largest earthquake?
Answer:
5.10
Q 7. For the assignment you wrote the method findLargestQuakes to print a given number of earthquakes from a data source in order from largest magnitude. Modify this method to run your program on the file nov20quakedata.atom (a file with information on 1518 quakes) for the 50 earthquakes with the largest magnitude.
What country had the 50th largest earthquake?
- Japan
- Kyrgyzstan
- Greece
- Solomon Islands
- Indonesia
Q 8. For the assignment you wrote the method quakesWithFilter in the class EarthQuakeClient2 to filter earthquakes using two criteria. Modify that criteria to be those that are 1,000,000 meters (1,000 km) from Denver, Colorado whose location is (39.7392, -104.9903), and that end with an ‘a’ in their title (for example, that might be an earthquake in Nevada as that ends in ‘a’). Run your program on the file nov20quakedata.atom (a file with information on 1518 quakes). You should also print out the number of such earthquakes found.
How many such earthquakes are there?
Answer:
74
Q 9. For the assignment you wrote the method quakesWithFilter in the class EarthQuakeClient2 to filter earthquakes using two criteria. Modify that critieria to be magnitude between 3.5 and 4.5 inclusive and depth between -55,000.0 and -20,000.0 inclusive. Run your program on the file nov20quakedata.atom (a file with information on 1518 quakes) with this criteria. You should also print out the number of such earthquakes found.
How many such earthquakes are there?
Answer:
15
Q 10. For the assignment you wrote the method testMatchAllFilter in the class EarthQuakeClient2 to filter earthquakes using three criteria. Modify that criteria to be those with magnitude between 1.0 and 4.0 inclusive, to test the depth between -180,000.0 and -30,000.0 inclusive, and if the letter “o” is in the title. Modify this method to run your program on the file nov20quakedata.atom (a file with information on 1518 quakes) with the same criteria. You should also print out the number of such earthquakes found.
How many such earthquakes are there?
Answer:
187
Q 11. For the assignment you wrote the method testMatchAllFilter2 in the class EarthQuakeClient2 to filter earthquakes using three criteria. Modify that criteria to be those with magnitude between 0.0 and 5.0 inclusive, to test for the distance from Billund, Denmark at location (55.7308, 9.1153) is less than 3,000,000 meters (3000 km), and if the letter “e” is in the title. Run your program on the file nov20quakedata.atom (a file with information on 1518 quakes) with the same criteria. You should also print out the number of such earthquakes found.
How many such earthquakes are there?
Answer:
17
Q 12. Suppose one adds the getName( ) method to the Filter interface and adds a String parameter to the constructor of all the classes that implement a Filter to include the name of the Filter. Suppose one then creates the following code segment after reading in earthquake data into the ArrayList of QuakeData called list.
MatchAllFilter maf = new MatchAllFilter();
maf.addFilter(new MagnitudeFilter(3.0,5.0,"Magnitude"));
Location city = new Location(51.7308,-34.1153);
maf.addFilter(new DistanceFromFilter(3000*1000, city, "Distance"));
maf.addFilter(new PhraseFilter("any", "i", "Phrase"));
System.out.println("Filters used are: " + maf.getName())
There are no compile errors—the DistanceFromFilter class includes “implements Filter” in the first line of the class definition, the program runs and the output is:
Filters used are: Magnitude null Phrase
Which of the following is most likely the error as to why “null” was printed instead of Distance?
- The getName( ) method in the MatchAllFilter class had a logic error in it.
- The filter method in the EarthQuakeClient2 class has a logic error.
- In the DistanceFromFilter class, a private variable for the name was not assigned the value of the parameter representing the name of the filter in the Constructor method.
- There was not a getName( ) method in the DistanceFromFilter class.
Java Programming: Principles of Software Design Week 02 Quiz Answer
Implementing Selection Sort
Q1. For the assignment you wrote the method sortByLargestDepth in the class QuakeSortInPlace to sort earthquakes by their depth from largest depth to smallest depth using the selection sort algorithm. Modify this method to do exactly 70 passes and then modify the testSort method to run this method on the file earthQuakeDataDec6sample1.atom. The file may not be completely sorted as there are more than 70 quakes in the file.
After running your program of 70 selection sort passes on this file, what is the depth of the last earthquake in the ArrayList?
- -100000.00
Q2. For the assignment you wrote the method sortByMagnitudeWithCheck in the class QuakeSortInPlace to sort earthquakes by their magnitude from smallest to largest using the selection sort algorithm, and stopping once the ArrayList is sorted. Modify testSort to run this method on the file earthQuakeDataDec6sample2.atom. Make sure you are using the updated (1/12/16) version of the EarthQuakeParser class.
How many passes are needed to sort this file?
Q3. For the assignment you wrote the method sortByMagnitudeWithBubbleSortWithCheck in the class QuakeSortInPlace to sort earthquakes by their magnitude from smallest to largest using the bubble sort algorithm, and stopping once the ArrayList is sorted. Modify testSort to run this method on the file earthQuakeDataDec6sample1.atom.
How many passes are needed to sort this file?
Q4. Consider the following ArrayList of six integers.
7 3 2 8 1 4
What does this ArrayList look like after two passes of selection sort that sorts the elements in numeric order from smallest to largest?
- 1 3 2 8 7 4
- 7 3 2 8 1 4
- 1 2 7 3 4 8
- 1 2 3 4 7 8
- 1 2 3 8 7 4
- 2 3 1 4 7 8
Q5. Consider the following ArrayList of six integers.
7 3 2 8 1 4
What does this ArrayList look like after two passes of bubble sort that sorts the elements in numeric order from smallest to largest?
- 3 2 7 1 4 8
- 2 1 3 4 7 8
- 2 3 1 4 7 8
- 2 3 4 1 7 8
- 7 3 2 8 1 4
- 3 2 1 7 4 8
Sorting at Scale
Q1. For the assignment you wrote the method sortByLargestDepth in the class QuakeSortInPlace to sort earthquakes by their depth from largest depth to smallest depth using the selection sort algorithm. Modify this method to do exactly 70 passes and then modify the testSort method to run this method on the file earthQuakeDataDec6sample1.atom. The file may not be completely sorted as there are more than 70 quakes in the file.
After running your program of 70 selection sort passes on this file, what is the depth of the last earthquake in the ArrayList?
- -100000.00
Q2. For the assignment you wrote the method sortByMagnitudeWithCheck in the class QuakeSortInPlace to sort earthquakes by their magnitude from smallest to largest using the selection sort algorithm, and stopping once the ArrayList is sorted. Modify testSort to run this method on the file earthQuakeDataDec6sample2.atom. Make sure you are using the updated (1/12/16) version of the EarthQuakeParser class.
How many passes are needed to sort this file?
Q3. For the assignment you wrote the method sortByMagnitudeWithBubbleSortWithCheck in the class QuakeSortInPlace to sort earthquakes by their magnitude from smallest to largest using the bubble sort algorithm, and stopping once the ArrayList is sorted. Modify testSort to run this method on the file earthQuakeDataDec6sample1.atom.
How many passes are needed to sort this file?
Q4. Consider the following ArrayList of six integers.
7 3 2 8 1 4
What does this ArrayList look like after two passes of selection sort that sorts the elements in numeric order from smallest to largest?
- 1 3 2 8 7 4
- 7 3 2 8 1 4
- 1 2 7 3 4 8
- 1 2 3 4 7 8
- 1 2 3 8 7 4
- 2 3 1 4 7 8
Q5. Consider the following ArrayList of six integers.
7 3 2 8 1 4
What does this ArrayList look like after two passes of bubble sort that sorts the elements in numeric order from smallest to largest?
- 3 2 7 1 4 8
- 2 1 3 4 7 8
- 2 3 1 4 7 8
- 2 3 4 1 7 8
- 7 3 2 8 1 4
- 3 2 1 7 4 8
Sorting at Scale
Q1. For the assignment you wrote the method sortByLargestDepth in the class QuakeSortInPlace to sort earthquakes by their depth from largest depth to smallest depth using the selection sort algorithm. Modify this method to do exactly 70 passes and then modify the testSort method to run this method on the file earthQuakeDataDec6sample1.atom. The file may not be completely sorted as there are more than 70 quakes in the file.
After running your program of 70 selection sort passes on this file, what is the depth of the last earthquake in the ArrayList?
- -100000.00
Q2. For the assignment you wrote the method sortByMagnitudeWithCheck in the class QuakeSortInPlace to sort earthquakes by their magnitude from smallest to largest using the selection sort algorithm, and stopping once the ArrayList is sorted. Modify testSort to run this method on the file earthQuakeDataDec6sample2.atom. Make sure you are using the updated (1/12/16) version of the EarthQuakeParser class.
How many passes are needed to sort this file?
Q3. For the assignment you wrote the method sortByMagnitudeWithBubbleSortWithCheck in the class QuakeSortInPlace to sort earthquakes by their magnitude from smallest to largest using the bubble sort algorithm, and stopping once the ArrayList is sorted. Modify testSort to run this method on the file earthQuakeDataDec6sample1.atom.
How many passes are needed to sort this file?
Q4. Consider the following ArrayList of six integers.
7 3 2 8 1 4
What does this ArrayList look like after two passes of selection sort that sorts the elements in numeric order from smallest to largest?
- 1 3 2 8 7 4
- 7 3 2 8 1 4
- 1 2 7 3 4 8
- 1 2 3 4 7 8
- 1 2 3 8 7 4
- 2 3 1 4 7 8
Q5. Consider the following ArrayList of six integers.
7 3 2 8 1 4
What does this ArrayList look like after two passes of bubble sort that sorts the elements in numeric order from smallest to largest?
- 3 2 7 1 4 8
- 2 1 3 4 7 8
- 2 3 1 4 7 8
- 2 3 4 1 7 8
- 7 3 2 8 1 4
- 3 2 1 7 4 8
Sorting at Scale
Q 1. For the assignment, you modified the compareTo method in the class QuakeEntry to sort earthquakes by their magnitude first, from smallest magnitude to largest magnitude, and to break ties by their depth, from largest depth to smallest depth using the Collections.sort method. Modify the sortWithCompareTo method in the DifferentSorters class to print out the QuakeEntry in position 50 after sorting the QuakeEntry’s by the above method. Run this method on the file earthQuakeDataDec6sample2.atom.
What is the depth of the earthquake that is in position 50 after the earthquakes are sorted by the above method?
Answer:
please comment the answer:
Q 2. For the assignment, you wrote the TitleAndDepthComparator class to sort earthquakes by their title first, in alphabetical order, and to break ties by their depth, from smallest depth to largest depth. You then used the Collections.sort method with the TitleAndDepthComparator. Modify the sortByTitleAndDepth method in the DifferentSorters class to print out the QuakeEntry in position 50 after sorting the QuakeEntry’s by the above method. Run this method on the file earthQuakeDataDec6sample2.atom.
What is the depth of the earthquake that is in position 50 after the earthquakes are sorted by the above method?
Answer:
please comment the answer:
Q 3. For the assignment, you wrote the TitleLastAndMagnitudeComparator class to sort earthquakes by the last word in their title first, in alphabetical order, and to break ties by their magnitude, from smallest magnitude to largest magnitude. You then used the Collections.sort method with the TitleLastAndMagnitudeComparator. Modify the sortByLastWordInTitleThenByMagnitude method in the DifferentSorters class to print out the QuakeEntry in position 50 after sorting the QuakeEntry’s by the above method. Run this method on the file earthQuakeDataDec6sample1.atom.
What is the depth of the earthquake that is in position 50 after the earthquakes are sorted by the above method?
Answer:
please comment the answer:
Earthquakes: Sorting Algorithms
Q 1. For an assignment you wrote the method sortByLargestDepth in the class QuakeSortInPlace to sort earthquakes by their depth from largest depth to smallest depth using the selection sort algorithm. Modify this method to do exactly 70 passes and then modify testSort to run this method on the file earthQuakeDataDec6sample2.atom. The file may not be completely sorted as there are many quakes in the file.
After running your program of 70 selection sort passes on this file, what is the depth of the last earthquake in the ArrayList?
Note: This question has variations. If you attempt this quiz multiple times, make sure you are using the correct number of sort passes!
Answer:
-100000.00
Q 2. For an assignment you wrote the method sortByMagnitudeWithCheck in the class QuakeSortInPlace to sort earthquakes by their magnitude from smallest to largest using the selection sort algorithm, and stopping with passes once the ArrayList is sorted. Modify testSort to run this method on the file earthQuakeDataWeekDec6sample2.atom.
How many passes are needed to sort this file?
Note: This question has variations. If you attempt this quiz multiple times, make sure you are using the correct data file!
- 1259
- 1273
- 1277
- 1279
- 1280
- 1284
Q 3. For an assignment you wrote the method sortByMagnitudeWithBubbleSortWithCheck in the class QuakeSortInPlace to sort earthquakes by their magnitude from smallest to largest using the bubble sort algorithm, and stopping with passes once the ArrayList is sorted. Modify testSort to run this method on the file earthQuakeDataWeekDec6sample2.atom. Make sure you are using the updated (1/12/16) version of the EarthQuakeParser class.
How many passes are needed to sort this file?
Note: This question has variations. If you attempt this quiz multiple times, make sure you are using the correct data file!
- 1226
- 1233
- 1240
- 1255
- 1260
- 1267
Q 4. Consider an ArrayList of following six integers.
2 4 5 9 8 1
What does this ArrayList look like after two passes of selection sort that sorts the elements in numeric order from smallest to largest?1 point
- 1 2 4 9 8 5
- 1 2 5 4 9 8
- 1 2 5 9 8 4
- 1 4 5 9 8 2
- 2 4 5 9 8 1
- 4 1 5 2 8 9
Q 5. Consider an ArrayList of following six integers.
4 2 5 9 8 1
What does this ArrayList look like after two passes of bubble sort that sorts the elements in numeric order from smallest to largest?
- 2 4 1 5 8 9
- 2 4 5 1 8 9
- 2 4 5 8 1 9
- 4 2 5 1 8 9
- 4 2 5 8 1 9
- 4 2 5 9 8 1
Q 6. For an assignment, you modified the compareTo operator in the class QuakeEntry to sort earthquakes by their magnitude first, from smallest magnitude to largest magnitude, and to break ties by their depth, from largest depth to smallest depth. Then you wrote the method sortWithCompareTo in the DifferentSorters class using the Collections.sort method. Modify this method to print out the QuakeEntry in position 600 after sorting the QuakeEntry’s by the above method. Run this method on the file earthQuakeDataWeekDec6sample2.atom.
What is the depth of the earthquake that is in position 600 after the earthquakes are sorted by the above method?
Note: This question has variations. If you attempt this quiz multiple times, make sure you are using the correct data file!
Answer:
-53600.00
Q 7. For an assignment, you wrote the TitleAndDepthComparator to sort earthquakes by their title first, in alphabetical order, and to break ties by their depth, from smallest depth to largest depth. You then used the Collections.sort method with the TitleAndDepthComparator. Modify the sortByTitleAndDepth method in the DifferentSorters class to print out the QuakeEntry in position 500 after sorting the QuakeEntry’s by the above method. Run this method on the file earthQuakeDataWeekDec6sample2.atom.
What is the depth of the earthquake that is in position 500 after the earthquakes are sorted by the above method?
Note: This question has variations. If you attempt this quiz multiple times, make sure you are using the correct data file!
Answer:
-7630.00
Q 8. For an assignment, you wrote the TitleLastAndMagnitudeComparator to sort earthquakes by the last word in their title first, in alphabetical order, and to break ties by their magnitude, from smallest to largest. You then used the Collections.sort method with the TitleLastAndMagnitudeComparator. Modify the sortByLastWordInTitleThenByMagnitude method in the DifferentSorters class to print out the QuakeEntry in position 500 after sorting the QuakeEntry’s by the above method. Run this method on the file earthQuakeDataWeekDec6sample2.atom.
What is the depth of the earthquake that is in position 500 after the earthquakes are sorted by the above method?
Note: This question has variations. If you attempt this quiz multiple times, make sure you are using the correct data file!
Answer:
-1490.00
Java Programming: Principles of Software Design Week 03 Quiz Answer
Generating Random Text
Q 1. Here is the code for runMarkovZero.
1 public void runMarkovZero() {
2 FileResource fr = new FileResource();
3 String st = fr.asString();
4 st = st.replace('\n', ' ');
5 MarkovZero markov = new MarkovZero();
6 markov.setTraining(st);
7 for(int k=0; k < 3; k++) {
8 String text = markov.getRandomText(500);
9 printOut(text);
10 }
11 }
Which one of the following is the best choice for the correct line of code and the location for this code in the runMarkovZero method in the MarkovRunner class to set the random number seed to 36?
Answer:
Q 2. In the MarkovRunner class in the runMarkovZero method, set the random seed to 88 and run this method on the file confucius.txt.
Which one of the following is the first line of text when this program runs?
- ewe euwhfgtneAp .tnhnt I t.evtocTbhamaf eo;tnte ,yti,tae.epoleg
- rdceu aselAne e9etscered,dnanaceitt t v aes bsslwa ov nMa wse
- pA laIod en niega i rsm.ttohrp, hem.whs imkii,. ievo1rmeo ln,ou
- sin.ielfisivoaiec; eto. erds efr e5 l-i4heiYh iM.rCiee lnuo.aeryhtgini
- r2sa ahir Mi thiT neo [ea oad Duey uyrl ouR enae usytgEdliBo
Q 3 . In the Tester class, run the program testGetFollowsWithFile with the string “o” and the file melville.txt.
What is the size of the ArrayList of characters that follow “o”?
Answer:
Q 4. In the Tester class, run the program testGetFollowsWithFile with the string “th” and the file melville.txt.
What is the size of the ArrayList of characters that follow “th”?
Answer:
Q 5. After the completion of the MarkovOne class to generate random text by finding all the characters that follow one character, set the random seed in the runMarkovOne method in the MarkovRunner class to 273. Then run runMarkovOne on the file confucius.txt.
What is the first line generated?
- s oldo ataid 1: Yin I at. llftho he Whirnict al hing, iisthoteeas
- f Hune whe, ane Whele wiuserretch’sy de be). l lif-pp: h’or.
- Wes She erainghemifof: frn fa 60, wiche h aof ct; n the s, f (it’s not correct)
- asaid teedar gemaungr Yü! oove Pr tang; thedy bem _, d me t wout
- id niius tof ch’it nde. amesye the hr thof Makerime tangurntr
Q 6. After the completion of the MarkovFour class to generate random text by finding all the characters that follow a String of four characters, set the random seed in the runMarkovFour method in the MarkovRunner class to 371. Then run runMarkovFour on the file confucius.txt.
What is the first line generated?
- nswered, The Masters the nations or a confidentity any died all
- hollow hear is good by the perhaps, in small the people’s upright
- to a general; vi. 20, is to gover said, Men’s last the people;
- be betterishes are below. My thing made available most to Ts’ai.
- _Ch’en K’ang; vii. 13, asks what Liu-hsia says the music; at
Q 7. After the completion of the MarkovModel class to generate random text by finding all the characters that follow a String of characters, set the random seed in the runMarkovModel method in the MarkovRunner class to 365 and pass in N as 8. Then run runMarkovModel on the file confucius.txt.
What is the first line generated?
- the Master said, It is the better, and die at eve were too ill
- ve is to mete out good form; now it is so no more reading Team
- to rise. Tzu-lu was everlastingly humming the second kingdom?
- 16. The Master angled, but these mine? 3. The Master said, Hen
- lture, he binds me with changed. 17. When mounting his sounding
Interface and Abstract Class
Q 1. After adding in the IMarkovModel interface and the abstract class AbstractMarkovModel, you added a seed variable in the runMarkov method in the MarkovRunnerWithInterface class and passed this variable as a 4th parameter named seed.
Which one of the following changes removes the compile error for the line “markov.setRandom(seed)” in the runMarkov method and so that the program runs correctly?
Answer:
Add this line in IMarkovModel:
public void setRandom(int seed);
Q 2. Which if the following is the best answer for how the HashMap in EfficientMarkovModel should be declared?
Answer:
private HashMap<String, ArrayList<String>> myMap;
Q 3. In the EfficientMarkovModel class, comment out the print statements in the printHashMapInfo method that prints out the map. Make sure printHashMapInfo is called right after the HashMap is built. Then in the MarkovRunnerWithInterface class call the runModel method on an EfficientMarkovModel of order 5 with seed 615 on the file romeo.txt.
How many keys are in the HashMap?
Answer:
comment the Answer
Q 4. In the EfficientMarkovModel class, comment out the print statements in the printHashMapInfo method that prints out the map. Make sure printHashMapInfo is called right after the HashMap is built. Then in the MarkovRunnerWithInterface class call the runModel method on an EfficientMarkovModel of order 5 with seed 615 on the file romeo.txt.
What was the size of the largest ArrayList in the HashMap?
Answer:
comment the Answer
Word N-Grams
Q 1. After the completion of the MarkovWordOne class to generate random text by finding all the single words that follow one word, set the random seed in the runMarkov method in the MarkovRunner class to 139. Then run runMarkov on the file confucius.txt. The first line generated should be:
no court was altered into it, give ear for three had ruled none
Answer:
comment the answer
Q 2. In debugging the indexOf method that was in the MarkovWordOne class, you wrote a public method named testIndexOf, also in the MarkovWordOne class. Suppose instead you put the testIndexOf method in the MarkovRunner class and created a MarkovWordOne object named mark and called mark.indexOf with appropriate parameters.
Which one of the following best describes why this way of testing indexOf does not work?
- The indexOf method is private.
- The testIndexOf method’s signature is not in the IMarkovModel interface.
- The indexOf method’s signature is not in the IMarkovModel interface.
- The String array myText is private.
Q 3. After the completion of the MarkovWordTwo class to generate random text by finding all the single words that follow two words, set the random seed in the runMarkovTwo method in the MarkovRunner class to 832. Then run runMarkovTwo on the file confucius.txt. The first line generated should be:
daring becomes unruliness, frankness becomes rudeness. When gentlemen
Answer:
comment the Answer
WordGram Class
Q 1. After the completion of the MarkovWord class with a WordGram to generate random text by finding all the single words that follow a key, run the following example. In the MarkovRunner class, for the method runMarkov, set a MarkovWord to the order 3 (using 3 words to predict the next word), the random seed in the runModel method to 621. Then run runMarkov on the file confucius.txt. The first line generated should be:
you must return the medium with your written explanation. The
What is the third line generated?
Answer:
Q 2. After the completion of the MarkovWord class with a WordGram to generate random text by finding all the single words that follow a key, run the following example. In the MarkovRunner class, for the method runMarkov, set a MarkovWord to the order 5 (using 5 words to predict the next word), the random seed in the runModel method to 844. Then run runMarkov on the file confucius.txt. The first line generated should be:
The last part of the music, when all the instruments were played
Answer:
comment the Answer
Q 3. In the EfficientMarkovWord class, comment out the print statements in the printHashMapInfo method that prints out the map. Make sure printHashMapInfo is called right after the HashMap is built. Then in the MarkovRunner class call the printHashMapInfo method on an EfficientMarkovModel of order 3 with random seed 371 on the file confucius.txt.
How many keys are in the HashMap?
Answer:
comment the answer
Q 4. In the EfficientMarkovWord class, comment out the print statements in the printHashMapInfo method that prints out the map. Make sure printHashMapInfo is called right after the HashMap is built. Then in the MarkovRunner class call the printHashMapInfo method on an EfficientMarkovModel of order 2 with random seed 65 on the file confucius.txt.
What is the size of the largest follow ArrayList in the map?
Answer:
comment the Asnswer
Q 5. Which one of the following is the best choice for the WordGram hashCode method?
Answer:
return toString().hashCode();
N-Grams: Predictive Text
Q 1. Here is the code for runMarkovZero.
1 public void runMarkovZero() {
2 FileResource fr = new FileResource();
3 String st = fr.asString();
4 st = st.replace('\n', ' ');
5 MarkovZero markov = new MarkovZero();
6 markov.setTraining(st);
7 for(int k=0; k < 3; k++) {
8 String text = markov.getRandomText(500);
9 printOut(text);
10 }
11 }
Suppose the line
markov.setRandom(18);
is put between lines 7 and 8, as the first line in the body of the for loop. Which one of the following options best describes how this line affects the program?
- Each of three randomly generated texts will all be identical.
- You will get three distinct randomly generated texts.
- You will get three randomly generated texts, but the second generated text has one new letter and then is identical to the first text minus one letter, and the third generated text has one new letter and then is identical to the second text minus two letters.
- You will get three randomly generated texts, but the second generated text has the first line with new random text, but the remaining lines are identical to the first n-1 lines where the first text had n lines. The third generated text has the first line with new random text, but the remaining lines are identical to the first n-1 lines of the second text.
Q 2. In the MarkovRunner class in the runMarkovZero method, set the random seed to 1024 and run this method on the file confucius.txt.
What is the first line of text output when this program runs?
Answer:
comment the Answer
Q 3. In the Tester class, run the program testGetFollowsWithFile with the string “o” and the file confucius.txt.
What is the size of the ArrayList of characters that follow “o”?
Answer:
10453
Q 4. In the Tester class, run the program testGetFollowsWithFile with the string “he” and the file confucius.txt.
What is the size of the ArrayList of characters that follow “he”?
Answer:
3715
Q 5. After the completion of the MarkovOne class to generate random text by finding all the characters that follow one character, set the random seed in the runMarkovOne method in the MarkovRunner class to 365. Then run runMarkovOne on the file romeo.txt.
What is the first line generated?
Answer:
y O wirs bloay Ger. fo. tifthy the, A my; st-ie d, s. bloulate
Q 6. After the completion of the MarkovFour class to generate random text by finding all the characters that follow four characters, set the random seed in the runMarkovFour method in the MarkovRunner class to 715. Then run runMarkovFour on the file romeo.txt.
What is the first line of text generated?
Answer:
man in a green, for that haste,for a foot in her from Tybalt!
Q 7. After the completion of the MarkovModel class to generate random text by finding all the characters that follow N characters, set the random seed in the runMarkovModel method in the MarkovRunner class to 953 and pass in N as 7. Then run runMarkovModel on the file romeo.txt.
What is the first line of text generated?
Answer:
e uncle Capulet’s orchard. Enter an officer, and light-more
Q 8. For the class EfficientMarkovModel, which one of the following is the best place to call the buildMap method?
- In the setTraining method.
- In the constructor.
- In the getRandomText method before the for loop.
- In the getRandomText method as the first line in the for loop.
- In the setRandomText method after myRandom is set.
Q 9. In the EfficientMarkovModel class, comment out the print statements in the printHashMapInfo method that prints out the map. Make sure printHashMapInfo is called right after the HashMap is built. Then in the MarkovRunnerWithInterface class call the runModel method on an EfficientMarkovModel of order 6 with seed 792 on the file confucius.txt.
How many keys are in the HashMap?
Answer:
comment the Answer
Q 10. In the EfficientMarkovModel class, comment out the print statements in the printHashMapInfo method that prints out the map. Make sure printHashMapInfo is called right after the HashMap is built. Then in the MarkovRunnerWithInterface class call the runModel method on an EfficientMarkovModel of order 5 with seed 531 on the file confucius.txt.
What was the size of the largest ArrayList in the HashMap?
Answer:
70162
Java Programming: Principles of Software Design Week 04 Quiz Answer
Q 1. What is the name of the method that starts a Java program?
- begin
- firstMethod
- _start
- main
Q 2. What argument type should the method that starts a Java program take?
- no arguments
- an ArrayList<String>
- an int and a String
- an array of Strings
Q 3. What effect comes from declaring a field “static”?
- The value in the field can only increase, never decrease.
- The value in the field cannot be changed once it is initialized.
- The value in the field can only be changed by code inside the class.
- There is only one copy of that field for the entire class, not one per instance.
Q 4. In Java, how is the keyword “throw” used?
- To transfer data across a network connection.
- To handle an exception that has already occurred.
- To make an exception occur when the program detects a problematic circumstance that it cannot directly handle.
- To open a file for writing.
Q 5. Creating a new socket, as with:
Socket s = new Socket(addr,port);
can throw an IOException according to the documentation of the java.net.Socket class.
Which of the following structures is the best way to create a socket while handling the exception?
Answer:
try {
Socket s = new Socket(addr, port);
}
catch(IOException ioe) {
//code to handle the exception
}
//code that uses s
Q 6. In Java, how is the keyword “finally” used?
- To indicate that you are finally done debugging a particularly complicated piece of code.
- To specify code to run when the program exits.
- In exception handling to specify code that should be executed regardless of whether an exception happened or not.
- To specify a piece of code to execute immediately after the end of the current loop.
Q 7. If you wanted to read the contents of a file without using the edu.duke package, you might call Files.newBufferedReader.
What would you pass into Files.newBufferedReader?
- an IOException
- a Path
- a Comparator
- a FileResource
Q 8. If you need to read data from a website without using the edu.duke package, you would probably want to use classes found in which package?
- java.net
- java.internet
- java.urls
- java.lolcats
Q 9. You saw that a BufferedReader can be used to read data from a file on the local computer, as well as from a website. You could also use it with other sources of data, as long as you have an appropriate Reader class to access the data.
These capabilities are a great example of the benefits of which programming principles? Select the two best options.
Answer:
- Open/Closed Principle: the BufferedReader class is designed such that it can have its functionality expanded (to read from new data sources) without having to modify its code
- Astrachan’s Law: you can only use the BufferedReader class to solve interesting problems, never for something boring or that you could do by hand
Get All Course Quiz Answers of Java Programming and Software Engineering Fundamentals Specialization
Programming Foundations with JavaScript, HTML and CSS Coursera Quiz Answers
Java Programming: Arrays, Lists, and Structured Data Coursera Quiz Answers
Java Programming: Principles of Software Design Coursera Quiz Answers