Problem 1
1. In the src → edu.neiu.p2 directory, create a package named problem1.
2. Create a Java class named StringParser with the following:
• A public static method named findInteger that takes a String and two char variables as parameters (in that order) and does not return anything.
• The method should find and print the integer value that is located in between the two characters. You can assume that the second char parameter will always follow the first char parameter. However, you cannot assume that the parameters will be different from each other. Print out the result in this format (where XX represents the integer value): Integer: XX
• You may not use any loops, conditional statements, or regex (including switch statements and the tertiary operator).
• You cannot assume that only valid integers or nothing will appear in between the specified characters. If the value is not valid or there is nothing in between the specified characters, print out "Invalid characters" followed by printing out the result of calling the toString method on the exception object.
• Hint: Think about the methods that you can use to convert Strings to integers and whether they throw exceptions! Make sure to handle the most specific exception class (i.e. do NOT use the superclass Exception to catch the exception object).
3. Once you’ve created the method, run the StringParserTest class in the tests package to see if you’ve created the code correctly.
4. To help yourself debug and test your code, create a Java class named StringParserDemo that has the main method. In the main method include at least three examples. Note that for the examples below, the method call is provided, followed by the expected output.
Problem 1: StringParserTest class
package neiu.edu.p2.tests.problem1; // UNCOMMENT THIS IMPORT //import neiu.edu.p2.problem1.StringParser; import java.io.ByteArrayOutputStream; import java.io.PrintStream; public class StringParserTest { public static void main(String[] args) { // TESTS - UNCOMMENT THE TEST METHOD CALLS /*shouldTestIntegerSurroundedByDifferentCharacters(); shouldTestIntegerSurroundedBySameCharacters(); shouldTestNoValuesInBetweenCharacters(); shouldTestForInvalidValuesInBetweenCharacters();*/ } // UNCOMMENT ALL OF THESE TESTS /*private static void shouldTestIntegerSurroundedByDifferentCharacters() { System.out.println("Test Name: " + Thread.currentThread().getStackTrace()[1].getMethodName()); System.out.print("Test Outcome: "); final ByteArrayOutputStream outContent = new ByteArrayOutputStream(); final PrintStream originalOut = System.out; System.setOut(new PrintStream(outContent)); try { StringParser.findInteger("rugtsbckgus!32*", '!', '*'); String out = outContent.toString(); System.setOut(originalOut); System.out.println("Integer: 32\n".equals(out) ? "PASSED" : "FAILED"); System.out.println("Expected:\nInteger: 32"); System.out.println("Actual:\n" + out); } catch (RuntimeException e) { System.out.println("FAILED. ERROR!!"); e.printStackTrace(); } System.out.println(); } private static void shouldTestIntegerSurroundedBySameCharacters() { System.out.println("Test Name: " + Thread.currentThread().getStackTrace()[1].getMethodName()); System.out.print("Test Outcome: "); final ByteArrayOutputStream outContent = new ByteArrayOutputStream(); final PrintStream originalOut = System.out; System.setOut(new PrintStream(outContent)); try { StringParser.findInteger("rusbdi#1038#jjdksu", '#', '#'); String out = outContent.toString(); System.setOut(originalOut); System.out.println("Integer: 1038\n".equals(out) ? "PASSED" : "FAILED"); System.out.println("Expected:\nInteger: 1038"); System.out.println("Actual:\n" + out); } catch (RuntimeException e) { System.out.println("FAILED. ERROR!!"); e.printStackTrace(); } System.out.println(); } private static void shouldTestNoValuesInBetweenCharacters() { System.out.println("Test Name: " + Thread.currentThread().getStackTrace()[1].getMethodName()); System.out.print("Test Outcome: "); final ByteArrayOutputStream outContent = new ByteArrayOutputStream(); final PrintStream originalOut = System.out; System.setOut(new PrintStream(outContent)); try { StringParser.findInteger("rujfbgl&%fkslga", '&', '%'); String out = outContent.toString(); System.setOut(originalOut); String expected = "Invalid integer\n" + "java.lang.NumberFormatException: For input string: \"\"\n"; System.out.println(expected.equals(out) ? "PASSED" : "FAILED"); System.out.println("Expected:\n" + expected); System.out.println("Actual:\n" + out); } catch (RuntimeException e) { System.out.println("FAILED. ERROR!!"); e.printStackTrace(); } System.out.println(); } private static void shouldTestForInvalidValuesInBetweenCharacters() { System.out.println("Test Name: " + Thread.currentThread().getStackTrace()[1].getMethodName()); System.out.print("Test Outcome: "); final ByteArrayOutputStream outContent = new ByteArrayOutputStream(); final PrintStream originalOut = System.out; System.setOut(new PrintStream(outContent)); try { StringParser.findInteger("rusbdi^10.38^jjdksu", '^', '^'); String out = outContent.toString(); System.setOut(originalOut); String expected = "Invalid integer\n" + "java.lang.NumberFormatException: For input string: \"10.38\"\n"; System.out.println(expected.equals(out) ? "PASSED" : "FAILED"); System.out.println("Expected:\n" + expected); System.out.println("Actual:\n" + out); } catch (RuntimeException e) { System.out.println("FAILED. ERROR!!"); e.printStackTrace(); } System.out.println(); }*/ }
please help
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks
// StringParser.java
package neiu.edu.p2.problem1;
public class StringParser {
// required method
public static void findInteger(String text, char first, char second) {
// putting the code inside a try catch block
try {
// finding index of char first in text, assuming first and second
// exist on text
int index1 = text.indexOf(first);
// finding index of char second, starting from index1+1 index
int index2 = text.indexOf(second, index1 + 1);
// parsing integer value between index1+1 and index2-1 as integer
int i = Integer.parseInt(text.substring(index1 + 1, index2));
// if parsing is successful, displaying in format 'Integer: XX\n'
System.out.print("Integer: " + i + "\n");
} catch (NumberFormatException nfe) {
// if any exception occurred, displaying 'Invalid integer\n'
// followed by exception message
System.out.print("Invalid integer\n");
System.out.print(nfe + "\n");
}
}
}
// StringParserTest.java
package neiu.edu.p2.tests.problem1;
import neiu.edu.p2.problem1.StringParser;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
public class StringParserTest {
public static void main(String[] args) {
// TESTS - UNCOMMENT THE TEST METHOD CALLS
shouldTestIntegerSurroundedByDifferentCharacters();
shouldTestIntegerSurroundedBySameCharacters();
shouldTestNoValuesInBetweenCharacters();
shouldTestForInvalidValuesInBetweenCharacters();
}
// UNCOMMENT ALL OF THESE TESTS
private static void shouldTestIntegerSurroundedByDifferentCharacters() {
System.out.println("Test Name: " + Thread.currentThread().getStackTrace()[1].getMethodName());
System.out.print("Test Outcome: ");
final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
final PrintStream originalOut = System.out;
System.setOut(new PrintStream(outContent));
try {
StringParser.findInteger("rugtsbckgus!32*", '!', '*');
String out = outContent.toString();
System.setOut(originalOut);
System.out.println("Integer: 32\n".equals(out) ? "PASSED" : "FAILED");
System.out.println("Expected:\nInteger: 32");
System.out.println("Actual:\n" + out);
} catch (RuntimeException e) {
System.out.println("FAILED. ERROR!!");
e.printStackTrace();
}
System.out.println();
}
private static void shouldTestIntegerSurroundedBySameCharacters() {
System.out.println("Test Name: " + Thread.currentThread().getStackTrace()[1].getMethodName());
System.out.print("Test Outcome: ");
final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
final PrintStream originalOut = System.out;
System.setOut(new PrintStream(outContent));
try {
StringParser.findInteger("rusbdi#1038#jjdksu", '#', '#');
String out = outContent.toString();
System.setOut(originalOut);
System.out.println("Integer: 1038\n".equals(out) ? "PASSED" : "FAILED");
System.out.println("Expected:\nInteger: 1038");
System.out.println("Actual:\n" + out);
} catch (RuntimeException e) {
System.out.println("FAILED. ERROR!!");
e.printStackTrace();
}
System.out.println();
}
private static void shouldTestNoValuesInBetweenCharacters() {
System.out.println("Test Name: " + Thread.currentThread().getStackTrace()[1].getMethodName());
System.out.print("Test Outcome: ");
final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
final PrintStream originalOut = System.out;
System.setOut(new PrintStream(outContent));
try {
StringParser.findInteger("rujfbgl&%fkslga", '&', '%');
String out = outContent.toString();
System.setOut(originalOut);
String expected = "Invalid integer\n" + "java.lang.NumberFormatException: For input string: \"\"\n";
System.out.println(expected.equals(out) ? "PASSED" : "FAILED");
System.out.println("Expected:\n" + expected);
System.out.println("Actual:\n" + out);
} catch (RuntimeException e) {
System.out.println("FAILED. ERROR!!");
e.printStackTrace();
}
System.out.println();
}
private static void shouldTestForInvalidValuesInBetweenCharacters() {
System.out.println("Test Name: " + Thread.currentThread().getStackTrace()[1].getMethodName());
System.out.print("Test Outcome: ");
final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
final PrintStream originalOut = System.out;
System.setOut(new PrintStream(outContent));
try {
StringParser.findInteger("rusbdi^10.38^jjdksu", '^', '^');
String out = outContent.toString();
System.setOut(originalOut);
String expected = "Invalid integer\n" + "java.lang.NumberFormatException: For input string: \"10.38\"\n";
System.out.println(expected.equals(out) ? "PASSED" : "FAILED");
System.out.println("Expected:\n" + expected);
System.out.println("Actual:\n" + out);
} catch (RuntimeException e) {
System.out.println("FAILED. ERROR!!");
e.printStackTrace();
}
System.out.println();
}
}
/*OUTPUT*/
Test Name: shouldTestIntegerSurroundedByDifferentCharacters
Test Outcome: PASSED
Expected:
Integer: 32
Actual:
Integer: 32
Test Name: shouldTestIntegerSurroundedBySameCharacters
Test Outcome: PASSED
Expected:
Integer: 1038
Actual:
Integer: 1038
Test Name: shouldTestNoValuesInBetweenCharacters
Test Outcome: PASSED
Expected:
Invalid integer
java.lang.NumberFormatException: For input string: ""
Actual:
Invalid integer
java.lang.NumberFormatException: For input string: ""
Test Name: shouldTestForInvalidValuesInBetweenCharacters
Test Outcome: PASSED
Expected:
Invalid integer
java.lang.NumberFormatException: For input string: "10.38"
Actual:
Invalid integer
java.lang.NumberFormatException: For input string: "10.38"
Problem 1 1. In the src → edu.neiu.p2 directory, create a package named problem1. 2. Create...
Problem 1 1. In the src → edu.neiu.p2 → problem1 directory, create a Java class called HW4P1 and add the following: • The main method. Leave the main method empty for now. • Create a method named xyzPeriod that takes a String as a parameter and returns a boolean. • Return true if the String parameter contains the sequential characters xyz, and false otherwise. However, a period is never allowed to immediately precede (i.e. come before) the sequential characters xyz....
JAVA PROG HW Problem 1 1. In the src −→ edu.neiu.p2 directory, create a package named problem1. 2. Create a Java class named StringParser with the following: ApublicstaticmethodnamedfindIntegerthattakesaStringandtwocharvariables as parameters (in that order) and does not return anything. The method should find and print the integer value that is located in between the two characters. You can assume that the second char parameter will always follow the firstchar parameter. However, you cannot assume that the parameters will be different from...
Java -Create an interface and implement it In the murach.db package, create an interface named IProductDB. This interface should specify this abstract method: public abstract Product get(String productCode); Modify the ProductDB class so it implements the IProductDB interface. Write the code for the new ‘get’ method. Then remove the getProductByCode method. In the Main class, modify the code so it works with the new ProductDB class. This code should create an instance of the IProductDB interface like this: IProductDB db...
Question 1 (5 points) Question 1 Unsaved What is displayed on the console when running the following program? public class Quiz2B { public static void main(String[] args) { try { System.out.println("Welcome to Java"); int i = 0; int y = 2 / i; System.out.println("Welcome to Java"); } catch (RuntimeException ex) { System.out.println("Welcome to Java"); } finally { System.out.println("End of the block"); } } } Question 1 options: The program displays Welcome to Java two times. The program displays Welcome to...
Lab 3 Step One First, create an empty directory for lab3. There is no starter code for this lab. You will be throwing and catching exceptions in this exercise. Create a file called RuntimeException.h and put the following code in it. #include <string> class RuntimeException { private: string errorMsg; public: RuntimeException(const string& err) { errorMsg = err; } string getMessage() const { return errorMsg; } } Step Two In a new .cpp file in your directory, write a main function...
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 ...
Exercise 1): take the program “InteractiveCounting” (attached). Your task is to change the implementation of both the “ReadInput” and the “Count” classes. Currently these classes extends Thread. You should now use the Runnable interface to implement the thread. The output of the program should not change. I have also attached the class ThreadType2 to give you a working example on how to implement a thread using the Runnable interfacethe program is here package threadExamples; import java.util.Scanner; public class InteractiveCounting {...
Hi, So I have a finished class for the most part aside of the toFile method that takes a file absolute path +file name and writes to that file. I'd like to write everything that is in my run method also the toFile method. (They are the last two methods in the class). When I write to the file this is what I get. Instead of the desired That I get to my counsel. I am having trouble writing my...
Can you help me rearrange my code by incorporating switch I'm not really sure how to do it and it makes the code look cleaner. Can you help me do it but still give the same output? Also kindly add an in-line comment on what changes and rearrangement you've done so I can understand what's going on. import java.util.ArrayList; import java.util.Scanner; public class Bank { /** * Add and read bank information to the user. * @param arg A string,...
2. Write MinheapPriorityQueue constructor, which takes an array of data, and construct the max heap priority queue using bottom-up algorithm. The expected run time should be O(n), where n is the total number of data. BubbleDown method is provided. You may test it in this minHeap public class MinHeapPriorityQueue<E extends Comparable<? super E>>{ private E data[]; private int size; public MinHeapPriorityQueue(){ this(100); } public MinHeapPriorityQueue(int cap){ size = 0; data = (E[]) new Comparable[cap]; } public MinHeapPriorityQueue(int[] a){ } public...