Question
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
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. If .xyz occurs, the method should return false.
You may only use the three methods discussed recently in lecture: length(), charAt(), and indexOf()
2. In the main method, call your method and test it with the below examples. Print out your results to make sure that they match the given return values below.
3. Go to the HW4P1Test class in the tests problem1 directory and uncomment the import at the top, the block of tests in the main method, and the corresponding methods. Run the test to see if you created your code correctly. Make sure all 3 tests pass.
Sample Method Usage | Return Value |
boolean b1 = xyzPeriod("abcxyz") | true |
boolean b2 = xyzPeriod("4abc.xyzygxyzop") | false |
boolean b3 = xyzPeriod("xyz.yadda") | true |
boolean b4 = xyzPeriod("st.uffxyz") | true |
boolean b5 = xyzPeriod("xuyddz") | false |
Problem 2
1. In the src edu.neiu.p2 problem2 directory, create a Java class called HW4P2 and add the following:
The main method. Leave the main method empty for now.
Write a method named productOfDigits that takes a String parameter and does not return anything. You can assume that the parameter is a non-empty String.
The method should print out the product of all the digits in the String. If there are no digits, print out "No digits". You should take into account BigIntegers when creating your code.
You may use at most one loop.
You may only use the three methods discussed recently in lecture: length(), charAt(), and indexOf().
2. In the main method, call your method and test it with the below examples. Print out your results to make sure that they match the given return values below.
3. Go to the HW4P2Test class in the tests problem2 directory and uncomment the import at the top, the block of tests in the main method, and the corresponding methods. Run the test to see if you created your code correctly. Make sure all 3 tests pass.
Sample Method Usage | Output |
productOfDigits("tfg113f8f2okil"); | Product: 48 |
productOfDigits("h4739jf938473jfj983jfjoirui49484949848393d"); | Product: 91725702944299941888 |
productOfDigits("hdhgheielsldhf"); | No digits |
productOfDigits("dj37hg0kr7"); | Product: 0 |
Problem 3
1. In the src edu.neiu.p2 problem3 directory, create a Java class called HW4P3 and add the following:
The main method. Leave the main method empty for now.
Create a method named beforeAfter that takes two String parameters, str1 and str2, and returns aString. You can assume that str2 is a non-empty String.
The beforeAfter method should create a new String formed by concatenating the character found immediately before and immediately after every appearance of str2 in str1. If str2 cannot be found in str1, return null.
You may use at most one loop. You may only use the three String methods discussed recently in lecture: length(), charAt(), and indexOf().
2. In the main method, call your method and test it with the below examples. Print out your results to make sure that they match the given return values below.
3. Go to the HW4P3Test class in the tests problem3 directory and uncomment the import at the top, the block of tests in the main method, and the corresponding methods. Run the test to see if you created your code correctly. Make sure all 4 tests pass.
Sample Method Usage | Return Value |
String s1 = beforeAfter("abcXY123XYijk", "XY"); | c13i |
String s2 = beforeAfter("ABC123ABC", "ABC"); | 13 |
String s3 = beforeAfter("ZY1YZ", "Z"); | YY |
String s4 = beforeAfter("GHsjd38", "44"); | null |
Running Tests:
At various steps of the assignment, you will be told to run tests to check your code. All tests can be found in src test directory. DO NOT MODIFY THE TEST PACKAGE LOCATION OR THE TEST CODE OTHER THAN TO UNCOMMENT.
All test classes have a main method.
Uncomment the method calls in the main method and the methods themselves as directed.
Each method call in the main method is a test.
To run the tests, right-click on the test file and choose Run (followed by the class name).
Check to make sure that all the tests display "PASSED". If not, look at the Expected vs the Actual values or the stack trace (i.e. error) if applicable.
Note: These problems are done through IntelliJ.
For Problem 1: HW4P1Test class
package edu.neiu.p2.tests.problem1; //import edu.neiu.p2.problem1.HW4P1; public class HW4P1Test { public static void main(String[] args) { // TESTS /*shouldReturnFalseIfNoXYZ(); shouldReturnTrueIfNoXYZAndNoPeriod(); shouldReturnFalseIfXYZAndPeriod();*/ } // UNCOMMENT ALL OF THESE TESTS /*private static void shouldReturnFalseIfNoXYZ() { System.out.println("Test Name: " + Thread.currentThread().getStackTrace()[1].getMethodName()); System.out.print("Test Outcome: "); try { boolean b = HW4P1.xyzPeriod("3xdydjz8d"); System.out.println(!b ? "PASSED" : "FAILED"); System.out.println("Expected: false"); System.out.println("Actual: " + b); } catch (RuntimeException e) { System.out.println("FAILED. ERROR!!"); e.printStackTrace(); } System.out.println(); } private static void shouldReturnTrueIfNoXYZAndNoPeriod() { System.out.println("Test Name: " + Thread.currentThread().getStackTrace()[1].getMethodName()); System.out.print("Test Outcome: "); try { boolean b = HW4P1.xyzPeriod("happyxyzhappy.happy"); System.out.println(b ? "PASSED" : "FAILED"); System.out.println("Expected: true"); System.out.println("Actual: " + b); } catch (RuntimeException e) { System.out.println("FAILED. ERROR!!"); e.printStackTrace(); } System.out.println(); } private static void shouldReturnFalseIfXYZAndPeriod() { System.out.println("Test Name: " + Thread.currentThread().getStackTrace()[1].getMethodName()); System.out.print("Test Outcome: "); try { boolean b = HW4P1.xyzPeriod("hxyzbb.xyziikxyzll"); System.out.println(!b ? "PASSED" : "FAILED"); System.out.println("Expected: false"); System.out.println("Actual: " + b); } catch (RuntimeException e) { System.out.println("FAILED. ERROR!!"); e.printStackTrace(); } System.out.println(); }*/ }
------------------------------------------------------------------------------------
For Problem 2: HW4P2Test class
package edu.neiu.p2.tests.problem2; //import edu.neiu.p2.problem2.HW4P2; import java.io.ByteArrayOutputStream; import java.io.PrintStream; public class HW4P2Test { public static void main(String[] args) { // TESTS /*shouldReturnProduct(); shouldReturnProductForLargeValues(); shouldReturnNoDigits();*/ } // UNCOMMENT ALL OF THESE TESTS /*private static void shouldReturnProduct() { 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 { HW4P2.productOfDigits("ghsl111dj11dgjs1"); String out = outContent.toString(); System.setOut(originalOut); System.out.println("Product: 1 ".equals(out) ? "PASSED" : "FAILED"); System.out.println("Expected: " + "Product: 1"); System.out.println("Actual: " + out.trim()); } catch (RuntimeException e) { System.out.println("FAILED. ERROR!!"); System.out.println(e.getMessage()); } System.out.println(); } private static void shouldReturnProductForLargeValues() { 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 { HW4P2.productOfDigits("9494949dhg372747492948dghf28282"); String out = outContent.toString(); System.setOut(originalOut); System.out.println("Product: 36698669445021696 ".equals(out) ? "PASSED" : "FAILED"); System.out.println("Expected: " + "Product: 36698669445021696"); System.out.println("Actual: " + out.trim()); } catch (RuntimeException e) { System.out.println("FAILED. ERROR!!"); System.out.println(e.getMessage()); } System.out.println(); } private static void shouldReturnNoDigits() { 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 { HW4P2.productOfDigits("yaddayaddayadda"); String out = outContent.toString(); System.setOut(originalOut); System.out.println("No digits ".equals(out) ? "PASSED" : "FAILED"); System.out.println("Expected: " + "No digits"); System.out.println("Actual: " + out.trim()); } catch (RuntimeException e) { System.out.println("FAILED. ERROR!!"); System.out.println(e.getMessage()); } System.out.println(); }*/ }
------------------------------------------------------------------------------------
For Problem 3: HW4P3Test class
package edu.neiu.p2.tests.problem3; //import edu.neiu.p2.problem3.HW4P3; public class HW4P3Test { public static void main(String[] args) { // TESTS /*shouldFindBeforeAndAfterStringCharacters(); shouldFindBeforeAfterForBeginningOfString(); shouldFindBeforeAfterForEndOfString(); shouldReturnNullIfNotInString();*/ } // UNCOMMENT ALL OF THESE TESTS /*private static void shouldFindBeforeAndAfterStringCharacters() { System.out.println("Test Name: " + Thread.currentThread().getStackTrace()[1].getMethodName()); System.out.print("Test Outcome: "); try { String s = HW4P3.beforeAfter("tyedeighed8idie3", "ed"); System.out.println(s.equals("yeh8") ? "PASSED" : "FAILED"); System.out.println("Expected: yeh8"); System.out.println("Actual: " + s); } catch (RuntimeException e) { System.out.println("FAILED. ERROR!!"); e.printStackTrace(); } System.out.println(); } private static void shouldFindBeforeAfterForBeginningOfString() { System.out.println("Test Name: " + Thread.currentThread().getStackTrace()[1].getMethodName()); System.out.print("Test Outcome: "); try { String s = HW4P3.beforeAfter("g873dghe8ls", "g87"); System.out.println(s.equals("3") ? "PASSED" : "FAILED"); System.out.println("Expected: 3"); System.out.println("Actual: " + s); } catch (RuntimeException e) { System.out.println("FAILED. ERROR!!"); e.printStackTrace(); } System.out.println(); } private static void shouldFindBeforeAfterForEndOfString() { System.out.println("Test Name: " + Thread.currentThread().getStackTrace()[1].getMethodName()); System.out.print("Test Outcome: "); try { String s = HW4P3.beforeAfter("eyellowjdug0yellow", "yellow"); System.out.println(s.equals("ej0") ? "PASSED" : "FAILED"); System.out.println("Expected: ej0"); System.out.println("Actual: " + s); } catch (RuntimeException e) { System.out.println("FAILED. ERROR!!"); e.printStackTrace(); } System.out.println(); } private static void shouldReturnNullIfNotInString() { System.out.println("Test Name: " + Thread.currentThread().getStackTrace()[1].getMethodName()); System.out.print("Test Outcome: "); try { String s = HW4P3.beforeAfter("8heg8f8ef", "8f86"); System.out.println(s == null ? "PASSED" : "FAILED"); System.out.println("Expected: null"); System.out.println("Actual: " + s); } catch (RuntimeException e) { System.out.println("FAILED. ERROR!!"); e.printStackTrace(); } System.out.println(); }*/ }
------------------------------------------------------------------------------------
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started