This homework has two parts. Each will be implemented in a separate file as a separate class.
First, download this hw1_templates zip file and extract the contents into a folder. The zip file contains three files (StringMethods.java, Combinable.java, and Combine.java) that describe a sketch of the classes we wish you to complete. You will use these to guide you in filling out the two implementing classes.
Learning about String methods
The primary objective of this homework is to learn about the functionality and usage of different string methods in java.
StringMethods.java is the only file that needs to be completed for part 1 of this homework. This is a full Class where you only need to fill out the methods.
This class contains the following six methods, and in order to get the full grade, your code should pass all the assertions successfully.
- public String findChar (String text, int ind)
This method takes the String and index of the target character as the arguments.
You should use the appropriate string method to return the character at the location ind of text.
Note that in Java we use indices that start from "0" (zero-indexed). Whenever we say "the first position" we are referring to index "0". The second position is index "1", and so on. If the requested character does not exist (index out of bounds), please return "none";
For example: The second character (at index 1) of the word "CS1111" is "S". - public boolean userChecker (String newUser, String oldUser)
This method takes two strings as the arguments, the first one is the newUser and the second one is the existing username. You need to check and verify the requirements of the usernames. Some of the requirements are listed below.
*The username should contain at least a number between 1 to 4
*The username should not start with a number
*The username should not have any of the following punctuations (!,@,?)
*The username should not be same as the oldUsername
*The oldUser cannot be null or the empty string ("")If any of the requirements are not satisfied the method needs to return false.
- public boolean urlChecker (String url)
This method takes the url argument and verifies the scheme of the url. For this homework we only expect you to check “.com”,”.edu”,”.net”. domains
- public String lengString (String text)
This method takes a string argument and returns the length of that as a string.
- public String emailGen (String fullname)
This method takes a fullname exp. Magic Johnson and generates the domain in the following format "info@first character of the first name+Lastname.com
- public String domainGen (String fullname)
This method takes the fullname and generated the domain in following scheme www.firstname-lastname.com
*Make sure to lowercase the domain
Simplified 2048 Left Shift
Second, you'll complete a class that emulates some of the logic in 2048. You will complete the Combine class (in the Combine.java file) that implements the Combinable interface with a number of methods specified below. First, have a look at and play a few games of 2048 to get an idea of what we're eventually going to be shooting for. 2048 can be a little like a drug addiction, so be sure to pry yourself away, and back to this homework. We aren't at the level yet where all of it can be implemented, but you'll implement the shuffling left of blocks with the collapsing of two blocks of like values together into a single block of double the value.
We will represent a single row of the 2048 square that includes only three blocks with a 3-length string. Each character in the string is either a '4', a '2', or a '_' which designates a "blank" space. Example rows are "42_", "4_4", and "242". Your program will execute a "shift blocks left" operation which will include combining like blocks into one of double the value. Note that newly created (double valued) blocks are not combined with other blocks. Examples of proper shift left operations include:
-
shifted ⇐ original
- "4__" ⇐ "__4"
- "8__" ⇐ "_44"
- "42_" ⇐ "222"
- "4__" ⇐ "2_2"
- "242" ⇐ "242"
- "44_" ⇐ "422"
Note that for this homework, you cannot use loops. We haven't learned them yet, and you should be able to complete the assignment with only concepts we've learned so far.
Error Checking: Your code must handle quite a bit of error checking. The strings being passed in must:
- include only the characters '4', '2' and '_',
- have exactly a length of three.
If any input string is not formatted according to this, you must return the string, unmodified.
You will implement four methods:
- public boolean validateChar(char ch)
This method takes a character, and validates that it is one of the allowed characters in the input string. It returns true if it is, and false otherwise.
- public boolean validateRow(String row)
This method takes a string as an argument, and checks that it adheres to the requirements for the input string above in "Error Checking". Return true if it the string is valid, and false otherwise. It must invoke the validateChar method for each character.
- public String moveLeft(String row)
If the row is not valid (you must invoke the validateRow method to determine this), return the string unmodified. Otherwise, return a new string with all of the non-blank spaces (i.e. '2's and '4's) shifted all the way to the left. This method does not combine blocks.
- public String combineLeft(String row)
This method first calls validateRow to determine that the input string is formatted correctly. If it isn't, then it returns the input unchanged. Otherwise, it calls moveLeft to move all blocks to the far left (this simplifies the problem). Then it must combine any blocks of comparable value starting from the left of the string. When two blocks are combined into a block of twice the value, any other blocks will need to be shifted left. Return the new string with blocks shifted left, and combined appropriately.
Example Assertions:
assert validateChar('2') : "2 should be a valid char"; assert validateChar('4') : "4 should be a valid char"; assert validateChar('_') : "_ should be a valid char"; assert !validateChar(' ') : "space should not be a valid char"; assert !validateChar('3') : "3 should not be a valid char"; assert !validateRow("2222") : "2222 should not be valid"; assert !validateRow("__") : "__ should not be valid"; assert !validateRow("aaa") : "aaa should not be valid"; assert !validateRow("333") : "333 should not be valid"; assert validateRow("22_") : "22_ should be valid"; assert validateRow("_2_") : "_2_ should be valid"; assert validateRow("242") : "242 should be valid"; assert "4__".equals(moveLeft("__4")) : "__4 doesn't change to 4__"; assert "4__".equals(moveLeft("_4_")) : "_4_ doesn't change to 4__"; assert "42_".equals(moveLeft("4_2")) : "4_2 doesn't change to 42_"; assert "4__".equals(combineLeft("__4")) : "__4 doesn't change to 4__"; assert "8__".equals(combineLeft("_44")) : "_44 doesn't change to 8__"; assert "4__".equals(combineLeft("2_2")) : "2_2 doesn't change to 4__"; assert "4__".equals(combineLeft("22_")) : "22_ doesn't change to 4__"; assert "42_".equals(combineLeft("222")) : "222 doesn't change to 42_"; assert "44_".equals(combineLeft("422")) : "422 doesn't change to 44_"; assert "242".equals(combineLeft("242")) : "242 doesn't change to 242"; assert "8__".equals(combineLeft(combineLeft("422"))) : "Double invocation doesn't work!"; // You should be using your validate methods to check for erroneous inputs! assert "_222".equals(combineLeft("_222")) : "2222 should be invalid!"; assert "333".equals(combineLeft("333")) : "333 should be invalid!"; assert "__".equals(combineLeft("__")) : "__ should be invalid!";
What to Submit
Please submit, via Blackboard, a zip file of a folder with a name following the format: <FirstnameLastname_Lab> (with Lab as Mon or Fri), with two files inside: the completed StringMethods.java, and the completed Combine.java (no need to submit the interface OR the generated class files).
Notes and grading: Your implementations must compile to get any credit. Please develop these in small chunks. Don't program everything, and hope it will work. Write each method separately, and test it separately. Your implementation will get credit proportional to how many assertions they pass. When we test your classes, your main method will not be invoked, so do not assume that it executes. For each of the two classes: make sure to submit your .java file, and that it has the correct class name.
If you simply implement your methods in such a way that they are customized to the provided specific test cases (i.e. by returning the desired string, but not having any of the logic required by the assignment), you'll receive no credit.