Tag Archives: String

String 1 and Array 1 Exercises

Exercises for String 1 and Array 1 (no loops)

When you are done, you can copy and past the score() method below into your class. It will test some (but, by no means all) of the things that your code should do

Create a class called StrArr1 and add each method below into it.

screenshot.1

 

 

For the absVals()  method below use the Math.abs() Java method.

screenshot.3

screenshot.1

 

screenshot.4

For the  randos1to10()   method below use the  Math.random()  Java method. Create a new array to store random integers. Each integer should be [1,10]

screenshot.5

String Loop Assignments [Java]

Write the body for the methods described below.

Tester File here  (This one does not yet test the newest method)

As always, do not import any external libraries. All of the coding can be accomplished by using just  loops, arrays, strings and variables. Using anything else will lead to total loss of credit on associated methods.

Once you have completed each method, copy and paste the code in this file into your class. Then run the test() method and you will get feedback on your code. Note: This does not test all the methods. I have added new methods since writing that tester file. Also, please make sure that:

  • all methods are spelled/capitalized exactly  as shown on this page
  • variables are camelcase and have meaningful names
  • for i  loops use i as a variable
  • for-each  loops do not use i  as the variable

For each method below, you can assume that the parameters are not null

Part I

 

 boolean isAfterN(String str, String chrs, int index)

DescriptionThis method returns true if chrs  appears in  str  after index .

@precondition: index + chrs.length() < str.length() 

screenshot.24

 

int countF(String str)

Description: This method returns the number of times that the the lower case letter ‘f’ occurs in str .

Method Call return value/output
countF(“abcdef”) 1
countF(“abcdfef”) 2
countF(“fff”) 3
countF(“xxx”) 0

boolean hasN_Fs(String str, int n )

Description: This method returns true if str has exactly n occurrences of the letter ‘f’ in it.

hasN_fs

String threeTimes(String str)

Description: This method returns the String str concatenated with itself three times

Method Call return value/output
threeTimes(“foog”) “foogfoogfoog”
threeTimes(“abc”) “abcabcabc”
threeTimes(“zt”) “ztztzt”
threeTimes(“”) “”

String nTimes(String str, int n)

Description: This method returns the String str concatenated with itself n times

Method Call return value/output
nTimes(“fg” , 2 ) “fgfg”
nTimes(“abc” , 0 ) “”
nTimes(“zt” , 3 ) “ztztzt”
nTimes(“uiz” , 4 ) “uizuizuizuiz”

int countMiddleChar(String str)

Precondition: str.length() ≥ 3.

Description: This method returns the number of times the middle letter of str appears in str.

Method Call return value/output
countMiddleChar(“acbcb” ) 2
countMiddleChar(“acbcx” ) 1
countMiddleChar(“bbbbb” ) 5
countMiddleChar(“xytbtzy”) 1

int indexOf(String haystack, String needle)

Description: Write your own indexOf() method.  Just to avoid any confusion – you cannot make use of the String’s built in indexOf() method. Our method returns the index of the 1st occurrence of needle in haystack . (Full credit if you can get this to work with Strings whose length is greater than 1).

indexOfSampleCall

int countChars(String str, String chars)

@precondtion : chars.length() <= str.length()  

Description: This method returns the number of times that chars  occurs in String str .

Method Call return value/output
countChars(“momdadmom” , “dad” ) 1
countChars(“foobofoo” , “foo”) 2
countChars(“foobofoofoo” , “foo”) 3
countChars(“foobofoofoo” , “xy” ) 0

Part II

String[] toArray(String str)

Description: This method returns an array comprised of the individual characters of str 

Method Call return value/output
toArray(“abc”  ) { “a”, “b”, “c”}
toArray(“xyz”  ) {“x”, “y”, “z”}

String[] toArraySansChar(String str, char ch)

DescriptionThis method returns an array of Strings comprised of  single letter strings extracted from the input String str ; however, we will always skip the char ch :

 

toArraySansChar

 

String[] reversedBy2s(String str)

DescriptionThis method returns an array of Strings comprised of pairs of characters from the input, — in reverse order as shown below:

reversed_by2s

 

String reverseBy3s(String str)

DescriptionThis is like the prior method, except you are going by 3’s and returning a String , not an array of Strings.

reverseby-3s

 

 

boolean isPalindrome(String str)

Description: This method returns true  if  str  is a palindrome

Method Call return value/output
isPalindrome(“abc”  ) false
isPalindrome(“aba”  ) true
 isPalindrome(“abba”  )  true

 

 

Java String Project Bases and Decimals

Create a class called BaseMaster (spelled exactly like this)

 

//helper functions

the isNumeric() method returns true if every digit is an integer . It also allows for a single ‘.’ to represent a decimal point.

 

 

example call return value
isNumeric("3.1") true
isNumeric("3.1.1") false
isNumeric("3a") true (assume base 16)
 isNumeric("3D")  true (assume base 16)

To get A+, you need to make base10toOtherBase() support bases 2-10 AND base 16

Java String Loops 2

public int countF(String str)

Description: This method returns the number of times that the letter ‘f’ occurs in str.

Method Call return value/output
countF(“abcdef”) 1
countF(“abcdfef”) 2
countF(“fff”) 3
countF(“xxx”) 0

 

public boolean has2Fs(String str)

Description: This method returns true if str has exactly two occurrences of the letter ‘f’ in it.

Method Call return value/output
has2Fs(“foo”) false
has2Fs(“fofo”) true
has2Fs(“foffo”) false

 

public boolean has2Fs3Gs(String str)

Description: This method returns true if str has exactly two occurrences of the letter ‘f’ in it and 3 occurences ‘g’.

Method Call return value/output
has2Fs3Gs(“foog”) false
has2Fs3Gs(“foggfog”) true
has2Fs3Gs(“foffoggg”) false

public int momOrDad(String str)

Description: This method returns the number of times that the String “mom” or the string “dad” occurs in the parameter str .

Method Call return value/output
momOrDad(“foog”) 0
momOrDad(“momf”) 1
momOrDad(“momdadmo”) 2
momOrDad(“momdadmom”) 3

 

public String threeTimes(String str)

Description: This method returns the String str concatenated with itself three times

Method Call return value/output
threeTimes(“foog”) “foogfoogfoog”
threeTimes(“abc”) “abcabcabc”
threeTimes(“zt”) “ztztzt”
threeTimes(“”) “”

 

public String nTimes(String str, int n)

Description: This method returns the String str concatenated with itself n times

Method Call return value/output
nTimes(“fg” , 2 ) “fgfg”
nTimes(“abc” , 0 ) “”
nTimes(“zt” , 3 ) “ztztzt”
nTimes(“uiz” , 4 ) “uizuizuizuiz”

 

 

public String countMiddleChar(String str)

Precondition: str.length() ≥ 3.

Description: This method returns the number of times the middle letter of str appears in str.

Method Call return value/output
countMiddleChar(“acbcb” ) 2
countMiddleChar(“acbcx” ) 1
countMiddleChar(“bbbbb” ) 5
countMiddleChar(“xytbtzy”) 1

 

 

public int indexOf(String haystack, String needle)

Description: Write your own indexOf() method. Obviously, you cannot make use of the String’s build in indexOf() method. This method returns the index of the first occurrence of needle in haystack . (Full credit if you can get this to work with Strings whose length is greater than 1).

Method Call return value/output
indexOf(“acxb”, “x” ) 2
indexOf(“zazt”, “z” ) 0
indexOf(“tayv”, “g” ) -1
indexOf(“hijklj”, “j” ) 2

If you want the extra credit, then it must also be able to complete the following example calls

Method Call return value/output
indexOf(“acxb”, “cx” ) 1
indexOf(“acxb”, “cxb” ) 1
indexOf(“acxb”, “cxbt” ) -1

 

public int countChars(String str, String chars)

Precondition:The length of chars is less than or equal to the length of str

Description: This method returns the number of times that charsoccurs in String str.

Method Call return value/output
countChars(“momdadmom” , “dad” ) 1
countChars(“foobofoo” , “foo”) 2
countChars(“foobofoofoo” , “foo”) 3
countChars(“foobofoofoo” , “xy” ) 0