Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Problem 1 1. In the src edu.neiu.p2 problem1 directory, create a Java class called HW5P1 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 HW5P1 and add the following:

The main method. Leave the main method empty for now.

Create a method named anagram that takes two Strings as parameters and returns a boolean.

The method should return true if the two String parameters are anagrams of each other. Anagrams are words that have the exact same characters but in a different order (see examples below). For example, cat and tac are anagrams, but catt and taac are not anagrams.

You may use at most one loop in your code.

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 HW5P1Test 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.

image text in transcribed

Problem 2

1. In the src edu.neiu.p2 problem2 directory, create a Java class called HW5P2 and add the following:

The main method. Leave the main method empty for now.

Write a method named camelCase that takes a String parameter and returns a String array. You can assume that the parameter is a non-empty String.

The method should return an array containing each of the words beginning with a capital letter. You can assume that each word in the String begins with a capital letter.

You may use at most one loop and no conditional blocks (if/if-else/switch) in your code.

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 HW5P2Test 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 2 tests pass.

image text in transcribed

Problem 3

1. In the src edu.neiu.p2 problem3 directory, create a Java class called HW5P3 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 returns a BigInteger.

The method should return the product of all the digits in the String. If there are no digits, return null.

You may use at most one loop and no conditional blocks inside the loop.

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 HW5P3Test 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.

image text in transcribed

Problem 4

1. In the src edu.neiu.p2 problem4 directory, create a Java class called HW5P4 and add the following:

The main method. Leave the main method empty for now.

Write a method named reverseVowels that takes a String parameter and returns a String.

The method should return a new String with only the vowels reversed from the original String parameter. All the other letters stay in their given places. For example, "extraneous" would turn into "uxtroneaes". As a reminder, vowels are the characters a, e, i, o, u.

You may not use any nested loops and no conditional operators (and, or, etc).

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 HW5P4Test class in the tests problem4 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.

image text in transcribed

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: HW5P1Test class

package edu.neiu.p2.tests.problem1; // UNCOMMENT THIS IMPORT! //import edu.neiu.p2.problem1.HW5P1; public class HW5P1Test { public static void main(String[] args) { // TESTS /*shouldReturnFalseIfStringsDifferentLength(); shouldReturnFalseIfSameLengthDiffLetters(); shouldReturnTrueIfAnagram();*/ } // UNCOMMENT ALL OF THESE TESTS /*private static void shouldReturnFalseIfStringsDifferentLength() { System.out.println("Test Name: " + Thread.currentThread().getStackTrace()[1].getMethodName()); System.out.print("Test Outcome: "); try { boolean b = HW5P1.anagram("hello", "hellooo"); 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 shouldReturnFalseIfSameLengthDiffLetters() { System.out.println("Test Name: " + Thread.currentThread().getStackTrace()[1].getMethodName()); System.out.print("Test Outcome: "); try { boolean b = HW5P1.anagram("ahahaha", "hahahah"); 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 shouldReturnTrueIfAnagram() { System.out.println("Test Name: " + Thread.currentThread().getStackTrace()[1].getMethodName()); System.out.print("Test Outcome: "); try { boolean b = HW5P1.anagram("stressed", "desserts"); 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(); }*/ } 

------------------------------------------------------------------------------------

For problem 2: HW5P2Test class

package edu.neiu.p2.tests.problem2; // UNCOMMENT THIS IMPORT! //import edu.neiu.p2.problem2.HW5P2; import java.util.Arrays; public class HW5P2Test { public static void main(String[] args) { // TESTS /*shouldReturnStringArrayWithAllWords(); shouldReturnArrayOfLengthOneForOneWord();*/ } // UNCOMMENT ALL OF THESE TESTS /*private static void shouldReturnStringArrayWithAllWords() { System.out.println("Test Name: " + Thread.currentThread().getStackTrace()[1].getMethodName()); System.out.print("Test Outcome: "); try { String[] w = HW5P2.camelCase("ILoveCodingSoVeryMuch"); String[] wAns = {"I", "Love", "Coding", "So", "Very", "Much"}; System.out.println(Arrays.equals(w, wAns) ? "PASSED" : "FAILED"); System.out.println("Expected: " + Arrays.toString(wAns)); System.out.println("Actual: " + Arrays.toString(w)); } catch (RuntimeException e) { System.out.println("FAILED. ERROR!!"); e.printStackTrace(); } System.out.println(); } private static void shouldReturnArrayOfLengthOneForOneWord() { System.out.println("Test Name: " + Thread.currentThread().getStackTrace()[1].getMethodName()); System.out.print("Test Outcome: "); try { String[] w = HW5P2.camelCase("Fantastical!"); String[] wAns = {"Fantastical!"}; System.out.println(Arrays.equals(w, wAns) ? "PASSED" : "FAILED"); System.out.println("Expected: " + Arrays.toString(wAns)); System.out.println("Actual: " + Arrays.toString(w)); } catch (RuntimeException e) { System.out.println("FAILED. ERROR!!"); e.printStackTrace(); } System.out.println(); }*/ } 

------------------------------------------------------------------------------------

For problem 3: HW5P3Test class

package edu.neiu.p2.tests.problem3; // UNCOMMENT THIS IMPORT! //import edu.neiu.p2.problem3.HW5P3; import java.math.BigInteger; public class HW5P3Test { public static void main(String[] args) { // TESTS /*shouldReturnLargeValue(); shouldReturnOneForOnlyOnes(); shouldReturnNullForNoDigits(); shouldReturnZeroWithAZeroDigit();*/ } // UNCOMMENT ALL OF THESE TESTS /*private static void shouldReturnLargeValue() { System.out.println("Test Name: " + Thread.currentThread().getStackTrace()[1].getMethodName()); System.out.print("Test Outcome: "); try { BigInteger b = HW5P3.productOfDigits("e9e9e9999347&3o2**392838498fhhfgd99"); BigInteger bAns = new BigInteger("64782557359865856"); System.out.println(b.equals(bAns) ? "PASSED" : "FAILED"); System.out.println("Expected: " + bAns); System.out.println("Actual: " + b); } catch (RuntimeException e) { System.out.println("FAILED. ERROR!!"); e.printStackTrace(); } System.out.println(); } private static void shouldReturnOneForOnlyOnes() { System.out.println("Test Name: " + Thread.currentThread().getStackTrace()[1].getMethodName()); System.out.print("Test Outcome: "); try { BigInteger b = HW5P3.productOfDigits("nhy%11%kol1"); BigInteger bAns = BigInteger.ONE; System.out.println(b.equals(bAns) ? "PASSED" : "FAILED"); System.out.println("Expected: " + bAns); System.out.println("Actual: " + b); } catch (RuntimeException e) { System.out.println("FAILED. ERROR!!"); e.printStackTrace(); } System.out.println(); } private static void shouldReturnNullForNoDigits() { System.out.println("Test Name: " + Thread.currentThread().getStackTrace()[1].getMethodName()); System.out.print("Test Outcome: "); try { BigInteger b = HW5P3.productOfDigits("jfhugldlshdhg"); BigInteger bAns = null; System.out.println(b == bAns? "PASSED" : "FAILED"); System.out.println("Expected: " + bAns); System.out.println("Actual: " + b); } catch (RuntimeException e) { System.out.println("FAILED. ERROR!!"); e.printStackTrace(); } System.out.println(); } private static void shouldReturnZeroWithAZeroDigit() { System.out.println("Test Name: " + Thread.currentThread().getStackTrace()[1].getMethodName()); System.out.print("Test Outcome: "); try { BigInteger b = HW5P3.productOfDigits("hfn63jj374ksjd0j8"); BigInteger bAns = BigInteger.ZERO; System.out.println(b.equals(bAns) ? "PASSED" : "FAILED"); System.out.println("Expected: " + bAns); System.out.println("Actual: " + b); } catch (RuntimeException e) { System.out.println("FAILED. ERROR!!"); e.printStackTrace(); } System.out.println(); }*/ } 

------------------------------------------------------------------------------------

For problem 4: HW5P4Test class

package edu.neiu.p2.tests.problem4; // UNCOMMENT THIS IMPORT! //import edu.neiu.p2.problem4.HW5P4; public class HW5P4Test { public static void main(String[] args) { // TESTS /*shouldReverseVowelsSimplestCase(); shouldStaySameWhenVowelsSame(); shouldHandleCapitalVowels();*/ } // UNCOMMENT ALL OF THESE TESTS /*private static void shouldReverseVowelsSimplestCase() { System.out.println("Test Name: " + Thread.currentThread().getStackTrace()[1].getMethodName()); System.out.print("Test Outcome: "); try { String s = HW5P4.reverseVowels("epically crazy"); System.out.println(s.equals("apacilly crezy") ? "PASSED" : "FAILED"); System.out.println("Expected: " + "apacilly crezy"); System.out.println("Actual: " + s); } catch (RuntimeException e) { System.out.println("FAILED. ERROR!!"); e.printStackTrace(); } System.out.println(); } private static void shouldStaySameWhenVowelsSame() { System.out.println("Test Name: " + Thread.currentThread().getStackTrace()[1].getMethodName()); System.out.print("Test Outcome: "); try { String s = HW5P4.reverseVowels("grammar"); System.out.println(s.equals("grammar") ? "PASSED" : "FAILED"); System.out.println("Expected: " + "grammar"); System.out.println("Actual: " + s); } catch (RuntimeException e) { System.out.println("FAILED. ERROR!!"); e.printStackTrace(); } System.out.println(); } private static void shouldHandleCapitalVowels() { System.out.println("Test Name: " + Thread.currentThread().getStackTrace()[1].getMethodName()); System.out.print("Test Outcome: "); try { String s = HW5P4.reverseVowels("O hAPPy surprIse"); System.out.println(s.equals("e hIPPy surprAsO") ? "PASSED" : "FAILED"); System.out.println("Expected: " + "e hIPPy surprAsO"); System.out.println("Actual: " + s); } catch (RuntimeException e) { System.out.println("FAILED. ERROR!!"); e.printStackTrace(); } System.out.println(); }*/ } 
Sample Method Usage Return Value boolean b1 = anagram("banana", "bananab"); false boolean b2 = anagram("happy", "phhay"); false boolean b3 = anagram("a gentleman", "elegant man"); true Sample Method Usage Return Value String[] wl = {"Save", "Changes", camelCase ("SaveChangesInTheEditor"); "In", "The", "Editor" } String[] w2 = {"This", "IS", "A", "Long" camel Case ("ThisIsALong SentenceNoHardCoding"); "Sentence", "No", "Hard", "Coding" } String[] w3 = camelCase("Short"); {"Short"} Sample Method Usage Return Value 48 BigInteger b1 = productofDigits("tfg113f8f2okil"); BigInteger b2 = productOfDigits("h4739jf93847**3jfj983jfjoir ui 49484949&848393d"); 91725702944299941888 BigInteger b3 = productOfDigits("lei#111* ()11ylyly1"); BigInteger b4 = productOfDigits ("hdhgheielsldhf"); null BigInteger b5 = productOfDigits("dj37hgOkr7"); Sample Method Usage Return Value String sl = reverseVowels("extraneous"); uxtroneaes String s2 = reverseVowels("banana"); banana String s3 = reverseVowels("cApital LettErs") null

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Was ignoring the problem an option? Why?

Answered: 1 week ago