Category Archives: Computer Science
Array Fun 2 [Java]
Description: This method creates an array that is filled with all of the digits of
nums
.
Method Call | return value/output |
digitsToArray( 523 ) | {5,2,3} |
digitsToArray(1267 ) | { 1 , 2 , 6 , 7} |
Description: This method returns true if each element of
nums
is a factor of the sum ofnums
.
Method Call | return value/output |
allFactorsOfSum( {6,1,2,3} ) | true (because all of the elements are factors of the sum which is12) |
allFactorsOfSum( {1,4,7 }) | false (because 7 is not a factor of the sum of this array which is 12) |
Description: This method returns a new version of
strs
with each element appearing twice.
Method Call | return value/output |
doubleArr( {“a”,”b”,”c”} ) | {“a”,”a”,”b”,”b”, “c” , “c”} |
doubleArr( {“math”,”ware”,”house”,”.com” }) | {“math”,”math”,”ware”,”ware”,”house”,”house”,”.com”, “.com”} |
Description: This method returns true if
num
is an elementnums
.
Method Call | return value/output |
isThere( {6,1,2,3}, 6) | true |
isThere( {6,1,2,3}, 5 ) | false |
Description: This returns the index of the first occurrence element ‘5’ or -1 if 5 does not exist.
Method Call | return value/output |
indexOf5( { 2 , 3 , 5 , 4 } ) | 2 |
indexOf5( { 2 , 3 , 5 , 4, 5 } ) | 2 |
indexOf5( { 2 , 3 ,7 , 4, 3, } ) | -1 |
Description: This method returns the index value of the first appearance of
num
or -1 ifnum
is not an element ofnums
.
Method Call | return value/output |
indexOf( {6,4 ,7,3, 4 }, 4) | 1 |
indexOf( {6,4 7 ,3,2,7}, 7) | 2 |
indexOf( {6,4 ,2,3}, 22) | -1 |
Description: This method returns the index value of the last appearance of
num
or -1 ifnum
is not an element ofnums
.
Method Call | return value/output |
lastIndexOf( {6, 4 ,7 ,3, 11, ,4}, 4) | 5 |
lastIndexOf( {7, 6,4 , 7 ,3}, 7) | 3 |
lastIndexOf( {6,4 ,2,3}, 22) | -1 |
Description: This method returns true if each element in nums is greater than the element to its left.
Method Call | return value/output |
isIncreasing( {1,2,3,4 }) | true |
isIncreasing( {1,0 ,3,4 }) | false |
isIncreasing( {1,1, 2,3,4 }) | false |
isIncreasing( {-1, –1, -2, -3, -4 }) | false |
Description: This method returns the largest span of consecutive increasing numbers
Method Call | return value/output |
largestSpan( {4, 3, 1, 3, 6 , 1} ) | 3 |
largestSpan( {4 3 1 , 12 , 31 , 44 , 52 , 1} ) | 5 |
largestSpan( {1 , 7 8 , 12,4, 3, 0, 4 , 1) | 3 |
Description: This method ‘inverts’ an array by spliting the array in halves and ‘inverting’ each half. See sample calls to understand.
Method Call | return value/output |
invert( {5 , 21, 5, 13,4 }) | {21 , 5 , 5, 4,13 } |
Description: This method returns the array with all digits shifted by
delta
. Any digits that are circulated off the end of the array should be returned to the other side. Note: delta could be positive or negative. Please keep in mind that Math.abs(delta) > nums.length could be possible 😉
Method Call | return value/output |
shiftByN( {5 , 21, 13,4 } 1) | { 4 , 5 , 21, 13 } |
shiftByN( {5 , 21, 13, 4 , 11} 2) | { 4 , 11 , 5 , 21, 13} |
shiftByN( {5 , 21, 13, 4 } -1) | { 21, 13, 4 , 5} |
shiftByN( {5 , 21, 13, 4 ,7 } -2) | { 13, 4 ,7 , 5 , 21} |
1 < base < 11
@precondition
num1.length = 5 and num2.length = 5
@postcondition: the returned array has 5 elements representing the sum or an ArithmeticException is thrown
Description: This method attempts to replicate addition. Consider both num1 and num2 represent the five digits of a number. Each element in the array stores one of the digits. For instance, the number 143 would be represented as {0,0,1,4,3 }. Numbers can be in any base between 2 and 10 inclusive , and the number 101102 in binary would appear in an arrays as : {1, 0, 1 , 1, 0 } . Let’s assume the numbers are positive.You should return an array representing the digits of the sum of num1 and num2. If the number of digits in the sum exceeds the maximum number of digits (5) , you should throw an arithmetic exception as shown in the code below:
12 if(overFlowAdditionOccurred)throw new ArithmeticException("Addition Overflow Error");
Note: You may not use any kind of helper or utility methods that are built into pre-defined Java classes for converting numbers between bases. Only use techniques taught in this class.
Method Call | return value/output |
add( {0,0,0 ,4,2},{0,0,0,5,1}, 10) | {0, 0, 0, 9, 3} ie (42 + 51 = 93) |
add( {0, 0, 0 ,7,2},{0,0,0,5,1}, 10) | {0, 0 , 1 , 2, 3} ie (72 + 51 = 123) |
add( {0, 0 , 0 , 1 , 1}, { 0 , 0 , 0 , 1 }, 2) | {0, 0 , 1 , 0 , 0} ie ( 112 + 12= 1002 ) |
while-if-elif-else Jeroo[Python]
Assignment if_elif_else_while_D
- start at (0,0)
- end at (0,23)
- use 1 loop
- pick up the 1 flower and use the following while loo
Use this code at the start of your program
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
tim = Jeroo() while not ( tim. isWater(AHEAD) and tim.hasFlower() ) : if tim.isFlower(AHEAD) : tim.hop() tim.pick() elif not tim.isClear(AHEAD) : tim.turn(RIGHT) tim.hop() tim.turn(LEFT) tim.hop(2) tim.turn(LEFT) tim.hop() tim.turn(RIGHT) else : tim.hop() |
The map
What the map and Jeroo looks like at the completion of the loop
1 |
Java String Assignments -intro-1
Java String Assignments
These exercises are introductory methods that require use of basic String methods including
- substring()
- length()
- “abc”.length() – >3
- concatenation
public boolean sameStrings(String string1 ,String string2)
Description: This method returns true if string1 and string2 are the same. Use the ‘.equals()’ method.
Method Call | return value/output |
sameStrings(“foo”,”f”) | false |
sameStrings(“foo”,”foo”) | true |
sameStrings(“abc”, “cba”) | false |
public boolean any2Same(String a,String b, String c)
Description: This method returns true if any 2 of the strings are the same. Remember: Use the ‘.equals()’ method.
Method Call | return value/output |
any2Same(“xz”,”f”, “xz”) | true |
any2Same(“xz”,”f”, “xt”) | false |
any2Same(“xz”,”xz”, “fff”) | true |
any2Same(“xtz”,”abc”, “abc”) | true |
any2Same(“xtz”,”a^c”, “a!c”) | false |
public String firstThirdLettters(String str)
Description: This method returns the first and third letters of str concatenated together.
Method Call | return value/output |
firstThirdLettters(“foo“) | “fo” |
firstThirdLettters(“abcdefg”) | “ac” |
firstThirdLettters(“ad!kjkj”) | “a!” |
public boolean sameFirst2Letters(String a, String b)
Description: This method returns the first 2 letters of a and of b are the same .
Method Call | return value/output |
sameFirst2Letters(“axt”, “axjjj”) | true |
sameFirst2Letters(“1%3″ , “3$1″) | false |
sameFirst2Letters(“a~dd” ,”~adt” ) | false |
public String concatTwice(String str)
Description: This method returns str concatenated with itself .
Method Call | return value/output |
concatTwice(“foo”) | “foofoo” |
concatTwice(“a”) | “aa” |
concatTwice(“abcdd”) | “abcddabcdd” |
public String concatWithComma(String str)
Description: This method returns str concatenated with itself and with a comma in between
Method Call | return value/output |
concatWithComma(“foo”) | “foo,foo” |
concatWithComma(“a”) | “a,a” |
concatWithComma(“abcdd”) | “abcdd,abcdd” |
public String sandwich(String bread, String meat)
Description: This method is easiest to understand by looking at the sample calls below
Method Call | return value/output |
sandwich(“a“,”b“) | “aba“ |
sandwich(“xy“,”ab“) | “xyabxy“ |
sandwich(“hi“,”bye“) | “hibyehi“ |
public int lengthTimesTwo(String str)
Description: This method returns the length of str times 2.
Method Call | return value/output |
lengthTimesTwo(“foo”) | 6 |
lengthTimesTwo(“a”) | 2 |
lengthTimesTwo(“abcdd”) | 10 |
String prePendFoo(String str)
Description: prepend “foo ” to the input and return the concatenation.
Method Call | return value/output |
prePendFoo(“abc”) | “foo abc” |
prePendFoo(“x”) | “foo x” |
prePendFoo(“abcdd”) | foo abcdd” |
public int sumOfLengths(String a, String a)
Description: This method returns the sum of the lengths of String a and String b .
Method Call | return value/output |
sumOfLengths(“ab”, “jk1”) | 5 ie ( 2 +3) |
sumOfLengths(“jj”, “”) | 2 (ie 2 + 0) |
sumOfLengths(“a~dd” ,”6″ ) | 5 ie ( 4 + 1) |
**public String concat5Times(String str)
Description: This method returns str concatenated with itself 5 times (Do this with a loop)
Method Call | return value/output |
concat5Times(“foo”) | “foofoofoofoofoo” |
concat5Times(“a”) | “aaaaa” |
concat5Times(“abcdd”) | “abcddabcddabcddabcdd” |
Searching Sorting
Java Strings -Introduction
Java Strings
Java Strings
- Strings in Java
- String Assignments #1
- String Loops Review (word doc)
-
- Strings Assignments This should be too easy for anyone who took the Java course so skip these if they are too easy for you
- Looping over String Projects
- String Loops 1
- String Loops 2 (must do) Due:EOD Tuesday 10/22
- String Projects #1 do the (Validate User Input Project)
- testcases_validate_form
- Required Coding Bat Problems
- Some Practice Problems
-
Recursion in Java
Recur
- What happens, when you Google recursion?
- First : go to and read this page
- Download and run this code
- Complete these excercises in BlueJ
- Coding bat excercises
bunnyEars2() (try to figure out what goes where the question marks are)
1 2 3 4 5 6 |
if(bunnies == 0) return 0; if(bunnies % 2 == 0) return 3 +bunnyEars2(?????); return 2 +bunnyEars2( ????? ); |
Recursion problems (bunny ears is a good, straight forward one to start with)
- fibonacci()
- countHi()
- then try sumDigits()
- array6()
- array220()
- powerN()
- countHi()
- ** http://codingbat.com/prob/p195413
- ** http://codingbat.com/prob/p118182
- any of the last 6 problems
- ** print Pascal’s Triangle. Use recursion and no loops
Jeroo in Python — while loop vs if test
Two Friends Meet
Two Friends Meet
- Create two different Jeroos
- one Jeroo should start at (0,0) and be facing EAST
- the other should start at (0,23) and be facing west
- they should move at “the same time” and plant flowers in the pattern shown below
- use a single while loop
- the isJeroo( relative_diretion) method might be helpful
- Note: To get 100%
- Your loop MUST end (not infinite)
- You can not use hasFlower()