Question: Can someone help me with the case changer code these are the steps I am trying to follow Creating a factory class Create a new
Can someone help me with the case changer code these are the steps I am trying to follow
Creating a factory class
- Create a new class called CaseChangerFactory.
- Give this class a single public static method called createCaseChanger which takes for its parameter a CaseType value (you will need to add an import statement for the CaseType enum).
- Following the pattern of the last slide in the lecture, make this method create and return an instance of the CaseChanger class that matches the given CaseType value.
- Back in the main method of CaseUtils, add the following code to the main method. This code replicates the demonstration that is already there, but this time uses the static createCaseChanger method of CaseChangerFactory to obtain an object capable of performing the required operation (see highlighted lines).
for (CaseType t : CaseType.values()) {
System.out.println("=" + t + "=");
CaseChanger changer = CaseChangerFactory.createCaseChanger(t);
for (String s : strings) {
System.out.println(changer.changeCase(s));
}
System.out.println();
}
So far I have achieved this
public class CaseUtilities { public enum CaseType { UPPER, LOWER, TITLE, SENTENCE }; public static String changeCase(String word, CaseType type) { switch (type) { case LOWER: return word.toLowerCase(); case UPPER: return word.toUpperCase(); case TITLE: return word.substring(0, 1).toUpperCase() + word.substring(1).toLowerCase(); case SENTENCE: return word.toSentenceCase(); default: throw new RuntimeException("Unknown case type: " + type); } } public static void main( String[] args){ String[] strings = new String[]{ "HELLO", "Goodbye", "the killer pigeon", " OOPS!"}; for (CaseType t : CaseType.values()) { System.out.println("=" + t + "="); CaseChanger changer = CaseChangerFactory.createCaseChanger(t); for (String s : strings) { System.out.println(changer.changeCase(s)); } System.out.println(); } } } The lower case changer works, and so does the upper case changer but I can't seem to get the sentence case changer to work, below are the methods for the lower case and upper case that I am using
public class LowerCaseChanger implements CaseChanger { @Override public String changeCase(String target) { return target.toLowerCase(); } } public class UpperCaseChanger implements CaseChanger { @Override public String changeCase(String target) { return target.toUpperCase(); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
