From a1f8ff96089fcc544e2f06f89fb83bfd82566b48 Mon Sep 17 00:00:00 2001 From: Raphael Roberts Date: Fri, 19 Oct 2018 22:07:05 -0500 Subject: [PATCH] third one done boom! --- MiniString.java | 68 +++++++++++++++++++++++++++++++++++++++++ TestStringPractice.java | 34 ++++++++++----------- 2 files changed, 85 insertions(+), 17 deletions(-) create mode 100644 MiniString.java diff --git a/MiniString.java b/MiniString.java new file mode 100644 index 0000000..d65851e --- /dev/null +++ b/MiniString.java @@ -0,0 +1,68 @@ +public class MiniString { + private String str; + public MiniString(String str) { + this.str = str; + } + public String makeSubstring(int beginIndex) { + int length = this.str.length(); + String ret = ""; + for (int i = beginIndex; i < length; i++){ + ret += this.str.charAt(i); + } + return ret; + } + public String makeSubstring(int beginIndex, int endIndex) { + int length = this.str.length(); + String ret = ""; + for (int i = beginIndex; i < endIndex; i++){ + ret += this.str.charAt(i); + } + return ret; + } + public int findIndexOf(char ch) { + int ret = -1; + for (int i = 0; i < this.str.length(); i ++ ){ + if (this.str.charAt(i) == ch) { + ret = i; + break; + } + } + return ret; + } + public int findIndexOf(char ch, int fromIndex) { + int ret = -1; + for (int i = fromIndex; i < this.str.length(); i ++ ){ + if (this.str.charAt(i) == ch) { + ret = i; + break; + } + } + return ret; + } + public int findIndexOf(String str) { + int ret = -1; + int sub_length = str.length(); + String cur_str; + for (int i = 0; i < this.str.length() - sub_length; i++){ + cur_str = this.makeSubstring(i,i + sub_length); + if (cur_str.equals(str)) { + ret = i; + break; + } + } + return ret; + } + public int findIndexOf(String str, int fromIndex) { + int ret = -1; + int sub_length = str.length(); + String cur_str; + for (int i = fromIndex; i < this.str.length() - sub_length; i++){ + cur_str = this.makeSubstring(i,i + sub_length); + if (cur_str.equals(str)) { + ret = i; + break; + } + } + return ret; + } +} \ No newline at end of file diff --git a/TestStringPractice.java b/TestStringPractice.java index a15f134..4284a9f 100644 --- a/TestStringPractice.java +++ b/TestStringPractice.java @@ -1,59 +1,59 @@ -/* - * NEIU CS207-2 Fall 2018 - * Homework 4 TestStringPractice.java - * This is the class with a main(), to test StringPractice - */ + + // * NEIU CS207-2 Fall 2018 + // * Homework 4 TestMiniString.java + // * This is the class with a main(), to test MiniString + public class TestStringPractice { public static void main(String[] args) { - StringPractice s1 = new StringPractice("Hello world, welcome to the Java world."); + MiniString s1 = new MiniString("Hello world, welcome to the Java world."); + - /* System.out.println(s1.makeSubstring(0)); System.out.println(s1.makeSubstring(6)); System.out.println(s1.makeSubstring(6, 14)); System.out.println(s1.makeSubstring(13)); System.out.println(s1.makeSubstring(0, 12)); System.out.println(); - */ + - /* + System.out.println(s1.findIndexOf('w')); System.out.println(s1.findIndexOf('m')); System.out.println(s1.findIndexOf('x')); System.out.println(s1.findIndexOf('J')); System.out.println(s1.findIndexOf('a')); System.out.println(); - */ + - /* + System.out.println(s1.findIndexOf('w', 7)); System.out.println(s1.findIndexOf('m', 20)); System.out.println(s1.findIndexOf('x', 10)); System.out.println(s1.findIndexOf('J', 30)); System.out.println(s1.findIndexOf('a', 30)); System.out.println(); - */ + - /* + System.out.println(s1.findIndexOf("wo")); System.out.println(s1.findIndexOf("we")); System.out.println(s1.findIndexOf("He")); System.out.println(s1.findIndexOf("Ja")); System.out.println(s1.findIndexOf("va")); System.out.println(); - */ + - /* + System.out.println(s1.findIndexOf("wo", 7)); System.out.println(s1.findIndexOf("we", 20)); System.out.println(s1.findIndexOf("He", 10)); System.out.println(s1.findIndexOf("Ja", 30)); System.out.println(s1.findIndexOf("va", 25)); System.out.println(); - */ + } // End main() -} // End class TestStringPractice \ No newline at end of file +} // End class TestMiniString \ No newline at end of file