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 2 1. In the src → edu.neiu.p2 directory, create a package named problem1. 2. Create a Java class named Complement with the following: • A public static method named onesComplement that takes a String as a parameter and returns a String that is the 1’s complement of the parameter. • If the String is not a valid binary number (i.e. only has 0s and 1s), you should throw an IllegalArgumentException with the message "Not a valid binary number". •...
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...
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...
please help. About java tcp. this is what i have so far. how to convert input value into ASCII sequence For example the message: “Hello World” would become “Ifmmp!Xpsme”For example the message: “Hello World” would become “Ifmmp!Xpsme”in server.java. Client.java: import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintStream; import java.net.Socket; import java.net.SocketTimeoutException; public class Client { public static void main(String[] args) throws IOException { Socket client = new Socket("127.0.0.1", 20006); client.setSoTimeout(10000); BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); PrintStream out...
Running a program in Java about slidsender and slidreceiver with a brief explanation what each one is doing. slidreceiver import java.net.*; import java.io.*; class slidreceiver { public static void main(String a[])throws Exception { Socket s=new Socket(InetAddress.getLocalHost(),10); DataInputStream in=new DataInputStream(s.getInputStream()); PrintStream p=new PrintStream(s.getOutputStream()); int i=0,rptr=-1,nf,rws=8; String rbuf[]=new String[8]; String ch; System.out.println(); do { nf=Integer.parseInt(in.readLine()); if(nf<=rws-1) { for(i=1;i<=nf;i++) { rptr=++rptr%8; rbuf[rptr]=in.readLine(); System.out.println("The received Frame " +rptr+" is : "+rbuf[rptr]); } rws-=nf; System.out.println("\nAcknowledgment sent\n"); p.println(rptr+1); rws+=nf; } else break; ch=in.readLine(); } while(ch.equals("yes")); }...
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 ...