Please help with this one using a try-catch. Thanks!
import java.util.LinkedList;
/**
This question uses the same code as Question 5 in Question_5_Add_Exception_Handling.java
Instead of waiting for an exception and catching it, it is usually better to try and prevent problems.
Can you think of a better way to stop this code crashing?
Modify the printLanguageList and wordCount methods program so that both methods work correctly without using a try-catch statements.
*/
public class Question_6_Fix_Code_No_Exception_Handling {
public static void main(String[] args) {
Question_6_Fix_Code_No_Exception_Handling q6 = new Question_6_Fix_Code_No_Exception_Handling();
// Test the printLanguageList() method
q6.printLanguageList();
// Test the wordCount() method
String sentence1 = "This is an example sentence.";
String sentence2 = null;
int words1 = q6.wordCount(sentence1);
int words2 = q6.wordCount(sentence2);
System.out.println(sentence1 + " has this many words: " + words1);
System.out.println(sentence2 + " has this many words: " + words2);
}
/* Adds some example programming languages to a LinkedList, and then prints them in reverse order.
*/
//Start of printLanguageList. Don't change or move this comment. The Autograder needs it.
public void printLanguageList() {
// TODO stop this code crashing by modifying it
// to check for possible errors before they happen.
// Make sure you still print all of the languages from the list.
// Also make sure your code will still work for any number of languages in the LinkedList.
LinkedList<String> languages = new LinkedList<>();
languages.push("JavaScript");
languages.push("Python");
languages.push("C#");
while (true) {
String oneLanguage = languages.pop();
System.out.println(oneLanguage);
}
}
//Start of printLanguageList. Don't change or move this comment. The Autograder needs it.
/* A very simple word count function.
This function should return the number of words in a string.
For this program, each word is assumed to be separated by a single space character.
If the String sentence is null, this method should return 0.
Counting words in real-world situations can be a much trickier problem,
with various special cases to consider.
For example, is "sugar-free" one word, or two? How many words in "D.B. Cooper" ? */
//Start of wordCount. Don't change or move this comment, the Autograder needs it.
public int wordCount(String sentence) {
// TODO This code throws a NullPointerException if the String sentence is null.
// Add code to test if the String is null before splitting it.
// Return 0 if the String is null.
String[] words = sentence.split(" ");
return words.length;
}
//End of wordCount. Don't change or move this comment, the Autograder needs it
}
import java.util.LinkedList;
import java.util.NoSuchElementException;
/**
* This question uses the same code as Question 5 in Question_5_Add_Exception_Handling.java
* <p>
* Instead of waiting for an exception and catching it, it is usually better to try and prevent problems.
* <p>
* Can you think of a better way to stop this code crashing?
* <p>
* Modify the printLanguageList and wordCount methods program so that both methods work correctly without using a try-catch statements.
*/
public class Question_6_Fix_Code_No_Exception_Handling {
public static void main(String[] args) {
Question_6_Fix_Code_No_Exception_Handling q6 = new Question_6_Fix_Code_No_Exception_Handling();
// Test the printLanguageList() method
q6.printLanguageList();
// Test the wordCount() method
String sentence1 = "This is an example sentence.";
String sentence2 = null;
int words1 = q6.wordCount(sentence1);
int words2 = q6.wordCount(sentence2);
System.out.println(sentence1 + " has this many words: " + words1);
System.out.println(sentence2 + " has this many words: " + words2);
}
/* Adds some example programming languages to a LinkedList, and then prints them in reverse order.
*/
//Start of printLanguageList. Don't change or move this comment. The Autograder needs it.
public void printLanguageList() {
// to check for possible errors before they happen.
// Make sure you still print all of the languages from the list.
// Also make sure your code will still work for any number of languages in the LinkedList.
LinkedList<String> languages = new LinkedList<>();
languages.push("JavaScript");
languages.push("Python");
languages.push("C#");
try {
while (true) {
String oneLanguage = languages.pop();
System.out.println(oneLanguage);
}
} catch (NoSuchElementException e) {
}
}
//Start of printLanguageList. Don't change or move this comment. The Autograder needs it.
/* A very simple word count function.
This function should return the number of words in a string.
For this program, each word is assumed to be separated by a single space character.
If the String sentence is null, this method should return 0.
Counting words in real-world situations can be a much trickier problem,
with various special cases to consider.
For example, is "sugar-free" one word, or two? How many words in "D.B. Cooper" ? */
//Start of wordCount. Don't change or move this comment, the Autograder needs it.
public int wordCount(String sentence) {
// Add code to test if the String is null before splitting it.
// Return 0 if the String is null.
try {
String[] words = sentence.split(" ");
return words.length;
} catch (NullPointerException e) {
return 0;
}
}
//End of wordCount. Don't change or move this comment, the Autograder needs it
}
Please help with this one using a try-catch. Thanks! import java.util.LinkedList; /** This question uses the...
Complete the code: package hw4; import java.io.File; import java.io.IOException; import java.util.LinkedList; import java.util.Scanner; /* * This class is used by: * 1. FindSpacing.java * 2. FindSpacingDriver.java * 3. WordGame.java * 4. WordGameDriver.java */ public class WordGameHelperClass { /* * Returns true if an only the string s * is equal to one of the strings in dict. * Assumes dict is in alphabetical order. */ public static boolean inDictionary(String [] dict, String s) { // TODO Implement using binary search...
//MultiValuedTreeMap.java import java.util.Iterator; import java.util.LinkedList; import java.util.TreeMap; //import java.util.ArrayList; public class MultiValuedTreeMap<K, V> extends TreeMap<K, LinkedList<V>> implements Iterable<Pair<K, V>> { private static final long serialVersionUID = -6229569372944782075L; public void add(K k, V v) { // Problem 1 method // empty linked list, with key=k if (!containsKey(k)) { put(k, new LinkedList<V>()); } // adding v to the linked list associated with key k get(k).add(v); } public V removeFirst(K k)...
The DictionaryClient program featured in the class lecture also used the try-catch when creating a socket. Socket are auto-closeable and thus can use a try-with-resources instead. Edit the dictionary program to use try-with-resources instead. package MySockets; import java.net.*; import java.io.*; public class DictionaryClient { private static final String SERVER = "dict.org"; private static final int PORT = 2628; private static final int TIMEOUT = 15000; public static void main(String[] args) { Socket ...
import java.util.List;
import java.util.ArrayList;
import java.util.LinkedList;
public class ListPractice {
private static final int[] arr = new int[100000];
public static void main(String[] args) {
for(int i=0; i<100000;
i++)
arr[i] =
i;
//TODO comment out this line
LinkedList<Integer> list =
new LinkedList<Integer>();
//TODO uncomment this line
//List<Integer> list = new
ArrayList<Integer>();
//TODO change the rest of the...
package hw3; import java.util.LinkedList; /* *********************************************************************** * A simple BST with int keys and no values * * Complete each function below. * Write each function as a separate recursive definition (do not use more than one helper per function). * Depth of root==0. * Height of leaf==0. * Size of empty tree==0. * Height of empty tree=-1. * * TODO: complete the functions in this file. * DO NOT change the Node class. * DO NOT change the name...
I need help with my code when I run my code running the wrong thing like this After downSize() words.length=60003 wordCount=60003 vowelCount=206728 this is my code here import java.io.*; import java.util.*; public class Project02 { static final int INITIAL_CAPACITY = 10; public static void main (String[] args) throws Exception { // ALWAYS TEST FIRST TO VERIFY USER PUT REQUIRED INPUT FILE NAME ON THE COMMAND LINE if (args.length < 1 ) ...
Trying to practice this assignment Argument list: the *yahoonews.txt Data file: yahoonews.txt Write a program named WordCount.java, in this program, implement two static methods as specified below: public static int countWord(Sting word, String str) this method counts the number of occurrence of the word in the String (str) public static int countWord(String word, File file) This method counts the number of occurrence of the word in the file. Ignore case in the word. Possible punctuation and symbals in the file...
import java.util.Iterator; import java.util.LinkedList; import java.util.TreeMap; import java.util.ArrayList; public class MultiValuedTreeMap<K, V> extends TreeMap<K, LinkedList<V>> implements Iterable<Pair<K, V>> { private static final long serialVersionUID = -6229569372944782075L; public void add(K k, V v) { if (!containsKey(k)) { put(k, new LinkedList<V>()); } get(k).add(v); } public V removeFirst(K k) { if (!containsKey(k)) { return null; } V value = get(k).removeFirst(); if (get(k).isEmpty()) { super.remove(k); } return value; }...
Hi. I require your wonderful help with figuring out this challenging code. import java.awt.Color; import java.awt.image.BufferedImage; import java.io.*; import javax.imageio.ImageIO; public class ImageLab { /* * This is the grayscale example. Use it for inspiration / help, but you dont * need to change it. * * Creates and returns a new BufferedImage of the same size as the input * image. The new image is the grayscale version of the input (red, green, and * blue components averaged together)....
ANNOTATE BRIEFLY LINE BY LINE THE FOLLOWING CODE: package shop.data; import junit.framework.Assert; import junit.framework.TestCase; public class DataTEST extends TestCase { public DataTEST(String name) { super(name); } public void testConstructorAndAttributes() { String title1 = "XX"; String director1 = "XY"; String title2 = " XX "; String director2 = " XY "; int year = 2002; Video v1 = Data.newVideo(title1, year, director1); Assert.assertSame(title1, v1.title()); Assert.assertEquals(year, v1.year()); Assert.assertSame(director1, v1.director()); Video v2 = Data.newVideo(title2, year, director2); Assert.assertEquals(title1, v2.title()); Assert.assertEquals(director1, v2.director()); } public void testConstructorExceptionYear()...