In: Computer Science
IDS 201 Lab Exercise 5
Within a class named LX5, write methods as follows:
1. a method named valueCheck that accepts a parameter int named value and, using a switch statement, displays text output as follows:
2. a method named textMatch that accepts two parameter String objects named string1 and string2 and returns true if their text is identical and false otherwise
3. a method named sameObject that accepts two Objects as parameters and returns true if they are the same object and false otherwise (note that the Object class does not require any import statement)
4. a method named factorCheck that accepts a parameter int named value and returns values as follows:
5. Write an equivalent version of String's indexOf() method. The method will accept as parameters two Strings, big and small. If the same text of small is contained in big, the method will return the index of the first character of small from its first appearance in big.
6. Write an extended version of String's indexOf() method. The method will accept as parameters two Strings, big and small, and two ints, start and end. Like 3) above, this method will search big for matching text with the String small and return the index of the first character of small from its first appearance in big. However, in this case the search will cover not the entire String big but the match must begin at or after index start and end at or before index end.
Lab Exercise 5:
Java code:
// Lab Exercise 5 public class LX5 { // Part 1 public void valueCheck(int value) { switch (value) { case 0: System.out.println("zero"); break; case 1: System.out.println("one"); break; case 2: System.out.println("two"); break; case 3: System.out.println("three"); break; default: if (value >= 4) System.out.println("LOTS"); else System.out.println("NEGATIVE"); } } // Part 2 public boolean textMatch(String string1, String string2) { if (string1.equalsIgnoreCase(string2)) return true; else return false; } // Part 3 public boolean sameObject(Object object1, Object object2) { if (object1.getClass().equals(object2.getClass())) return true; else return false; } // Part 4 public int factorCheck(int value) { if (value > 49 || value < 1) return -1; if (value == 1) return 0; if (value % 7 == 0) return 7; if (value % 5 == 0) return 5; if (value % 3 == 0) return 3; if (value % 2 == 0) return 2; return 1; } // Part 5 public int indexOf(String big, String small) { int index = -1; for (int i = 0; i <= big.length() - small.length(); i++) { boolean contains = true; for (int j = 0; j < small.length(); j++) { if (big.charAt(i + j) != small.charAt(j)) { contains = false; break; } } if (contains) { index = i; break; } } return index; } // Part 6 public int indexOfExtended(String big, String small, int start, int end) { int index = -1; for (int i = start; i <= end - small.length(); i++) { boolean contains = true; for (int j = 0; j < small.length(); j++) { if (big.charAt(i + j) != small.charAt(j)) { contains = false; break; } } if (contains) { index = i; break; } } return index; } }
Kindly rate the answer and for any help just drop a comment